blob: ad34caa4697fcfd423fe609bbdb26c660dc71aff [file] [log] [blame]
Elliott Hughes90e10d42012-11-02 17:05:20 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
Elliott Hughes18a19572017-11-02 16:18:43 -070019#include "utils.h"
20
Elliott Hughes90e10d42012-11-02 17:05:20 -070021#include <fenv.h>
22#include <stdint.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070023#include <sys/cdefs.h>
Elliott Hughes90e10d42012-11-02 17:05:20 -070024
25static void TestRounding(float expectation1, float expectation2) {
Elliott Hughes7cda75f2020-10-22 13:22:35 -070026 // Volatile to prevent compile-time evaluation.
Elliott Hughes90e10d42012-11-02 17:05:20 -070027 volatile float f = 1.968750f;
28 volatile float m = 0x1.0p23f;
Elliott Hughes7cda75f2020-10-22 13:22:35 -070029 float x;
30 DoNotOptimize(x = f + m);
Elliott Hughes90e10d42012-11-02 17:05:20 -070031 ASSERT_FLOAT_EQ(expectation1, x);
Elliott Hughes7cda75f2020-10-22 13:22:35 -070032 DoNotOptimize(x = x - m);
Elliott Hughes90e10d42012-11-02 17:05:20 -070033 ASSERT_EQ(expectation2, x);
34}
35
36static void DivideByZero() {
Elliott Hughes7cda75f2020-10-22 13:22:35 -070037 // Volatile to prevent compile-time evaluation.
Elliott Hughes90e10d42012-11-02 17:05:20 -070038 volatile float zero = 0.0f;
Elliott Hughes7cda75f2020-10-22 13:22:35 -070039 DoNotOptimize(123.0f / zero);
Elliott Hughes90e10d42012-11-02 17:05:20 -070040}
41
42TEST(fenv, fesetround_fegetround_FE_TONEAREST) {
43 fesetround(FE_TONEAREST);
44 ASSERT_EQ(FE_TONEAREST, fegetround());
45 TestRounding(8388610.0f, 2.0f);
46}
47
48TEST(fenv, fesetround_fegetround_FE_TOWARDZERO) {
49 fesetround(FE_TOWARDZERO);
50 ASSERT_EQ(FE_TOWARDZERO, fegetround());
51 TestRounding(8388609.0f, 1.0f);
52}
53
54TEST(fenv, fesetround_fegetround_FE_UPWARD) {
55 fesetround(FE_UPWARD);
56 ASSERT_EQ(FE_UPWARD, fegetround());
57 TestRounding(8388610.0f, 2.0f);
58}
59
60TEST(fenv, fesetround_fegetround_FE_DOWNWARD) {
61 fesetround(FE_DOWNWARD);
62 ASSERT_EQ(FE_DOWNWARD, fegetround());
63 TestRounding(8388609.0f, 1.0f);
64}
65
66TEST(fenv, feclearexcept_fetestexcept) {
67 // Clearing clears.
68 feclearexcept(FE_ALL_EXCEPT);
69 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
70
71 // Dividing by zero sets FE_DIVBYZERO.
72 DivideByZero();
73 int raised = fetestexcept(FE_DIVBYZERO | FE_OVERFLOW);
74 ASSERT_TRUE((raised & FE_OVERFLOW) == 0);
75 ASSERT_TRUE((raised & FE_DIVBYZERO) != 0);
76
77 // Clearing an unset bit is a no-op.
78 feclearexcept(FE_OVERFLOW);
79 ASSERT_TRUE((raised & FE_OVERFLOW) == 0);
80 ASSERT_TRUE((raised & FE_DIVBYZERO) != 0);
81
82 // Clearing a set bit works.
83 feclearexcept(FE_DIVBYZERO);
84 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
85}
Elliott Hughesa0ee0782013-01-30 19:06:37 -080086
87TEST(fenv, FE_DFL_ENV_macro) {
88 ASSERT_EQ(0, fesetenv(FE_DFL_ENV));
89}
Elliott Hughes18a19572017-11-02 16:18:43 -070090
91TEST(fenv, feraiseexcept) {
92 feclearexcept(FE_ALL_EXCEPT);
93 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
94
95 ASSERT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW));
96 ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
97}
98
99TEST(fenv, fegetenv_fesetenv) {
100 // Set FE_OVERFLOW only.
101 feclearexcept(FE_ALL_EXCEPT);
102 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
103 ASSERT_EQ(0, feraiseexcept(FE_OVERFLOW));
104
105 // fegetenv (unlike feholdexcept) leaves the current state untouched...
106 fenv_t state;
107 ASSERT_EQ(0, fegetenv(&state));
108 ASSERT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
109
110 // Dividing by zero sets the appropriate flag...
111 DivideByZero();
112 ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
113
114 // And fesetenv (unlike feupdateenv) clobbers that to return to where
115 // we started.
116 ASSERT_EQ(0, fesetenv(&state));
117 ASSERT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
118}
119
Elliott Hughesd25a5e72022-10-14 20:55:23 +0000120TEST(fenv, fegetenv_fesetenv_rounding_mode) {
121 // Test that fegetenv()/fesetenv() includes the rounding mode.
122 fesetround(FE_DOWNWARD);
123 ASSERT_EQ(FE_DOWNWARD, fegetround());
124
125 fenv_t env;
126 fegetenv(&env);
127
128 fesetround(FE_UPWARD);
129 ASSERT_EQ(FE_UPWARD, fegetround());
130
131 fesetenv(&env);
132 ASSERT_EQ(FE_DOWNWARD, fegetround());
133}
134
Elliott Hughes18a19572017-11-02 16:18:43 -0700135TEST(fenv, feholdexcept_feupdateenv) {
136 // Set FE_OVERFLOW only.
137 feclearexcept(FE_ALL_EXCEPT);
138 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
139 ASSERT_EQ(0, feraiseexcept(FE_OVERFLOW));
140
141 // feholdexcept (unlike fegetenv) clears everything...
142 fenv_t state;
143 ASSERT_EQ(0, feholdexcept(&state));
144 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
145
146 // Dividing by zero sets the appropriate flag...
147 DivideByZero();
148 ASSERT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT));
149
150 // And feupdateenv (unlike fesetenv) merges what we started with
151 // (FE_OVERFLOW) with what we now have (FE_DIVBYZERO).
152 ASSERT_EQ(0, feupdateenv(&state));
153 ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
154}
155
156TEST(fenv, fegetexceptflag_fesetexceptflag) {
157 // Set three flags.
158 feclearexcept(FE_ALL_EXCEPT);
159 ASSERT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW));
160 ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
161
162 fexcept_t all; // FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW
163 fexcept_t two; // FE_OVERFLOW | FE_UNDERFLOW
164 ASSERT_EQ(0, fegetexceptflag(&all, FE_ALL_EXCEPT));
165 ASSERT_EQ(0, fegetexceptflag(&two, FE_OVERFLOW | FE_UNDERFLOW));
166
167 // Check we can restore all.
168 feclearexcept(FE_ALL_EXCEPT);
169 ASSERT_EQ(0, fesetexceptflag(&all, FE_ALL_EXCEPT));
170 ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
171
172 // Check that `two` only stored a subset.
173 feclearexcept(FE_ALL_EXCEPT);
174 ASSERT_EQ(0, fesetexceptflag(&two, FE_ALL_EXCEPT));
175 ASSERT_EQ(FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
176
177 // Check that we can restore a single flag.
178 feclearexcept(FE_ALL_EXCEPT);
179 ASSERT_EQ(0, fesetexceptflag(&all, FE_DIVBYZERO));
180 ASSERT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT));
181
182 // Check that we can restore a subset of flags.
183 feclearexcept(FE_ALL_EXCEPT);
184 ASSERT_EQ(0, fesetexceptflag(&all, FE_OVERFLOW | FE_UNDERFLOW));
185 ASSERT_EQ(FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
186}
187
188TEST(fenv, fedisableexcept_fegetexcept) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700189#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes18a19572017-11-02 16:18:43 -0700190 feclearexcept(FE_ALL_EXCEPT);
191 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
192
193 // No SIGFPE please...
194 ASSERT_EQ(0, fedisableexcept(FE_ALL_EXCEPT));
195 ASSERT_EQ(0, fegetexcept());
196 ASSERT_EQ(0, feraiseexcept(FE_INVALID));
197 ASSERT_EQ(FE_INVALID, fetestexcept(FE_ALL_EXCEPT));
Colin Cross7da20342021-07-28 11:18:11 -0700198#else
199 GTEST_SKIP() << "musl doesn't have fegetexcept";
200#endif
Elliott Hughes18a19572017-11-02 16:18:43 -0700201}
202
203TEST(fenv, feenableexcept_fegetexcept) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700204#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes18a19572017-11-02 16:18:43 -0700205#if defined(__aarch64__) || defined(__arm__)
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700206 // ARM doesn't support this. They used to if you go back far enough, but it was removed in
207 // the Cortex-A8 between r3p1 and r3p2.
208 ASSERT_EQ(-1, feenableexcept(FE_INVALID));
209 ASSERT_EQ(0, fegetexcept());
210 ASSERT_EQ(-1, feenableexcept(FE_DIVBYZERO));
211 ASSERT_EQ(0, fegetexcept());
212 ASSERT_EQ(-1, feenableexcept(FE_OVERFLOW));
213 ASSERT_EQ(0, fegetexcept());
214 ASSERT_EQ(-1, feenableexcept(FE_UNDERFLOW));
215 ASSERT_EQ(0, fegetexcept());
216 ASSERT_EQ(-1, feenableexcept(FE_INEXACT));
217 ASSERT_EQ(0, fegetexcept());
218 ASSERT_EQ(-1, feenableexcept(FE_DENORMAL));
219 ASSERT_EQ(0, fegetexcept());
Elliott Hughes18a19572017-11-02 16:18:43 -0700220#else
221 // We can't recover from SIGFPE, so sacrifice a child...
222 pid_t pid = fork();
223 ASSERT_NE(-1, pid) << strerror(errno);
224
225 if (pid == 0) {
Elliott Hughes6249f9b2021-02-18 17:24:01 -0800226 signal(SIGFPE, SIG_DFL); // Disable debuggerd.
Elliott Hughes18a19572017-11-02 16:18:43 -0700227 feclearexcept(FE_ALL_EXCEPT);
228 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
229 ASSERT_EQ(0, feenableexcept(FE_INVALID));
230 ASSERT_EQ(FE_INVALID, fegetexcept());
231 ASSERT_EQ(0, feraiseexcept(FE_INVALID));
232 _exit(123);
233 }
234
235 AssertChildExited(pid, -SIGFPE);
236#endif
Colin Cross7da20342021-07-28 11:18:11 -0700237#else
238 GTEST_SKIP() << "musl doesn't have fegetexcept";
239#endif
Elliott Hughes18a19572017-11-02 16:18:43 -0700240}