xref: /llvm-project/libc/test/src/math/exp2_test.cpp (revision 46944b0cbc9a9d8daad0182c40fcd3560bc9ca35)
18ca614aaSTue Ly //===-- Unittests for 2^x -------------------------------------------------===//
28ca614aaSTue Ly //
38ca614aaSTue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48ca614aaSTue Ly // See https://llvm.org/LICENSE.txt for license information.
58ca614aaSTue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68ca614aaSTue Ly //
78ca614aaSTue Ly //===----------------------------------------------------------------------===//
88ca614aaSTue Ly 
95748ad84Slntue #include "hdr/math_macros.h"
108ca614aaSTue Ly #include "src/__support/FPUtil/FPBits.h"
118ca614aaSTue Ly #include "src/errno/libc_errno.h"
128ca614aaSTue Ly #include "src/math/exp2.h"
138ca614aaSTue Ly #include "test/UnitTest/FPMatcher.h"
148ca614aaSTue Ly #include "test/UnitTest/Test.h"
158ca614aaSTue Ly #include "utils/MPFRWrapper/MPFRUtils.h"
168ca614aaSTue Ly 
178ca614aaSTue Ly #include <stdint.h>
188ca614aaSTue Ly 
193fd5113cSlntue using LlvmLibcExp2Test = LIBC_NAMESPACE::testing::FPTest<double>;
203fd5113cSlntue 
21b6bc9d72SGuillaume Chatelet namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
22b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::testing::tlog;
238ca614aaSTue Ly 
243fd5113cSlntue TEST_F(LlvmLibcExp2Test, SpecialNumbers) {
25b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::exp2(aNaN));
26b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ(inf, LIBC_NAMESPACE::exp2(inf));
27b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::exp2(neg_inf));
28b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_WITH_EXCEPTION(zero, LIBC_NAMESPACE::exp2(-0x1.0p20),
29b6bc9d72SGuillaume Chatelet                               FE_UNDERFLOW);
30b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2(0x1.0p20), FE_OVERFLOW);
31b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::exp2(0.0));
32b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::exp2(-0.0));
338ca614aaSTue Ly }
348ca614aaSTue Ly 
353fd5113cSlntue TEST_F(LlvmLibcExp2Test, TrickyInputs) {
368ca614aaSTue Ly   constexpr int N = 16;
378ca614aaSTue Ly   constexpr uint64_t INPUTS[N] = {
388ca614aaSTue Ly       0x3FD79289C6E6A5C0,
398ca614aaSTue Ly       0x3FD05DE80A173EA0, // 0x1.05de80a173eap-2
408ca614aaSTue Ly       0xbf1eb7a4cb841fcc, // -0x1.eb7a4cb841fccp-14
418ca614aaSTue Ly       0xbf19a61fb925970d,
428ca614aaSTue Ly       0x3fda7b764e2cf47a, // 0x1.a7b764e2cf47ap-2
438ca614aaSTue Ly       0xc04757852a4b93aa, // -0x1.757852a4b93aap+5
448ca614aaSTue Ly       0x4044c19e5712e377, // x=0x1.4c19e5712e377p+5
458ca614aaSTue Ly       0xbf19a61fb925970d, // x=-0x1.9a61fb925970dp-14
468ca614aaSTue Ly       0xc039a74cdab36c28, // x=-0x1.9a74cdab36c28p+4
478ca614aaSTue Ly       0xc085b3e4e2e3bba9, // x=-0x1.5b3e4e2e3bba9p+9
488ca614aaSTue Ly       0xc086960d591aec34, // x=-0x1.6960d591aec34p+9
498ca614aaSTue Ly       0xc086232c09d58d91, // x=-0x1.6232c09d58d91p+9
508ca614aaSTue Ly       0xc0874910d52d3051, // x=-0x1.74910d52d3051p9
518ca614aaSTue Ly       0xc0867a172ceb0990, // x=-0x1.67a172ceb099p+9
528ca614aaSTue Ly       0xc08ff80000000000, // x=-0x1.ff8p+9
538ca614aaSTue Ly       0xbc971547652b82fe, // x=-0x1.71547652b82fep-54
548ca614aaSTue Ly   };
558ca614aaSTue Ly   for (int i = 0; i < N; ++i) {
562856db0dSGuillaume Chatelet     double x = FPBits(INPUTS[i]).get_val();
578ca614aaSTue Ly     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
58b6bc9d72SGuillaume Chatelet                                    LIBC_NAMESPACE::exp2(x), 0.5);
598ca614aaSTue Ly   }
608ca614aaSTue Ly }
618ca614aaSTue Ly 
623fd5113cSlntue TEST_F(LlvmLibcExp2Test, InDoubleRange) {
638ca614aaSTue Ly   constexpr uint64_t COUNT = 1'231;
64b6bc9d72SGuillaume Chatelet   uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0.25).uintval();
65b6bc9d72SGuillaume Chatelet   uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(4.0).uintval();
668ca614aaSTue Ly   uint64_t STEP = (STOP - START) / COUNT;
678ca614aaSTue Ly 
688ca614aaSTue Ly   auto test = [&](mpfr::RoundingMode rounding_mode) {
698ca614aaSTue Ly     mpfr::ForceRoundingMode __r(rounding_mode);
708ca614aaSTue Ly     if (!__r.success)
718ca614aaSTue Ly       return;
728ca614aaSTue Ly 
738ca614aaSTue Ly     uint64_t fails = 0;
748ca614aaSTue Ly     uint64_t count = 0;
758ca614aaSTue Ly     uint64_t cc = 0;
768ca614aaSTue Ly     double mx, mr = 0.0;
778ca614aaSTue Ly     double tol = 0.5;
788ca614aaSTue Ly 
798ca614aaSTue Ly     for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
808ca614aaSTue Ly       double x = FPBits(v).get_val();
81*f8f5b175SNhat Nguyen       if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
828ca614aaSTue Ly         continue;
833eb1e6d8Smichaelrj-google       LIBC_NAMESPACE::libc_errno = 0;
84b6bc9d72SGuillaume Chatelet       double result = LIBC_NAMESPACE::exp2(x);
858ca614aaSTue Ly       ++cc;
86*f8f5b175SNhat Nguyen       if (FPBits(result).is_nan() || FPBits(result).is_inf())
878ca614aaSTue Ly         continue;
888ca614aaSTue Ly 
898ca614aaSTue Ly       ++count;
908ca614aaSTue Ly 
918ca614aaSTue Ly       if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Exp2, x, result,
928ca614aaSTue Ly                                              0.5, rounding_mode)) {
938ca614aaSTue Ly         ++fails;
948ca614aaSTue Ly         while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Exp2, x,
958ca614aaSTue Ly                                                   result, tol, rounding_mode)) {
968ca614aaSTue Ly           mx = x;
978ca614aaSTue Ly           mr = result;
988ca614aaSTue Ly 
998ca614aaSTue Ly           if (tol > 1000.0)
1008ca614aaSTue Ly             break;
1018ca614aaSTue Ly 
1028ca614aaSTue Ly           tol *= 2.0;
1038ca614aaSTue Ly         }
1048ca614aaSTue Ly       }
1058ca614aaSTue Ly     }
1068ca614aaSTue Ly     tlog << " Exp2 failed: " << fails << "/" << count << "/" << cc
1078ca614aaSTue Ly          << " tests.\n";
1088ca614aaSTue Ly     tlog << "   Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
1098ca614aaSTue Ly     if (fails) {
1108ca614aaSTue Ly       EXPECT_MPFR_MATCH(mpfr::Operation::Exp2, mx, mr, 0.5, rounding_mode);
1118ca614aaSTue Ly     }
1128ca614aaSTue Ly   };
1138ca614aaSTue Ly 
1148ca614aaSTue Ly   tlog << " Test Rounding To Nearest...\n";
1158ca614aaSTue Ly   test(mpfr::RoundingMode::Nearest);
1168ca614aaSTue Ly 
1178ca614aaSTue Ly   tlog << " Test Rounding Downward...\n";
1188ca614aaSTue Ly   test(mpfr::RoundingMode::Downward);
1198ca614aaSTue Ly 
1208ca614aaSTue Ly   tlog << " Test Rounding Upward...\n";
1218ca614aaSTue Ly   test(mpfr::RoundingMode::Upward);
1228ca614aaSTue Ly 
1238ca614aaSTue Ly   tlog << " Test Rounding Toward Zero...\n";
1248ca614aaSTue Ly   test(mpfr::RoundingMode::TowardZero);
1258ca614aaSTue Ly }
126