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