1 //===-- Unittests for copysign --------------------------------------------===// 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/copysign.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<double>; 16 17 static const double zero = FPBits::zero(); 18 static const double negZero = FPBits::negZero(); 19 static const double nan = FPBits::buildNaN(1); 20 static const double inf = FPBits::inf(); 21 static const double negInf = FPBits::negInf(); 22 23 TEST(CopySignTest, SpecialNumbers) { 24 EXPECT_FP_EQ(nan, __llvm_libc::copysign(nan, -1.0)); 25 EXPECT_FP_EQ(nan, __llvm_libc::copysign(nan, 1.0)); 26 27 EXPECT_FP_EQ(negInf, __llvm_libc::copysign(inf, -1.0)); 28 EXPECT_FP_EQ(inf, __llvm_libc::copysign(negInf, 1.0)); 29 30 EXPECT_FP_EQ(negZero, __llvm_libc::copysign(zero, -1.0)); 31 EXPECT_FP_EQ(zero, __llvm_libc::copysign(negZero, 1.0)); 32 } 33 34 TEST(CopySignTest, InDoubleRange) { 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 double x = FPBits(v); 40 if (isnan(x) || isinf(x) || x == 0) 41 continue; 42 43 double res1 = __llvm_libc::copysign(x, -x); 44 ASSERT_FP_EQ(res1, -x); 45 46 double res2 = __llvm_libc::copysign(x, x); 47 ASSERT_FP_EQ(res2, x); 48 } 49 } 50