1//===-- Unittests for the quick rounding mode checks ----------------------===//
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 "src/__support/FPUtil/rounding_mode.h"
10#include "test/UnitTest/Test.h"
11#include "utils/MPFRWrapper/MPFRUtils.h"
12
13#include "hdr/fenv_macros.h"
14
15using LIBC_NAMESPACE::testing::mpfr::ForceRoundingMode;
16using LIBC_NAMESPACE::testing::mpfr::RoundingMode;
17
18TEST(LlvmLibcFEnvImplTest, QuickRoundingUpTest) {
19 using LIBC_NAMESPACE::fputil::fenv_is_round_up;
20 {
21 ForceRoundingMode __r(RoundingMode::Upward);
22 if (__r.success) {
23 ASSERT_TRUE(fenv_is_round_up());
24 }
25 }
26 {
27 ForceRoundingMode __r(RoundingMode::Downward);
28 if (__r.success) {
29 ASSERT_FALSE(fenv_is_round_up());
30 }
31 }
32 {
33 ForceRoundingMode __r(RoundingMode::Nearest);
34 if (__r.success) {
35 ASSERT_FALSE(fenv_is_round_up());
36 }
37 }
38 {
39 ForceRoundingMode __r(RoundingMode::TowardZero);
40 if (__r.success) {
41 ASSERT_FALSE(fenv_is_round_up());
42 }
43 }
44}
45
46TEST(LlvmLibcFEnvImplTest, QuickRoundingDownTest) {
47 using LIBC_NAMESPACE::fputil::fenv_is_round_down;
48 {
49 ForceRoundingMode __r(RoundingMode::Upward);
50 if (__r.success) {
51 ASSERT_FALSE(fenv_is_round_down());
52 }
53 }
54 {
55 ForceRoundingMode __r(RoundingMode::Downward);
56 if (__r.success) {
57 ASSERT_TRUE(fenv_is_round_down());
58 }
59 }
60 {
61 ForceRoundingMode __r(RoundingMode::Nearest);
62 if (__r.success) {
63 ASSERT_FALSE(fenv_is_round_down());
64 }
65 }
66 {
67 ForceRoundingMode __r(RoundingMode::TowardZero);
68 if (__r.success) {
69 ASSERT_FALSE(fenv_is_round_down());
70 }
71 }
72}
73
74TEST(LlvmLibcFEnvImplTest, QuickRoundingNearestTest) {
75 using LIBC_NAMESPACE::fputil::fenv_is_round_to_nearest;
76 {
77 ForceRoundingMode __r(RoundingMode::Upward);
78 if (__r.success) {
79 ASSERT_FALSE(fenv_is_round_to_nearest());
80 }
81 }
82 {
83 ForceRoundingMode __r(RoundingMode::Downward);
84 if (__r.success) {
85 ASSERT_FALSE(fenv_is_round_to_nearest());
86 }
87 }
88 {
89 ForceRoundingMode __r(RoundingMode::Nearest);
90 if (__r.success) {
91 ASSERT_TRUE(fenv_is_round_to_nearest());
92 }
93 }
94 {
95 ForceRoundingMode __r(RoundingMode::TowardZero);
96 if (__r.success) {
97 ASSERT_FALSE(fenv_is_round_to_nearest());
98 }
99 }
100}
101
102TEST(LlvmLibcFEnvImplTest, QuickRoundingTowardZeroTest) {
103 using LIBC_NAMESPACE::fputil::fenv_is_round_to_zero;
104 {
105 ForceRoundingMode __r(RoundingMode::Upward);
106 if (__r.success) {
107 ASSERT_FALSE(fenv_is_round_to_zero());
108 }
109 }
110 {
111 ForceRoundingMode __r(RoundingMode::Downward);
112 if (__r.success) {
113 ASSERT_FALSE(fenv_is_round_to_zero());
114 }
115 }
116 {
117 ForceRoundingMode __r(RoundingMode::Nearest);
118 if (__r.success) {
119 ASSERT_FALSE(fenv_is_round_to_zero());
120 }
121 }
122 {
123 ForceRoundingMode __r(RoundingMode::TowardZero);
124 if (__r.success) {
125 ASSERT_TRUE(fenv_is_round_to_zero());
126 }
127 }
128}
129
130TEST(LlvmLibcFEnvImplTest, QuickGetRoundTest) {
131 using LIBC_NAMESPACE::fputil::quick_get_round;
132 {
133 ForceRoundingMode __r(RoundingMode::Upward);
134 if (__r.success) {
135 ASSERT_EQ(quick_get_round(), FE_UPWARD);
136 }
137 }
138 {
139 ForceRoundingMode __r(RoundingMode::Downward);
140 if (__r.success) {
141 ASSERT_EQ(quick_get_round(), FE_DOWNWARD);
142 }
143 }
144 {
145 ForceRoundingMode __r(RoundingMode::Nearest);
146 if (__r.success) {
147 ASSERT_EQ(quick_get_round(), FE_TONEAREST);
148 }
149 }
150 {
151 ForceRoundingMode __r(RoundingMode::TowardZero);
152 if (__r.success) {
153 ASSERT_EQ(quick_get_round(), FE_TOWARDZERO);
154 }
155 }
156}
157

source code of libc/test/src/__support/FPUtil/rounding_mode_test.cpp