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 "src/math/copysignl.h"
10 #include "utils/FPUtil/FPBits.h"
11 #include "utils/FPUtil/TestHelpers.h"
12 #include "utils/UnitTest/Test.h"
13 #include <math.h>
14 
15 using FPBits = __llvm_libc::fputil::FPBits<long double>;
16 
17 DECLARE_SPECIAL_CONSTANTS(long double)
18 
19 TEST(LlvmLibcCopySinlTest, SpecialNumbers) {
20   EXPECT_FP_EQ(aNaN, __llvm_libc::copysignl(aNaN, -1.0));
21   EXPECT_FP_EQ(aNaN, __llvm_libc::copysignl(aNaN, 1.0));
22 
23   EXPECT_FP_EQ(negInf, __llvm_libc::copysignl(inf, -1.0));
24   EXPECT_FP_EQ(inf, __llvm_libc::copysignl(negInf, 1.0));
25 
26   EXPECT_FP_EQ(negZero, __llvm_libc::copysignl(zero, -1.0));
27   EXPECT_FP_EQ(zero, __llvm_libc::copysignl(negZero, 1.0));
28 }
29 
30 TEST(LlvmLibcCopySinlTest, InLongDoubleRange) {
31   using UIntType = FPBits::UIntType;
32   constexpr UIntType count = 10000000;
33   constexpr UIntType step = UIntType(-1) / count;
34   for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
35     long double x = FPBits(v);
36     if (isnan(x) || isinf(x) || x == 0)
37       continue;
38 
39     long double res1 = __llvm_libc::copysignl(x, -x);
40     ASSERT_FP_EQ(res1, -x);
41 
42     long double res2 = __llvm_libc::copysignl(x, x);
43     ASSERT_FP_EQ(res2, x);
44   }
45 }
46