gsl_ieee-utils__fp-gnuc99.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* ieee-utils/fp-gnuc99.c
  2. *
  3. * Copyright (C) 2003, 2004, 2007 Brian Gough
  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. #define _GNU_SOURCE 1
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <fenv.h>
  23. #include "gsl_ieee_utils.h"
  24. #include "gsl_errno.h"
  25. int
  26. gsl_ieee_set_mode (int precision, int rounding, int exception_mask)
  27. {
  28. int mode;
  29. switch (precision)
  30. {
  31. case GSL_IEEE_SINGLE_PRECISION:
  32. GSL_ERROR ("single precision rounding is not supported by <fenv.h>",
  33. GSL_EUNSUP) ;
  34. break ;
  35. case GSL_IEEE_DOUBLE_PRECISION:
  36. GSL_ERROR ("double precision rounding is not supported by <fenv.h>",
  37. GSL_EUNSUP) ;
  38. break ;
  39. case GSL_IEEE_EXTENDED_PRECISION:
  40. GSL_ERROR ("extended precision rounding is not supported by <fenv.h>",
  41. GSL_EUNSUP) ;
  42. break ;
  43. }
  44. switch (rounding)
  45. {
  46. case GSL_IEEE_ROUND_TO_NEAREST:
  47. #ifdef FE_TONEAREST
  48. fesetround (FE_TONEAREST) ;
  49. #else
  50. GSL_ERROR ("round-to-nearest is not supported by <fenv.h>", GSL_EUNSUP) ;
  51. #endif
  52. break ;
  53. case GSL_IEEE_ROUND_DOWN:
  54. #ifdef FE_DOWNWARD
  55. fesetround (FE_DOWNWARD) ;
  56. #else
  57. GSL_ERROR ("round-down is not supported by <fenv.h>", GSL_EUNSUP) ;
  58. #endif
  59. break ;
  60. case GSL_IEEE_ROUND_UP:
  61. #ifdef FE_UPWARD
  62. fesetround (FE_UPWARD) ;
  63. #else
  64. GSL_ERROR ("round-up is not supported by <fenv.h>", GSL_EUNSUP) ;
  65. #endif
  66. break ;
  67. case GSL_IEEE_ROUND_TO_ZERO:
  68. #ifdef FE_TOWARDZERO
  69. fesetround (FE_TOWARDZERO) ;
  70. #else
  71. GSL_ERROR ("round-toward-zero is not supported by <fenv.h>", GSL_EUNSUP) ;
  72. #endif
  73. break ;
  74. default:
  75. #ifdef FE_TONEAREST
  76. fesetround (FE_TONEAREST) ;
  77. #else
  78. GSL_ERROR ("default round-to-nearest mode is not supported by <fenv.h>", GSL_EUNSUP) ;
  79. #endif
  80. }
  81. /* Turn on all the exceptions apart from 'inexact' */
  82. mode = 0;
  83. #ifdef FE_INVALID
  84. mode |= FE_INVALID;
  85. #endif
  86. #ifdef FE_DIVBYZERO
  87. mode |= FE_DIVBYZERO;
  88. #endif
  89. #ifdef FE_OVERFLOW
  90. mode |= FE_OVERFLOW ;
  91. #endif
  92. #ifdef FE_UNDERFLOW
  93. mode |= FE_UNDERFLOW ;
  94. #endif
  95. if (exception_mask & GSL_IEEE_MASK_INVALID)
  96. {
  97. #ifdef FE_INVALID
  98. mode &= ~ FE_INVALID ;
  99. #else
  100. GSL_ERROR ("invalid operation exception not supported by <fenv.h>",
  101. GSL_EUNSUP);
  102. #endif
  103. }
  104. if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
  105. {
  106. /* do nothing */
  107. }
  108. else
  109. {
  110. GSL_ERROR ("denormalized operand exception not supported by <fenv.h>. "
  111. "Use 'mask-denormalized' to work around this.", GSL_EUNSUP) ;
  112. }
  113. if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
  114. {
  115. #ifdef FE_DIVBYZERO
  116. mode &= ~ FE_DIVBYZERO ;
  117. #else
  118. GSL_ERROR ("division by zero exception not supported by <fenv.h>",
  119. GSL_EUNSUP);
  120. #endif
  121. }
  122. if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
  123. {
  124. #ifdef FE_OVERFLOW
  125. mode &= ~ FE_OVERFLOW ;
  126. #else
  127. GSL_ERROR ("overflow exception not supported by <fenv.h>", GSL_EUNSUP);
  128. #endif
  129. }
  130. if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
  131. {
  132. #ifdef FE_UNDERFLOW
  133. mode &= ~ FE_UNDERFLOW ;
  134. #else
  135. GSL_ERROR ("underflow exception not supported by <fenv.h>", GSL_EUNSUP);
  136. #endif
  137. }
  138. if (exception_mask & GSL_IEEE_TRAP_INEXACT)
  139. {
  140. #ifdef FE_INEXACT
  141. mode |= FE_INEXACT ;
  142. #else
  143. GSL_ERROR ("inexact exception not supported by <fenv.h>", GSL_EUNSUP);
  144. #endif
  145. }
  146. else
  147. {
  148. #ifdef FE_INEXACT
  149. mode &= ~ FE_INEXACT ;
  150. #else
  151. /* do nothing */
  152. #endif
  153. }
  154. #if HAVE_DECL_FEENABLEEXCEPT
  155. feenableexcept (mode) ;
  156. #elif HAVE_DECL_FESETTRAPENABLE
  157. fesettrapenable (mode);
  158. #else
  159. GSL_ERROR ("unknown exception trap method", GSL_EUNSUP)
  160. #endif
  161. return GSL_SUCCESS ;
  162. }