frexpl_test.cpp
3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===-- Unittests for frexpl ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "include/math.h"
#include "src/math/frexpl.h"
#include "utils/FPUtil/BasicOperations.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/Test.h"
using FPBits = __llvm_libc::fputil::FPBits<long double>;
namespace mpfr = __llvm_libc::testing::mpfr;
TEST(FrexplTest, SpecialNumbers) {
int exponent;
EXPECT_TRUE(FPBits::inf() == __llvm_libc::frexpl(FPBits::inf(), &exponent));
EXPECT_TRUE(FPBits::negInf() ==
__llvm_libc::frexpl(FPBits::negInf(), &exponent));
EXPECT_TRUE(FPBits::zero() == __llvm_libc::frexpl(FPBits::zero(), &exponent));
EXPECT_EQ(exponent, 0);
EXPECT_TRUE(FPBits::negZero() ==
__llvm_libc::frexpl(FPBits::negZero(), &exponent));
EXPECT_EQ(exponent, 0);
EXPECT_TRUE(
FPBits(__llvm_libc::frexpl(FPBits::buildNaN(1), &exponent)).isNaN());
}
TEST(FrexplTest, PowersOfTwo) {
int exponent;
EXPECT_TRUE(0.5l == __llvm_libc::frexpl(1.0l, &exponent));
EXPECT_EQ(exponent, 1);
EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-1.0l, &exponent));
EXPECT_EQ(exponent, 1);
EXPECT_TRUE(0.5l == __llvm_libc::frexpl(2.0l, &exponent));
EXPECT_EQ(exponent, 2);
EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-2.0l, &exponent));
EXPECT_EQ(exponent, 2);
EXPECT_TRUE(0.5l == __llvm_libc::frexpl(4.0l, &exponent));
EXPECT_EQ(exponent, 3);
EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-4.0l, &exponent));
EXPECT_EQ(exponent, 3);
EXPECT_TRUE(0.5l == __llvm_libc::frexpl(8.0l, &exponent));
EXPECT_EQ(exponent, 4);
EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-8.0l, &exponent));
EXPECT_EQ(exponent, 4);
EXPECT_TRUE(0.5l == __llvm_libc::frexpl(16.0l, &exponent));
EXPECT_EQ(exponent, 5);
EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-16.0l, &exponent));
EXPECT_EQ(exponent, 5);
EXPECT_TRUE(0.5l == __llvm_libc::frexpl(32.0l, &exponent));
EXPECT_EQ(exponent, 6);
EXPECT_TRUE(-0.5l == __llvm_libc::frexpl(-32.0l, &exponent));
EXPECT_EQ(exponent, 6);
}
TEST(FrexplTest, SomeIntegers) {
int exponent;
EXPECT_TRUE(0.75l == __llvm_libc::frexpl(24.0l, &exponent));
EXPECT_EQ(exponent, 5);
EXPECT_TRUE(-0.75l == __llvm_libc::frexpl(-24.0l, &exponent));
EXPECT_EQ(exponent, 5);
EXPECT_TRUE(0.625l == __llvm_libc::frexpl(40.0l, &exponent));
EXPECT_EQ(exponent, 6);
EXPECT_TRUE(-0.625l == __llvm_libc::frexpl(-40.0l, &exponent));
EXPECT_EQ(exponent, 6);
EXPECT_TRUE(0.78125l == __llvm_libc::frexpl(800.0l, &exponent));
EXPECT_EQ(exponent, 10);
EXPECT_TRUE(-0.78125l == __llvm_libc::frexpl(-800.0l, &exponent));
EXPECT_EQ(exponent, 10);
}
TEST(FrexplTest, LongDoubleRange) {
using UIntType = FPBits::UIntType;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
long double x = FPBits(v);
if (isnan(x) || isinf(x) || x == 0.0l)
continue;
mpfr::BinaryOutput<long double> result;
result.f = __llvm_libc::frexpl(x, &result.i);
ASSERT_TRUE(__llvm_libc::fputil::abs(result.f) < 1.0);
ASSERT_TRUE(__llvm_libc::fputil::abs(result.f) >= 0.5);
ASSERT_MPFR_MATCH(mpfr::Operation::Frexp, x, result, 0.0);
}
}