Skip to content

Commit

Permalink
[libc] Fix improper initialization of StorageType (#75610)
Browse files Browse the repository at this point in the history
`StorageType` may be a `BigInt` under the hood. Initializing it with
`-1` does not yields the maximum value.
  • Loading branch information
gchatelet authored Dec 15, 2023
1 parent 32f9983 commit 57fcc23
Show file tree
Hide file tree
Showing 23 changed files with 37 additions and 28 deletions.
5 changes: 5 additions & 0 deletions libc/test/UnitTest/FPMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H
#define LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H

#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/fpbits_str.h"
Expand Down Expand Up @@ -62,6 +63,8 @@ template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {
template <typename T> struct FPTest : public Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
static constexpr StorageType STORAGE_MAX =
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
static constexpr T zero = FPBits::zero();
static constexpr T neg_zero = FPBits::neg_zero();
static constexpr T aNaN = FPBits::build_quiet_nan(1);
Expand All @@ -88,6 +91,8 @@ template <typename T> struct FPTest : public Test {
#define DECLARE_SPECIAL_CONSTANTS(T) \
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; \
using StorageType = typename FPBits::StorageType; \
static constexpr StorageType STORAGE_MAX = \
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max(); \
const T zero = FPBits::zero(); \
const T neg_zero = FPBits::neg_zero(); \
const T aNaN = FPBits::build_quiet_nan(1); \
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/CeilTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <typename T> class CeilTest : public LIBC_NAMESPACE::testing::Test {

void testRange(CeilFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/CopySignTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CopySignTest : public LIBC_NAMESPACE::testing::Test {

void testRange(CopySignFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/FAbsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ template <typename T> class FAbsTest : public LIBC_NAMESPACE::testing::Test {

void testRange(FabsFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
Expand Down
6 changes: 4 additions & 2 deletions libc/test/src/math/FDimTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
}

void test_in_range(FuncPtr func) {
constexpr StorageType STORAGE_MAX =
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/FMaxTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {

void testRange(FMaxFunc func) {
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/FMinTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {

void testRange(FMinFunc func) {
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/FloorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <typename T> class FloorTest : public LIBC_NAMESPACE::testing::Test {

void testRange(FloorFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/FrexpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ template <typename T> class FrexpTest : public LIBC_NAMESPACE::testing::Test {
void testRange(FrexpFunc func) {
using StorageType = typename FPBits::StorageType;
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = static_cast<T>(FPBits(v));
if (isnan(x) || isinf(x) || x == 0.0l)
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/LogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
void testRange(LogbFunc func) {
using StorageType = typename FPBits::StorageType;
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = static_cast<T>(FPBits(v));
if (isnan(x) || isinf(x) || x == 0.0l)
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/ModfTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {

void testRange(ModfFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x) || x == T(0.0))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/RoundTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <typename T> class RoundTest : public LIBC_NAMESPACE::testing::Test {

void testRange(RoundFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/SqrtTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ template <typename T> class SqrtTest : public LIBC_NAMESPACE::testing::Test {

void test_normal_range(SqrtFunc func) {
constexpr StorageType COUNT = 200'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = LIBC_NAMESPACE::cpp::bit_cast<T>(v);
if (isnan(x) || (x < 0)) {
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/TruncTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <typename T> class TruncTest : public LIBC_NAMESPACE::testing::Test {

void testRange(TruncFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/cos_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
TEST_F(LlvmLibcCosTest, Range) {
static constexpr double _2pi = 6.283185307179586;
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
double x = double(FPBits(v));
// TODO: Expand the range of testing after range reduction is implemented.
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/sin_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
TEST_F(LlvmLibcSinTest, Range) {
static constexpr double _2pi = 6.283185307179586;
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
double x = double(FPBits(v));
// TODO: Expand the range of testing after range reduction is implemented.
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/smoke/CopySignTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CopySignTest : public LIBC_NAMESPACE::testing::Test {

void testRange(CopySignFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
FPBits x_bits = FPBits(v);
T x = T(v);
Expand Down
6 changes: 4 additions & 2 deletions libc/test/src/math/smoke/FDimTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
}

void test_in_range(FuncPtr func) {
constexpr StorageType STORAGE_MAX =
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/smoke/FMaxTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {

void testRange(FMaxFunc func) {
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/smoke/FMinTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {

void testRange(FMinFunc func) {
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/smoke/LogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
void testRange(LogbFunc func) {
using StorageType = typename FPBits::StorageType;
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = static_cast<T>(FPBits(v));
if (isnan(x) || isinf(x) || x == 0.0l)
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/smoke/ModfTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {

void testRange(ModfFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x) || x == T(0.0))
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/tan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
TEST_F(LlvmLibcTanTest, Range) {
static constexpr double _2pi = 6.283185307179586;
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
double x = double(FPBits(v));
// TODO: Expand the range of testing after range reduction is implemented.
Expand Down

0 comments on commit 57fcc23

Please sign in to comment.