bsd_ieeefp.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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*FLD and FP*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. * FPU control word bit-field offsets (shift counts).
  89. */
  90. #define FP_MSKS_OFF 0 /* exception masks offset */
  91. #define FP_PRC_OFF 8 /* precision control offset */
  92. #define FP_RND_OFF 10 /* rounding control offset */
  93. /*
  94. * FPU status word bit-field offsets (shift counts).
  95. */
  96. #define FP_STKY_OFF 0 /* sticky flags offset */
  97. #ifdef __GNUCLIKE_ASM
  98. #define __fldcw(addr) __asm __volatile("fldcw %0" : : "m" (*(addr)))
  99. #define __fldenv(addr) __asm __volatile("fldenv %0" : : "m" (*(addr)))
  100. #define __fnclex() __asm __volatile("fnclex")
  101. #define __fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr)))
  102. #define __fnstenv(addr) __asm __volatile("fnstenv %0" : "=m" (*(addr)))
  103. #define __fnstsw(addr) __asm __volatile("fnstsw %0" : "=m" (*(addr)))
  104. /*
  105. * Load the control word. Be careful not to trap if there is a currently
  106. * unmasked exception (ones that will become freshly unmasked are not a
  107. * problem). This case must be handled by a save/restore of the
  108. * environment or even of the full x87 state. Accessing the environment
  109. * is very inefficient, so only do it when necessary.
  110. */
  111. static __inline void
  112. __fnldcw(unsigned short _cw, unsigned short _newcw)
  113. {
  114. struct {
  115. unsigned _cw;
  116. unsigned _other[6];
  117. } _env;
  118. unsigned short _sw;
  119. if ((_cw & FP_MSKS_FLD) != FP_MSKS_FLD) {
  120. __fnstsw(&_sw);
  121. if (((_sw & ~_cw) & FP_STKY_FLD) != 0) {
  122. __fnstenv(&_env);
  123. _env._cw = _newcw;
  124. __fldenv(&_env);
  125. return;
  126. }
  127. }
  128. __fldcw(&_newcw);
  129. }
  130. static __inline fp_rnd_t
  131. fpgetround(void)
  132. {
  133. unsigned short _cw;
  134. __fnstcw(&_cw);
  135. return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF));
  136. }
  137. static __inline fp_rnd_t
  138. fpsetround(fp_rnd_t _m)
  139. {
  140. fp_rnd_t _p;
  141. unsigned short _cw, _newcw;
  142. __fnstcw(&_cw);
  143. _p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF);
  144. _newcw = _cw & ~FP_RND_FLD;
  145. _newcw |= (_m << FP_RND_OFF) & FP_RND_FLD;
  146. __fnldcw(_cw, _newcw);
  147. return (_p);
  148. }
  149. static __inline fp_prec_t
  150. fpgetprec(void)
  151. {
  152. unsigned short _cw;
  153. __fnstcw(&_cw);
  154. return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF));
  155. }
  156. static __inline fp_prec_t
  157. fpsetprec(fp_prec_t _m)
  158. {
  159. fp_prec_t _p;
  160. unsigned short _cw, _newcw;
  161. __fnstcw(&_cw);
  162. _p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
  163. _newcw = _cw & ~FP_PRC_FLD;
  164. _newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
  165. __fnldcw(_cw, _newcw);
  166. return (_p);
  167. }
  168. /*
  169. * Get or set the exception mask.
  170. * Note that the x87 mask bits are inverted by the API -- a mask bit of 1
  171. * means disable for x87 and SSE, but for fp*mask() it means enable.
  172. */
  173. static __inline fp_except_t
  174. fpgetmask(void)
  175. {
  176. unsigned short _cw;
  177. __fnstcw(&_cw);
  178. return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF);
  179. }
  180. static __inline fp_except_t
  181. fpsetmask(fp_except_t _m)
  182. {
  183. fp_except_t _p;
  184. unsigned short _cw, _newcw;
  185. __fnstcw(&_cw);
  186. _p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF;
  187. _newcw = _cw & ~FP_MSKS_FLD;
  188. _newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD;
  189. __fnldcw(_cw, _newcw);
  190. return (_p);
  191. }
  192. static __inline fp_except_t
  193. fpgetsticky(void)
  194. {
  195. unsigned _ex;
  196. unsigned short _sw;
  197. __fnstsw(&_sw);
  198. _ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF;
  199. return ((fp_except_t)_ex);
  200. }
  201. static __inline fp_except_t
  202. fpresetsticky(fp_except_t _m)
  203. {
  204. struct {
  205. unsigned _cw;
  206. unsigned _sw;
  207. unsigned _other[5];
  208. } _env;
  209. fp_except_t _p;
  210. _m &= FP_STKY_FLD >> FP_STKY_OFF;
  211. _p = fpgetsticky();
  212. if ((_p & ~_m) == _p)
  213. return (_p);
  214. if ((_p & ~_m) == 0) {
  215. __fnclex();
  216. return (_p);
  217. }
  218. __fnstenv(&_env);
  219. _env._sw &= ~_m;
  220. __fldenv(&_env);
  221. return (_p);
  222. }
  223. #endif /* __GNUCLIKE_ASM */
  224. #endif /* !_MACHINE_IEEEFP_H_ */