/
LongDoubleBits.h
223 lines (179 loc) · 6.62 KB
/
LongDoubleBits.h
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//===-- Bit representation of x86 long double numbers -----------*- 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_LONG_DOUBLE_BITS_H
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_LONG_DOUBLE_BITS_H
#include "src/__support/CPP/bit.h"
#include "src/__support/UInt128.h"
#include "src/__support/common.h"
#include "src/__support/macros/properties/architectures.h"
#if !defined(LIBC_TARGET_ARCH_IS_X86)
#error "Invalid include"
#endif
#include "src/__support/FPUtil/FPBits.h"
#include <stdint.h>
namespace __llvm_libc {
namespace fputil {
template <unsigned Width> struct Padding;
// i386 padding.
template <> struct Padding<4> {
static constexpr unsigned VALUE = 16;
};
// x86_64 padding.
template <> struct Padding<8> {
static constexpr unsigned VALUE = 48;
};
template <> struct FPBits<long double> {
using UIntType = UInt128;
static constexpr int EXPONENT_BIAS = 0x3FFF;
static constexpr int MAX_EXPONENT = 0x7FFF;
static constexpr UIntType MIN_SUBNORMAL = UIntType(1);
// Subnormal numbers include the implicit bit in x86 long double formats.
static constexpr UIntType MAX_SUBNORMAL =
(UIntType(1) << (MantissaWidth<long double>::VALUE)) - 1;
static constexpr UIntType MIN_NORMAL =
(UIntType(3) << MantissaWidth<long double>::VALUE);
static constexpr UIntType MAX_NORMAL =
(UIntType(MAX_EXPONENT - 1) << (MantissaWidth<long double>::VALUE + 1)) |
(UIntType(1) << MantissaWidth<long double>::VALUE) | MAX_SUBNORMAL;
using FloatProp = FloatProperties<long double>;
UIntType bits;
LIBC_INLINE void set_mantissa(UIntType mantVal) {
mantVal &= (FloatProp::MANTISSA_MASK);
bits &= ~(FloatProp::MANTISSA_MASK);
bits |= mantVal;
}
LIBC_INLINE UIntType get_mantissa() const {
return bits & FloatProp::MANTISSA_MASK;
}
LIBC_INLINE UIntType get_explicit_mantissa() const {
return bits & (FloatProp::MANTISSA_MASK | FloatProp::EXPLICIT_BIT_MASK);
}
LIBC_INLINE void set_unbiased_exponent(UIntType expVal) {
expVal =
(expVal << (FloatProp::BIT_WIDTH - 1 - FloatProp::EXPONENT_WIDTH)) &
FloatProp::EXPONENT_MASK;
bits &= ~(FloatProp::EXPONENT_MASK);
bits |= expVal;
}
LIBC_INLINE uint16_t get_unbiased_exponent() const {
return uint16_t((bits & FloatProp::EXPONENT_MASK) >>
(FloatProp::BIT_WIDTH - 1 - FloatProp::EXPONENT_WIDTH));
}
LIBC_INLINE void set_implicit_bit(bool implicitVal) {
bits &= ~(UIntType(1) << FloatProp::MANTISSA_WIDTH);
bits |= (UIntType(implicitVal) << FloatProp::MANTISSA_WIDTH);
}
LIBC_INLINE bool get_implicit_bit() const {
return bool((bits & (UIntType(1) << FloatProp::MANTISSA_WIDTH)) >>
FloatProp::MANTISSA_WIDTH);
}
LIBC_INLINE void set_sign(bool signVal) {
bits &= ~(FloatProp::SIGN_MASK);
UIntType sign1 = UIntType(signVal) << (FloatProp::BIT_WIDTH - 1);
bits |= sign1;
}
LIBC_INLINE bool get_sign() const {
return bool((bits & FloatProp::SIGN_MASK) >> (FloatProp::BIT_WIDTH - 1));
}
FPBits() : bits(0) {}
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<long double, XType>, int> = 0>
explicit FPBits(XType x) : bits(cpp::bit_cast<UIntType>(x)) {
// bits starts uninitialized, and setting it to a long double only
// overwrites the first 80 bits. This clears those upper bits.
bits = bits & ((UIntType(1) << 80) - 1);
}
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<XType, UIntType>, int> = 0>
explicit FPBits(XType x) : bits(x) {}
LIBC_INLINE operator long double() {
return cpp::bit_cast<long double>(bits);
}
LIBC_INLINE UIntType uintval() {
// We zero the padding bits as they can contain garbage.
static constexpr UIntType MASK =
(UIntType(1) << (sizeof(long double) * 8 -
Padding<sizeof(uintptr_t)>::VALUE)) -
1;
return bits & MASK;
}
LIBC_INLINE long double get_val() const {
return cpp::bit_cast<long double>(bits);
}
LIBC_INLINE int get_exponent() const {
if (get_unbiased_exponent() == 0)
return int(1) - EXPONENT_BIAS;
return int(get_unbiased_exponent()) - EXPONENT_BIAS;
}
LIBC_INLINE bool is_zero() const {
return get_unbiased_exponent() == 0 && get_mantissa() == 0 &&
get_implicit_bit() == 0;
}
LIBC_INLINE bool is_inf() const {
return get_unbiased_exponent() == MAX_EXPONENT && get_mantissa() == 0 &&
get_implicit_bit() == 1;
}
LIBC_INLINE bool is_nan() const {
if (get_unbiased_exponent() == MAX_EXPONENT) {
return (get_implicit_bit() == 0) || get_mantissa() != 0;
} else if (get_unbiased_exponent() != 0) {
return get_implicit_bit() == 0;
}
return false;
}
LIBC_INLINE bool is_inf_or_nan() const {
return (get_unbiased_exponent() == MAX_EXPONENT) ||
(get_unbiased_exponent() != 0 && get_implicit_bit() == 0);
}
// Methods below this are used by tests.
LIBC_INLINE static FPBits<long double> zero() {
return FPBits<long double>(0.0l);
}
LIBC_INLINE static FPBits<long double> neg_zero() {
FPBits<long double> bits(0.0l);
bits.set_sign(1);
return bits;
}
LIBC_INLINE static FPBits<long double> inf() {
FPBits<long double> bits(0.0l);
bits.set_unbiased_exponent(MAX_EXPONENT);
bits.set_implicit_bit(1);
return bits;
}
LIBC_INLINE static FPBits<long double> neg_inf() {
FPBits<long double> bits(0.0l);
bits.set_unbiased_exponent(MAX_EXPONENT);
bits.set_implicit_bit(1);
bits.set_sign(1);
return bits;
}
LIBC_INLINE static long double build_nan(UIntType v) {
FPBits<long double> bits(0.0l);
bits.set_unbiased_exponent(MAX_EXPONENT);
bits.set_implicit_bit(1);
bits.set_mantissa(v);
return bits;
}
LIBC_INLINE static long double build_quiet_nan(UIntType v) {
return build_nan(FloatProp::QUIET_NAN_MASK | v);
}
LIBC_INLINE static FPBits<long double>
create_value(bool sign, UIntType unbiased_exp, UIntType mantissa) {
FPBits<long double> result;
result.set_sign(sign);
result.set_unbiased_exponent(unbiased_exp);
result.set_mantissa(mantissa);
return result;
}
};
static_assert(
sizeof(FPBits<long double>) == sizeof(long double),
"Internal long double representation does not match the machine format.");
} // namespace fputil
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_LONG_DOUBLE_BITS_H