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 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "RoundingModeUtils.h" |
10 | #include "src/__support/FPUtil/FEnvImpl.h" |
11 | #include "src/__support/FPUtil/rounding_mode.h" |
12 | |
13 | #include "hdr/fenv_macros.h" |
14 | |
15 | namespace LIBC_NAMESPACE { |
16 | namespace fputil { |
17 | namespace testing { |
18 | |
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; |
29 | } |
30 | __builtin_unreachable(); |
31 | } |
32 | |
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; |
41 | } |
42 | } |
43 | |
44 | ForceRoundingMode::~ForceRoundingMode() { |
45 | if (old_rounding_mode != rounding_mode) |
46 | set_round(old_rounding_mode); |
47 | } |
48 | |
49 | } // namespace testing |
50 | } // namespace fputil |
51 | } // namespace LIBC_NAMESPACE |
52 |