bsd_ieeefp.h 8.2 KB

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