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