| Elliott Hughes | 90e10d4 | 2012-11-02 17:05:20 -0700 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 19 | #include "utils.h" | 
|  | 20 |  | 
| Elliott Hughes | 90e10d4 | 2012-11-02 17:05:20 -0700 | [diff] [blame] | 21 | #include <fenv.h> | 
|  | 22 | #include <stdint.h> | 
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 23 | #include <sys/cdefs.h> | 
| Elliott Hughes | 90e10d4 | 2012-11-02 17:05:20 -0700 | [diff] [blame] | 24 |  | 
|  | 25 | static void TestRounding(float expectation1, float expectation2) { | 
| Elliott Hughes | 7cda75f | 2020-10-22 13:22:35 -0700 | [diff] [blame] | 26 | // Volatile to prevent compile-time evaluation. | 
| Elliott Hughes | 90e10d4 | 2012-11-02 17:05:20 -0700 | [diff] [blame] | 27 | volatile float f = 1.968750f; | 
|  | 28 | volatile float m = 0x1.0p23f; | 
| Elliott Hughes | 7cda75f | 2020-10-22 13:22:35 -0700 | [diff] [blame] | 29 | float x; | 
|  | 30 | DoNotOptimize(x = f + m); | 
| Elliott Hughes | 90e10d4 | 2012-11-02 17:05:20 -0700 | [diff] [blame] | 31 | ASSERT_FLOAT_EQ(expectation1, x); | 
| Elliott Hughes | 7cda75f | 2020-10-22 13:22:35 -0700 | [diff] [blame] | 32 | DoNotOptimize(x = x - m); | 
| Elliott Hughes | 90e10d4 | 2012-11-02 17:05:20 -0700 | [diff] [blame] | 33 | ASSERT_EQ(expectation2, x); | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | static void DivideByZero() { | 
| Elliott Hughes | 7cda75f | 2020-10-22 13:22:35 -0700 | [diff] [blame] | 37 | // Volatile to prevent compile-time evaluation. | 
| Elliott Hughes | 90e10d4 | 2012-11-02 17:05:20 -0700 | [diff] [blame] | 38 | volatile float zero = 0.0f; | 
| Elliott Hughes | 7cda75f | 2020-10-22 13:22:35 -0700 | [diff] [blame] | 39 | DoNotOptimize(123.0f / zero); | 
| Elliott Hughes | 90e10d4 | 2012-11-02 17:05:20 -0700 | [diff] [blame] | 40 | } | 
|  | 41 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 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 | } | 
| Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 86 |  | 
|  | 87 | TEST(fenv, FE_DFL_ENV_macro) { | 
|  | 88 | ASSERT_EQ(0, fesetenv(FE_DFL_ENV)); | 
|  | 89 | } | 
| Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 90 |  | 
|  | 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 |  | 
|  | 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 |  | 
| Elliott Hughes | d25a5e7 | 2022-10-14 20:55:23 +0000 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 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 |  | 
|  | 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 |  | 
|  | 188 | TEST(fenv, fedisableexcept_fegetexcept) { | 
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 189 | #if !defined(ANDROID_HOST_MUSL) | 
| Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 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)); | 
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 198 | #else | 
|  | 199 | GTEST_SKIP() << "musl doesn't have fegetexcept"; | 
|  | 200 | #endif | 
| Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
|  | 203 | TEST(fenv, feenableexcept_fegetexcept) { | 
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 204 | #if !defined(ANDROID_HOST_MUSL) | 
| Elliott Hughes | 017bd98 | 2022-10-18 23:46:23 +0000 | [diff] [blame] | 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. | 
| Elliott Hughes | b6c7f6e | 2017-11-03 16:46:32 -0700 | [diff] [blame] | 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()); | 
| Elliott Hughes | 017bd98 | 2022-10-18 23:46:23 +0000 | [diff] [blame] | 219 | #if defined(_FE_DENORMAL)  // riscv64 doesn't support this. | 
| Elliott Hughes | b6c7f6e | 2017-11-03 16:46:32 -0700 | [diff] [blame] | 220 | ASSERT_EQ(-1, feenableexcept(FE_DENORMAL)); | 
|  | 221 | ASSERT_EQ(0, fegetexcept()); | 
| Elliott Hughes | 017bd98 | 2022-10-18 23:46:23 +0000 | [diff] [blame] | 222 | #endif | 
| Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 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) { | 
| Elliott Hughes | 6249f9b | 2021-02-18 17:24:01 -0800 | [diff] [blame] | 229 | signal(SIGFPE, SIG_DFL);  // Disable debuggerd. | 
| Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 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 | 
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 240 | #else | 
|  | 241 | GTEST_SKIP() << "musl doesn't have fegetexcept"; | 
|  | 242 | #endif | 
| Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 243 | } |