1 /*
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 
19 #include "utils.h"
20 
21 #include <fenv.h>
22 #include <stdint.h>
23 #include <sys/cdefs.h>
24 
TestRounding(float expectation1,float expectation2)25 static void TestRounding(float expectation1, float expectation2) {
26   // Volatile to prevent compile-time evaluation.
27   volatile float f = 1.968750f;
28   volatile float m = 0x1.0p23f;
29   float x;
30   DoNotOptimize(x = f + m);
31   ASSERT_FLOAT_EQ(expectation1, x);
32   DoNotOptimize(x = x - m);
33   ASSERT_EQ(expectation2, x);
34 }
35 
DivideByZero()36 static void DivideByZero() {
37   // Volatile to prevent compile-time evaluation.
38   volatile float zero = 0.0f;
39   DoNotOptimize(123.0f / zero);
40 }
41 
TEST(fenv,fesetround_fegetround_FE_TONEAREST)42 TEST(fenv, fesetround_fegetround_FE_TONEAREST) {
43   fesetround(FE_TONEAREST);
44   ASSERT_EQ(FE_TONEAREST, fegetround());
45   TestRounding(8388610.0f, 2.0f);
46 }
47 
TEST(fenv,fesetround_fegetround_FE_TOWARDZERO)48 TEST(fenv, fesetround_fegetround_FE_TOWARDZERO) {
49   fesetround(FE_TOWARDZERO);
50   ASSERT_EQ(FE_TOWARDZERO, fegetround());
51   TestRounding(8388609.0f, 1.0f);
52 }
53 
TEST(fenv,fesetround_fegetround_FE_UPWARD)54 TEST(fenv, fesetround_fegetround_FE_UPWARD) {
55   fesetround(FE_UPWARD);
56   ASSERT_EQ(FE_UPWARD, fegetround());
57   TestRounding(8388610.0f, 2.0f);
58 }
59 
TEST(fenv,fesetround_fegetround_FE_DOWNWARD)60 TEST(fenv, fesetround_fegetround_FE_DOWNWARD) {
61   fesetround(FE_DOWNWARD);
62   ASSERT_EQ(FE_DOWNWARD, fegetround());
63   TestRounding(8388609.0f, 1.0f);
64 }
65 
TEST(fenv,feclearexcept_fetestexcept)66 TEST(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 }
86 
TEST(fenv,FE_DFL_ENV_macro)87 TEST(fenv, FE_DFL_ENV_macro) {
88   ASSERT_EQ(0, fesetenv(FE_DFL_ENV));
89 }
90 
TEST(fenv,feraiseexcept)91 TEST(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 
TEST(fenv,fegetenv_fesetenv)99 TEST(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 
TEST(fenv,fegetenv_fesetenv_rounding_mode)120 TEST(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 
TEST(fenv,feholdexcept_feupdateenv)135 TEST(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 
TEST(fenv,fegetexceptflag_fesetexceptflag)156 TEST(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 
TEST(fenv,fedisableexcept_fegetexcept)188 TEST(fenv, fedisableexcept_fegetexcept) {
189 #if !defined(ANDROID_HOST_MUSL)
190   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));
198 #else
199   GTEST_SKIP() << "musl doesn't have fegetexcept";
200 #endif
201 }
202 
TEST(fenv,feenableexcept_fegetexcept)203 TEST(fenv, feenableexcept_fegetexcept) {
204 #if !defined(ANDROID_HOST_MUSL)
205 #if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
206   // ARM and RISC-V don't support hardware trapping of floating point
207   // exceptions. ARM used to if you go back far enough, but it was
208   // removed in the Cortex-A8 between r3p1 and r3p2. RISC-V never has.
209   ASSERT_EQ(-1, feenableexcept(FE_INVALID));
210   ASSERT_EQ(0, fegetexcept());
211   ASSERT_EQ(-1, feenableexcept(FE_DIVBYZERO));
212   ASSERT_EQ(0, fegetexcept());
213   ASSERT_EQ(-1, feenableexcept(FE_OVERFLOW));
214   ASSERT_EQ(0, fegetexcept());
215   ASSERT_EQ(-1, feenableexcept(FE_UNDERFLOW));
216   ASSERT_EQ(0, fegetexcept());
217   ASSERT_EQ(-1, feenableexcept(FE_INEXACT));
218   ASSERT_EQ(0, fegetexcept());
219 #if defined(_FE_DENORMAL)  // riscv64 doesn't support this.
220   ASSERT_EQ(-1, feenableexcept(FE_DENORMAL));
221   ASSERT_EQ(0, fegetexcept());
222 #endif
223 #else
224   // We can't recover from SIGFPE, so sacrifice a child...
225   pid_t pid = fork();
226   ASSERT_NE(-1, pid) << strerror(errno);
227 
228   if (pid == 0) {
229     signal(SIGFPE, SIG_DFL);  // Disable debuggerd.
230     feclearexcept(FE_ALL_EXCEPT);
231     ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
232     ASSERT_EQ(0, feenableexcept(FE_INVALID));
233     ASSERT_EQ(FE_INVALID, fegetexcept());
234     ASSERT_EQ(0, feraiseexcept(FE_INVALID));
235     _exit(123);
236   }
237 
238   AssertChildExited(pid, -SIGFPE);
239 #endif
240 #else
241   GTEST_SKIP() << "musl doesn't have fegetexcept";
242 #endif
243 }
244