bsd_ieeefp.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /* VBS
  46. #ifndef _SYS_CDEFS_H_
  47. #error this file needs sys/cdefs.h as a prerequisite
  48. #endif
  49. */
  50. /*
  51. * Rounding modes.
  52. */
  53. typedef enum {
  54. FP_RN=0, /* round to nearest */
  55. FP_RM, /* round down towards minus infinity */
  56. FP_RP, /* round up towards plus infinity */
  57. FP_RZ /* truncate */
  58. } fp_rnd_t;
  59. /*
  60. * Precision (i.e., rounding precision) modes.
  61. */
  62. typedef enum {
  63. FP_PS=0, /* 24 bit (single-precision) */
  64. FP_PRS, /* reserved */
  65. FP_PD, /* 53 bit (double-precision) */
  66. FP_PE /* 64 bit (extended-precision) */
  67. } fp_prec_t;
  68. #define fp_except_t int
  69. /*
  70. * Exception bit masks.
  71. */
  72. #define FP_X_INV 0x01 /* invalid operation */
  73. #define FP_X_DNML 0x02 /* denormal */
  74. #define FP_X_DZ 0x04 /* zero divide */
  75. #define FP_X_OFL 0x08 /* overflow */
  76. #define FP_X_UFL 0x10 /* underflow */
  77. #define FP_X_IMP 0x20 /* (im)precision */
  78. #define FP_X_STK 0x40 /* stack fault */
  79. /*
  80. * FPU control word bit-field masks.
  81. */
  82. #define FP_MSKS_FLD 0x3f /* exception masks field */
  83. #define FP_PRC_FLD 0x300 /* precision control field */
  84. #define FP_RND_FLD 0xc00 /* rounding control field */
  85. /*
  86. * FPU status word bit-field masks.
  87. */
  88. #define FP_STKY_FLD 0x3f /* sticky flags field */
  89. /*
  90. * FPU control word bit-field offsets (shift counts).
  91. */
  92. #define FP_MSKS_OFF 0 /* exception masks offset */
  93. #define FP_PRC_OFF 8 /* precision control offset */
  94. #define FP_RND_OFF 10 /* rounding control offset */
  95. /*
  96. * FPU status word bit-field offsets (shift counts).
  97. */
  98. #define FP_STKY_OFF 0 /* sticky flags offset */
  99. //VBS
  100. //#ifdef __GNUCLIKE_ASM
  101. #define __fldcw(addr) __asm __volatile("fldcw %0" : : "m" (*(addr)))
  102. #define __fldenv(addr) __asm __volatile("fldenv %0" : : "m" (*(addr)))
  103. #define __fnclex() __asm __volatile("fnclex")
  104. #define __fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr)))
  105. #define __fnstenv(addr) __asm __volatile("fnstenv %0" : "=m" (*(addr)))
  106. #define __fnstsw(addr) __asm __volatile("fnstsw %0" : "=m" (*(addr)))
  107. /*
  108. * Load the control word. Be careful not to trap if there is a currently
  109. * unmasked exception (ones that will become freshly unmasked are not a
  110. * problem). This case must be handled by a save/restore of the
  111. * environment or even of the full x87 state. Accessing the environment
  112. * is very inefficient, so only do it when necessary.
  113. */
  114. static __inline void
  115. __fnldcw(unsigned short _cw, unsigned short _newcw)
  116. {
  117. struct {
  118. unsigned _cw;
  119. unsigned _other[6];
  120. } _env;
  121. unsigned short _sw;
  122. if ((_cw & FP_MSKS_FLD) != FP_MSKS_FLD) {
  123. __fnstsw(&_sw);
  124. if (((_sw & ~_cw) & FP_STKY_FLD) != 0) {
  125. __fnstenv(&_env);
  126. _env._cw = _newcw;
  127. __fldenv(&_env);
  128. return;
  129. }
  130. }
  131. __fldcw(&_newcw);
  132. }
  133. static __inline fp_rnd_t
  134. fpgetround(void)
  135. {
  136. unsigned short _cw;
  137. __fnstcw(&_cw);
  138. return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF));
  139. }
  140. static __inline fp_rnd_t
  141. fpsetround(fp_rnd_t _m)
  142. {
  143. fp_rnd_t _p;
  144. unsigned short _cw, _newcw;
  145. __fnstcw(&_cw);
  146. _p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF);
  147. _newcw = _cw & ~FP_RND_FLD;
  148. _newcw |= (_m << FP_RND_OFF) & FP_RND_FLD;
  149. __fnldcw(_cw, _newcw);
  150. return (_p);
  151. }
  152. //static __inline fp_prec_t
  153. DLLEXPORT fp_prec_t
  154. fpgetprec(void)
  155. {
  156. unsigned short _cw;
  157. __fnstcw(&_cw);
  158. return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF));
  159. }
  160. //static __inline fp_prec_t
  161. DLLEXPORT fp_prec_t
  162. fpsetprec(fp_prec_t _m)
  163. {
  164. fp_prec_t _p;
  165. unsigned short _cw, _newcw;
  166. __fnstcw(&_cw);
  167. _p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
  168. _newcw = _cw & ~FP_PRC_FLD;
  169. _newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
  170. __fnldcw(_cw, _newcw);
  171. return (_p);
  172. }
  173. /*
  174. * Get or set the exception mask.
  175. * Note that the x87 mask bits are inverted by the API -- a mask bit of 1
  176. * means disable for x87 and SSE, but for fp*mask() it means enable.
  177. */
  178. static __inline fp_except_t
  179. fpgetmask(void)
  180. {
  181. unsigned short _cw;
  182. __fnstcw(&_cw);
  183. return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF);
  184. }
  185. static __inline fp_except_t
  186. fpsetmask(fp_except_t _m)
  187. {
  188. fp_except_t _p;
  189. unsigned short _cw, _newcw;
  190. __fnstcw(&_cw);
  191. _p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF;
  192. _newcw = _cw & ~FP_MSKS_FLD;
  193. _newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD;
  194. __fnldcw(_cw, _newcw);
  195. return (_p);
  196. }
  197. static __inline fp_except_t
  198. fpgetsticky(void)
  199. {
  200. unsigned _ex;
  201. unsigned short _sw;
  202. __fnstsw(&_sw);
  203. _ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF;
  204. return ((fp_except_t)_ex);
  205. }
  206. static __inline fp_except_t
  207. fpresetsticky(fp_except_t _m)
  208. {
  209. struct {
  210. unsigned _cw;
  211. unsigned _sw;
  212. unsigned _other[5];
  213. } _env;
  214. fp_except_t _p;
  215. _m &= FP_STKY_FLD >> FP_STKY_OFF;
  216. _p = fpgetsticky();
  217. if ((_p & ~_m) == _p)
  218. return (_p);
  219. if ((_p & ~_m) == 0) {
  220. __fnclex();
  221. return (_p);
  222. }
  223. __fnstenv(&_env);
  224. _env._sw &= ~_m;
  225. __fldenv(&_env);
  226. return (_p);
  227. }
  228. //#endif /* __GNUCLIKE_ASM */
  229. #endif /* !_MACHINE_IEEEFP_H_ */