1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
// SafeC Standard Library — Floating-point environment (C99 <fenv.h>)
// Read and set floating-point exception flags and rounding mode.
// Rounding modes and exception flags (C99 FE_* constants). Their values
// are the platform's <fenv.h> values, which differ per architecture.
namespace std {
#if defined(__aarch64__) || defined(__arm__)
static const int FE_TONEAREST() { return 0; }
static const int FE_UPWARD() { return 0x400000; }
static const int FE_DOWNWARD() { return 0x800000; }
static const int FE_TOWARDZERO() { return 0xC00000; }
static const int FE_INVALID() { return 1; }
static const int FE_DIVBYZERO() { return 2; }
static const int FE_OVERFLOW() { return 4; }
static const int FE_UNDERFLOW() { return 8; }
static const int FE_INEXACT() { return 16; }
static const int FE_ALL_EXCEPT() { return 31; }
#elif defined(__riscv)
static const int FE_TONEAREST() { return 0; }
static const int FE_TOWARDZERO() { return 1; }
static const int FE_DOWNWARD() { return 2; }
static const int FE_UPWARD() { return 3; }
static const int FE_INEXACT() { return 1; }
static const int FE_UNDERFLOW() { return 2; }
static const int FE_OVERFLOW() { return 4; }
static const int FE_DIVBYZERO() { return 8; }
static const int FE_INVALID() { return 16; }
static const int FE_ALL_EXCEPT() { return 31; }
#else // x86 / x86-64
static const int FE_TONEAREST() { return 0; }
static const int FE_DOWNWARD() { return 1024; }
static const int FE_UPWARD() { return 2048; }
static const int FE_TOWARDZERO() { return 3072; }
static const int FE_INVALID() { return 1; }
static const int FE_DIVBYZERO() { return 4; }
static const int FE_OVERFLOW() { return 8; }
static const int FE_UNDERFLOW() { return 16; }
static const int FE_INEXACT() { return 32; }
static const int FE_ALL_EXCEPT() { return 63; }
#endif
// Clear specified exception flags (pass FE_ALL_EXCEPT() to clear all).
int fenv_clear(int excepts);
// Test whether any of the specified exceptions are raised.
int fenv_test(int excepts);
// Raise specified exceptions.
int fenv_raise(int excepts);
// Get current rounding mode (returns FE_* constant).
int fenv_get_round();
// Set rounding mode (pass FE_* constant). Returns 0 on success.
int fenv_set_round(int mode);
// Save and restore the complete FP environment.
// env must point to a 32-byte buffer.
int fenv_save(&unsigned char env);
int fenv_restore(const &unsigned char env);
int fenv_save_clear(&unsigned char env); // save + clear all exceptions
} // namespace std