gsl_ieee-utils__fp-freebsd.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* ieee-utils/fp-freebsd.c
  2. *
  3. * Copyright (C) 2000 Vladimir Kushnir
  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_prec_t prec = 0 ;
  26. fp_except_t mode = 0 ;
  27. fp_rnd_t rnd = 0 ;
  28. switch (precision)
  29. {
  30. case GSL_IEEE_SINGLE_PRECISION:
  31. prec = FP_PS;
  32. fpsetprec(prec);
  33. break ;
  34. case GSL_IEEE_DOUBLE_PRECISION:
  35. prec = FP_PD;
  36. fpsetprec(prec);
  37. break ;
  38. case GSL_IEEE_EXTENDED_PRECISION:
  39. prec = FP_PE;
  40. fpsetprec(prec);
  41. break ;
  42. }
  43. switch (rounding)
  44. {
  45. case GSL_IEEE_ROUND_TO_NEAREST:
  46. rnd = FP_RN ;
  47. fpsetround (rnd) ;
  48. break ;
  49. case GSL_IEEE_ROUND_DOWN:
  50. rnd = FP_RM ;
  51. fpsetround (rnd) ;
  52. break ;
  53. case GSL_IEEE_ROUND_UP:
  54. rnd = FP_RP ;
  55. fpsetround (rnd) ;
  56. break ;
  57. case GSL_IEEE_ROUND_TO_ZERO:
  58. rnd = FP_RZ ;
  59. fpsetround (rnd) ;
  60. break ;
  61. default:
  62. rnd = FP_RN ;
  63. fpsetround (rnd) ;
  64. }
  65. /* Turn on all the exceptions apart from 'inexact' */
  66. mode = FP_X_INV | FP_X_DNML | FP_X_DZ | FP_X_OFL | FP_X_UFL ;
  67. if (exception_mask & GSL_IEEE_MASK_INVALID)
  68. mode &= ~ FP_X_INV ;
  69. if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
  70. mode &= ~ FP_X_DNML ;
  71. if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
  72. mode &= ~ FP_X_DZ ;
  73. if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
  74. mode &= ~ FP_X_OFL ;
  75. if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
  76. mode &= ~ FP_X_UFL ;
  77. if (exception_mask & GSL_IEEE_TRAP_INEXACT)
  78. {
  79. mode |= FP_X_IMP ;
  80. }
  81. else
  82. {
  83. mode &= ~ FP_X_IMP ;
  84. }
  85. fpsetmask (mode) ;
  86. return GSL_SUCCESS ;
  87. }