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