bsd_ieeefp.h 8.8 KB

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