frexp_test.cpp
5.34 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//===-- Unittests for frexp -----------------------------------------------===//
//
// 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/frexp.h"
#include "utils/FPUtil/BasicOperations.h"
#include "utils/FPUtil/BitPatterns.h"
#include "utils/FPUtil/ClassificationFunctions.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/FPUtil/FloatOperations.h"
#include "utils/FPUtil/FloatProperties.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/Test.h"
using FPBits = __llvm_libc::fputil::FPBits<double>;
using __llvm_libc::fputil::valueAsBits;
using __llvm_libc::fputil::valueFromBits;
namespace mpfr = __llvm_libc::testing::mpfr;
using BitPatterns = __llvm_libc::fputil::BitPatterns<double>;
using Properties = __llvm_libc::fputil::FloatProperties<double>;
TEST(FrexpTest, SpecialNumbers) {
int exponent;
EXPECT_EQ(BitPatterns::aQuietNaN,
valueAsBits(__llvm_libc::frexp(
valueFromBits(BitPatterns::aQuietNaN), &exponent)));
EXPECT_EQ(BitPatterns::aNegativeQuietNaN,
valueAsBits(__llvm_libc::frexp(
valueFromBits(BitPatterns::aNegativeQuietNaN), &exponent)));
EXPECT_EQ(BitPatterns::aSignallingNaN,
valueAsBits(__llvm_libc::frexp(
valueFromBits(BitPatterns::aSignallingNaN), &exponent)));
EXPECT_EQ(
BitPatterns::aNegativeSignallingNaN,
valueAsBits(__llvm_libc::frexp(
valueFromBits(BitPatterns::aNegativeSignallingNaN), &exponent)));
EXPECT_EQ(BitPatterns::inf, valueAsBits(__llvm_libc::frexp(
valueFromBits(BitPatterns::inf), &exponent)));
EXPECT_EQ(BitPatterns::negInf,
valueAsBits(__llvm_libc::frexp(valueFromBits(BitPatterns::negInf),
&exponent)));
EXPECT_EQ(BitPatterns::zero,
valueAsBits(__llvm_libc::frexp(valueFromBits(BitPatterns::zero),
&exponent)));
EXPECT_EQ(exponent, 0);
EXPECT_EQ(BitPatterns::negZero,
valueAsBits(__llvm_libc::frexp(valueFromBits(BitPatterns::negZero),
&exponent)));
EXPECT_EQ(exponent, 0);
}
TEST(FrexpTest, PowersOfTwo) {
int exponent;
EXPECT_EQ(valueAsBits(0.5), valueAsBits(__llvm_libc::frexp(1.0, &exponent)));
EXPECT_EQ(exponent, 1);
EXPECT_EQ(valueAsBits(-0.5),
valueAsBits(__llvm_libc::frexp(-1.0, &exponent)));
EXPECT_EQ(exponent, 1);
EXPECT_EQ(valueAsBits(0.5), valueAsBits(__llvm_libc::frexp(2.0, &exponent)));
EXPECT_EQ(exponent, 2);
EXPECT_EQ(valueAsBits(-0.5),
valueAsBits(__llvm_libc::frexp(-2.0, &exponent)));
EXPECT_EQ(exponent, 2);
EXPECT_EQ(valueAsBits(0.5), valueAsBits(__llvm_libc::frexp(4.0, &exponent)));
EXPECT_EQ(exponent, 3);
EXPECT_EQ(valueAsBits(-0.5),
valueAsBits(__llvm_libc::frexp(-4.0, &exponent)));
EXPECT_EQ(exponent, 3);
EXPECT_EQ(valueAsBits(0.5), valueAsBits(__llvm_libc::frexp(8.0, &exponent)));
EXPECT_EQ(exponent, 4);
EXPECT_EQ(valueAsBits(-0.5),
valueAsBits(__llvm_libc::frexp(-8.0, &exponent)));
EXPECT_EQ(exponent, 4);
EXPECT_EQ(valueAsBits(0.5), valueAsBits(__llvm_libc::frexp(16.0, &exponent)));
EXPECT_EQ(exponent, 5);
EXPECT_EQ(valueAsBits(-0.5),
valueAsBits(__llvm_libc::frexp(-16.0, &exponent)));
EXPECT_EQ(exponent, 5);
EXPECT_EQ(valueAsBits(0.5), valueAsBits(__llvm_libc::frexp(32.0, &exponent)));
EXPECT_EQ(exponent, 6);
EXPECT_EQ(valueAsBits(-0.5),
valueAsBits(__llvm_libc::frexp(-32.0, &exponent)));
EXPECT_EQ(exponent, 6);
EXPECT_EQ(valueAsBits(0.5), valueAsBits(__llvm_libc::frexp(64.0, &exponent)));
EXPECT_EQ(exponent, 7);
EXPECT_EQ(valueAsBits(-0.5),
valueAsBits(__llvm_libc::frexp(-64.0, &exponent)));
EXPECT_EQ(exponent, 7);
}
TEST(FrexpTest, SomeIntegers) {
int exponent;
EXPECT_EQ(valueAsBits(0.75),
valueAsBits(__llvm_libc::frexp(24.0, &exponent)));
EXPECT_EQ(exponent, 5);
EXPECT_EQ(valueAsBits(-0.75),
valueAsBits(__llvm_libc::frexp(-24.0, &exponent)));
EXPECT_EQ(exponent, 5);
EXPECT_EQ(valueAsBits(0.625),
valueAsBits(__llvm_libc::frexp(40.0, &exponent)));
EXPECT_EQ(exponent, 6);
EXPECT_EQ(valueAsBits(-0.625),
valueAsBits(__llvm_libc::frexp(-40.0, &exponent)));
EXPECT_EQ(exponent, 6);
EXPECT_EQ(valueAsBits(0.78125),
valueAsBits(__llvm_libc::frexp(800.0, &exponent)));
EXPECT_EQ(exponent, 10);
EXPECT_EQ(valueAsBits(-0.78125),
valueAsBits(__llvm_libc::frexp(-800.0, &exponent)));
EXPECT_EQ(exponent, 10);
}
TEST(FrexpTest, InDoubleRange) {
using UIntType = FPBits::UIntType;
constexpr UIntType count = 1000001;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
double x = FPBits(v);
if (isnan(x) || isinf(x) || x == 0.0)
continue;
mpfr::BinaryOutput<double> result;
result.f = __llvm_libc::frexp(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);
}
}