gsl_ieee-utils__fp-darwin86.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* ieee-utils/fp-darwin86.c
  2. *
  3. * Copyright (C) 2006 Erik Schnetter
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include "gsl_ieee_utils.h"
  21. #include "gsl_errno.h"
  22. /* Here is the dirty part. Set up your 387 through the control word
  23. * (cw) register.
  24. *
  25. * 15-13 12 11-10 9-8 7-6 5 4 3 2 1 0
  26. * | reserved | IC | RC | PC | reserved | PM | UM | OM | ZM | DM | IM
  27. *
  28. * IM: Invalid operation mask
  29. * DM: Denormalized operand mask
  30. * ZM: Zero-divide mask
  31. * OM: Overflow mask
  32. * UM: Underflow mask
  33. * PM: Precision (inexact result) mask
  34. *
  35. * Mask bit is 1 means no interrupt.
  36. *
  37. * PC: Precision control
  38. * 11 - round to extended precision
  39. * 10 - round to double precision
  40. * 00 - round to single precision
  41. *
  42. * RC: Rounding control
  43. * 00 - rounding to nearest
  44. * 01 - rounding down (toward - infinity)
  45. * 10 - rounding up (toward + infinity)
  46. * 11 - rounding toward zero
  47. *
  48. * IC: Infinity control
  49. * That is for 8087 and 80287 only.
  50. *
  51. * The hardware default is 0x037f which we use.
  52. */
  53. /* masking of interrupts */
  54. #define _FPU_MASK_IM 0x01
  55. #define _FPU_MASK_DM 0x02
  56. #define _FPU_MASK_ZM 0x04
  57. #define _FPU_MASK_OM 0x08
  58. #define _FPU_MASK_UM 0x10
  59. #define _FPU_MASK_PM 0x20
  60. /* precision control */
  61. #define _FPU_EXTENDED 0x300 /* libm requires double extended precision. */
  62. #define _FPU_DOUBLE 0x200
  63. #define _FPU_SINGLE 0x0
  64. /* rounding control */
  65. #define _FPU_RC_NEAREST 0x0 /* RECOMMENDED */
  66. #define _FPU_RC_DOWN 0x400
  67. #define _FPU_RC_UP 0x800
  68. #define _FPU_RC_ZERO 0xC00
  69. #define _FPU_RESERVED 0xF0C0 /* Reserved bits in cw */
  70. /* The fdlibm code requires strict IEEE double precision arithmetic,
  71. and no interrupts for exceptions, rounding to nearest. */
  72. #define _FPU_DEFAULT 0x037f
  73. /* IEEE: same as above. */
  74. #define _FPU_IEEE 0x037f
  75. /* Type of the control word. */
  76. typedef unsigned int fpu_control_t __attribute__ ((__mode__ (__HI__)));
  77. /* Macros for accessing the hardware control word.
  78. Note that the use of these macros is no sufficient anymore with
  79. recent hardware. Some floating point operations are executed in
  80. the SSE/SSE2 engines which have their own control and status register. */
  81. #define _FPU_GETCW(cw) __asm__ __volatile__ ("fnstcw %0" : "=m" (*&cw))
  82. #define _FPU_SETCW(cw) __asm__ __volatile__ ("fldcw %0" : : "m" (*&cw))
  83. /* Default control word set at startup. */
  84. extern fpu_control_t __fpu_control;
  85. #define _FPU_GETMXCSR(cw_sse) asm volatile ("stmxcsr %0" : "=m" (cw_sse))
  86. #define _FPU_SETMXCSR(cw_sse) asm volatile ("ldmxcsr %0" : : "m" (cw_sse))
  87. int
  88. gsl_ieee_set_mode (int precision, int rounding, int exception_mask)
  89. {
  90. fpu_control_t mode, mode_sse;
  91. _FPU_GETCW (mode) ;
  92. mode &= _FPU_RESERVED ;
  93. switch (precision)
  94. {
  95. case GSL_IEEE_SINGLE_PRECISION:
  96. mode |= _FPU_SINGLE ;
  97. break ;
  98. case GSL_IEEE_DOUBLE_PRECISION:
  99. mode |= _FPU_DOUBLE ;
  100. break ;
  101. case GSL_IEEE_EXTENDED_PRECISION:
  102. mode |= _FPU_EXTENDED ;
  103. break ;
  104. default:
  105. mode |= _FPU_EXTENDED ;
  106. }
  107. switch (rounding)
  108. {
  109. case GSL_IEEE_ROUND_TO_NEAREST:
  110. mode |= _FPU_RC_NEAREST ;
  111. break ;
  112. case GSL_IEEE_ROUND_DOWN:
  113. mode |= _FPU_RC_DOWN ;
  114. break ;
  115. case GSL_IEEE_ROUND_UP:
  116. mode |= _FPU_RC_UP ;
  117. break ;
  118. case GSL_IEEE_ROUND_TO_ZERO:
  119. mode |= _FPU_RC_ZERO ;
  120. break ;
  121. default:
  122. mode |= _FPU_RC_NEAREST ;
  123. }
  124. if (exception_mask & GSL_IEEE_MASK_INVALID)
  125. mode |= _FPU_MASK_IM ;
  126. if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
  127. mode |= _FPU_MASK_DM ;
  128. if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
  129. mode |= _FPU_MASK_ZM ;
  130. if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
  131. mode |= _FPU_MASK_OM ;
  132. if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
  133. mode |= _FPU_MASK_UM ;
  134. if (exception_mask & GSL_IEEE_TRAP_INEXACT)
  135. {
  136. mode &= ~ _FPU_MASK_PM ;
  137. }
  138. else
  139. {
  140. mode |= _FPU_MASK_PM ;
  141. }
  142. _FPU_SETCW (mode) ;
  143. _FPU_GETMXCSR (mode_sse) ;
  144. mode_sse &= 0xFFFF0000 ;
  145. if (exception_mask & GSL_IEEE_MASK_INVALID)
  146. mode_sse |= _FPU_MASK_IM << 7 ;
  147. if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
  148. mode_sse |= _FPU_MASK_DM << 7 ;
  149. if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
  150. mode_sse |= _FPU_MASK_ZM << 7 ;
  151. if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
  152. mode_sse |= _FPU_MASK_OM << 7 ;
  153. if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
  154. mode_sse |= _FPU_MASK_UM << 7 ;
  155. if (exception_mask & GSL_IEEE_TRAP_INEXACT)
  156. {
  157. mode_sse &= ~ _FPU_MASK_PM << 7 ;
  158. }
  159. else
  160. {
  161. mode_sse |= _FPU_MASK_PM << 7 ;
  162. }
  163. _FPU_SETMXCSR (mode_sse) ;
  164. return GSL_SUCCESS ;
  165. }