2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Evaluate/host.cpp ---------------------------------------------===//
|
2019-03-22 17:01:22 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-03-22 17:01:22 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-22 17:01:22 +08:00
|
|
|
|
|
|
|
#include "host.h"
|
|
|
|
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Common/idioms.h"
|
2020-02-28 23:11:03 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2019-03-22 17:01:22 +08:00
|
|
|
#include <cfenv>
|
2020-07-08 20:19:01 +08:00
|
|
|
#if __x86_64__
|
|
|
|
#include <xmmintrin.h>
|
|
|
|
#endif
|
2019-03-22 17:01:22 +08:00
|
|
|
|
|
|
|
namespace Fortran::evaluate::host {
|
|
|
|
using namespace Fortran::parser::literals;
|
|
|
|
|
|
|
|
void HostFloatingPointEnvironment::SetUpHostFloatingPointEnvironment(
|
|
|
|
FoldingContext &context) {
|
|
|
|
errno = 0;
|
2020-07-08 20:19:01 +08:00
|
|
|
std::fenv_t currentFenv;
|
2019-03-22 17:01:22 +08:00
|
|
|
if (feholdexcept(&originalFenv_) != 0) {
|
|
|
|
common::die("Folding with host runtime: feholdexcept() failed: %s",
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::sys::StrError(errno).c_str());
|
2019-03-22 17:01:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-07-08 20:19:01 +08:00
|
|
|
if (fegetenv(¤tFenv) != 0) {
|
2019-03-22 17:01:22 +08:00
|
|
|
common::die("Folding with host runtime: fegetenv() failed: %s",
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::sys::StrError(errno).c_str());
|
2019-03-22 17:01:22 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if __x86_64__
|
2019-04-12 17:31:54 +08:00
|
|
|
hasSubnormalFlushingHardwareControl_ = true;
|
2020-07-08 20:19:01 +08:00
|
|
|
originalMxcsr = _mm_getcsr();
|
|
|
|
unsigned int currentMxcsr{originalMxcsr};
|
2019-03-22 17:01:22 +08:00
|
|
|
if (context.flushSubnormalsToZero()) {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentMxcsr |= 0x8000;
|
|
|
|
currentMxcsr |= 0x0040;
|
2019-03-22 17:01:22 +08:00
|
|
|
} else {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentMxcsr &= ~0x8000;
|
|
|
|
currentMxcsr &= ~0x0040;
|
2019-03-22 17:01:22 +08:00
|
|
|
}
|
2019-04-10 19:38:36 +08:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
#if defined(__GNU_LIBRARY__)
|
2019-04-15 16:54:35 +08:00
|
|
|
hasSubnormalFlushingHardwareControl_ = true;
|
2019-04-10 19:38:36 +08:00
|
|
|
if (context.flushSubnormalsToZero()) {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentFenv.__fpcr |= (1U << 24); // control register
|
2019-04-10 19:38:36 +08:00
|
|
|
} else {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentFenv.__fpcr &= ~(1U << 24); // control register
|
2019-04-10 19:38:36 +08:00
|
|
|
}
|
|
|
|
#elif defined(__BIONIC__)
|
2019-04-15 16:54:35 +08:00
|
|
|
hasSubnormalFlushingHardwareControl_ = true;
|
2019-04-10 19:38:36 +08:00
|
|
|
if (context.flushSubnormalsToZero()) {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentFenv.__control |= (1U << 24); // control register
|
2019-04-10 19:38:36 +08:00
|
|
|
} else {
|
2020-07-08 20:19:01 +08:00
|
|
|
currentFenv.__control &= ~(1U << 24); // control register
|
2019-04-10 19:38:36 +08:00
|
|
|
}
|
|
|
|
#else
|
2019-04-15 16:54:35 +08:00
|
|
|
// If F18 is built with other C libraries on AArch64, software flushing will
|
|
|
|
// be performed around host library calls if subnormal flushing is requested
|
2019-04-10 19:38:36 +08:00
|
|
|
#endif
|
2019-03-22 17:01:22 +08:00
|
|
|
#else
|
2019-04-15 16:54:35 +08:00
|
|
|
// If F18 is not built on one of the above host architecture, software
|
|
|
|
// flushing will be performed around host library calls if needed.
|
2019-03-22 17:01:22 +08:00
|
|
|
#endif
|
2019-06-06 18:24:35 +08:00
|
|
|
|
|
|
|
#ifdef __clang__
|
|
|
|
// clang does not ensure that floating point environment flags are meaningful.
|
|
|
|
// It may perform optimizations that will impact the floating point
|
|
|
|
// environment. For instance, libc++ complex float tan and tanh compilation
|
|
|
|
// with clang -O2 introduces a division by zero on X86 in unused slots of xmm
|
|
|
|
// registers. Therefore, fetestexcept should not be used.
|
|
|
|
hardwareFlagsAreReliable_ = false;
|
|
|
|
#endif
|
2019-03-22 17:01:22 +08:00
|
|
|
errno = 0;
|
2020-07-08 20:19:01 +08:00
|
|
|
if (fesetenv(¤tFenv) != 0) {
|
2019-03-22 17:01:22 +08:00
|
|
|
common::die("Folding with host runtime: fesetenv() failed: %s",
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::sys::StrError(errno).c_str());
|
2019-03-22 17:01:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-07-08 20:19:01 +08:00
|
|
|
#if __x86_64__
|
|
|
|
_mm_setcsr(currentMxcsr);
|
|
|
|
#endif
|
|
|
|
|
2019-03-22 17:01:22 +08:00
|
|
|
switch (context.rounding().mode) {
|
2020-03-29 12:00:16 +08:00
|
|
|
case common::RoundingMode::TiesToEven:
|
|
|
|
fesetround(FE_TONEAREST);
|
|
|
|
break;
|
|
|
|
case common::RoundingMode::ToZero:
|
|
|
|
fesetround(FE_TOWARDZERO);
|
|
|
|
break;
|
|
|
|
case common::RoundingMode::Up:
|
|
|
|
fesetround(FE_UPWARD);
|
|
|
|
break;
|
|
|
|
case common::RoundingMode::Down:
|
|
|
|
fesetround(FE_DOWNWARD);
|
|
|
|
break;
|
2020-01-10 00:10:57 +08:00
|
|
|
case common::RoundingMode::TiesAwayFromZero:
|
2019-03-22 17:01:22 +08:00
|
|
|
fesetround(FE_TONEAREST);
|
|
|
|
context.messages().Say(
|
2019-09-18 23:04:52 +08:00
|
|
|
"TiesAwayFromZero rounding mode is not available when folding constants"
|
2022-03-08 05:57:37 +08:00
|
|
|
" with host runtime; using TiesToEven instead"_warn_en_US);
|
2019-03-22 17:01:22 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-06-06 18:24:35 +08:00
|
|
|
flags_.clear();
|
2019-03-22 17:01:22 +08:00
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
void HostFloatingPointEnvironment::CheckAndRestoreFloatingPointEnvironment(
|
|
|
|
FoldingContext &context) {
|
|
|
|
int errnoCapture{errno};
|
2019-06-06 18:24:35 +08:00
|
|
|
if (hardwareFlagsAreReliable()) {
|
|
|
|
int exceptions{fetestexcept(FE_ALL_EXCEPT)};
|
|
|
|
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);
|
|
|
|
}
|
2019-03-22 17:01:22 +08:00
|
|
|
}
|
|
|
|
|
2019-06-06 18:24:35 +08:00
|
|
|
if (flags_.empty()) {
|
2019-03-22 17:01:22 +08:00
|
|
|
if (errnoCapture == EDOM) {
|
2019-06-06 18:24:35 +08:00
|
|
|
flags_.set(RealFlag::InvalidArgument);
|
2019-03-22 17:01:22 +08:00
|
|
|
}
|
|
|
|
if (errnoCapture == ERANGE) {
|
|
|
|
// can't distinguish over/underflow from errno
|
2019-06-06 18:24:35 +08:00
|
|
|
flags_.set(RealFlag::Overflow);
|
2019-03-22 17:01:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 18:24:35 +08:00
|
|
|
if (!flags_.empty()) {
|
|
|
|
RealFlagWarnings(context, flags_, "intrinsic function");
|
2019-03-22 17:01:22 +08:00
|
|
|
}
|
|
|
|
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());
|
2019-03-22 17:01:22 +08:00
|
|
|
common::die(
|
|
|
|
"Folding with host runtime: fesetenv() failed while restoring fenv: %s",
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::sys::StrError(errno).c_str());
|
2019-03-22 17:01:22 +08:00
|
|
|
}
|
2020-07-08 20:19:01 +08:00
|
|
|
#if __x86_64__
|
|
|
|
_mm_setcsr(originalMxcsr);
|
|
|
|
#endif
|
|
|
|
|
2019-03-22 17:01:22 +08:00
|
|
|
errno = 0;
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::evaluate::host
|