ManipulationFunctions.h
5.1 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//===-- Floating-point manipulation functions -------------------*- C++ -*-===//
//
// 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 "FPBits.h"
#include "NearestIntegerOperations.h"
#include "utils/CPP/TypeTraits.h"
#ifndef LLVM_LIBC_UTILS_FPUTIL_MANIPULATION_FUNCTIONS_H
#define LLVM_LIBC_UTILS_FPUTIL_MANIPULATION_FUNCTIONS_H
namespace __llvm_libc {
namespace fputil {
#if defined(__x86_64__) || defined(__i386__)
template <typename T> struct Standard754Type {
static constexpr bool Value =
cpp::IsSame<float, cpp::RemoveCVType<T>>::Value ||
cpp::IsSame<double, cpp::RemoveCVType<T>>::Value;
};
#else
template <typename T> struct Standard754Type {
static constexpr bool Value = cpp::IsFloatingPointType<T>::Value;
};
#endif
template <typename T> static inline T frexp_impl(FPBits<T> &bits, int &exp) {
exp = bits.getExponent() + 1;
static constexpr uint16_t resultExponent = FPBits<T>::exponentBias - 1;
bits.exponent = resultExponent;
return bits;
}
template <typename T, cpp::EnableIfType<Standard754Type<T>::Value, int> = 0>
static inline T frexp(T x, int &exp) {
FPBits<T> bits(x);
if (bits.isInfOrNaN())
return x;
if (bits.isZero()) {
exp = 0;
return x;
}
return frexp_impl(bits, exp);
}
#if defined(__x86_64__) || defined(__i386__)
static inline long double frexp(long double x, int &exp) {
FPBits<long double> bits(x);
if (bits.isInfOrNaN())
return x;
if (bits.isZero()) {
exp = 0;
return x;
}
if (bits.exponent != 0 || bits.implicitBit == 1)
return frexp_impl(bits, exp);
exp = bits.getExponent();
int shiftCount = 0;
uint64_t fullMantissa = *reinterpret_cast<uint64_t *>(&bits);
static constexpr uint64_t msBitMask = uint64_t(1) << 63;
for (; (fullMantissa & msBitMask) == uint64_t(0);
fullMantissa <<= 1, ++shiftCount) {
// This for loop will terminate as fullMantissa is != 0. If it were 0,
// then x will be NaN and handled before control reaches here.
// When the loop terminates, fullMantissa will represent the full mantissa
// of a normal long double value. That is, the implicit bit has the value
// of 1.
}
exp = exp - shiftCount + 1;
*reinterpret_cast<uint64_t *>(&bits) = fullMantissa;
bits.exponent = FPBits<long double>::exponentBias - 1;
return bits;
}
#endif
template <typename T,
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T modf(T x, T &iptr) {
FPBits<T> bits(x);
if (bits.isZero() || bits.isNaN()) {
iptr = x;
return x;
} else if (bits.isInf()) {
iptr = x;
return bits.sign ? FPBits<T>::negZero() : FPBits<T>::zero();
} else {
iptr = trunc(x);
if (x == iptr) {
// If x is already an integer value, then return zero with the right
// sign.
return bits.sign ? FPBits<T>::negZero() : FPBits<T>::zero();
} else {
return x - iptr;
}
}
}
template <typename T,
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T copysign(T x, T y) {
FPBits<T> xbits(x);
xbits.sign = FPBits<T>(y).sign;
return xbits;
}
template <typename T> static inline T logb_impl(const FPBits<T> &bits) {
return bits.getExponent();
}
template <typename T, cpp::EnableIfType<Standard754Type<T>::Value, int> = 0>
static inline T logb(T x) {
FPBits<T> bits(x);
if (bits.isZero()) {
// TODO(Floating point exception): Raise div-by-zero exception.
// TODO(errno): POSIX requires setting errno to ERANGE.
return FPBits<T>::negInf();
} else if (bits.isNaN()) {
return x;
} else if (bits.isInf()) {
// Return positive infinity.
return FPBits<T>::inf();
}
return logb_impl(bits);
}
#if defined(__x86_64__) || defined(__i386__)
static inline long double logb(long double x) {
FPBits<long double> bits(x);
if (bits.isZero()) {
// TODO(Floating point exception): Raise div-by-zero exception.
// TODO(errno): POSIX requires setting errno to ERANGE.
return FPBits<long double>::negInf();
} else if (bits.isNaN()) {
return x;
} else if (bits.isInf()) {
// Return positive infinity.
return FPBits<long double>::inf();
}
if (bits.exponent != 0 || bits.implicitBit == 1)
return logb_impl(bits);
int exp = bits.getExponent();
int shiftCount = 0;
uint64_t fullMantissa = *reinterpret_cast<uint64_t *>(&bits);
static constexpr uint64_t msBitMask = uint64_t(1) << 63;
for (; (fullMantissa & msBitMask) == uint64_t(0);
fullMantissa <<= 1, ++shiftCount) {
// This for loop will terminate as fullMantissa is != 0. If it were 0,
// then x will be NaN and handled before control reaches here.
// When the loop terminates, fullMantissa will represent the full mantissa
// of a normal long double value. That is, the implicit bit has the value
// of 1.
}
return exp - shiftCount;
}
#endif
} // namespace fputil
} // namespace __llvm_libc
#endif // LLVM_LIBC_UTILS_FPUTIL_MANIPULATION_FUNCTIONS_H