llvm-project/libc/test/UnitTest/RoundingModeUtils.cpp
Tue Ly 055be3c30c [libc] Enable hermetic floating point tests again.
Fixing an issue with LLVM libc's fenv.h defined rounding mode macros
differently from system libc, making get_round() return different values from
fegetround().  Also letting math tests to skip rounding modes that cannot be
set.  This should allow math tests to be run on platforms in which fenv.h is not
implemented yet.

This allows us to re-enable hermatic floating point tests in
https://reviews.llvm.org/D151123 and reverting https://reviews.llvm.org/D152742.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D152873
2023-06-14 10:53:35 -04:00

57 lines
1.4 KiB
C++

//===-- RoundingModeUtils.cpp ---------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "RoundingModeUtils.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include <fenv.h>
namespace __llvm_libc {
namespace fputil {
namespace testing {
int get_fe_rounding(RoundingMode mode) {
switch (mode) {
case RoundingMode::Upward:
return FE_UPWARD;
break;
case RoundingMode::Downward:
return FE_DOWNWARD;
break;
case RoundingMode::TowardZero:
return FE_TOWARDZERO;
break;
case RoundingMode::Nearest:
return FE_TONEAREST;
break;
default:
__builtin_unreachable();
}
}
ForceRoundingMode::ForceRoundingMode(RoundingMode mode) {
old_rounding_mode = quick_get_round();
rounding_mode = get_fe_rounding(mode);
if (old_rounding_mode != rounding_mode) {
int status = set_round(rounding_mode);
success = (status == 0);
} else {
success = true;
}
}
ForceRoundingMode::~ForceRoundingMode() {
if (old_rounding_mode != rounding_mode)
set_round(old_rounding_mode);
}
} // namespace testing
} // namespace fputil
} // namespace __llvm_libc