diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -49,7 +49,7 @@ endif() option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF) - +option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON) option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF) if(LLVM_LIBC_CLANG_TIDY) diff --git a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h --- a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h +++ b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h @@ -30,7 +30,7 @@ if (ybits.is_nan()) return y; if (xbits.is_inf() || ybits.is_zero()) - return FPBits::build_nan(1); + return FPBits::build_quiet_nan(1); if (xbits.is_zero()) { q = 0; diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h --- a/libc/src/__support/FPUtil/FPBits.h +++ b/libc/src/__support/FPUtil/FPBits.h @@ -170,6 +170,10 @@ return T(bits); } + static constexpr T build_quiet_nan(UIntType v) { + return build_nan(FloatProp::QUIET_NAN_MASK | v); + } + // The function convert integer number and unbiased exponent to proper float // T type: // Result = number * 2^(ep+1 - exponent_bias) diff --git a/libc/src/__support/FPUtil/generic/FMod.h b/libc/src/__support/FPUtil/generic/FMod.h --- a/libc/src/__support/FPUtil/generic/FMod.h +++ b/libc/src/__support/FPUtil/generic/FMod.h @@ -124,7 +124,7 @@ static bool PreCheck(T x, T y, T &out) { using FPB = fputil::FPBits; - const T quiet_NaN = FPB::build_nan(FPB::FloatProp::QUIET_NAN_MASK); + const T quiet_NaN = FPB::build_quiet_nan(0); FPB sx(x), sy(y); if (likely(!sy.is_zero() && !sy.is_inf_or_nan() && !sx.is_inf_or_nan())) { return false; diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h --- a/libc/src/__support/FPUtil/generic/sqrt.h +++ b/libc/src/__support/FPUtil/generic/sqrt.h @@ -79,7 +79,7 @@ if (bits.is_inf_or_nan()) { if (bits.get_sign() && (bits.get_mantissa() == 0)) { // sqrt(-Inf) = NaN - return FPBits::build_nan(ONE >> 1); + return FPBits::build_quiet_nan(ONE >> 1); } else { // sqrt(NaN) = NaN // sqrt(+Inf) = +Inf @@ -91,7 +91,7 @@ return x; } else if (bits.get_sign()) { // sqrt( negative numbers ) = NaN - return FPBits::build_nan(ONE >> 1); + return FPBits::build_quiet_nan(ONE >> 1); } else { int x_exp = bits.get_exponent(); UIntType x_mant = bits.get_mantissa(); diff --git a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h --- a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h +++ b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h @@ -44,7 +44,7 @@ if (bits.is_inf_or_nan()) { if (bits.get_sign() && (bits.get_mantissa() == 0)) { // sqrt(-Inf) = NaN - return FPBits::build_nan(ONE >> 1); + return FPBits::build_quiet_nan(ONE >> 1); } else { // sqrt(NaN) = NaN // sqrt(+Inf) = +Inf @@ -56,7 +56,7 @@ return x; } else if (bits.get_sign()) { // sqrt( negative numbers ) = NaN - return FPBits::build_nan(ONE >> 1); + return FPBits::build_quiet_nan(ONE >> 1); } else { int x_exp = bits.get_exponent(); UIntType x_mant = bits.get_mantissa(); diff --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h --- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h +++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h @@ -185,6 +185,10 @@ return bits; } + static long double build_quiet_nan(UIntType v) { + return build_nan(FloatProp::QUIET_NAN_MASK | v); + } + inline static FPBits create_value(bool sign, UIntType unbiased_exp, UIntType mantissa) { FPBits result; diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h --- a/libc/src/__support/str_to_float.h +++ b/libc/src/__support/str_to_float.h @@ -971,11 +971,11 @@ } nan_mantissa |= fputil::FloatProperties::QUIET_NAN_MASK; if (result.get_sign()) { - result = fputil::FPBits(result.build_nan(nan_mantissa)); + result = fputil::FPBits(result.build_quiet_nan(nan_mantissa)); result.set_sign(true); } else { result.set_sign(false); - result = fputil::FPBits(result.build_nan(nan_mantissa)); + result = fputil::FPBits(result.build_quiet_nan(nan_mantissa)); } } } else if ((*src | 32) == 'i') { // INF diff --git a/libc/test/src/math/FDimTest.h b/libc/test/src/math/FDimTest.h --- a/libc/test/src/math/FDimTest.h +++ b/libc/test/src/math/FDimTest.h @@ -74,7 +74,7 @@ private: // constexpr does not work on FPBits yet, so we cannot have these constants as // static. - const T nan = T(__llvm_libc::fputil::FPBits::build_nan(1)); + const T nan = T(__llvm_libc::fputil::FPBits::build_quiet_nan(1)); const T inf = T(__llvm_libc::fputil::FPBits::inf()); const T neg_inf = T(__llvm_libc::fputil::FPBits::neg_inf()); const T zero = T(__llvm_libc::fputil::FPBits::zero()); diff --git a/libc/test/src/math/FmaTest.h b/libc/test/src/math/FmaTest.h --- a/libc/test/src/math/FmaTest.h +++ b/libc/test/src/math/FmaTest.h @@ -23,7 +23,7 @@ using Func = T (*)(T, T, T); using FPBits = __llvm_libc::fputil::FPBits; using UIntType = typename FPBits::UIntType; - const T nan = T(__llvm_libc::fputil::FPBits::build_nan(1)); + const T nan = T(__llvm_libc::fputil::FPBits::build_quiet_nan(1)); const T inf = T(__llvm_libc::fputil::FPBits::inf()); const T neg_inf = T(__llvm_libc::fputil::FPBits::neg_inf()); const T zero = T(__llvm_libc::fputil::FPBits::zero()); diff --git a/libc/test/src/math/HypotTest.h b/libc/test/src/math/HypotTest.h --- a/libc/test/src/math/HypotTest.h +++ b/libc/test/src/math/HypotTest.h @@ -24,7 +24,7 @@ using Func = T (*)(T, T); using FPBits = __llvm_libc::fputil::FPBits; using UIntType = typename FPBits::UIntType; - const T nan = T(FPBits::build_nan(1)); + const T nan = T(FPBits::build_quiet_nan(1)); const T inf = T(FPBits::inf()); const T neg_inf = T(FPBits::neg_inf()); const T zero = T(FPBits::zero()); diff --git a/libc/test/src/math/ILogbTest.h b/libc/test/src/math/ILogbTest.h --- a/libc/test/src/math/ILogbTest.h +++ b/libc/test/src/math/ILogbTest.h @@ -26,7 +26,7 @@ EXPECT_EQ(FP_ILOGB0, func(T(__llvm_libc::fputil::FPBits::neg_zero()))); EXPECT_EQ(FP_ILOGBNAN, - func(T(__llvm_libc::fputil::FPBits::build_nan(1)))); + func(T(__llvm_libc::fputil::FPBits::build_quiet_nan(1)))); EXPECT_EQ(INT_MAX, func(T(__llvm_libc::fputil::FPBits::inf()))); EXPECT_EQ(INT_MAX, func(T(__llvm_libc::fputil::FPBits::neg_inf()))); diff --git a/libc/test/src/math/LdExpTest.h b/libc/test/src/math/LdExpTest.h --- a/libc/test/src/math/LdExpTest.h +++ b/libc/test/src/math/LdExpTest.h @@ -32,7 +32,7 @@ const T neg_zero = T(__llvm_libc::fputil::FPBits::neg_zero()); const T inf = T(__llvm_libc::fputil::FPBits::inf()); const T neg_inf = T(__llvm_libc::fputil::FPBits::neg_inf()); - const T nan = T(__llvm_libc::fputil::FPBits::build_nan(1)); + const T nan = T(__llvm_libc::fputil::FPBits::build_quiet_nan(1)); public: typedef T (*LdExpFunc)(T, int); diff --git a/libc/test/src/math/NextAfterTest.h b/libc/test/src/math/NextAfterTest.h --- a/libc/test/src/math/NextAfterTest.h +++ b/libc/test/src/math/NextAfterTest.h @@ -30,7 +30,7 @@ const T neg_zero = T(FPBits::neg_zero()); const T inf = T(FPBits::inf()); const T neg_inf = T(FPBits::neg_inf()); - const T nan = T(FPBits::build_nan(1)); + const T nan = T(FPBits::build_quiet_nan(1)); const UIntType min_subnormal = FPBits::MIN_SUBNORMAL; const UIntType max_subnormal = FPBits::MAX_SUBNORMAL; const UIntType min_normal = FPBits::MIN_NORMAL; diff --git a/libc/test/src/math/RIntTest.h b/libc/test/src/math/RIntTest.h --- a/libc/test/src/math/RIntTest.h +++ b/libc/test/src/math/RIntTest.h @@ -37,7 +37,7 @@ const T neg_zero = T(FPBits::neg_zero()); const T inf = T(FPBits::inf()); const T neg_inf = T(FPBits::neg_inf()); - const T nan = T(FPBits::build_nan(1)); + const T nan = T(FPBits::build_quiet_nan(1)); static inline mpfr::RoundingMode to_mpfr_rounding_mode(int mode) { switch (mode) { diff --git a/libc/test/src/math/RemQuoTest.h b/libc/test/src/math/RemQuoTest.h --- a/libc/test/src/math/RemQuoTest.h +++ b/libc/test/src/math/RemQuoTest.h @@ -27,7 +27,7 @@ const T neg_zero = T(__llvm_libc::fputil::FPBits::neg_zero()); const T inf = T(__llvm_libc::fputil::FPBits::inf()); const T neg_inf = T(__llvm_libc::fputil::FPBits::neg_inf()); - const T nan = T(__llvm_libc::fputil::FPBits::build_nan(1)); + const T nan = T(__llvm_libc::fputil::FPBits::build_quiet_nan(1)); public: typedef T (*RemQuoFunc)(T, T, int *); diff --git a/libc/test/src/math/RoundToIntegerTest.h b/libc/test/src/math/RoundToIntegerTest.h --- a/libc/test/src/math/RoundToIntegerTest.h +++ b/libc/test/src/math/RoundToIntegerTest.h @@ -36,7 +36,7 @@ const F neg_zero = F(__llvm_libc::fputil::FPBits::neg_zero()); const F inf = F(__llvm_libc::fputil::FPBits::inf()); const F neg_inf = F(__llvm_libc::fputil::FPBits::neg_inf()); - const F nan = F(__llvm_libc::fputil::FPBits::build_nan(1)); + const F nan = F(__llvm_libc::fputil::FPBits::build_quiet_nan(1)); static constexpr I INTEGER_MIN = I(1) << (sizeof(I) * 8 - 1); static constexpr I INTEGER_MAX = -(INTEGER_MIN + 1); @@ -84,7 +84,10 @@ void do_infinity_and_na_n_test(RoundToIntegerFunc func) { test_one_input(func, inf, INTEGER_MAX, true); test_one_input(func, neg_inf, INTEGER_MIN, true); +#if LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR + // Result is not well-defined, we always returns INTEGER_MAX test_one_input(func, nan, INTEGER_MAX, true); +#endif } void testInfinityAndNaN(RoundToIntegerFunc func) { diff --git a/libc/utils/UnitTest/FPMatcher.h b/libc/utils/UnitTest/FPMatcher.h --- a/libc/utils/UnitTest/FPMatcher.h +++ b/libc/utils/UnitTest/FPMatcher.h @@ -72,7 +72,7 @@ using UIntType = typename FPBits::UIntType; \ const T zero = T(FPBits::zero()); \ const T neg_zero = T(FPBits::neg_zero()); \ - const T aNaN = T(FPBits::build_nan(1)); \ + const T aNaN = T(FPBits::build_quiet_nan(1)); \ const T inf = T(FPBits::inf()); \ const T neg_inf = T(FPBits::neg_inf());