gsl_ieee-utils__fp-netbsd.c 2.9 KB

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