11834Swollman/*- 2122296Speter * Copyright (c) 2003 Peter Wemm. 31834Swollman * Copyright (c) 1990 Andrew Moore, Talke Studio 41834Swollman * All rights reserved. 51834Swollman * 61834Swollman * Redistribution and use in source and binary forms, with or without 71834Swollman * modification, are permitted provided that the following conditions 81834Swollman * are met: 91834Swollman * 1. Redistributions of source code must retain the above copyright 101834Swollman * notice, this list of conditions and the following disclaimer. 111834Swollman * 2. Redistributions in binary form must reproduce the above copyright 121834Swollman * notice, this list of conditions and the following disclaimer in the 131834Swollman * documentation and/or other materials provided with the distribution. 141834Swollman * 3. All advertising materials mentioning features or use of this software 151834Swollman * must display the following acknowledgement: 161834Swollman * This product includes software developed by the University of 171834Swollman * California, Berkeley and its contributors. 181834Swollman * 4. Neither the name of the University nor the names of its contributors 191834Swollman * may be used to endorse or promote products derived from this software 201834Swollman * without specific prior written permission. 211834Swollman * 221834Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231834Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241834Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251834Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261834Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271834Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281834Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291834Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301834Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311834Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321834Swollman * SUCH DAMAGE. 331834Swollman * 341834Swollman * from: @(#) ieeefp.h 1.0 (Berkeley) 9/23/93 3550477Speter * $FreeBSD: stable/11/sys/amd64/include/ieeefp.h 334771 2018-06-07 09:03:42Z dim $ 361834Swollman */ 371834Swollman 38175178Sbde#ifndef _MACHINE_IEEEFP_H_ 39175178Sbde#define _MACHINE_IEEEFP_H_ 40175178Sbde 411834Swollman/* 42226607Sdas * Deprecated historical FPU control interface 43226607Sdas * 44175178Sbde * IEEE floating point type, constant and function definitions. 45175228Sbde * XXX: {FP,SSE}*FLD and {FP,SSE}*OFF are undocumented pollution. 461834Swollman */ 471834Swollman 48143063Sjoerg#ifndef _SYS_CDEFS_H_ 49143063Sjoerg#error this file needs sys/cdefs.h as a prerequisite 50143063Sjoerg#endif 51143063Sjoerg 521834Swollman/* 53175228Sbde * Rounding modes. 541834Swollman */ 551834Swollmantypedef enum { 561834Swollman FP_RN=0, /* round to nearest */ 57175228Sbde FP_RM, /* round down towards minus infinity */ 58175228Sbde FP_RP, /* round up towards plus infinity */ 591834Swollman FP_RZ /* truncate */ 601834Swollman} fp_rnd_t; 611834Swollman 621834Swollman/* 63175228Sbde * Precision (i.e., rounding precision) modes. 641834Swollman */ 651834Swollmantypedef enum { 6613765Smpp FP_PS=0, /* 24 bit (single-precision) */ 671834Swollman FP_PRS, /* reserved */ 681834Swollman FP_PD, /* 53 bit (double-precision) */ 6913765Smpp FP_PE /* 64 bit (extended-precision) */ 701834Swollman} fp_prec_t; 711834Swollman 721834Swollman#define fp_except_t int 731834Swollman 741834Swollman/* 75175228Sbde * Exception bit masks. 761834Swollman */ 771834Swollman#define FP_X_INV 0x01 /* invalid operation */ 781834Swollman#define FP_X_DNML 0x02 /* denormal */ 791834Swollman#define FP_X_DZ 0x04 /* zero divide */ 801834Swollman#define FP_X_OFL 0x08 /* overflow */ 811834Swollman#define FP_X_UFL 0x10 /* underflow */ 821834Swollman#define FP_X_IMP 0x20 /* (im)precision */ 8349081Scracauer#define FP_X_STK 0x40 /* stack fault */ 841834Swollman 851834Swollman/* 86175228Sbde * FPU control word bit-field masks. 871834Swollman */ 88175228Sbde#define FP_MSKS_FLD 0x3f /* exception masks field */ 89175228Sbde#define FP_PRC_FLD 0x300 /* precision control field */ 90175228Sbde#define FP_RND_FLD 0xc00 /* rounding control field */ 911834Swollman 921834Swollman/* 93175228Sbde * FPU status word bit-field masks. 941834Swollman */ 951834Swollman#define FP_STKY_FLD 0x3f /* sticky flags field */ 961834Swollman 971834Swollman/* 98175228Sbde * SSE mxcsr register bit-field masks. 99117863Speter */ 100117863Speter#define SSE_STKY_FLD 0x3f /* exception flags */ 101117863Speter#define SSE_DAZ_FLD 0x40 /* Denormals are zero */ 102117863Speter#define SSE_MSKS_FLD 0x1f80 /* exception masks field */ 103117863Speter#define SSE_RND_FLD 0x6000 /* rounding control */ 104117863Speter#define SSE_FZ_FLD 0x8000 /* flush to zero on underflow */ 105117863Speter 106117863Speter/* 107175228Sbde * FPU control word bit-field offsets (shift counts). 1081834Swollman */ 1091834Swollman#define FP_MSKS_OFF 0 /* exception masks offset */ 1101834Swollman#define FP_PRC_OFF 8 /* precision control offset */ 111175228Sbde#define FP_RND_OFF 10 /* rounding control offset */ 112175228Sbde 113175228Sbde/* 114175228Sbde * FPU status word bit-field offsets (shift counts). 115175228Sbde */ 1161834Swollman#define FP_STKY_OFF 0 /* sticky flags offset */ 1171834Swollman 118117863Speter/* 119175228Sbde * SSE mxcsr register bit-field offsets (shift counts). 120117863Speter */ 121117863Speter#define SSE_STKY_OFF 0 /* exception flags offset */ 122117863Speter#define SSE_DAZ_OFF 6 /* DAZ exception mask offset */ 123117863Speter#define SSE_MSKS_OFF 7 /* other exception masks offset */ 124117863Speter#define SSE_RND_OFF 13 /* rounding control offset */ 125117863Speter#define SSE_FZ_OFF 15 /* flush to zero offset */ 126109520Smarcel 127175180Sbde#ifdef __GNUCLIKE_ASM 128117863Speter 129175179Sbde#define __fldcw(addr) __asm __volatile("fldcw %0" : : "m" (*(addr))) 130175231Sbde#define __fldenv(addr) __asm __volatile("fldenv %0" : : "m" (*(addr))) 131334771Sdim#define __fnclex() __asm __volatile("fnclex") 132175179Sbde#define __fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr))) 133175231Sbde#define __fnstenv(addr) __asm __volatile("fnstenv %0" : "=m" (*(addr))) 134109520Smarcel#define __fnstsw(addr) __asm __volatile("fnstsw %0" : "=m" (*(addr))) 135144968Sjhb#define __ldmxcsr(addr) __asm __volatile("ldmxcsr %0" : : "m" (*(addr))) 136117863Speter#define __stmxcsr(addr) __asm __volatile("stmxcsr %0" : "=m" (*(addr))) 137109520Smarcel 138109520Smarcel/* 139175231Sbde * Load the control word. Be careful not to trap if there is a currently 140175231Sbde * unmasked exception (ones that will become freshly unmasked are not a 141175231Sbde * problem). This case must be handled by a save/restore of the 142175231Sbde * environment or even of the full x87 state. Accessing the environment 143175231Sbde * is very inefficient, so only do it when necessary. 144175231Sbde */ 145175231Sbdestatic __inline void 146175231Sbde__fnldcw(unsigned short _cw, unsigned short _newcw) 147175231Sbde{ 148175231Sbde struct { 149175231Sbde unsigned _cw; 150175231Sbde unsigned _other[6]; 151175231Sbde } _env; 152175231Sbde unsigned short _sw; 153175231Sbde 154175231Sbde if ((_cw & FP_MSKS_FLD) != FP_MSKS_FLD) { 155175231Sbde __fnstsw(&_sw); 156175231Sbde if (((_sw & ~_cw) & FP_STKY_FLD) != 0) { 157175231Sbde __fnstenv(&_env); 158175231Sbde _env._cw = _newcw; 159175231Sbde __fldenv(&_env); 160175231Sbde return; 161175231Sbde } 162175231Sbde } 163175231Sbde __fldcw(&_newcw); 164175231Sbde} 165175231Sbde 166175231Sbde/* 167117863Speter * General notes about conflicting SSE vs FP status bits. 168117863Speter * This code assumes that software will not fiddle with the control 169117863Speter * bits of the SSE and x87 in such a way to get them out of sync and 170117863Speter * still expect this to work. Break this at your peril. 171117863Speter * Because I based this on the i386 port, the x87 state is used for 172117863Speter * the fpget*() functions, and is shadowed into the SSE state for 173117863Speter * the fpset*() functions. For dual source fpget*() functions, I 174117863Speter * merge the two together. I think. 175109520Smarcel */ 176117863Speter 177175179Sbdestatic __inline fp_rnd_t 178117863Speter__fpgetround(void) 179109520Smarcel{ 180117863Speter unsigned short _cw; 181109520Smarcel 182117863Speter __fnstcw(&_cw); 183175180Sbde return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF)); 184109520Smarcel} 185109520Smarcel 186175179Sbdestatic __inline fp_rnd_t 187117863Speter__fpsetround(fp_rnd_t _m) 188117863Speter{ 189175180Sbde fp_rnd_t _p; 190175180Sbde unsigned _mxcsr; 191175231Sbde unsigned short _cw, _newcw; 192117863Speter 193117863Speter __fnstcw(&_cw); 194175180Sbde _p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF); 195175231Sbde _newcw = _cw & ~FP_RND_FLD; 196175231Sbde _newcw |= (_m << FP_RND_OFF) & FP_RND_FLD; 197175231Sbde __fnldcw(_cw, _newcw); 198117863Speter __stmxcsr(&_mxcsr); 199117863Speter _mxcsr &= ~SSE_RND_FLD; 200117863Speter _mxcsr |= (_m << SSE_RND_OFF) & SSE_RND_FLD; 201117863Speter __ldmxcsr(&_mxcsr); 202117863Speter return (_p); 203117863Speter} 204117863Speter 205109520Smarcel/* 206175178Sbde * Get or set the rounding precision for x87 arithmetic operations. 207117863Speter * There is no equivalent SSE mode or control. 208109520Smarcel */ 209175178Sbde 210175179Sbdestatic __inline fp_prec_t 211117863Speter__fpgetprec(void) 212109520Smarcel{ 213117863Speter unsigned short _cw; 214117863Speter 215117863Speter __fnstcw(&_cw); 216175180Sbde return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF)); 217117863Speter} 218117863Speter 219175179Sbdestatic __inline fp_prec_t 220175180Sbde__fpsetprec(fp_prec_t _m) 221117863Speter{ 222175179Sbde fp_prec_t _p; 223175231Sbde unsigned short _cw, _newcw; 224117863Speter 225117863Speter __fnstcw(&_cw); 226175180Sbde _p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF); 227175231Sbde _newcw = _cw & ~FP_PRC_FLD; 228175231Sbde _newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD; 229175231Sbde __fnldcw(_cw, _newcw); 230117863Speter return (_p); 231117863Speter} 232117863Speter 233117863Speter/* 234175178Sbde * Get or set the exception mask. 235175178Sbde * Note that the x87 mask bits are inverted by the API -- a mask bit of 1 236175178Sbde * means disable for x87 and SSE, but for fp*mask() it means enable. 237117863Speter */ 238175178Sbde 239175179Sbdestatic __inline fp_except_t 240117863Speter__fpgetmask(void) 241117863Speter{ 242117863Speter unsigned short _cw; 243117863Speter 244117863Speter __fnstcw(&_cw); 245175179Sbde return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF); 246117863Speter} 247117863Speter 248175179Sbdestatic __inline fp_except_t 249117863Speter__fpsetmask(fp_except_t _m) 250117863Speter{ 251175179Sbde fp_except_t _p; 252175179Sbde unsigned _mxcsr; 253175231Sbde unsigned short _cw, _newcw; 254117863Speter 255117863Speter __fnstcw(&_cw); 256175179Sbde _p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF; 257175231Sbde _newcw = _cw & ~FP_MSKS_FLD; 258175231Sbde _newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD; 259175231Sbde __fnldcw(_cw, _newcw); 260117863Speter __stmxcsr(&_mxcsr); 261117863Speter /* XXX should we clear non-ieee SSE_DAZ_FLD and SSE_FZ_FLD ? */ 262117863Speter _mxcsr &= ~SSE_MSKS_FLD; 263175179Sbde _mxcsr |= (~_m << SSE_MSKS_OFF) & SSE_MSKS_FLD; 264117863Speter __ldmxcsr(&_mxcsr); 265117863Speter return (_p); 266117863Speter} 267117863Speter 268175179Sbdestatic __inline fp_except_t 269117863Speter__fpgetsticky(void) 270117863Speter{ 271175180Sbde unsigned _ex, _mxcsr; 272117863Speter unsigned short _sw; 273117863Speter 274117863Speter __fnstsw(&_sw); 275175179Sbde _ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF; 276117863Speter __stmxcsr(&_mxcsr); 277175179Sbde _ex |= (_mxcsr & SSE_STKY_FLD) >> SSE_STKY_OFF; 278175180Sbde return ((fp_except_t)_ex); 279117863Speter} 280117863Speter 281175180Sbde#endif /* __GNUCLIKE_ASM */ 282109520Smarcel 283175180Sbde#if !defined(__IEEEFP_NOINLINES__) && defined(__GNUCLIKE_ASM) 284109520Smarcel 285175228Sbde#define fpgetmask() __fpgetmask() 286175228Sbde#define fpgetprec() __fpgetprec() 287118331Speter#define fpgetround() __fpgetround() 288117863Speter#define fpgetsticky() __fpgetsticky() 289175228Sbde#define fpsetmask(m) __fpsetmask(m) 290175228Sbde#define fpsetprec(m) __fpsetprec(m) 291175228Sbde#define fpsetround(m) __fpsetround(m) 292117863Speter 293175180Sbde#else /* !(!__IEEEFP_NOINLINES__ && __GNUCLIKE_ASM) */ 294117863Speter 295175178Sbde/* Augment the userland declarations. */ 296117863Speter__BEGIN_DECLS 297226607Sdasextern fp_rnd_t fpgetround(void); 298226607Sdasextern fp_rnd_t fpsetround(fp_rnd_t); 299226607Sdasextern fp_except_t fpgetmask(void); 300226607Sdasextern fp_except_t fpsetmask(fp_except_t); 301226607Sdasextern fp_except_t fpgetsticky(void); 302226607Sdasextern fp_except_t fpsetsticky(fp_except_t); 303175179Sbdefp_prec_t fpgetprec(void); 304175179Sbdefp_prec_t fpsetprec(fp_prec_t); 305117863Speter__END_DECLS 306117863Speter 307175180Sbde#endif /* !__IEEEFP_NOINLINES__ && __GNUCLIKE_ASM */ 308117863Speter 3091862Swollman#endif /* !_MACHINE_IEEEFP_H_ */ 310