fp-testing.cpp
2.73 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "fp-testing.h"
#include "llvm/Support/Errno.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#if __x86_64__
#include <xmmintrin.h>
#endif
using Fortran::common::RoundingMode;
using Fortran::evaluate::RealFlag;
ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
#if __x86_64__
bool treatSubnormalOperandsAsZero, bool flushSubnormalResultsToZero
#else
bool, bool
#endif
) {
errno = 0;
if (feholdexcept(&originalFenv_) != 0) {
std::fprintf(stderr, "feholdexcept() failed: %s\n",
llvm::sys::StrError(errno).c_str());
std::abort();
}
fenv_t currentFenv;
if (fegetenv(¤tFenv) != 0) {
std::fprintf(
stderr, "fegetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
#if __x86_64__
originalMxcsr = _mm_getcsr();
unsigned int currentMxcsr{originalMxcsr};
if (treatSubnormalOperandsAsZero) {
currentMxcsr |= 0x0040;
} else {
currentMxcsr &= ~0x0040;
}
if (flushSubnormalResultsToZero) {
currentMxcsr |= 0x8000;
} else {
currentMxcsr &= ~0x8000;
}
#else
// TODO others
#endif
errno = 0;
if (fesetenv(¤tFenv) != 0) {
std::fprintf(
stderr, "fesetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
#if __x86_64__
_mm_setcsr(currentMxcsr);
#endif
}
ScopedHostFloatingPointEnvironment::~ScopedHostFloatingPointEnvironment() {
errno = 0;
if (fesetenv(&originalFenv_) != 0) {
std::fprintf(
stderr, "fesetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
#if __x86_64__
_mm_setcsr(originalMxcsr);
#endif
}
void ScopedHostFloatingPointEnvironment::ClearFlags() const {
feclearexcept(FE_ALL_EXCEPT);
}
RealFlags ScopedHostFloatingPointEnvironment::CurrentFlags() {
int exceptions = fetestexcept(FE_ALL_EXCEPT);
RealFlags flags;
if (exceptions & FE_INVALID) {
flags.set(RealFlag::InvalidArgument);
}
if (exceptions & FE_DIVBYZERO) {
flags.set(RealFlag::DivideByZero);
}
if (exceptions & FE_OVERFLOW) {
flags.set(RealFlag::Overflow);
}
if (exceptions & FE_UNDERFLOW) {
flags.set(RealFlag::Underflow);
}
if (exceptions & FE_INEXACT) {
flags.set(RealFlag::Inexact);
}
return flags;
}
void ScopedHostFloatingPointEnvironment::SetRounding(Rounding rounding) {
switch (rounding.mode) {
case RoundingMode::TiesToEven:
fesetround(FE_TONEAREST);
break;
case RoundingMode::ToZero:
fesetround(FE_TOWARDZERO);
break;
case RoundingMode::Up:
fesetround(FE_UPWARD);
break;
case RoundingMode::Down:
fesetround(FE_DOWNWARD);
break;
case RoundingMode::TiesAwayFromZero:
std::fprintf(stderr, "SetRounding: TiesAwayFromZero not available");
std::abort();
break;
}
}