| 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 | #include "src/__support/macros/config.h" |
| 15 | |
| 16 | namespace LIBC_NAMESPACE_DECL { |
| 17 | namespace fputil { |
| 18 | namespace testing { |
| 19 | |
| 20 | int get_fe_rounding(RoundingMode mode) { |
| 21 | switch (mode) { |
| 22 | case RoundingMode::Upward: |
| 23 | return FE_UPWARD; |
| 24 | case RoundingMode::Downward: |
| 25 | return FE_DOWNWARD; |
| 26 | case RoundingMode::TowardZero: |
| 27 | return FE_TOWARDZERO; |
| 28 | case RoundingMode::Nearest: |
| 29 | return FE_TONEAREST; |
| 30 | } |
| 31 | __builtin_unreachable(); |
| 32 | } |
| 33 | |
| 34 | ForceRoundingMode::ForceRoundingMode(RoundingMode mode) { |
| 35 | old_rounding_mode = quick_get_round(); |
| 36 | rounding_mode = get_fe_rounding(mode); |
| 37 | if (old_rounding_mode != rounding_mode) { |
| 38 | int status = set_round(rounding_mode); |
| 39 | success = (status == 0); |
| 40 | } else { |
| 41 | success = true; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | ForceRoundingMode::~ForceRoundingMode() { |
| 46 | if (old_rounding_mode != rounding_mode) |
| 47 | set_round(old_rounding_mode); |
| 48 | } |
| 49 | |
| 50 | } // namespace testing |
| 51 | } // namespace fputil |
| 52 | } // namespace LIBC_NAMESPACE_DECL |
| 53 |
