2018-06-09 01:24:15 +08:00
|
|
|
#include "fp-testing.h"
|
2020-02-28 23:11:03 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2018-06-09 01:24:15 +08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2020-07-08 20:19:01 +08:00
|
|
|
#if __x86_64__
|
|
|
|
#include <xmmintrin.h>
|
|
|
|
#endif
|
2018-06-09 01:24:15 +08:00
|
|
|
|
2020-01-10 00:10:57 +08:00
|
|
|
using Fortran::common::RoundingMode;
|
2018-06-09 01:24:15 +08:00
|
|
|
using Fortran::evaluate::RealFlag;
|
|
|
|
|
2018-06-09 06:49:06 +08:00
|
|
|
ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
|
2019-08-16 05:25:05 +08:00
|
|
|
#if __x86_64__
|
|
|
|
bool treatSubnormalOperandsAsZero, bool flushSubnormalResultsToZero
|
|
|
|
#else
|
|
|
|
bool, bool
|
|
|
|
#endif
|
2019-12-24 03:16:00 +08:00
|
|
|
) {
|
2018-06-09 01:24:15 +08:00
|
|
|
errno = 0;
|
|
|
|
if (feholdexcept(&originalFenv_) != 0) {
|
2020-02-28 23:11:03 +08:00
|
|
|
std::fprintf(stderr, "feholdexcept() failed: %s\n",
|
|
|
|
llvm::sys::StrError(errno).c_str());
|
2018-06-09 01:24:15 +08:00
|
|
|
std::abort();
|
|
|
|
}
|
2020-07-08 20:19:01 +08:00
|
|
|
fenv_t currentFenv;
|
|
|
|
if (fegetenv(¤tFenv) != 0) {
|
2020-02-28 23:11:03 +08:00
|
|
|
std::fprintf(
|
|
|
|
stderr, "fegetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
|
2018-06-09 01:24:15 +08:00
|
|
|
std::abort();
|
|
|
|
}
|
2020-07-08 20:19:01 +08:00
|
|
|
|
2019-01-23 03:13:44 +08:00
|
|
|
#if __x86_64__
|
2020-07-08 20:19:01 +08:00
|
|
|
originalMxcsr = _mm_getcsr();
|
|
|
|
unsigned int currentMxcsr{originalMxcsr};
|
2019-01-30 08:47:41 +08:00
|
|
|
if (treatSubnormalOperandsAsZero) {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentMxcsr |= 0x0040;
|
2018-06-09 01:24:15 +08:00
|
|
|
} else {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentMxcsr &= ~0x0040;
|
2018-06-09 01:24:15 +08:00
|
|
|
}
|
2019-01-30 08:47:41 +08:00
|
|
|
if (flushSubnormalResultsToZero) {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentMxcsr |= 0x8000;
|
2018-06-09 01:24:15 +08:00
|
|
|
} else {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentMxcsr &= ~0x8000;
|
2018-06-09 01:24:15 +08:00
|
|
|
}
|
|
|
|
#else
|
2018-10-27 07:31:20 +08:00
|
|
|
// TODO others
|
2018-06-09 01:24:15 +08:00
|
|
|
#endif
|
|
|
|
errno = 0;
|
2020-07-08 20:19:01 +08:00
|
|
|
if (fesetenv(¤tFenv) != 0) {
|
2020-02-28 23:11:03 +08:00
|
|
|
std::fprintf(
|
|
|
|
stderr, "fesetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
|
2018-06-09 01:24:15 +08:00
|
|
|
std::abort();
|
|
|
|
}
|
2020-07-08 20:19:01 +08:00
|
|
|
#if __x86_64__
|
|
|
|
_mm_setcsr(currentMxcsr);
|
|
|
|
#endif
|
2018-06-09 01:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedHostFloatingPointEnvironment::~ScopedHostFloatingPointEnvironment() {
|
|
|
|
errno = 0;
|
|
|
|
if (fesetenv(&originalFenv_) != 0) {
|
2020-02-28 23:11:03 +08:00
|
|
|
std::fprintf(
|
|
|
|
stderr, "fesetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
|
2018-06-09 01:24:15 +08:00
|
|
|
std::abort();
|
|
|
|
}
|
2020-07-08 20:19:01 +08:00
|
|
|
#if __x86_64__
|
|
|
|
_mm_setcsr(originalMxcsr);
|
|
|
|
#endif
|
2018-06-09 01:24:15 +08:00
|
|
|
}
|
|
|
|
|
2018-06-09 06:49:06 +08:00
|
|
|
void ScopedHostFloatingPointEnvironment::ClearFlags() const {
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
}
|
|
|
|
|
2018-06-12 02:35:53 +08:00
|
|
|
RealFlags ScopedHostFloatingPointEnvironment::CurrentFlags() {
|
2018-06-09 01:24:15 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-06-12 02:35:53 +08:00
|
|
|
|
|
|
|
void ScopedHostFloatingPointEnvironment::SetRounding(Rounding rounding) {
|
2019-01-23 03:13:44 +08:00
|
|
|
switch (rounding.mode) {
|
2020-03-29 12:00:16 +08:00
|
|
|
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;
|
2019-01-23 03:13:44 +08:00
|
|
|
case RoundingMode::TiesAwayFromZero:
|
2018-06-12 02:35:53 +08:00
|
|
|
std::fprintf(stderr, "SetRounding: TiesAwayFromZero not available");
|
|
|
|
std::abort();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|