1 //===-- Unittests for copysignf -------------------------------------------===//
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/copysignf.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<float>;
16 
17 DECLARE_SPECIAL_CONSTANTS(float)
18 
TEST(CopySinfTest,SpecialNumbers)19 TEST(CopySinfTest, SpecialNumbers) {
20   EXPECT_FP_EQ(nan, __llvm_libc::copysignf(nan, -1.0));
21   EXPECT_FP_EQ(nan, __llvm_libc::copysignf(nan, 1.0));
22 
23   EXPECT_FP_EQ(negInf, __llvm_libc::copysignf(inf, -1.0));
24   EXPECT_FP_EQ(inf, __llvm_libc::copysignf(negInf, 1.0));
25 
26   EXPECT_FP_EQ(negZero, __llvm_libc::copysignf(zero, -1.0));
27   EXPECT_FP_EQ(zero, __llvm_libc::copysignf(negZero, 1.0));
28 }
29 
TEST(CopySinfTest,InFloatRange)30 TEST(CopySinfTest, InFloatRange) {
31   using UIntType = FPBits::UIntType;
32   constexpr UIntType count = 1000000;
33   constexpr UIntType step = UIntType(-1) / count;
34   for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
35     float x = FPBits(v);
36     if (isnan(x) || isinf(x) || x == 0)
37       continue;
38 
39     float res1 = __llvm_libc::copysignf(x, -x);
40     ASSERT_FP_EQ(res1, -x);
41 
42     float res2 = __llvm_libc::copysignf(x, x);
43     ASSERT_FP_EQ(res2, x);
44   }
45 }
46