1 //===-- Unittests for frexpl ----------------------------------------------===// 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/frexpl.h" 11 #include "utils/FPUtil/BasicOperations.h" 12 #include "utils/FPUtil/FPBits.h" 13 #include "utils/UnitTest/Test.h" 14 15 #include <iostream> 16 17 using FPBits = __llvm_libc::fputil::FPBits<long double>; 18 19 TEST(FrexplTest, SpecialNumbers) { 20 int exponent; 21 22 EXPECT_TRUE(FPBits::inf() == __llvm_libc::frexpl(FPBits::inf(), &exponent)); 23 EXPECT_TRUE(FPBits::negInf() == 24 __llvm_libc::frexpl(FPBits::negInf(), &exponent)); 25 26 EXPECT_TRUE(FPBits::zero() == __llvm_libc::frexpl(FPBits::zero(), &exponent)); 27 EXPECT_EQ(exponent, 0); 28 29 EXPECT_TRUE(FPBits::negZero() == 30 __llvm_libc::frexpl(FPBits::negZero(), &exponent)); 31 EXPECT_EQ(exponent, 0); 32 33 EXPECT_TRUE( 34 FPBits(__llvm_libc::frexpl(FPBits::buildNaN(1), &exponent)).isNaN()); 35 } 36 37 TEST(FrexplTest, PowersOfTwo) { 38 int exponent; 39 40 EXPECT_TRUE(0.5l == __llvm_libc::frexpl(1.0l, &exponent)); 41 EXPECT_EQ(exponent, 1); 42 EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-1.0l, &exponent)); 43 EXPECT_EQ(exponent, 1); 44 45 EXPECT_TRUE(0.5l == __llvm_libc::frexpl(2.0l, &exponent)); 46 EXPECT_EQ(exponent, 2); 47 EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-2.0l, &exponent)); 48 EXPECT_EQ(exponent, 2); 49 50 EXPECT_TRUE(0.5l == __llvm_libc::frexpl(4.0l, &exponent)); 51 EXPECT_EQ(exponent, 3); 52 EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-4.0l, &exponent)); 53 EXPECT_EQ(exponent, 3); 54 55 EXPECT_TRUE(0.5l == __llvm_libc::frexpl(8.0l, &exponent)); 56 EXPECT_EQ(exponent, 4); 57 EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-8.0l, &exponent)); 58 EXPECT_EQ(exponent, 4); 59 60 EXPECT_TRUE(0.5l == __llvm_libc::frexpl(16.0l, &exponent)); 61 EXPECT_EQ(exponent, 5); 62 EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-16.0l, &exponent)); 63 EXPECT_EQ(exponent, 5); 64 65 EXPECT_TRUE(0.5l == __llvm_libc::frexpl(32.0l, &exponent)); 66 EXPECT_EQ(exponent, 6); 67 EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-32.0l, &exponent)); 68 EXPECT_EQ(exponent, 6); 69 } 70 71 TEST(FrexplTest, SomeIntegers) { 72 int exponent; 73 74 EXPECT_TRUE(0.75l == __llvm_libc::frexpl(24.0l, &exponent)); 75 EXPECT_EQ(exponent, 5); 76 EXPECT_TRUE(-0.75l == __llvm_libc::frexpl(-24.0l, &exponent)); 77 EXPECT_EQ(exponent, 5); 78 79 EXPECT_TRUE(0.625l == __llvm_libc::frexpl(40.0l, &exponent)); 80 EXPECT_EQ(exponent, 6); 81 EXPECT_TRUE(-0.625l == __llvm_libc::frexpl(-40.0l, &exponent)); 82 EXPECT_EQ(exponent, 6); 83 84 EXPECT_TRUE(0.78125l == __llvm_libc::frexpl(800.0l, &exponent)); 85 EXPECT_EQ(exponent, 10); 86 EXPECT_TRUE(-0.78125l == __llvm_libc::frexpl(-800.0l, &exponent)); 87 EXPECT_EQ(exponent, 10); 88 } 89 90 TEST(FrexplTest, LongDoubleRange) { 91 using UIntType = FPBits::UIntType; 92 constexpr UIntType count = 10000000; 93 constexpr UIntType step = UIntType(-1) / count; 94 for (UIntType i = 0, v = 0; i <= count; ++i, v += step) { 95 long double x = FPBits(v); 96 if (isnan(x) || isinf(x) || x == 0.0l) 97 continue; 98 99 int exponent; 100 long double frac = __llvm_libc::frexpl(x, &exponent); 101 102 ASSERT_TRUE(__llvm_libc::fputil::abs(frac) < 1.0l); 103 ASSERT_TRUE(__llvm_libc::fputil::abs(frac) >= 0.5l); 104 } 105 } 106