| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _GNU_SOURCE 1 |
| 21 |
|
| 22 |
#include <math.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <fenv.h> |
| 25 |
#include "gsl_ieee_utils.h" |
| 26 |
#include "gsl_errno.h" |
| 27 |
|
| 28 |
int |
| 29 |
gsl_ieee_set_mode (int precision, int rounding, int exception_mask) |
| 30 |
{ |
| 31 |
int mode; |
| 32 |
|
| 33 |
switch (precision) |
| 34 |
{ |
| 35 |
case GSL_IEEE_SINGLE_PRECISION: |
| 36 |
GSL_ERROR ("single precision rounding is not supported by <fenv.h>", |
| 37 |
GSL_EUNSUP) ; |
| 38 |
break ; |
| 39 |
case GSL_IEEE_DOUBLE_PRECISION: |
| 40 |
GSL_ERROR ("double precision rounding is not supported by <fenv.h>", |
| 41 |
GSL_EUNSUP) ; |
| 42 |
break ; |
| 43 |
case GSL_IEEE_EXTENDED_PRECISION: |
| 44 |
GSL_ERROR ("extended precision rounding is not supported by <fenv.h>", |
| 45 |
GSL_EUNSUP) ; |
| 46 |
break ; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
switch (rounding) |
| 51 |
{ |
| 52 |
case GSL_IEEE_ROUND_TO_NEAREST: |
| 53 |
#ifdef FE_TONEAREST |
| 54 |
fesetround (FE_TONEAREST) ; |
| 55 |
#else |
| 56 |
GSL_ERROR ("round-to-nearest is not supported by <fenv.h>", GSL_EUNSUP) ; |
| 57 |
#endif |
| 58 |
break ; |
| 59 |
case GSL_IEEE_ROUND_DOWN: |
| 60 |
#ifdef FE_DOWNWARD |
| 61 |
fesetround (FE_DOWNWARD) ; |
| 62 |
#else |
| 63 |
GSL_ERROR ("round-down is not supported by <fenv.h>", GSL_EUNSUP) ; |
| 64 |
#endif |
| 65 |
break ; |
| 66 |
case GSL_IEEE_ROUND_UP: |
| 67 |
#ifdef FE_UPWARD |
| 68 |
fesetround (FE_UPWARD) ; |
| 69 |
#else |
| 70 |
GSL_ERROR ("round-up is not supported by <fenv.h>", GSL_EUNSUP) ; |
| 71 |
#endif |
| 72 |
break ; |
| 73 |
case GSL_IEEE_ROUND_TO_ZERO: |
| 74 |
#ifdef FE_TOWARDZERO |
| 75 |
fesetround (FE_TOWARDZERO) ; |
| 76 |
#else |
| 77 |
GSL_ERROR ("round-toward-zero is not supported by <fenv.h>", GSL_EUNSUP) ; |
| 78 |
#endif |
| 79 |
break ; |
| 80 |
default: |
| 81 |
#ifdef FE_TONEAREST |
| 82 |
fesetround (FE_TONEAREST) ; |
| 83 |
#else |
| 84 |
GSL_ERROR ("default round-to-nearest mode is not supported by <fenv.h>", GSL_EUNSUP) ; |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
mode = 0; |
| 91 |
|
| 92 |
#ifdef FE_INVALID |
| 93 |
mode |= FE_INVALID; |
| 94 |
#endif |
| 95 |
|
| 96 |
#ifdef FE_DIVBYZERO |
| 97 |
mode |= FE_DIVBYZERO; |
| 98 |
#endif |
| 99 |
|
| 100 |
#ifdef FE_OVERFLOW |
| 101 |
mode |= FE_OVERFLOW ; |
| 102 |
#endif |
| 103 |
|
| 104 |
#ifdef FE_UNDERFLOW |
| 105 |
mode |= FE_UNDERFLOW ; |
| 106 |
#endif |
| 107 |
|
| 108 |
if (exception_mask & GSL_IEEE_MASK_INVALID) |
| 109 |
{ |
| 110 |
#ifdef FE_INVALID |
| 111 |
mode &= ~ FE_INVALID ; |
| 112 |
#else |
| 113 |
GSL_ERROR ("invalid operation exception not supported by <fenv.h>", |
| 114 |
GSL_EUNSUP); |
| 115 |
#endif |
| 116 |
} |
| 117 |
|
| 118 |
if (exception_mask & GSL_IEEE_MASK_DENORMALIZED) |
| 119 |
{ |
| 120 |
|
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
GSL_ERROR ("denormalized operand exception not supported by <fenv.h>. " |
| 125 |
"Use 'mask-denormalized' to work around this.", GSL_EUNSUP) ; |
| 126 |
} |
| 127 |
|
| 128 |
if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO) |
| 129 |
{ |
| 130 |
#ifdef FE_DIVBYZERO |
| 131 |
mode &= ~ FE_DIVBYZERO ; |
| 132 |
#else |
| 133 |
GSL_ERROR ("division by zero exception not supported by <fenv.h>", |
| 134 |
GSL_EUNSUP); |
| 135 |
#endif |
| 136 |
} |
| 137 |
|
| 138 |
if (exception_mask & GSL_IEEE_MASK_OVERFLOW) |
| 139 |
{ |
| 140 |
#ifdef FE_OVERFLOW |
| 141 |
mode &= ~ FE_OVERFLOW ; |
| 142 |
#else |
| 143 |
GSL_ERROR ("overflow exception not supported by <fenv.h>", GSL_EUNSUP); |
| 144 |
#endif |
| 145 |
} |
| 146 |
|
| 147 |
if (exception_mask & GSL_IEEE_MASK_UNDERFLOW) |
| 148 |
{ |
| 149 |
#ifdef FE_UNDERFLOW |
| 150 |
mode &= ~ FE_UNDERFLOW ; |
| 151 |
#else |
| 152 |
GSL_ERROR ("underflow exception not supported by <fenv.h>", GSL_EUNSUP); |
| 153 |
#endif |
| 154 |
} |
| 155 |
|
| 156 |
if (exception_mask & GSL_IEEE_TRAP_INEXACT) |
| 157 |
{ |
| 158 |
#ifdef FE_INEXACT |
| 159 |
mode |= FE_INEXACT ; |
| 160 |
#else |
| 161 |
GSL_ERROR ("inexact exception not supported by <fenv.h>", GSL_EUNSUP); |
| 162 |
#endif |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
#ifdef FE_INEXACT |
| 167 |
mode &= ~ FE_INEXACT ; |
| 168 |
#else |
| 169 |
|
| 170 |
#endif |
| 171 |
} |
| 172 |
|
| 173 |
#if HAVE_DECL_FEENABLEEXCEPT |
| 174 |
feenableexcept (mode) ; |
| 175 |
#elif HAVE_DECL_FESETTRAPENABLE |
| 176 |
fesettrapenable (mode); |
| 177 |
#else |
| 178 |
GSL_ERROR ("unknown exception trap method", GSL_EUNSUP) |
| 179 |
#endif |
| 180 |
|
| 181 |
return GSL_SUCCESS ; |
| 182 |
} |