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 | |
| 120 | TEST(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 | |
| 141 | TEST(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 | |
| 173 | TEST(fenv, fedisableexcept_fegetexcept) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 174 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 175 | 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 Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 183 | #else |
| 184 | GTEST_SKIP() << "musl doesn't have fegetexcept"; |
| 185 | #endif |
Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST(fenv, feenableexcept_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 | #if defined(__aarch64__) || defined(__arm__) |
Elliott Hughes | b6c7f6e | 2017-11-03 16:46:32 -0700 | [diff] [blame] | 191 | // 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 Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 205 | #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 Hughes | 6249f9b | 2021-02-18 17:24:01 -0800 | [diff] [blame] | 211 | signal(SIGFPE, SIG_DFL); // Disable debuggerd. |
Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 212 | 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 Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 222 | #else |
| 223 | GTEST_SKIP() << "musl doesn't have fegetexcept"; |
| 224 | #endif |
Elliott Hughes | 18a1957 | 2017-11-02 16:18:43 -0700 | [diff] [blame] | 225 | } |