1 //===-- Unittests for copysignl -------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "include/math.h" 10 #include "src/math/copysignl.h" 11 #include "utils/FPUtil/FPBits.h" 12 #include "utils/FPUtil/TestHelpers.h" 13 #include "utils/UnitTest/Test.h" 14 15 using FPBits = __llvm_libc::fputil::FPBits<long double>; 16 17 static const long double zero = FPBits::zero(); 18 static const long double negZero = FPBits::negZero(); 19 static const long double nan = FPBits::buildNaN(1); 20 static const long double inf = FPBits::inf(); 21 static const long double negInf = FPBits::negInf(); 22 23 TEST(CopySinlTest, SpecialNumbers) { 24 EXPECT_FP_EQ(nan, __llvm_libc::copysignl(nan, -1.0)); 25 EXPECT_FP_EQ(nan, __llvm_libc::copysignl(nan, 1.0)); 26 27 EXPECT_FP_EQ(negInf, __llvm_libc::copysignl(inf, -1.0)); 28 EXPECT_FP_EQ(inf, __llvm_libc::copysignl(negInf, 1.0)); 29 30 EXPECT_FP_EQ(negZero, __llvm_libc::copysignl(zero, -1.0)); 31 EXPECT_FP_EQ(zero, __llvm_libc::copysignl(negZero, 1.0)); 32 } 33 34 TEST(CopySinlTest, InLongDoubleRange) { 35 using UIntType = FPBits::UIntType; 36 constexpr UIntType count = 10000000; 37 constexpr UIntType step = UIntType(-1) / count; 38 for (UIntType i = 0, v = 0; i <= count; ++i, v += step) { 39 long double x = FPBits(v); 40 if (isnan(x) || isinf(x) || x == 0) 41 continue; 42 43 long double res1 = __llvm_libc::copysignl(x, -x); 44 ASSERT_FP_EQ(res1, -x); 45 46 long double res2 = __llvm_libc::copysignl(x, x); 47 ASSERT_FP_EQ(res2, x); 48 } 49 } 50