Home | History | Annotate | Line # | Download | only in arm
fenv.c revision 1.4
      1  1.1    matt /*-
      2  1.1    matt  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      3  1.1    matt  * All rights reserved.
      4  1.1    matt  *
      5  1.1    matt  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1    matt  * by Matt Thomas of 3am Software Foundry.
      7  1.1    matt  *
      8  1.1    matt  * Redistribution and use in source and binary forms, with or without
      9  1.1    matt  * modification, are permitted provided that the following conditions
     10  1.1    matt  * are met:
     11  1.1    matt  * 1. Redistributions of source code must retain the above copyright
     12  1.1    matt  *    notice, this list of conditions and the following disclaimer.
     13  1.1    matt  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1    matt  *    notice, this list of conditions and the following disclaimer in the
     15  1.1    matt  *    documentation and/or other materials provided with the distribution.
     16  1.1    matt  *
     17  1.1    matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1    matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1    matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1    matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1    matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1    matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1    matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1    matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1    matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1    matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1    matt  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1    matt  */
     29  1.1    matt 
     30  1.1    matt #include <sys/cdefs.h>
     31  1.4  martin __RCSID("$NetBSD: fenv.c,v 1.4 2014/12/27 17:54:24 martin Exp $");
     32  1.1    matt 
     33  1.1    matt #include <sys/types.h>
     34  1.1    matt #include <assert.h>
     35  1.1    matt #include <fenv.h>
     36  1.1    matt #include <string.h>
     37  1.1    matt #include <unistd.h>
     38  1.2    matt #include <inttypes.h>
     39  1.1    matt 
     40  1.1    matt #ifdef __SOFTFP__
     41  1.1    matt #include <ieeefp.h>
     42  1.1    matt #include <sys/signal.h>
     43  1.1    matt #include <sys/siginfo.h>
     44  1.1    matt #else
     45  1.1    matt #include <arm/armreg.h>
     46  1.1    matt #endif
     47  1.1    matt 
     48  1.1    matt #include <arm/vfpreg.h>
     49  1.1    matt 
     50  1.1    matt const fenv_t __fe_dfl_env = VFP_FPSCR_FZ|VFP_FPSCR_DN|VFP_FPSCR_RN;
     51  1.1    matt 
     52  1.1    matt /*
     53  1.1    matt  * The feclearexcept() function shall attempt to clear the supported
     54  1.1    matt  * floating-point exceptions represented by excepts.
     55  1.1    matt  */
     56  1.1    matt int
     57  1.1    matt feclearexcept(int excepts)
     58  1.1    matt {
     59  1.2    matt #ifndef lint
     60  1.4  martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
     61  1.2    matt #endif
     62  1.1    matt #ifdef __SOFTFP__
     63  1.3    matt 	fpsetsticky(fpgetsticky() & ~excepts);
     64  1.3    matt 	return 0;
     65  1.1    matt #else
     66  1.1    matt 	int tmp = armreg_fpscr_read() & ~__SHIFTIN(excepts, VFP_FPSCR_CSUM);
     67  1.1    matt 	armreg_fpscr_write(tmp);
     68  1.3    matt 	return 0;
     69  1.1    matt #endif
     70  1.1    matt }
     71  1.1    matt 
     72  1.1    matt /*
     73  1.1    matt  * The fegetexceptflag() function shall attempt to store an
     74  1.1    matt  * implementation-defined representation of the states of the floating-point
     75  1.1    matt  * status flags indicated by the argument excepts in the object pointed to by
     76  1.1    matt  * the argument flagp.
     77  1.1    matt  */
     78  1.1    matt int
     79  1.1    matt fegetexceptflag(fexcept_t *flagp, int excepts)
     80  1.1    matt {
     81  1.4  martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
     82  1.1    matt #ifdef __SOFTFP__
     83  1.1    matt 	*flagp = fpgetsticky() & excepts;
     84  1.1    matt #else
     85  1.1    matt 	*flagp = __SHIFTOUT(armreg_fpscr_read(), VFP_FPSCR_CSUM) & excepts;
     86  1.1    matt #endif
     87  1.1    matt 	return 0;
     88  1.1    matt }
     89  1.1    matt 
     90  1.1    matt /*
     91  1.1    matt  * The feraiseexcept() function shall attempt to raise the supported
     92  1.1    matt  * floating-point exceptions represented by the argument excepts. The order
     93  1.1    matt  * in which these floating-point exceptions are raised is unspecified.
     94  1.1    matt  */
     95  1.1    matt int
     96  1.1    matt feraiseexcept(int excepts)
     97  1.1    matt {
     98  1.2    matt #ifndef lint
     99  1.4  martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
    100  1.2    matt #endif
    101  1.1    matt #ifdef __SOFTFP__
    102  1.1    matt 	excepts &= fpgetsticky();
    103  1.1    matt 
    104  1.1    matt 	if (excepts) {
    105  1.1    matt 		siginfo_t info;
    106  1.1    matt 		memset(&info, 0, sizeof info);
    107  1.1    matt 		info.si_signo = SIGFPE;
    108  1.1    matt 		info.si_pid = getpid();
    109  1.1    matt 		info.si_uid = geteuid();
    110  1.1    matt 		if (excepts & FE_UNDERFLOW)
    111  1.1    matt 			info.si_code = FPE_FLTUND;
    112  1.1    matt 		else if (excepts & FE_OVERFLOW)
    113  1.1    matt 			info.si_code = FPE_FLTOVF;
    114  1.1    matt 		else if (excepts & FE_DIVBYZERO)
    115  1.1    matt 			info.si_code = FPE_FLTDIV;
    116  1.1    matt 		else if (excepts & FE_INVALID)
    117  1.1    matt 			info.si_code = FPE_FLTINV;
    118  1.1    matt 		else if (excepts & FE_INEXACT)
    119  1.1    matt 			info.si_code = FPE_FLTRES;
    120  1.1    matt 		sigqueueinfo(getpid(), &info);
    121  1.1    matt 	}
    122  1.1    matt #else
    123  1.2    matt 	int fpscr = armreg_fpscr_read();
    124  1.1    matt 	fpscr = (fpscr & ~VFP_FPSCR_ESUM) | __SHIFTIN(excepts, VFP_FPSCR_ESUM);
    125  1.1    matt 	armreg_fpscr_write(fpscr);
    126  1.1    matt #endif
    127  1.1    matt 	return 0;
    128  1.1    matt }
    129  1.1    matt 
    130  1.1    matt /*
    131  1.1    matt  * The fesetexceptflag() function shall attempt to set the floating-point
    132  1.1    matt  * status flags indicated by the argument excepts to the states stored in the
    133  1.1    matt  * object pointed to by flagp. The value pointed to by flagp shall have been
    134  1.1    matt  * set by a previous call to fegetexceptflag() whose second argument
    135  1.1    matt  * represented at least those floating-point exceptions represented by the
    136  1.1    matt  * argument excepts. This function does not raise floating-point exceptions,
    137  1.1    matt  * but only sets the state of the flags.
    138  1.1    matt  */
    139  1.1    matt int
    140  1.1    matt fesetexceptflag(const fexcept_t *flagp, int excepts)
    141  1.1    matt {
    142  1.2    matt #ifndef lint
    143  1.4  martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
    144  1.2    matt #endif
    145  1.1    matt #ifdef __SOFTFP__
    146  1.1    matt 	fpsetsticky((fpgetsticky() & ~excepts) | (excepts & *flagp));
    147  1.1    matt #else
    148  1.1    matt 	int fpscr = armreg_fpscr_read();
    149  1.1    matt 	fpscr &= ~__SHIFTIN(excepts, VFP_FPSCR_CSUM);
    150  1.1    matt 	fpscr |= __SHIFTIN((*flagp & excepts), VFP_FPSCR_CSUM);
    151  1.1    matt 	armreg_fpscr_write(fpscr);
    152  1.1    matt #endif
    153  1.1    matt 	return 0;
    154  1.1    matt }
    155  1.1    matt 
    156  1.4  martin int
    157  1.4  martin feenableexcept(int excepts)
    158  1.4  martin {
    159  1.4  martin #ifndef lint
    160  1.4  martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
    161  1.4  martin #endif
    162  1.4  martin #ifdef __SOFTFP__
    163  1.4  martin 	int old = fpgetsticky();
    164  1.4  martin 	fpsetsticky(old | excepts);
    165  1.4  martin #else
    166  1.4  martin 	int fpscr = armreg_fpscr_read();
    167  1.4  martin 	armreg_fpscr_write(fpscr | __SHIFTIN((excepts), VFP_FPSCR_CSUM));
    168  1.4  martin 	return __SHIFTOUT(fpscr, VFP_FPSCR_CSUM) & FE_ALL_EXCEPT;
    169  1.4  martin #endif
    170  1.4  martin }
    171  1.4  martin 
    172  1.4  martin int
    173  1.4  martin fedisableexcept(int excepts)
    174  1.4  martin {
    175  1.4  martin #ifndef lint
    176  1.4  martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
    177  1.4  martin #endif
    178  1.4  martin #ifdef __SOFTFP__
    179  1.4  martin 	int old = fpgetsticky();
    180  1.4  martin 	fpsetsticky(old & ~excepts);
    181  1.4  martin 	return old;
    182  1.4  martin #else
    183  1.4  martin 	int fpscr = armreg_fpscr_read();
    184  1.4  martin 	armreg_fpscr_write(fpscr & ~__SHIFTIN((excepts), VFP_FPSCR_CSUM));
    185  1.4  martin 	return __SHIFTOUT(fpscr, VFP_FPSCR_CSUM) & FE_ALL_EXCEPT;
    186  1.4  martin #endif
    187  1.4  martin }
    188  1.4  martin 
    189  1.1    matt /*
    190  1.1    matt  * The fetestexcept() function shall determine which of a specified subset of
    191  1.1    matt  * the floating-point exception flags are currently set. The excepts argument
    192  1.1    matt  * specifies the floating-point status flags to be queried.
    193  1.1    matt  */
    194  1.1    matt int
    195  1.1    matt fetestexcept(int excepts)
    196  1.1    matt {
    197  1.4  martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
    198  1.1    matt #ifdef __SOFTFP__
    199  1.1    matt 	return fpgetsticky() & excepts;
    200  1.1    matt #else
    201  1.1    matt 	return __SHIFTOUT(armreg_fpscr_read(), VFP_FPSCR_CSUM) & excepts;
    202  1.1    matt #endif
    203  1.1    matt }
    204  1.1    matt 
    205  1.4  martin int
    206  1.4  martin fegetexcept(void)
    207  1.4  martin {
    208  1.4  martin 	return fetestexcept(FE_ALL_EXCEPT);
    209  1.4  martin }
    210  1.4  martin 
    211  1.1    matt /*
    212  1.1    matt  * The fegetround() function shall get the current rounding direction.
    213  1.1    matt  */
    214  1.1    matt int
    215  1.1    matt fegetround(void)
    216  1.1    matt {
    217  1.1    matt #ifdef __SOFTFP__
    218  1.1    matt 	return fpgetround();
    219  1.1    matt #else
    220  1.1    matt 	return __SHIFTOUT(armreg_fpscr_read(), VFP_FPSCR_RMODE);
    221  1.1    matt #endif
    222  1.1    matt }
    223  1.1    matt 
    224  1.1    matt /*
    225  1.1    matt  * The fesetround() function shall establish the rounding direction represented
    226  1.1    matt  * by its argument round. If the argument is not equal to the value of a
    227  1.1    matt  * rounding direction macro, the rounding direction is not changed.
    228  1.1    matt  */
    229  1.1    matt int
    230  1.1    matt fesetround(int round)
    231  1.1    matt {
    232  1.2    matt #ifndef lint
    233  1.1    matt 	_DIAGASSERT(!(round & ~__SHIFTOUT(VFP_FPSCR_RMODE, VFP_FPSCR_RMODE)));
    234  1.2    matt #endif
    235  1.1    matt #ifdef __SOFTFP__
    236  1.1    matt 	(void)fpsetround(round);
    237  1.1    matt #else
    238  1.1    matt 	int fpscr = armreg_fpscr_read() & ~VFP_FPSCR_RMODE;
    239  1.1    matt 	fpscr |= __SHIFTIN(round, VFP_FPSCR_RMODE);
    240  1.1    matt 	armreg_fpscr_write(fpscr);
    241  1.1    matt #endif
    242  1.1    matt 	return 0;
    243  1.1    matt }
    244  1.1    matt 
    245  1.1    matt /*
    246  1.1    matt  * The fegetenv() function shall attempt to store the current floating-point
    247  1.1    matt  * environment in the object pointed to by envp.
    248  1.1    matt  */
    249  1.1    matt int
    250  1.1    matt fegetenv(fenv_t *envp)
    251  1.1    matt {
    252  1.1    matt #ifdef __SOFTFP__
    253  1.1    matt 	*envp = __SHIFTIN(fpgetround(), VFP_FPSCR_RMODE)
    254  1.1    matt 	    | __SHIFTIN(fpgetmask(), VFP_FPSCR_ESUM)
    255  1.1    matt 	    | __SHIFTIN(fpgetsticky(), VFP_FPSCR_CSUM);
    256  1.1    matt #else
    257  1.1    matt 	*envp = armreg_fpscr_read();
    258  1.1    matt #endif
    259  1.1    matt 	return 0;
    260  1.1    matt }
    261  1.1    matt 
    262  1.1    matt /*
    263  1.1    matt  * The feholdexcept() function shall save the current floating-point
    264  1.1    matt  * environment in the object pointed to by envp, clear the floating-point
    265  1.1    matt  * status flags, and then install a non-stop (continue on floating-point
    266  1.1    matt  * exceptions) mode, if available, for all floating-point exceptions.
    267  1.1    matt  */
    268  1.1    matt int
    269  1.1    matt feholdexcept(fenv_t *envp)
    270  1.1    matt {
    271  1.1    matt #ifdef __SOFTFP__
    272  1.1    matt 	*envp = __SHIFTIN(fpgetround(), VFP_FPSCR_RMODE)
    273  1.1    matt 	    | __SHIFTIN(fpgetmask(), VFP_FPSCR_ESUM)
    274  1.1    matt 	    | __SHIFTIN(fpgetsticky(), VFP_FPSCR_CSUM);
    275  1.1    matt 	fpsetmask(0);
    276  1.1    matt 	fpsetsticky(0);
    277  1.1    matt #else
    278  1.1    matt 	*envp = armreg_fpscr_read();
    279  1.1    matt 	armreg_fpscr_write((*envp) & ~(VFP_FPSCR_ESUM|VFP_FPSCR_CSUM));
    280  1.1    matt #endif
    281  1.1    matt 	return 0;
    282  1.1    matt }
    283  1.1    matt 
    284  1.1    matt /*
    285  1.1    matt  * The fesetenv() function shall attempt to establish the floating-point
    286  1.1    matt  * environment represented by the object pointed to by envp. The fesetenv()
    287  1.1    matt  * function does not raise floating-point exceptions, but only installs the
    288  1.1    matt  * state of the floating-point status flags represented through its argument.
    289  1.1    matt  */
    290  1.1    matt int
    291  1.1    matt fesetenv(const fenv_t *envp)
    292  1.1    matt {
    293  1.1    matt #ifdef __SOFTFP__
    294  1.1    matt 	(void)fpsetround(__SHIFTIN(*envp, VFP_FPSCR_RMODE));
    295  1.1    matt 	(void)fpsetmask(__SHIFTOUT(*envp, VFP_FPSCR_ESUM));
    296  1.1    matt 	(void)fpsetsticky(__SHIFTOUT(*envp, VFP_FPSCR_CSUM));
    297  1.1    matt #else
    298  1.1    matt 	armreg_fpscr_write(*envp);
    299  1.1    matt #endif
    300  1.1    matt 	return 0;
    301  1.1    matt }
    302  1.1    matt 
    303  1.1    matt /*
    304  1.1    matt  * The feupdateenv() function shall attempt to save the currently raised
    305  1.1    matt  * floating-point exceptions in its automatic storage, attempt to install the
    306  1.1    matt  * floating-point environment represented by the object pointed to by envp,
    307  1.1    matt  * and then attempt to raise the saved floating-point exceptions.
    308  1.1    matt  */
    309  1.1    matt int
    310  1.1    matt feupdateenv(const fenv_t *envp)
    311  1.1    matt {
    312  1.2    matt #ifndef lint
    313  1.1    matt 	_DIAGASSERT(envp != NULL);
    314  1.2    matt #endif
    315  1.1    matt #ifdef __SOFTFP__
    316  1.1    matt 	(void)fpsetround(__SHIFTIN(*envp, VFP_FPSCR_RMODE));
    317  1.1    matt 	(void)fpsetmask(fpgetmask() | __SHIFTOUT(*envp, VFP_FPSCR_ESUM));
    318  1.1    matt 	(void)fpsetsticky(fpgetsticky() | __SHIFTOUT(*envp, VFP_FPSCR_CSUM));
    319  1.1    matt #else
    320  1.1    matt 	int fpscr = armreg_fpscr_read() & ~(VFP_FPSCR_ESUM|VFP_FPSCR_CSUM);
    321  1.1    matt 	fpscr |= *envp;
    322  1.1    matt 	armreg_fpscr_write(fpscr);
    323  1.1    matt #endif
    324  1.1    matt 
    325  1.1    matt 	/* Success */
    326  1.1    matt 	return 0;
    327  1.1    matt }
    328