gsl_ieee-utils__fp-openbsd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* fp-openbsd.c
  2. *
  3. * Copyright (C) 2001 Jason Beegan
  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 <ieeefp.h>
  20. #include "gsl_ieee_utils.h"
  21. #include "gsl_errno.h"
  22. /* This is a copy of fp-netbsd.c, modified for openbsd by Toby White
  23. --- Brian Gough */
  24. int
  25. gsl_ieee_set_mode (int precision, int rounding, int exception_mask)
  26. {
  27. fp_except mode = 0;
  28. fp_rnd rnd = 0;
  29. switch (precision)
  30. {
  31. case GSL_IEEE_SINGLE_PRECISION:
  32. GSL_ERROR ("OpenBSD only supports default precision rounding",
  33. GSL_EUNSUP);
  34. break;
  35. case GSL_IEEE_DOUBLE_PRECISION:
  36. GSL_ERROR ("OpenBSD only supports default precision rounding",
  37. GSL_EUNSUP);
  38. break;
  39. case GSL_IEEE_EXTENDED_PRECISION:
  40. GSL_ERROR ("OpenBSD only supports default precision rounding",
  41. GSL_EUNSUP);
  42. break;
  43. }
  44. switch (rounding)
  45. {
  46. case GSL_IEEE_ROUND_TO_NEAREST:
  47. rnd = FP_RN;
  48. fpsetround (rnd);
  49. break;
  50. case GSL_IEEE_ROUND_DOWN:
  51. rnd = FP_RM;
  52. fpsetround (rnd);
  53. break;
  54. case GSL_IEEE_ROUND_UP:
  55. rnd = FP_RP;
  56. fpsetround (rnd);
  57. break;
  58. case GSL_IEEE_ROUND_TO_ZERO:
  59. rnd = FP_RZ;
  60. fpsetround (rnd);
  61. break;
  62. default:
  63. rnd = FP_RN;
  64. fpsetround (rnd);
  65. }
  66. /* Turn on all available exceptions apart from 'inexact'.
  67. Denormalized operand exception not available on all platforms. */
  68. mode = FP_X_INV | FP_X_DZ | FP_X_OFL | FP_X_UFL;
  69. #ifdef FP_X_DNML
  70. mode = mode | FP_X_DNML;
  71. #endif
  72. if (exception_mask & GSL_IEEE_MASK_INVALID)
  73. mode &= ~ FP_X_INV;
  74. if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
  75. {
  76. #ifdef FP_X_DNML
  77. mode &= ~ FP_X_DNML;
  78. #endif
  79. }
  80. else
  81. {
  82. #ifndef FP_X_DNML
  83. GSL_ERROR ("OpenBSD does not support the denormalized operand exception on this platform. "
  84. "Use 'mask-denormalized' to work around this.",
  85. GSL_EUNSUP);
  86. #endif
  87. }
  88. if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
  89. mode &= ~ FP_X_DZ;
  90. if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
  91. mode &= ~ FP_X_OFL;
  92. if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
  93. mode &= ~ FP_X_UFL;
  94. if (exception_mask & GSL_IEEE_TRAP_INEXACT)
  95. {
  96. mode |= FP_X_IMP;
  97. }
  98. else
  99. {
  100. mode &= ~ FP_X_IMP;
  101. }
  102. fpsetmask (mode);
  103. return GSL_SUCCESS;
  104. }