1 /* $NetBSD: fenv.c,v 1.3 2017/03/22 23:11:08 chs 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.3 2017/03/22 23:11:08 chs Exp $"); 34 35 #include "namespace.h" 36 37 #include <sys/param.h> 38 #include <sys/types.h> 39 #include <assert.h> 40 #include <fenv.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include <inttypes.h> 44 45 #include <aarch64/armreg.h> 46 47 #ifdef __weak_alias 48 __weak_alias(feclearexcept,_feclearexcept) 49 __weak_alias(fedisableexcept,_fedisableexcept) 50 __weak_alias(feenableexcept,_feenableexcept) 51 __weak_alias(fegetenv,_fegetenv) 52 __weak_alias(fegetexcept,_fegetexcept) 53 __weak_alias(fegetexceptflag,_fegetexceptflag) 54 __weak_alias(fegetround,_fegetround) 55 __weak_alias(feholdexcept,_feholdexcept) 56 __weak_alias(feraiseexcept,_feraiseexcept) 57 __weak_alias(fesetenv,_fesetenv) 58 __weak_alias(fesetexceptflag,_fesetexceptflag) 59 __weak_alias(fesetround,_fesetround) 60 __weak_alias(fetestexcept,_fetestexcept) 61 __weak_alias(feupdateenv,_feupdateenv) 62 #endif 63 64 const fenv_t __fe_dfl_env = { 65 .__fpsr = 0, 66 .__fpcr = FPCR_FZ|FPCR_DN|FPCR_RN, 67 }; 68 69 /* 70 * The feclearexcept() function shall attempt to clear the supported 71 * floating-point exceptions represented by excepts. 72 */ 73 int 74 feclearexcept(int excepts) 75 { 76 #ifndef lint 77 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0); 78 #endif 79 unsigned int tmp = reg_fpsr_read() & ~__SHIFTIN(excepts, FPSR_CSUM); 80 reg_fpsr_write(tmp); 81 return 0; 82 } 83 84 /* 85 * The fegetexceptflag() function shall attempt to store an 86 * implementation-defined representation of the states of the floating-point 87 * status flags indicated by the argument excepts in the object pointed to by 88 * the argument flagp. 89 */ 90 int 91 fegetexceptflag(fexcept_t *flagp, int excepts) 92 { 93 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0); 94 *flagp = __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM) & excepts; 95 return 0; 96 } 97 98 /* 99 * The feraiseexcept() function shall attempt to raise the supported 100 * floating-point exceptions represented by the argument excepts. The order 101 * in which these floating-point exceptions are raised is unspecified. 102 */ 103 int 104 feraiseexcept(int excepts) 105 { 106 #ifndef lint 107 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0); 108 #endif 109 unsigned int fpsr = reg_fpsr_read(); 110 fpsr = (fpsr & ~FPSR_CSUM) | __SHIFTIN(excepts, FPSR_CSUM); 111 reg_fpsr_write(fpsr); 112 unsigned int fpcr = reg_fpcr_read(); 113 fpcr = (fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM); 114 reg_fpcr_write(fpcr); 115 return 0; 116 } 117 118 /* 119 * The fesetexceptflag() function shall attempt to set the floating-point 120 * status flags indicated by the argument excepts to the states stored in the 121 * object pointed to by flagp. The value pointed to by flagp shall have been 122 * set by a previous call to fegetexceptflag() whose second argument 123 * represented at least those floating-point exceptions represented by the 124 * argument excepts. This function does not raise floating-point exceptions, 125 * but only sets the state of the flags. 126 */ 127 int 128 fesetexceptflag(const fexcept_t *flagp, int excepts) 129 { 130 #ifndef lint 131 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0); 132 #endif 133 unsigned int fpsr = reg_fpsr_read(); 134 fpsr &= ~__SHIFTIN(excepts, FPSR_CSUM); 135 fpsr |= __SHIFTIN((*flagp & excepts), FPSR_CSUM); 136 reg_fpsr_write(fpsr); 137 return 0; 138 } 139 140 /* 141 * The fetestexcept() function shall determine which of a specified subset of 142 * the floating-point exception flags are currently set. The excepts argument 143 * specifies the floating-point status flags to be queried. 144 */ 145 int 146 fetestexcept(int excepts) 147 { 148 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0); 149 return __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM) & excepts; 150 } 151 152 /* 153 * The fegetround() function shall get the current rounding direction. 154 */ 155 int 156 fegetround(void) 157 { 158 return __SHIFTOUT(reg_fpcr_read(), FPCR_RMODE); 159 } 160 161 /* 162 * The fesetround() function shall establish the rounding direction represented 163 * by its argument round. If the argument is not equal to the value of a 164 * rounding direction macro, the rounding direction is not changed. 165 */ 166 int 167 fesetround(int round) 168 { 169 #ifndef lint 170 _DIAGASSERT(!(round & ~__SHIFTOUT(FPCR_RMODE, FPCR_RMODE))); 171 #endif 172 unsigned int fpcr = reg_fpcr_read() & ~FPCR_RMODE; 173 fpcr |= __SHIFTIN(round, FPCR_RMODE); 174 reg_fpcr_write(fpcr); 175 return 0; 176 } 177 178 /* 179 * The fegetenv() function shall attempt to store the current floating-point 180 * environment in the object pointed to by envp. 181 */ 182 int 183 fegetenv(fenv_t *envp) 184 { 185 envp->__fpcr = reg_fpcr_read(); 186 envp->__fpsr = reg_fpsr_read(); 187 return 0; 188 } 189 190 /* 191 * The feholdexcept() function shall save the current floating-point 192 * environment in the object pointed to by envp, clear the floating-point 193 * status flags, and then install a non-stop (continue on floating-point 194 * exceptions) mode, if available, for all floating-point exceptions. 195 */ 196 int 197 feholdexcept(fenv_t *envp) 198 { 199 envp->__fpsr = reg_fpsr_read(); 200 envp->__fpcr = reg_fpcr_read(); 201 reg_fpsr_write(envp->__fpsr & ~FPSR_CSUM); 202 reg_fpcr_write(envp->__fpcr & ~FPCR_ESUM); 203 return 0; 204 } 205 206 /* 207 * The fesetenv() function shall attempt to establish the floating-point 208 * environment represented by the object pointed to by envp. The fesetenv() 209 * function does not raise floating-point exceptions, but only installs the 210 * state of the floating-point status flags represented through its argument. 211 */ 212 int 213 fesetenv(const fenv_t *envp) 214 { 215 reg_fpsr_write(envp->__fpsr); 216 return 0; 217 } 218 219 /* 220 * The feupdateenv() function shall attempt to save the currently raised 221 * floating-point exceptions in its automatic storage, attempt to install the 222 * floating-point environment represented by the object pointed to by envp, 223 * and then attempt to raise the saved floating-point exceptions. 224 */ 225 int 226 feupdateenv(const fenv_t *envp) 227 { 228 #ifndef lint 229 _DIAGASSERT(envp != NULL); 230 #endif 231 reg_fpsr_write(envp->__fpsr); 232 reg_fpcr_write(envp->__fpcr); 233 234 /* Success */ 235 return 0; 236 } 237 238 int 239 feenableexcept(int excepts) 240 { 241 const uint32_t __fpcr = reg_fpcr_read(); 242 reg_fpcr_write((__fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM)); 243 return __SHIFTOUT(__fpcr, FPCR_ESUM); 244 } 245 246 int 247 fedisableexcept(int excepts) 248 { 249 const uint32_t __fpcr = reg_fpcr_read(); 250 reg_fpcr_write(__fpcr & ~__SHIFTIN(excepts, FPCR_ESUM)); 251 return __SHIFTOUT(__fpcr, FPCR_ESUM); 252 } 253 254 int 255 fegetexcept(void) 256 { 257 const uint32_t __fpcr = reg_fpcr_read(); 258 return __SHIFTOUT(__fpcr, FPCR_ESUM); 259 } 260