1 /* ieee-utils/fp-gnuc99.c
3 * Copyright (C) 2003, 2004, 2007 Brian Gough
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.
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.
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.
25 #include <gsl/gsl_ieee_utils.h>
26 #include <gsl/gsl_errno.h>
29 gsl_ieee_set_mode (int precision, int rounding, int exception_mask)
35 case GSL_IEEE_SINGLE_PRECISION:
36 GSL_ERROR ("single precision rounding is not supported by <fenv.h>",
39 case GSL_IEEE_DOUBLE_PRECISION:
40 GSL_ERROR ("double precision rounding is not supported by <fenv.h>",
43 case GSL_IEEE_EXTENDED_PRECISION:
44 GSL_ERROR ("extended precision rounding is not supported by <fenv.h>",
52 case GSL_IEEE_ROUND_TO_NEAREST:
54 fesetround (FE_TONEAREST) ;
56 GSL_ERROR ("round-to-nearest is not supported by <fenv.h>", GSL_EUNSUP) ;
59 case GSL_IEEE_ROUND_DOWN:
61 fesetround (FE_DOWNWARD) ;
63 GSL_ERROR ("round-down is not supported by <fenv.h>", GSL_EUNSUP) ;
66 case GSL_IEEE_ROUND_UP:
68 fesetround (FE_UPWARD) ;
70 GSL_ERROR ("round-up is not supported by <fenv.h>", GSL_EUNSUP) ;
73 case GSL_IEEE_ROUND_TO_ZERO:
75 fesetround (FE_TOWARDZERO) ;
77 GSL_ERROR ("round-toward-zero is not supported by <fenv.h>", GSL_EUNSUP) ;
82 fesetround (FE_TONEAREST) ;
84 GSL_ERROR ("default round-to-nearest mode is not supported by <fenv.h>", GSL_EUNSUP) ;
88 /* Turn on all the exceptions apart from 'inexact' */
101 mode |= FE_OVERFLOW ;
105 mode |= FE_UNDERFLOW ;
108 if (exception_mask & GSL_IEEE_MASK_INVALID)
111 mode &= ~ FE_INVALID ;
113 GSL_ERROR ("invalid operation exception not supported by <fenv.h>",
118 if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
124 GSL_ERROR ("denormalized operand exception not supported by <fenv.h>. "
125 "Use 'mask-denormalized' to work around this.", GSL_EUNSUP) ;
128 if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
131 mode &= ~ FE_DIVBYZERO ;
133 GSL_ERROR ("division by zero exception not supported by <fenv.h>",
138 if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
141 mode &= ~ FE_OVERFLOW ;
143 GSL_ERROR ("overflow exception not supported by <fenv.h>", GSL_EUNSUP);
147 if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
150 mode &= ~ FE_UNDERFLOW ;
152 GSL_ERROR ("underflow exception not supported by <fenv.h>", GSL_EUNSUP);
156 if (exception_mask & GSL_IEEE_TRAP_INEXACT)
161 GSL_ERROR ("inexact exception not supported by <fenv.h>", GSL_EUNSUP);
167 mode &= ~ FE_INEXACT ;
173 #if HAVE_DECL_FEENABLEEXCEPT
174 feenableexcept (mode) ;
175 #elif HAVE_DECL_FESETTRAPENABLE
176 fesettrapenable (mode);
178 GSL_ERROR ("unknown exception trap method", GSL_EUNSUP)