1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 2003 Peter Wemm. 5 * Copyright (c) 1990 Andrew Moore, Talke Studio 6 * All rights reserved. 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#) ieeefp.h 1.0 (Berkeley) 9/23/93 37 * $FreeBSD$ 38 */ 39 40 #ifndef _MACHINE_IEEEFP_H_ 41 #define _MACHINE_IEEEFP_H_ 42 43 /* 44 * Deprecated historical FPU control interface 45 * 46 * IEEE floating point type, constant and function definitions. 47 * XXX: FP*FLD and FP*OFF are undocumented pollution. 48 */ 49 50 #ifndef _SYS_CDEFS_H_ 51 #error this file needs sys/cdefs.h as a prerequisite 52 #endif 53 54 /* 55 * Rounding modes. 56 */ 57 typedef enum { 58 FP_RN=0, /* round to nearest */ 59 FP_RM, /* round down towards minus infinity */ 60 FP_RP, /* round up towards plus infinity */ 61 FP_RZ /* truncate */ 62 } fp_rnd_t; 63 64 /* 65 * Precision (i.e., rounding precision) modes. 66 */ 67 typedef enum { 68 FP_PS=0, /* 24 bit (single-precision) */ 69 FP_PRS, /* reserved */ 70 FP_PD, /* 53 bit (double-precision) */ 71 FP_PE /* 64 bit (extended-precision) */ 72 } fp_prec_t; 73 74 #define fp_except_t int 75 76 /* 77 * Exception bit masks. 78 */ 79 #define FP_X_INV 0x01 /* invalid operation */ 80 #define FP_X_DNML 0x02 /* denormal */ 81 #define FP_X_DZ 0x04 /* zero divide */ 82 #define FP_X_OFL 0x08 /* overflow */ 83 #define FP_X_UFL 0x10 /* underflow */ 84 #define FP_X_IMP 0x20 /* (im)precision */ 85 #define FP_X_STK 0x40 /* stack fault */ 86 87 /* 88 * FPU control word bit-field masks. 89 */ 90 #define FP_MSKS_FLD 0x3f /* exception masks field */ 91 #define FP_PRC_FLD 0x300 /* precision control field */ 92 #define FP_RND_FLD 0xc00 /* rounding control field */ 93 94 /* 95 * FPU status word bit-field masks. 96 */ 97 #define FP_STKY_FLD 0x3f /* sticky flags field */ 98 99 /* 100 * FPU control word bit-field offsets (shift counts). 101 */ 102 #define FP_MSKS_OFF 0 /* exception masks offset */ 103 #define FP_PRC_OFF 8 /* precision control offset */ 104 #define FP_RND_OFF 10 /* rounding control offset */ 105 106 /* 107 * FPU status word bit-field offsets (shift counts). 108 */ 109 #define FP_STKY_OFF 0 /* sticky flags offset */ 110 111 #ifdef __GNUCLIKE_ASM 112 113 #define __fldcw(addr) __asm __volatile("fldcw %0" : : "m" (*(addr))) 114 #define __fldenv(addr) __asm __volatile("fldenv %0" : : "m" (*(addr))) 115 #define __fnclex() __asm __volatile("fnclex") 116 #define __fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr))) 117 #define __fnstenv(addr) __asm __volatile("fnstenv %0" : "=m" (*(addr))) 118 #define __fnstsw(addr) __asm __volatile("fnstsw %0" : "=m" (*(addr))) 119 #define __ldmxcsr(addr) __asm __volatile("ldmxcsr %0" : : "m" (*(addr))) 120 #define __stmxcsr(addr) __asm __volatile("stmxcsr %0" : "=m" (*(addr))) 121 122 /* 123 * Load the control word. Be careful not to trap if there is a currently 124 * unmasked exception (ones that will become freshly unmasked are not a 125 * problem). This case must be handled by a save/restore of the 126 * environment or even of the full x87 state. Accessing the environment 127 * is very inefficient, so only do it when necessary. 128 */ 129 static __inline void 130 __fnldcw(unsigned short _cw, unsigned short _newcw) 131 { 132 struct { 133 unsigned _cw; 134 unsigned _other[6]; 135 } _env; 136 unsigned short _sw; 137 138 if ((_cw & FP_MSKS_FLD) != FP_MSKS_FLD) { 139 __fnstsw(&_sw); 140 if (((_sw & ~_cw) & FP_STKY_FLD) != 0) { 141 __fnstenv(&_env); 142 _env._cw = _newcw; 143 __fldenv(&_env); 144 return; 145 } 146 } 147 __fldcw(&_newcw); 148 } 149 150 static __inline fp_rnd_t 151 fpgetround(void) 152 { 153 unsigned short _cw; 154 155 __fnstcw(&_cw); 156 return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF)); 157 } 158 159 static __inline fp_rnd_t 160 fpsetround(fp_rnd_t _m) 161 { 162 fp_rnd_t _p; 163 unsigned short _cw, _newcw; 164 165 __fnstcw(&_cw); 166 _p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF); 167 _newcw = _cw & ~FP_RND_FLD; 168 _newcw |= (_m << FP_RND_OFF) & FP_RND_FLD; 169 __fnldcw(_cw, _newcw); 170 return (_p); 171 } 172 173 static __inline fp_prec_t 174 fpgetprec(void) 175 { 176 unsigned short _cw; 177 178 __fnstcw(&_cw); 179 return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF)); 180 } 181 182 static __inline fp_prec_t 183 fpsetprec(fp_prec_t _m) 184 { 185 fp_prec_t _p; 186 unsigned short _cw, _newcw; 187 188 __fnstcw(&_cw); 189 _p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF); 190 _newcw = _cw & ~FP_PRC_FLD; 191 _newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD; 192 __fnldcw(_cw, _newcw); 193 return (_p); 194 } 195 196 /* 197 * Get or set the exception mask. 198 * Note that the x87 mask bits are inverted by the API -- a mask bit of 1 199 * means disable for x87 and SSE, but for fp*mask() it means enable. 200 */ 201 202 static __inline fp_except_t 203 fpgetmask(void) 204 { 205 unsigned short _cw; 206 207 __fnstcw(&_cw); 208 return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF); 209 } 210 211 static __inline fp_except_t 212 fpsetmask(fp_except_t _m) 213 { 214 fp_except_t _p; 215 unsigned short _cw, _newcw; 216 217 __fnstcw(&_cw); 218 _p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF; 219 _newcw = _cw & ~FP_MSKS_FLD; 220 _newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD; 221 __fnldcw(_cw, _newcw); 222 return (_p); 223 } 224 225 static __inline fp_except_t 226 fpgetsticky(void) 227 { 228 unsigned _ex; 229 unsigned short _sw; 230 231 __fnstsw(&_sw); 232 _ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF; 233 return ((fp_except_t)_ex); 234 } 235 236 static __inline fp_except_t 237 fpresetsticky(fp_except_t _m) 238 { 239 struct { 240 unsigned _cw; 241 unsigned _sw; 242 unsigned _other[5]; 243 } _env; 244 fp_except_t _p; 245 246 _m &= FP_STKY_FLD >> FP_STKY_OFF; 247 _p = fpgetsticky(); 248 if ((_p & ~_m) == _p) 249 return (_p); 250 if ((_p & ~_m) == 0) { 251 __fnclex(); 252 return (_p); 253 } 254 __fnstenv(&_env); 255 _env._sw &= ~_m; 256 __fldenv(&_env); 257 return (_p); 258 } 259 260 #endif /* __GNUCLIKE_ASM */ 261 262 #endif /* !_MACHINE_IEEEFP_H_ */ 263