The specified rounding mode will be used and restored to what it was before the test ran. Additionally, it moves ForceRoundingMode and RoundingMode out of MPFRUtils to be used in more places. Differential Revision: https://reviews.llvm.org/D129685
47 lines
1.2 KiB
C++
47 lines
1.2 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 <fenv.h>
|
|
|
|
namespace __llvm_libc {
|
|
namespace testutils {
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
ForceRoundingMode::ForceRoundingMode(RoundingMode mode) {
|
|
old_rounding_mode = fegetround();
|
|
rounding_mode = get_fe_rounding(mode);
|
|
if (old_rounding_mode != rounding_mode)
|
|
fesetround(rounding_mode);
|
|
}
|
|
|
|
ForceRoundingMode::~ForceRoundingMode() {
|
|
if (old_rounding_mode != rounding_mode)
|
|
fesetround(old_rounding_mode);
|
|
}
|
|
|
|
} // namespace testutils
|
|
} // namespace __llvm_libc
|