Skip to content

Commit 57fcc23

Browse files
authored
[libc] Fix improper initialization of StorageType (#75610)
`StorageType` may be a `BigInt` under the hood. Initializing it with `-1` does not yields the maximum value.
1 parent 32f9983 commit 57fcc23

23 files changed

+37
-28
lines changed

libc/test/UnitTest/FPMatcher.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H
1010
#define LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H
1111

12+
#include "src/__support/CPP/type_traits.h"
1213
#include "src/__support/FPUtil/FEnvImpl.h"
1314
#include "src/__support/FPUtil/FPBits.h"
1415
#include "src/__support/FPUtil/fpbits_str.h"
@@ -62,6 +63,8 @@ template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {
6263
template <typename T> struct FPTest : public Test {
6364
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
6465
using StorageType = typename FPBits::StorageType;
66+
static constexpr StorageType STORAGE_MAX =
67+
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
6568
static constexpr T zero = FPBits::zero();
6669
static constexpr T neg_zero = FPBits::neg_zero();
6770
static constexpr T aNaN = FPBits::build_quiet_nan(1);
@@ -88,6 +91,8 @@ template <typename T> struct FPTest : public Test {
8891
#define DECLARE_SPECIAL_CONSTANTS(T) \
8992
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; \
9093
using StorageType = typename FPBits::StorageType; \
94+
static constexpr StorageType STORAGE_MAX = \
95+
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max(); \
9196
const T zero = FPBits::zero(); \
9297
const T neg_zero = FPBits::neg_zero(); \
9398
const T aNaN = FPBits::build_quiet_nan(1); \

libc/test/src/math/CeilTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ template <typename T> class CeilTest : public LIBC_NAMESPACE::testing::Test {
6565

6666
void testRange(CeilFunc func) {
6767
constexpr StorageType COUNT = 100'000;
68-
constexpr StorageType STEP = StorageType(-1) / COUNT;
68+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
6969
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
7070
T x = T(FPBits(v));
7171
if (isnan(x) || isinf(x))

libc/test/src/math/CopySignTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CopySignTest : public LIBC_NAMESPACE::testing::Test {
3535

3636
void testRange(CopySignFunc func) {
3737
constexpr StorageType COUNT = 100'000;
38-
constexpr StorageType STEP = StorageType(-1) / COUNT;
38+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
3939
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
4040
T x = T(FPBits(v));
4141
if (isnan(x) || isinf(x))

libc/test/src/math/FAbsTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ template <typename T> class FAbsTest : public LIBC_NAMESPACE::testing::Test {
3333

3434
void testRange(FabsFunc func) {
3535
constexpr StorageType COUNT = 100'000;
36-
constexpr StorageType STEP = StorageType(-1) / COUNT;
36+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
3737
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
3838
T x = T(FPBits(v));
3939
if (isnan(x) || isinf(x))

libc/test/src/math/FDimTest.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
5353
}
5454

5555
void test_in_range(FuncPtr func) {
56+
constexpr StorageType STORAGE_MAX =
57+
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
5658
constexpr StorageType COUNT = 100'001;
57-
constexpr StorageType STEP = StorageType(-1) / COUNT;
58-
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
59+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
60+
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
5961
++i, v += STEP, w -= STEP) {
6062
T x = T(FPBits(v)), y = T(FPBits(w));
6163
if (isnan(x) || isinf(x))

libc/test/src/math/FMaxTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {
5656

5757
void testRange(FMaxFunc func) {
5858
constexpr StorageType COUNT = 100'001;
59-
constexpr StorageType STEP = StorageType(-1) / COUNT;
60-
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
59+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
60+
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
6161
++i, v += STEP, w -= STEP) {
6262
T x = T(FPBits(v)), y = T(FPBits(w));
6363
if (isnan(x) || isinf(x))

libc/test/src/math/FMinTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {
5656

5757
void testRange(FMinFunc func) {
5858
constexpr StorageType COUNT = 100'001;
59-
constexpr StorageType STEP = StorageType(-1) / COUNT;
60-
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
59+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
60+
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
6161
++i, v += STEP, w -= STEP) {
6262
T x = T(FPBits(v)), y = T(FPBits(w));
6363
if (isnan(x) || isinf(x))

libc/test/src/math/FloorTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ template <typename T> class FloorTest : public LIBC_NAMESPACE::testing::Test {
6565

6666
void testRange(FloorFunc func) {
6767
constexpr StorageType COUNT = 100'000;
68-
constexpr StorageType STEP = StorageType(-1) / COUNT;
68+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
6969
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
7070
T x = T(FPBits(v));
7171
if (isnan(x) || isinf(x))

libc/test/src/math/FrexpTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ template <typename T> class FrexpTest : public LIBC_NAMESPACE::testing::Test {
9595
void testRange(FrexpFunc func) {
9696
using StorageType = typename FPBits::StorageType;
9797
constexpr StorageType COUNT = 100'000;
98-
constexpr StorageType STEP = StorageType(-1) / COUNT;
98+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
9999
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
100100
T x = static_cast<T>(FPBits(v));
101101
if (isnan(x) || isinf(x) || x == 0.0l)

libc/test/src/math/LogbTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
7474
void testRange(LogbFunc func) {
7575
using StorageType = typename FPBits::StorageType;
7676
constexpr StorageType COUNT = 100'000;
77-
constexpr StorageType STEP = StorageType(-1) / COUNT;
77+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
7878
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
7979
T x = static_cast<T>(FPBits(v));
8080
if (isnan(x) || isinf(x) || x == 0.0l)

libc/test/src/math/ModfTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {
8585

8686
void testRange(ModfFunc func) {
8787
constexpr StorageType COUNT = 100'000;
88-
constexpr StorageType STEP = StorageType(-1) / COUNT;
88+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
8989
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
9090
T x = T(FPBits(v));
9191
if (isnan(x) || isinf(x) || x == T(0.0))

libc/test/src/math/RoundTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ template <typename T> class RoundTest : public LIBC_NAMESPACE::testing::Test {
6565

6666
void testRange(RoundFunc func) {
6767
constexpr StorageType COUNT = 100'000;
68-
constexpr StorageType STEP = StorageType(-1) / COUNT;
68+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
6969
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
7070
T x = T(FPBits(v));
7171
if (isnan(x) || isinf(x))

libc/test/src/math/SqrtTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ template <typename T> class SqrtTest : public LIBC_NAMESPACE::testing::Test {
5656

5757
void test_normal_range(SqrtFunc func) {
5858
constexpr StorageType COUNT = 200'001;
59-
constexpr StorageType STEP = StorageType(-1) / COUNT;
59+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
6060
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
6161
T x = LIBC_NAMESPACE::cpp::bit_cast<T>(v);
6262
if (isnan(x) || (x < 0)) {

libc/test/src/math/TruncTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ template <typename T> class TruncTest : public LIBC_NAMESPACE::testing::Test {
6565

6666
void testRange(TruncFunc func) {
6767
constexpr StorageType COUNT = 100'000;
68-
constexpr StorageType STEP = StorageType(-1) / COUNT;
68+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
6969
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
7070
T x = T(FPBits(v));
7171
if (isnan(x) || isinf(x))

libc/test/src/math/cos_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2020
TEST_F(LlvmLibcCosTest, Range) {
2121
static constexpr double _2pi = 6.283185307179586;
2222
constexpr StorageType COUNT = 100'000;
23-
constexpr StorageType STEP = StorageType(-1) / COUNT;
23+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
2424
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
2525
double x = double(FPBits(v));
2626
// TODO: Expand the range of testing after range reduction is implemented.

libc/test/src/math/sin_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2121
TEST_F(LlvmLibcSinTest, Range) {
2222
static constexpr double _2pi = 6.283185307179586;
2323
constexpr StorageType COUNT = 100'000;
24-
constexpr StorageType STEP = StorageType(-1) / COUNT;
24+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
2525
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
2626
double x = double(FPBits(v));
2727
// TODO: Expand the range of testing after range reduction is implemented.

libc/test/src/math/smoke/CopySignTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CopySignTest : public LIBC_NAMESPACE::testing::Test {
3232

3333
void testRange(CopySignFunc func) {
3434
constexpr StorageType COUNT = 100'000;
35-
constexpr StorageType STEP = StorageType(-1) / COUNT;
35+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
3636
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
3737
FPBits x_bits = FPBits(v);
3838
T x = T(v);

libc/test/src/math/smoke/FDimTest.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
5353
}
5454

5555
void test_in_range(FuncPtr func) {
56+
constexpr StorageType STORAGE_MAX =
57+
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
5658
constexpr StorageType COUNT = 100'001;
57-
constexpr StorageType STEP = StorageType(-1) / COUNT;
58-
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
59+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
60+
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
5961
++i, v += STEP, w -= STEP) {
6062
T x = T(FPBits(v)), y = T(FPBits(w));
6163
if (isnan(x) || isinf(x))

libc/test/src/math/smoke/FMaxTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {
5353

5454
void testRange(FMaxFunc func) {
5555
constexpr StorageType COUNT = 100'001;
56-
constexpr StorageType STEP = StorageType(-1) / COUNT;
57-
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
56+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
57+
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
5858
++i, v += STEP, w -= STEP) {
5959
T x = T(FPBits(v)), y = T(FPBits(w));
6060
if (isnan(x) || isinf(x))

libc/test/src/math/smoke/FMinTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {
5353

5454
void testRange(FMinFunc func) {
5555
constexpr StorageType COUNT = 100'001;
56-
constexpr StorageType STEP = StorageType(-1) / COUNT;
57-
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
56+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
57+
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
5858
++i, v += STEP, w -= STEP) {
5959
T x = T(FPBits(v)), y = T(FPBits(w));
6060
if (isnan(x) || isinf(x))

libc/test/src/math/smoke/LogbTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
7171
void testRange(LogbFunc func) {
7272
using StorageType = typename FPBits::StorageType;
7373
constexpr StorageType COUNT = 100'000;
74-
constexpr StorageType STEP = StorageType(-1) / COUNT;
74+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
7575
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
7676
T x = static_cast<T>(FPBits(v));
7777
if (isnan(x) || isinf(x) || x == 0.0l)

libc/test/src/math/smoke/ModfTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {
8282

8383
void testRange(ModfFunc func) {
8484
constexpr StorageType COUNT = 100'000;
85-
constexpr StorageType STEP = StorageType(-1) / COUNT;
85+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
8686
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
8787
T x = T(FPBits(v));
8888
if (isnan(x) || isinf(x) || x == T(0.0))

libc/test/src/math/tan_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2020
TEST_F(LlvmLibcTanTest, Range) {
2121
static constexpr double _2pi = 6.283185307179586;
2222
constexpr StorageType COUNT = 100'000;
23-
constexpr StorageType STEP = StorageType(-1) / COUNT;
23+
constexpr StorageType STEP = STORAGE_MAX / COUNT;
2424
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
2525
double x = double(FPBits(v));
2626
// TODO: Expand the range of testing after range reduction is implemented.

0 commit comments

Comments
 (0)