1 //===-- Unittests for the quick rounding mode checks ----------------------===//
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 "src/__support/FPUtil/rounding_mode.h"
10 #include "test/UnitTest/Test.h"
11 #include "utils/MPFRWrapper/MPFRUtils.h"
15 using LIBC_NAMESPACE::testing::mpfr::ForceRoundingMode
;
16 using LIBC_NAMESPACE::testing::mpfr::RoundingMode
;
18 TEST(LlvmLibcFEnvImplTest
, QuickRoundingUpTest
) {
19 using LIBC_NAMESPACE::fputil::fenv_is_round_up
;
21 ForceRoundingMode
__r(RoundingMode::Upward
);
23 ASSERT_TRUE(fenv_is_round_up());
27 ForceRoundingMode
__r(RoundingMode::Downward
);
29 ASSERT_FALSE(fenv_is_round_up());
33 ForceRoundingMode
__r(RoundingMode::Nearest
);
35 ASSERT_FALSE(fenv_is_round_up());
39 ForceRoundingMode
__r(RoundingMode::TowardZero
);
41 ASSERT_FALSE(fenv_is_round_up());
46 TEST(LlvmLibcFEnvImplTest
, QuickRoundingDownTest
) {
47 using LIBC_NAMESPACE::fputil::fenv_is_round_down
;
49 ForceRoundingMode
__r(RoundingMode::Upward
);
51 ASSERT_FALSE(fenv_is_round_down());
55 ForceRoundingMode
__r(RoundingMode::Downward
);
57 ASSERT_TRUE(fenv_is_round_down());
61 ForceRoundingMode
__r(RoundingMode::Nearest
);
63 ASSERT_FALSE(fenv_is_round_down());
67 ForceRoundingMode
__r(RoundingMode::TowardZero
);
69 ASSERT_FALSE(fenv_is_round_down());
74 TEST(LlvmLibcFEnvImplTest
, QuickRoundingNearestTest
) {
75 using LIBC_NAMESPACE::fputil::fenv_is_round_to_nearest
;
77 ForceRoundingMode
__r(RoundingMode::Upward
);
79 ASSERT_FALSE(fenv_is_round_to_nearest());
83 ForceRoundingMode
__r(RoundingMode::Downward
);
85 ASSERT_FALSE(fenv_is_round_to_nearest());
89 ForceRoundingMode
__r(RoundingMode::Nearest
);
91 ASSERT_TRUE(fenv_is_round_to_nearest());
95 ForceRoundingMode
__r(RoundingMode::TowardZero
);
97 ASSERT_FALSE(fenv_is_round_to_nearest());
102 TEST(LlvmLibcFEnvImplTest
, QuickRoundingTowardZeroTest
) {
103 using LIBC_NAMESPACE::fputil::fenv_is_round_to_zero
;
105 ForceRoundingMode
__r(RoundingMode::Upward
);
107 ASSERT_FALSE(fenv_is_round_to_zero());
111 ForceRoundingMode
__r(RoundingMode::Downward
);
113 ASSERT_FALSE(fenv_is_round_to_zero());
117 ForceRoundingMode
__r(RoundingMode::Nearest
);
119 ASSERT_FALSE(fenv_is_round_to_zero());
123 ForceRoundingMode
__r(RoundingMode::TowardZero
);
125 ASSERT_TRUE(fenv_is_round_to_zero());
130 TEST(LlvmLibcFEnvImplTest
, QuickGetRoundTest
) {
131 using LIBC_NAMESPACE::fputil::quick_get_round
;
133 ForceRoundingMode
__r(RoundingMode::Upward
);
135 ASSERT_EQ(quick_get_round(), FE_UPWARD
);
139 ForceRoundingMode
__r(RoundingMode::Downward
);
141 ASSERT_EQ(quick_get_round(), FE_DOWNWARD
);
145 ForceRoundingMode
__r(RoundingMode::Nearest
);
147 ASSERT_EQ(quick_get_round(), FE_TONEAREST
);
151 ForceRoundingMode
__r(RoundingMode::TowardZero
);
153 ASSERT_EQ(quick_get_round(), FE_TOWARDZERO
);