Debian Codegraph @

src/h/impl/math_fenv.h

raw symbols SCIP
1 /* -*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=8 shiftwidth=4 expandtab: */
3
4 /*
5 * Copyright (c) 2010, Juan Jose Garcia Ripoll.
6 *
7 * See file 'LICENSE' for the copyright details.
8 *
9 */
10
11 /* math_fenv.h -- inlined versions of fenv.h */
12 #ifndef ECL_MATH_FENV_H
13 #define ECL_MATH_FENV_H
14
15 /*
16 * ECL admits two ways to process floating point errors
17 * - Based on hardware exceptions
18 * - Based on explicit checks
19 *
20 * In the hardware exception model we simply expect the floating point
21 * unit to complain about a computation. In order for this to happen
22 * we may need two things:
23 *
24 * - Activate explicitely signaling of exceptions
25 * - Insert explicit checks for exceptions
26 *
27 * The first task is achieved using feenableexcept() or an equivalent
28 * function. The second task is only needed on some platforms where
29 * exceptions are activated by one floating point computation but are
30 * only signaled with the _next_ floating point instruction (Read x86
31 * processors)
32 *
33 * The second model is more portable and safer and it is based on
34 * using the C99 routines in fenv.h or equivalent functions. In this
35 * model hardware exceptions are never triggered and instead we
36 * surround the computation with explicit checks for problems.
37 */
38
39 #if defined(HAVE_FEENABLEEXCEPT) && !defined(_GNU_SOURCE)
40 # define _GNU_SOURCE
41 #endif
42 #ifdef HAVE_FENV_H
43 # include <fenv.h>
44 #endif
45 #if defined(ECL_MS_WINDOWS_HOST)
46 # include <ecl/impl/math_fenv_msvc.h>
47 #endif
48
49 #if defined(HAVE_FENV_H) && !defined(__COSMOPOLITAN__)
50 # define ECL_WITHOUT_FPE_BEGIN do { fenv_t env; feholdexcept(&env);
51 # define ECL_WITHOUT_FPE_END fesetenv(&env); } while (0)
52 # if !defined(FE_DIVBYZERO)
53 # define FE_DIVBYZERO 0
54 # endif
55 # if !defined(FE_INVALID)
56 # define FE_INVALID 0
57 # endif
58 # if !defined(FE_OVERFLOW)
59 # define FE_OVERFLOW 0
60 # endif
61 # if !defined(FE_UNDERFLOW)
62 # define FE_UNDERFLOW 0
63 # endif
64 # if !defined(FE_INEXACT)
65 # define FE_INEXACT 0
66 # endif
67 #else
68 # define FE_INVALID 1
69 # define FE_DIVBYZERO 2
70 # define FE_INEXACT 0
71 # define FE_OVERFLOW 3
72 # define FE_UNDERFLOW 0
73 # define ECL_WITHOUT_FPE_BEGIN
74 # define ECL_WITHOUT_FPE_END
75 # define feclearexcept(x)
76 #endif /* !HAVE_FENV_H */
77
78 #ifndef FE_ALL_EXCEPT
79 # define FE_ALL_EXCEPT FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID
80 #endif
81
82 #if defined(HAVE_FENV_H) && !defined(ECL_AVOID_FPE_H)
83 # if defined(HAVE_FEENABLEEXCEPT)
84 # define ECL_WITH_LISP_FPE_BEGIN do { \
85 fenv_t __fenv; \
86 fegetenv(&__fenv); \
87 feclearexcept(FE_ALL_EXCEPT); \
88 if (ecl_get_option(ECL_OPT_BOOTED) > 0) { \
89 int bits = ecl_process_env()->trap_fpe_bits; \
90 fedisableexcept(FE_ALL_EXCEPT & ~bits); \
91 feenableexcept(FE_ALL_EXCEPT & bits); \
92 }
93 # else
94 # define ECL_WITH_LISP_FPE_BEGIN do { \
95 fenv_t __fenv; \
96 fegetenv(&__fenv); \
97 feclearexcept(FE_ALL_EXCEPT);
98 # endif
99 # define ECL_WITH_LISP_FPE_END \
100 fesetenv(&__fenv); } while (0)
101 #else
102 # define ECL_WITH_LISP_FPE_BEGIN do {
103 # define ECL_WITH_LISP_FPE_END } while (0)
104 #endif
105
106 #if defined(HAVE_FENV_H) && defined(ECL_IEEE_FP) && !defined(HAVE_FEENABLEEXCEPT) && !defined(ECL_AVOID_FPE_H)
107 # define ECL_USED_EXCEPTIONS (FE_DIVBYZERO|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW)
108 # define ECL_MATHERR_CLEAR feclearexcept(FE_ALL_EXCEPT)
109 # define ECL_MATHERR_TEST do { \
110 int bits = fetestexcept(ECL_USED_EXCEPTIONS); \
111 unlikely_if (bits) { \
112 bits &= ecl_process_env()->trap_fpe_bits; \
113 if (bits) ecl_deliver_fpe(bits); \
114 } \
115 } while(0)
116 #else
117 # define ECL_MATHERR_CLEAR
118 # define ECL_MATHERR_TEST
119 #endif
120
121 extern void ecl_deliver_fpe(int flags);
122
123 #endif /* !ECL_MATH_FENV_H */