2012-11-03 01:05:20 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2017-11-03 00:18:43 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
2012-11-03 01:05:20 +01:00
|
|
|
#include <fenv.h>
|
|
|
|
#include <stdint.h>
|
2021-08-17 00:51:59 +02:00
|
|
|
#include <sys/cdefs.h>
|
2012-11-03 01:05:20 +01:00
|
|
|
|
|
|
|
static void TestRounding(float expectation1, float expectation2) {
|
2020-10-22 22:22:35 +02:00
|
|
|
// Volatile to prevent compile-time evaluation.
|
2012-11-03 01:05:20 +01:00
|
|
|
volatile float f = 1.968750f;
|
|
|
|
volatile float m = 0x1.0p23f;
|
2020-10-22 22:22:35 +02:00
|
|
|
float x;
|
|
|
|
DoNotOptimize(x = f + m);
|
2012-11-03 01:05:20 +01:00
|
|
|
ASSERT_FLOAT_EQ(expectation1, x);
|
2020-10-22 22:22:35 +02:00
|
|
|
DoNotOptimize(x = x - m);
|
2012-11-03 01:05:20 +01:00
|
|
|
ASSERT_EQ(expectation2, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DivideByZero() {
|
2020-10-22 22:22:35 +02:00
|
|
|
// Volatile to prevent compile-time evaluation.
|
2012-11-03 01:05:20 +01:00
|
|
|
volatile float zero = 0.0f;
|
2020-10-22 22:22:35 +02:00
|
|
|
DoNotOptimize(123.0f / zero);
|
2012-11-03 01:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, fesetround_fegetround_FE_TONEAREST) {
|
|
|
|
fesetround(FE_TONEAREST);
|
|
|
|
ASSERT_EQ(FE_TONEAREST, fegetround());
|
|
|
|
TestRounding(8388610.0f, 2.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, fesetround_fegetround_FE_TOWARDZERO) {
|
|
|
|
fesetround(FE_TOWARDZERO);
|
|
|
|
ASSERT_EQ(FE_TOWARDZERO, fegetround());
|
|
|
|
TestRounding(8388609.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, fesetround_fegetround_FE_UPWARD) {
|
|
|
|
fesetround(FE_UPWARD);
|
|
|
|
ASSERT_EQ(FE_UPWARD, fegetround());
|
|
|
|
TestRounding(8388610.0f, 2.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, fesetround_fegetround_FE_DOWNWARD) {
|
|
|
|
fesetround(FE_DOWNWARD);
|
|
|
|
ASSERT_EQ(FE_DOWNWARD, fegetround());
|
|
|
|
TestRounding(8388609.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, feclearexcept_fetestexcept) {
|
|
|
|
// Clearing clears.
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// Dividing by zero sets FE_DIVBYZERO.
|
|
|
|
DivideByZero();
|
|
|
|
int raised = fetestexcept(FE_DIVBYZERO | FE_OVERFLOW);
|
|
|
|
ASSERT_TRUE((raised & FE_OVERFLOW) == 0);
|
|
|
|
ASSERT_TRUE((raised & FE_DIVBYZERO) != 0);
|
|
|
|
|
|
|
|
// Clearing an unset bit is a no-op.
|
|
|
|
feclearexcept(FE_OVERFLOW);
|
|
|
|
ASSERT_TRUE((raised & FE_OVERFLOW) == 0);
|
|
|
|
ASSERT_TRUE((raised & FE_DIVBYZERO) != 0);
|
|
|
|
|
|
|
|
// Clearing a set bit works.
|
|
|
|
feclearexcept(FE_DIVBYZERO);
|
|
|
|
ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
}
|
Upgrade libm.
This brings us up to date with FreeBSD HEAD, fixes various bugs, unifies
the set of functions we support on ARM, MIPS, and x86, fixes "long double",
adds ISO C99 support, and adds basic unit tests.
It turns out that our "long double" functions have always been broken
for non-normal numbers. This patch fixes that by not using the upstream
implementations and just forwarding to the regular "double" implementation
instead (since "long double" on Android is just "double" anyway, which is
what BSD doesn't support).
All the tests pass on ARM, MIPS, and x86, plus glibc on x86-64.
Bug: 3169850
Bug: 8012787
Bug: https://code.google.com/p/android/issues/detail?id=6697
Change-Id: If0c343030959c24bfc50d4d21c9530052c581837
2013-01-31 04:06:37 +01:00
|
|
|
|
|
|
|
TEST(fenv, FE_DFL_ENV_macro) {
|
|
|
|
ASSERT_EQ(0, fesetenv(FE_DFL_ENV));
|
|
|
|
}
|
2017-11-03 00:18:43 +01:00
|
|
|
|
|
|
|
TEST(fenv, feraiseexcept) {
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW));
|
|
|
|
ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, fegetenv_fesetenv) {
|
|
|
|
// Set FE_OVERFLOW only.
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
ASSERT_EQ(0, feraiseexcept(FE_OVERFLOW));
|
|
|
|
|
|
|
|
// fegetenv (unlike feholdexcept) leaves the current state untouched...
|
|
|
|
fenv_t state;
|
|
|
|
ASSERT_EQ(0, fegetenv(&state));
|
|
|
|
ASSERT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// Dividing by zero sets the appropriate flag...
|
|
|
|
DivideByZero();
|
|
|
|
ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// And fesetenv (unlike feupdateenv) clobbers that to return to where
|
|
|
|
// we started.
|
|
|
|
ASSERT_EQ(0, fesetenv(&state));
|
|
|
|
ASSERT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, feholdexcept_feupdateenv) {
|
|
|
|
// Set FE_OVERFLOW only.
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
ASSERT_EQ(0, feraiseexcept(FE_OVERFLOW));
|
|
|
|
|
|
|
|
// feholdexcept (unlike fegetenv) clears everything...
|
|
|
|
fenv_t state;
|
|
|
|
ASSERT_EQ(0, feholdexcept(&state));
|
|
|
|
ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// Dividing by zero sets the appropriate flag...
|
|
|
|
DivideByZero();
|
|
|
|
ASSERT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// And feupdateenv (unlike fesetenv) merges what we started with
|
|
|
|
// (FE_OVERFLOW) with what we now have (FE_DIVBYZERO).
|
|
|
|
ASSERT_EQ(0, feupdateenv(&state));
|
|
|
|
ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, fegetexceptflag_fesetexceptflag) {
|
|
|
|
// Set three flags.
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW));
|
|
|
|
ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
fexcept_t all; // FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW
|
|
|
|
fexcept_t two; // FE_OVERFLOW | FE_UNDERFLOW
|
|
|
|
ASSERT_EQ(0, fegetexceptflag(&all, FE_ALL_EXCEPT));
|
|
|
|
ASSERT_EQ(0, fegetexceptflag(&two, FE_OVERFLOW | FE_UNDERFLOW));
|
|
|
|
|
|
|
|
// Check we can restore all.
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fesetexceptflag(&all, FE_ALL_EXCEPT));
|
|
|
|
ASSERT_EQ(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// Check that `two` only stored a subset.
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fesetexceptflag(&two, FE_ALL_EXCEPT));
|
|
|
|
ASSERT_EQ(FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// Check that we can restore a single flag.
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fesetexceptflag(&all, FE_DIVBYZERO));
|
|
|
|
ASSERT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// Check that we can restore a subset of flags.
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fesetexceptflag(&all, FE_OVERFLOW | FE_UNDERFLOW));
|
|
|
|
ASSERT_EQ(FE_OVERFLOW | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, fedisableexcept_fegetexcept) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2017-11-03 00:18:43 +01:00
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
|
|
|
|
// No SIGFPE please...
|
|
|
|
ASSERT_EQ(0, fedisableexcept(FE_ALL_EXCEPT));
|
|
|
|
ASSERT_EQ(0, fegetexcept());
|
|
|
|
ASSERT_EQ(0, feraiseexcept(FE_INVALID));
|
|
|
|
ASSERT_EQ(FE_INVALID, fetestexcept(FE_ALL_EXCEPT));
|
2021-07-28 20:18:11 +02:00
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "musl doesn't have fegetexcept";
|
|
|
|
#endif
|
2017-11-03 00:18:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fenv, feenableexcept_fegetexcept) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2017-11-03 00:18:43 +01:00
|
|
|
#if defined(__aarch64__) || defined(__arm__)
|
2017-11-04 00:46:32 +01:00
|
|
|
// ARM doesn't support this. They used to if you go back far enough, but it was removed in
|
|
|
|
// the Cortex-A8 between r3p1 and r3p2.
|
|
|
|
ASSERT_EQ(-1, feenableexcept(FE_INVALID));
|
|
|
|
ASSERT_EQ(0, fegetexcept());
|
|
|
|
ASSERT_EQ(-1, feenableexcept(FE_DIVBYZERO));
|
|
|
|
ASSERT_EQ(0, fegetexcept());
|
|
|
|
ASSERT_EQ(-1, feenableexcept(FE_OVERFLOW));
|
|
|
|
ASSERT_EQ(0, fegetexcept());
|
|
|
|
ASSERT_EQ(-1, feenableexcept(FE_UNDERFLOW));
|
|
|
|
ASSERT_EQ(0, fegetexcept());
|
|
|
|
ASSERT_EQ(-1, feenableexcept(FE_INEXACT));
|
|
|
|
ASSERT_EQ(0, fegetexcept());
|
|
|
|
ASSERT_EQ(-1, feenableexcept(FE_DENORMAL));
|
|
|
|
ASSERT_EQ(0, fegetexcept());
|
2017-11-03 00:18:43 +01:00
|
|
|
#else
|
|
|
|
// We can't recover from SIGFPE, so sacrifice a child...
|
|
|
|
pid_t pid = fork();
|
|
|
|
ASSERT_NE(-1, pid) << strerror(errno);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
2021-02-19 02:24:01 +01:00
|
|
|
signal(SIGFPE, SIG_DFL); // Disable debuggerd.
|
2017-11-03 00:18:43 +01:00
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
ASSERT_EQ(0, fetestexcept(FE_ALL_EXCEPT));
|
|
|
|
ASSERT_EQ(0, feenableexcept(FE_INVALID));
|
|
|
|
ASSERT_EQ(FE_INVALID, fegetexcept());
|
|
|
|
ASSERT_EQ(0, feraiseexcept(FE_INVALID));
|
|
|
|
_exit(123);
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertChildExited(pid, -SIGFPE);
|
|
|
|
#endif
|
2021-07-28 20:18:11 +02:00
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "musl doesn't have fegetexcept";
|
|
|
|
#endif
|
2017-11-03 00:18:43 +01:00
|
|
|
}
|