1 //===-- RoundingModeUtils.cpp ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "RoundingModeUtils.h"
10 #include "src/__support/FPUtil/FEnvImpl.h"
11 #include "src/__support/FPUtil/rounding_mode.h"
13 #include "hdr/fenv_macros.h"
14 #include "src/__support/macros/config.h"
16 namespace LIBC_NAMESPACE_DECL
{
20 int get_fe_rounding(RoundingMode mode
) {
22 case RoundingMode::Upward
:
24 case RoundingMode::Downward
:
26 case RoundingMode::TowardZero
:
28 case RoundingMode::Nearest
:
31 __builtin_unreachable();
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);
45 ForceRoundingMode::~ForceRoundingMode() {
46 if (old_rounding_mode
!= rounding_mode
)
47 set_round(old_rounding_mode
);
50 } // namespace testing
52 } // namespace LIBC_NAMESPACE_DECL