gsl_ieee-utils__fp-tru64.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* ieee-utils/fp-tru64.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. /*
  20. * Under Compaq's Unix with the silly name, read the man pages for read_rnd,
  21. * write_rnd, and ieee(3) for more information on the functions used here.
  22. *
  23. * Note that enabling control of dynamic rounding mode (via write_rnd) requires
  24. * that you pass a special flag to your C compiler. For Compaq's C compiler
  25. * the flag is `-fprm d', for gcc it's `-mfp-rounding-mode=d'.
  26. *
  27. * Enabling the trap control (via ieee_set_fp_control) also requires a
  28. * flag be passed to the C compiler. The flag for Compaq's C compiler
  29. * is `-ieee' and for gcc it's `-mieee'.
  30. * We have not implemented the `inexact' case, since it is rarely used
  31. * and requires the library being built with an additional compiler
  32. * flag that can degrade performance for everything else. If you need
  33. * to add support for `inexact' the relevant flag for Compaq's
  34. * compiler is `-ieee_with_inexact', and the flag for gcc is
  35. * `-mieee-with-inexact'.
  36. *
  37. * Problem have been reported with the "fixed" float.h installed with
  38. * gcc-2.95 lacking some of the definitions in the system float.h (the
  39. * symptoms are errors like: `FP_RND_RN' undeclared). To work around
  40. * this we can include the system float.h before the gcc version, e.g.
  41. *
  42. * #include "/usr/include/float.h"
  43. * #include <float.h>
  44. */
  45. #include <float.h>
  46. #ifndef FP_RND_RN
  47. # undef _FLOAT_H_
  48. # include /usr/include/float.h
  49. # undef _FLOAT_H_
  50. # include <float.h>
  51. #endif
  52. #include <machine/fpu.h>
  53. #include <stdio.h>
  54. #include "gsl_ieee_utils.h"
  55. #include "gsl_errno.h"
  56. int
  57. gsl_ieee_set_mode (int precision, int rounding, int exception_mask)
  58. {
  59. unsigned long int mode = 0 ;
  60. unsigned int rnd = 0 ;
  61. /* I'm actually not completely sure that the alpha only supports default
  62. * precisions rounding, but I couldn't find any information regarding this, so
  63. * it seems safe to assume this for now until it's proven otherwise.
  64. */
  65. switch (precision)
  66. {
  67. case GSL_IEEE_SINGLE_PRECISION:
  68. GSL_ERROR ("Tru64 Unix on the alpha only supports default precision rounding",
  69. GSL_EUNSUP) ;
  70. break ;
  71. case GSL_IEEE_DOUBLE_PRECISION:
  72. GSL_ERROR ("Tru64 Unix on the alpha only supports default precision rounding",
  73. GSL_EUNSUP) ;
  74. break ;
  75. case GSL_IEEE_EXTENDED_PRECISION:
  76. GSL_ERROR ("Tru64 Unix on the alpha only supports default precision rounding",
  77. GSL_EUNSUP) ;
  78. break ;
  79. }
  80. switch (rounding)
  81. {
  82. case GSL_IEEE_ROUND_TO_NEAREST:
  83. rnd = FP_RND_RN ;
  84. write_rnd (rnd) ;
  85. break ;
  86. case GSL_IEEE_ROUND_DOWN:
  87. rnd = FP_RND_RM ;
  88. write_rnd (rnd) ;
  89. break ;
  90. case GSL_IEEE_ROUND_UP:
  91. rnd = FP_RND_RP ;
  92. write_rnd (rnd) ;
  93. break ;
  94. case GSL_IEEE_ROUND_TO_ZERO:
  95. rnd = FP_RND_RZ ;
  96. write_rnd (rnd) ;
  97. break ;
  98. default:
  99. rnd = FP_RND_RN ;
  100. write_rnd (rnd) ;
  101. }
  102. /* Turn on all the exceptions apart from 'inexact' */
  103. /* from the ieee(3) man page:
  104. * IEEE_TRAP_ENABLE_INV -> Invalid operation
  105. * IEEE_TRAP_ENABLE_DZE -> Divide by 0
  106. * IEEE_TRAP_ENABLE_OVF -> Overflow
  107. * IEEE_TRAP_ENABLE_UNF -> Underflow
  108. * IEEE_TRAP_ENABLE_INE -> Inexact (requires special option to C compiler)
  109. * IEEE_TRAP_ENABLE_DNO -> denormal operand
  110. * Note: IEEE_TRAP_ENABLE_DNO is not supported on OSF 3.x or Digital Unix
  111. * 4.0 - 4.0d(?).
  112. * IEEE_TRAP_ENABLE_MASK -> mask of all the trap enables
  113. * IEEE_MAP_DMZ -> map denormal inputs to zero
  114. * IEEE_MAP_UMZ -> map underflow results to zero
  115. */
  116. mode = IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF
  117. | IEEE_TRAP_ENABLE_UNF ;
  118. if (exception_mask & GSL_IEEE_MASK_INVALID)
  119. mode &= ~ IEEE_TRAP_ENABLE_INV ;
  120. if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
  121. {
  122. #ifdef IEEE_TRAP_ENABLE_DNO
  123. mode &= ~ IEEE_TRAP_ENABLE_DNO ;
  124. #else
  125. GSL_ERROR ("Sorry, this version of Digital Unix does not support denormalized operands", GSL_EUNSUP) ;
  126. #endif
  127. }
  128. if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
  129. mode &= ~ IEEE_TRAP_ENABLE_DZE ;
  130. if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
  131. mode &= ~ IEEE_TRAP_ENABLE_OVF ;
  132. if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
  133. mode &= ~ IEEE_TRAP_ENABLE_UNF ;
  134. if (exception_mask & GSL_IEEE_TRAP_INEXACT)
  135. {
  136. /* To implement this would require a special flag to the C
  137. compiler which can cause degraded performance */
  138. GSL_ERROR ("Sorry, GSL does not implement trap-inexact for Tru64 Unix on the alpha - see fp-tru64.c for details", GSL_EUNSUP) ;
  139. /* In case you need to add it, the appropriate line would be
  140. *
  141. * mode |= IEEE_TRAP_ENABLE_INE ;
  142. *
  143. */
  144. }
  145. else
  146. {
  147. mode &= ~ IEEE_TRAP_ENABLE_INE ;
  148. }
  149. ieee_set_fp_control (mode) ;
  150. return GSL_SUCCESS ;
  151. }