| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#ifndef ECL_MATH_FENV_H |
| 13 |
#define ECL_MATH_FENV_H |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
| 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 |