blob: c5b5eca29c8ca8e7d7685778d0e26d92f276c15d [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
120TEST(fenv, feholdexcept_feupdateenv) {
121 // Set FE_OVERFLOW only.
122 feclearexcept(FE_ALL_EXCEPT);
123 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
124 ASSERT_EQ(0, feraiseexcept(FE_OVERFLOW));
125
126 // feholdexcept (unlike fegetenv) clears everything...
127 fenv_t state;
128 ASSERT_EQ(0, feholdexcept(&state));
129 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
130
131 // Dividing by zero sets the appropriate flag...
132 DivideByZero();
133 ASSERT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT));
134
135 // And feupdateenv (unlike fesetenv) merges what we started with
136 // (FE_OVERFLOW) with what we now have (FE_DIVBYZERO).
137 ASSERT_EQ(0, feupdateenv(&state));
138 ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
139}
140
141TEST(fenv, fegetexceptflag_fesetexceptflag) {
142 // Set three flags.
143 feclearexcept(FE_ALL_EXCEPT);
144 ASSERT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW));
145 ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
146
147 fexcept_t all; // FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW
148 fexcept_t two; // FE_OVERFLOW | FE_UNDERFLOW
149 ASSERT_EQ(0, fegetexceptflag(&all, FE_ALL_EXCEPT));
150 ASSERT_EQ(0, fegetexceptflag(&two, FE_OVERFLOW | FE_UNDERFLOW));
151
152 // Check we can restore all.
153 feclearexcept(FE_ALL_EXCEPT);
154 ASSERT_EQ(0, fesetexceptflag(&all, FE_ALL_EXCEPT));
155 ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
156
157 // Check that `two` only stored a subset.
158 feclearexcept(FE_ALL_EXCEPT);
159 ASSERT_EQ(0, fesetexceptflag(&two, FE_ALL_EXCEPT));
160 ASSERT_EQ(FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
161
162 // Check that we can restore a single flag.
163 feclearexcept(FE_ALL_EXCEPT);
164 ASSERT_EQ(0, fesetexceptflag(&all, FE_DIVBYZERO));
165 ASSERT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT));
166
167 // Check that we can restore a subset of flags.
168 feclearexcept(FE_ALL_EXCEPT);
169 ASSERT_EQ(0, fesetexceptflag(&all, FE_OVERFLOW | FE_UNDERFLOW));
170 ASSERT_EQ(FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
171}
172
173TEST(fenv, fedisableexcept_fegetexcept) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700174#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes18a19572017-11-02 16:18:43 -0700175 feclearexcept(FE_ALL_EXCEPT);
176 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
177
178 // No SIGFPE please...
179 ASSERT_EQ(0, fedisableexcept(FE_ALL_EXCEPT));
180 ASSERT_EQ(0, fegetexcept());
181 ASSERT_EQ(0, feraiseexcept(FE_INVALID));
182 ASSERT_EQ(FE_INVALID, fetestexcept(FE_ALL_EXCEPT));
Colin Cross7da20342021-07-28 11:18:11 -0700183#else
184 GTEST_SKIP() << "musl doesn't have fegetexcept";
185#endif
Elliott Hughes18a19572017-11-02 16:18:43 -0700186}
187
188TEST(fenv, feenableexcept_fegetexcept) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700189#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes18a19572017-11-02 16:18:43 -0700190#if defined(__aarch64__) || defined(__arm__)
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700191 // ARM doesn't support this. They used to if you go back far enough, but it was removed in
192 // the Cortex-A8 between r3p1 and r3p2.
193 ASSERT_EQ(-1, feenableexcept(FE_INVALID));
194 ASSERT_EQ(0, fegetexcept());
195 ASSERT_EQ(-1, feenableexcept(FE_DIVBYZERO));
196 ASSERT_EQ(0, fegetexcept());
197 ASSERT_EQ(-1, feenableexcept(FE_OVERFLOW));
198 ASSERT_EQ(0, fegetexcept());
199 ASSERT_EQ(-1, feenableexcept(FE_UNDERFLOW));
200 ASSERT_EQ(0, fegetexcept());
201 ASSERT_EQ(-1, feenableexcept(FE_INEXACT));
202 ASSERT_EQ(0, fegetexcept());
203 ASSERT_EQ(-1, feenableexcept(FE_DENORMAL));
204 ASSERT_EQ(0, fegetexcept());
Elliott Hughes18a19572017-11-02 16:18:43 -0700205#else
206 // We can't recover from SIGFPE, so sacrifice a child...
207 pid_t pid = fork();
208 ASSERT_NE(-1, pid) << strerror(errno);
209
210 if (pid == 0) {
Elliott Hughes6249f9b2021-02-18 17:24:01 -0800211 signal(SIGFPE, SIG_DFL); // Disable debuggerd.
Elliott Hughes18a19572017-11-02 16:18:43 -0700212 feclearexcept(FE_ALL_EXCEPT);
213 ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
214 ASSERT_EQ(0, feenableexcept(FE_INVALID));
215 ASSERT_EQ(FE_INVALID, fegetexcept());
216 ASSERT_EQ(0, feraiseexcept(FE_INVALID));
217 _exit(123);
218 }
219
220 AssertChildExited(pid, -SIGFPE);
221#endif
Colin Cross7da20342021-07-28 11:18:11 -0700222#else
223 GTEST_SKIP() << "musl doesn't have fegetexcept";
224#endif
Elliott Hughes18a19572017-11-02 16:18:43 -0700225}