Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / UnitTest / RoundingModeUtils.cpp
blobc8f32f81e7134aa0c5c7121a06070c09cb68ee00
1 //===-- RoundingModeUtils.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "RoundingModeUtils.h"
10 #include "src/__support/FPUtil/FEnvImpl.h"
11 #include "src/__support/FPUtil/rounding_mode.h"
13 #include <fenv.h>
15 namespace LIBC_NAMESPACE {
16 namespace fputil {
17 namespace testing {
19 int get_fe_rounding(RoundingMode mode) {
20 switch (mode) {
21 case RoundingMode::Upward:
22 return FE_UPWARD;
23 case RoundingMode::Downward:
24 return FE_DOWNWARD;
25 case RoundingMode::TowardZero:
26 return FE_TOWARDZERO;
27 case RoundingMode::Nearest:
28 return FE_TONEAREST;
30 __builtin_unreachable();
33 ForceRoundingMode::ForceRoundingMode(RoundingMode mode) {
34 old_rounding_mode = quick_get_round();
35 rounding_mode = get_fe_rounding(mode);
36 if (old_rounding_mode != rounding_mode) {
37 int status = set_round(rounding_mode);
38 success = (status == 0);
39 } else {
40 success = true;
44 ForceRoundingMode::~ForceRoundingMode() {
45 if (old_rounding_mode != rounding_mode)
46 set_round(old_rounding_mode);
49 } // namespace testing
50 } // namespace fputil
51 } // namespace LIBC_NAMESPACE