bsd_ieeefp.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * from: @(#) ieeefp.h 1.0 (Berkeley) 9/23/93
  31. * $FreeBSD: src/sys/amd64/include/ieeefp.h,v 1.14 2005/04/12 23:12:00 jhb Exp $
  32. */
  33. /*
  34. * IEEE floating point type and constant definitions.
  35. */
  36. #ifndef _MACHINE_IEEEFP_H_
  37. #define _MACHINE_IEEEFP_H_
  38. #if !defined(_SYS_CDEFS_H) && !defined(_SYS_CDEFS_H_) && !defined(_CDEFS_H_)
  39. #error this file needs sys/cdefs.h as a prerequisite
  40. #endif
  41. /*
  42. * FP rounding modes
  43. */
  44. typedef enum {
  45. FP_RN=0, /* round to nearest */
  46. FP_RM, /* round down to minus infinity */
  47. FP_RP, /* round up to plus infinity */
  48. FP_RZ /* truncate */
  49. } fp_rnd_t;
  50. /*
  51. * FP precision modes
  52. */
  53. typedef enum {
  54. FP_PS=0, /* 24 bit (single-precision) */
  55. FP_PRS, /* reserved */
  56. FP_PD, /* 53 bit (double-precision) */
  57. FP_PE /* 64 bit (extended-precision) */
  58. } fp_prec_t;
  59. #define fp_except_t int
  60. /*
  61. * FP exception masks
  62. */
  63. #define FP_X_INV 0x01 /* invalid operation */
  64. #define FP_X_DNML 0x02 /* denormal */
  65. #define FP_X_DZ 0x04 /* zero divide */
  66. #define FP_X_OFL 0x08 /* overflow */
  67. #define FP_X_UFL 0x10 /* underflow */
  68. #define FP_X_IMP 0x20 /* (im)precision */
  69. #define FP_X_STK 0x40 /* stack fault */
  70. /*
  71. * FP registers
  72. */
  73. #define FP_MSKS_REG 0 /* exception masks */
  74. #define FP_PRC_REG 0 /* precision */
  75. #define FP_RND_REG 0 /* direction */
  76. #define FP_STKY_REG 1 /* sticky flags */
  77. /*
  78. * FP register 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 /* round control field */
  83. #define FP_STKY_FLD 0x3f /* sticky flags field */
  84. /*
  85. * SSE mxcsr register bit field masks
  86. */
  87. #define SSE_STKY_FLD 0x3f /* exception flags */
  88. #define SSE_DAZ_FLD 0x40 /* Denormals are zero */
  89. #define SSE_MSKS_FLD 0x1f80 /* exception masks field */
  90. #define SSE_RND_FLD 0x6000 /* rounding control */
  91. #define SSE_FZ_FLD 0x8000 /* flush to zero on underflow */
  92. /*
  93. * FP register bit field offsets
  94. */
  95. #define FP_MSKS_OFF 0 /* exception masks offset */
  96. #define FP_PRC_OFF 8 /* precision control offset */
  97. #define FP_RND_OFF 10 /* round control offset */
  98. #define FP_STKY_OFF 0 /* sticky flags offset */
  99. /*
  100. * SSE mxcsr register bit field offsets
  101. */
  102. #define SSE_STKY_OFF 0 /* exception flags offset */
  103. #define SSE_DAZ_OFF 6 /* DAZ exception mask offset */
  104. #define SSE_MSKS_OFF 7 /* other exception masks offset */
  105. #define SSE_RND_OFF 13 /* rounding control offset */
  106. #define SSE_FZ_OFF 15 /* flush to zero offset */
  107. #if (defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE__)) || defined(__WIN32__) \
  108. && !defined(__cplusplus)
  109. #define __fldenv(addr) __asm __volatile("fldenv %0" : : "m" (*(addr)))
  110. #define __fnstenv(addr) __asm __volatile("fnstenv %0" : "=m" (*(addr)))
  111. #define __fldcw(addr) __asm __volatile("fldcw %0" : : "m" (*(addr)))
  112. #define __fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr)))
  113. #define __fnstsw(addr) __asm __volatile("fnstsw %0" : "=m" (*(addr)))
  114. #define __ldmxcsr(addr) __asm __volatile("ldmxcsr %0" : : "m" (*(addr)))
  115. #define __stmxcsr(addr) __asm __volatile("stmxcsr %0" : "=m" (*(addr)))
  116. /*
  117. * General notes about conflicting SSE vs FP status bits.
  118. * This code assumes that software will not fiddle with the control
  119. * bits of the SSE and x87 in such a way to get them out of sync and
  120. * still expect this to work. Break this at your peril.
  121. * Because I based this on the i386 port, the x87 state is used for
  122. * the fpget*() functions, and is shadowed into the SSE state for
  123. * the fpset*() functions. For dual source fpget*() functions, I
  124. * merge the two together. I think.
  125. */
  126. /* Set rounding control */
  127. static __inline__ fp_rnd_t
  128. __fpgetround(void)
  129. {
  130. unsigned short _cw;
  131. __fnstcw(&_cw);
  132. return ((_cw & FP_RND_FLD) >> FP_RND_OFF);
  133. }
  134. static __inline__ fp_rnd_t
  135. __fpsetround(fp_rnd_t _m)
  136. {
  137. unsigned short _cw;
  138. unsigned int _mxcsr;
  139. fp_rnd_t _p;
  140. __fnstcw(&_cw);
  141. _p = (_cw & FP_RND_FLD) >> FP_RND_OFF;
  142. _cw &= ~FP_RND_FLD;
  143. _cw |= (_m << FP_RND_OFF) & FP_RND_FLD;
  144. __fldcw(&_cw);
  145. __stmxcsr(&_mxcsr);
  146. _mxcsr &= ~SSE_RND_FLD;
  147. _mxcsr |= (_m << SSE_RND_OFF) & SSE_RND_FLD;
  148. __ldmxcsr(&_mxcsr);
  149. return (_p);
  150. }
  151. /*
  152. * Set precision for fadd/fsub/fsqrt etc x87 instructions
  153. * There is no equivalent SSE mode or control.
  154. */
  155. static __inline__ fp_prec_t
  156. __fpgetprec(void)
  157. {
  158. unsigned short _cw;
  159. __fnstcw(&_cw);
  160. return ((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
  161. }
  162. static __inline__ fp_prec_t
  163. __fpsetprec(fp_rnd_t _m)
  164. {
  165. unsigned short _cw;
  166. fp_prec_t _p;
  167. __fnstcw(&_cw);
  168. _p = (_cw & FP_PRC_FLD) >> FP_PRC_OFF;
  169. _cw &= ~FP_PRC_FLD;
  170. _cw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
  171. __fldcw(&_cw);
  172. return (_p);
  173. }
  174. /*
  175. * Look at the exception masks
  176. * Note that x87 masks are inverse of the fp*() functions
  177. * API. ie: mask = 1 means disable for x87 and SSE, but
  178. * for the fp*() api, mask = 1 means enabled.
  179. */
  180. static __inline__ fp_except_t
  181. __fpgetmask(void)
  182. {
  183. unsigned short _cw;
  184. __fnstcw(&_cw);
  185. return ((~_cw) & FP_MSKS_FLD);
  186. }
  187. static __inline__ fp_except_t
  188. __fpsetmask(fp_except_t _m)
  189. {
  190. unsigned short _cw;
  191. unsigned int _mxcsr;
  192. fp_except_t _p;
  193. __fnstcw(&_cw);
  194. _p = (~_cw) & FP_MSKS_FLD;
  195. _cw &= ~FP_MSKS_FLD;
  196. _cw |= (~_m) & FP_MSKS_FLD;
  197. __fldcw(&_cw);
  198. __stmxcsr(&_mxcsr);
  199. /* XXX should we clear non-ieee SSE_DAZ_FLD and SSE_FZ_FLD ? */
  200. _mxcsr &= ~SSE_MSKS_FLD;
  201. _mxcsr |= ((~_m) << SSE_MSKS_OFF) & SSE_MSKS_FLD;
  202. __ldmxcsr(&_mxcsr);
  203. return (_p);
  204. }
  205. /* See which sticky exceptions are pending, and reset them */
  206. static __inline__ fp_except_t
  207. __fpgetsticky(void)
  208. {
  209. unsigned short _sw;
  210. unsigned int _mxcsr;
  211. fp_except_t _ex;
  212. __fnstsw(&_sw);
  213. _ex = _sw & FP_STKY_FLD;
  214. __stmxcsr(&_mxcsr);
  215. _ex |= _mxcsr & SSE_STKY_FLD;
  216. return (_ex);
  217. }
  218. #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE__ && !__cplusplus */
  219. #if !defined(__IEEEFP_NOINLINES__) && !defined(__cplusplus) \
  220. && defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE__)
  221. #define fpgetround() __fpgetround()
  222. #define fpsetround(_m) __fpsetround(_m)
  223. #define fpgetprec() __fpgetprec()
  224. #define fpsetprec(_m) __fpsetprec(_m)
  225. #define fpgetmask() __fpgetmask()
  226. #define fpsetmask(_m) __fpsetmask(_m)
  227. #define fpgetsticky() __fpgetsticky()
  228. /* Suppress prototypes in the MI header. */
  229. #define _IEEEFP_INLINED_ 1
  230. #else /* !__IEEEFP_NOINLINES__ && !__cplusplus && __GNUCLIKE_ASM
  231. && __CC_SUPPORTS___INLINE__ */
  232. /* Augment the userland declarations */
  233. __BEGIN_DECLS
  234. extern fp_prec_t fpgetprec(void);
  235. extern fp_prec_t fpsetprec(fp_prec_t);
  236. __END_DECLS
  237. #endif /* !__IEEEFP_NOINLINES__ && !__cplusplus && __GNUCLIKE_ASM
  238. && __CC_SUPPORTS___INLINE__ */
  239. #endif /* !_MACHINE_IEEEFP_H_ */