1 //===-- Unittests for truncl ----------------------------------------------===// 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/truncl.h" 11 #include "utils/FPUtil/FPBits.h" 12 #include "utils/MPFRWrapper/MPFRUtils.h" 13 #include "utils/UnitTest/Test.h" 14 15 using FPBits = __llvm_libc::fputil::FPBits<long double>; 16 17 namespace mpfr = __llvm_libc::testing::mpfr; 18 19 // Zero tolerance; As in, exact match with MPFR result. 20 static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::floatPrecision, 0, 21 0}; 22 23 TEST(TrunclTest, SpecialNumbers) { 24 ASSERT_TRUE(FPBits::zero() == __llvm_libc::truncl(FPBits::zero())); 25 ASSERT_TRUE(FPBits::negZero() == __llvm_libc::truncl(FPBits::negZero())); 26 27 ASSERT_TRUE(FPBits::inf() == __llvm_libc::truncl(FPBits::inf())); 28 ASSERT_TRUE(FPBits::negInf() == __llvm_libc::truncl(FPBits::negInf())); 29 30 long double nan = FPBits::buildNaN(1); 31 ASSERT_TRUE(isnan(nan) != 0); 32 ASSERT_TRUE(isnan(__llvm_libc::truncl(nan)) != 0); 33 } 34 35 TEST(TrunclTest, RoundedNumbers) { 36 ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::truncl(1.0l)); 37 ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::truncl(-1.0l)); 38 ASSERT_TRUE(FPBits(10.0l) == __llvm_libc::truncl(10.0l)); 39 ASSERT_TRUE(FPBits(-10.0l) == __llvm_libc::truncl(-10.0l)); 40 ASSERT_TRUE(FPBits(1234.0l) == __llvm_libc::truncl(1234.0l)); 41 ASSERT_TRUE(FPBits(-1234.0l) == __llvm_libc::truncl(-1234.0l)); 42 } 43 44 TEST(TrunclTest, Fractions) { 45 ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::truncl(1.5l)); 46 ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::truncl(-1.75l)); 47 ASSERT_TRUE(FPBits(10.0l) == __llvm_libc::truncl(10.32l)); 48 ASSERT_TRUE(FPBits(-10.0l) == __llvm_libc::truncl(-10.65l)); 49 ASSERT_TRUE(FPBits(1234.0l) == __llvm_libc::truncl(1234.78l)); 50 ASSERT_TRUE(FPBits(-1234.0l) == __llvm_libc::truncl(-1234.96l)); 51 } 52 53 TEST(TrunclTest, InLongDoubleRange) { 54 using UIntType = FPBits::UIntType; 55 constexpr UIntType count = 10000000; 56 constexpr UIntType step = UIntType(-1) / count; 57 for (UIntType i = 0, v = 0; i <= count; ++i, v += step) { 58 long double x = FPBits(v); 59 if (isnan(x) || isinf(x)) 60 continue; 61 62 ASSERT_MPFR_MATCH(mpfr::Operation::Trunc, x, __llvm_libc::truncl(x), 63 tolerance); 64 } 65 } 66