gsl_ieee-utils__fp-aix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* ieee-utils/fp-aix.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Tim Mooney
  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 <math.h>
  20. #include <fptrap.h>
  21. #include <float.h>
  22. #include "gsl_ieee_utils.h"
  23. #include "gsl_errno.h"
  24. int
  25. gsl_ieee_set_mode (int precision, int rounding, int exception_mask)
  26. {
  27. fptrap_t mode = 0 ;
  28. fprnd_t rnd = 0 ;
  29. switch (precision)
  30. {
  31. /* I'm not positive about AIX only supporting default precision rounding,
  32. * but this is the best assumption until it's proven otherwise. */
  33. case GSL_IEEE_SINGLE_PRECISION:
  34. GSL_ERROR ("AIX only supports default precision rounding",
  35. GSL_EUNSUP) ;
  36. break ;
  37. case GSL_IEEE_DOUBLE_PRECISION:
  38. GSL_ERROR ("AIX only supports default precision rounding",
  39. GSL_EUNSUP) ;
  40. break ;
  41. case GSL_IEEE_EXTENDED_PRECISION:
  42. GSL_ERROR ("AIX only supports default precision rounding",
  43. GSL_EUNSUP) ;
  44. break ;
  45. }
  46. switch (rounding)
  47. {
  48. case GSL_IEEE_ROUND_TO_NEAREST:
  49. rnd = FP_RND_RN ;
  50. fp_swap_rnd (rnd) ;
  51. break ;
  52. case GSL_IEEE_ROUND_DOWN:
  53. rnd = FP_RND_RM ;
  54. fp_swap_rnd (rnd) ;
  55. break ;
  56. case GSL_IEEE_ROUND_UP:
  57. rnd = FP_RND_RP ;
  58. fp_swap_rnd (rnd) ;
  59. break ;
  60. case GSL_IEEE_ROUND_TO_ZERO:
  61. rnd = FP_RND_RZ ;
  62. fp_swap_rnd (rnd) ;
  63. break ;
  64. default:
  65. rnd = FP_RND_RN ;
  66. fp_swap_rnd (rnd) ;
  67. }
  68. /* Turn on all the exceptions apart from 'inexact' */
  69. mode = TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW | TRP_UNDERFLOW ;
  70. if (exception_mask & GSL_IEEE_MASK_INVALID)
  71. mode &= ~ TRP_INVALID ;
  72. if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
  73. {
  74. /* do nothing */
  75. }
  76. else
  77. {
  78. GSL_ERROR ("AIX does not support the denormalized operand exception. "
  79. "Use 'mask-denormalized' to work around this.",
  80. GSL_EUNSUP) ;
  81. }
  82. if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
  83. mode &= ~ TRP_DIV_BY_ZERO ;
  84. if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
  85. mode &= ~ TRP_OVERFLOW ;
  86. if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
  87. mode &= ~ TRP_UNDERFLOW ;
  88. if (exception_mask & GSL_IEEE_TRAP_INEXACT)
  89. {
  90. mode |= TRP_INEXACT ;
  91. }
  92. else
  93. {
  94. mode &= ~ TRP_INEXACT ;
  95. }
  96. /* AIX appears to require two steps -- first enable floating point traps
  97. * in general... */
  98. fp_trap(FP_TRAP_SYNC);
  99. /* next, enable the traps we're interested in */
  100. fp_enable(mode);
  101. return GSL_SUCCESS ;
  102. }