26
26
27
27
namespace LIBC_NAMESPACE_DECL {
28
28
namespace fputil {
29
-
30
29
struct FEnv {
31
30
struct FPState {
32
31
uint32_t ControlWord;
@@ -42,31 +41,31 @@ struct FEnv {
42
41
static constexpr uint32_t DOWNWARD = 0x2 ;
43
42
static constexpr uint32_t TOWARDZERO = 0x3 ;
44
43
45
- static constexpr uint32_t INVALID = 0x1 ;
46
- static constexpr uint32_t DIVBYZERO = 0x2 ;
47
- static constexpr uint32_t OVERFLOW = 0x4 ;
48
- static constexpr uint32_t UNDERFLOW = 0x8 ;
49
- static constexpr uint32_t INEXACT = 0x10 ;
44
+ static constexpr uint32_t INVALID_F = 0x1 ;
45
+ static constexpr uint32_t DIVBYZERO_F = 0x2 ;
46
+ static constexpr uint32_t OVERFLOW_F = 0x4 ;
47
+ static constexpr uint32_t UNDERFLOW_F = 0x8 ;
48
+ static constexpr uint32_t INEXACT_F = 0x10 ;
50
49
51
50
// Zero-th bit is the first bit.
52
51
static constexpr uint32_t RoundingControlBitPosition = 22 ;
53
52
static constexpr uint32_t ExceptionStatusFlagsBitPosition = 0 ;
54
53
static constexpr uint32_t ExceptionControlFlagsBitPosition = 8 ;
55
54
56
55
LIBC_INLINE static uint32_t getStatusValueForExcept (int excepts) {
57
- return ((excepts & FE_INVALID) ? INVALID : 0 ) |
58
- ((excepts & FE_DIVBYZERO) ? DIVBYZERO : 0 ) |
59
- ((excepts & FE_OVERFLOW) ? OVERFLOW : 0 ) |
60
- ((excepts & FE_UNDERFLOW) ? UNDERFLOW : 0 ) |
61
- ((excepts & FE_INEXACT) ? INEXACT : 0 );
56
+ return ((excepts & FE_INVALID) ? INVALID_F : 0 ) |
57
+ ((excepts & FE_DIVBYZERO) ? DIVBYZERO_F : 0 ) |
58
+ ((excepts & FE_OVERFLOW) ? OVERFLOW_F : 0 ) |
59
+ ((excepts & FE_UNDERFLOW) ? UNDERFLOW_F : 0 ) |
60
+ ((excepts & FE_INEXACT) ? INEXACT_F : 0 );
62
61
}
63
62
64
63
LIBC_INLINE static int exceptionStatusToMacro (uint32_t status) {
65
- return ((status & INVALID ) ? FE_INVALID : 0 ) |
66
- ((status & DIVBYZERO ) ? FE_DIVBYZERO : 0 ) |
67
- ((status & OVERFLOW ) ? FE_OVERFLOW : 0 ) |
68
- ((status & UNDERFLOW ) ? FE_UNDERFLOW : 0 ) |
69
- ((status & INEXACT ) ? FE_INEXACT : 0 );
64
+ return ((status & INVALID_F ) ? FE_INVALID : 0 ) |
65
+ ((status & DIVBYZERO_F ) ? FE_DIVBYZERO : 0 ) |
66
+ ((status & OVERFLOW_F ) ? FE_OVERFLOW : 0 ) |
67
+ ((status & UNDERFLOW_F ) ? FE_UNDERFLOW : 0 ) |
68
+ ((status & INEXACT_F ) ? FE_INEXACT : 0 );
70
69
}
71
70
72
71
static uint32_t getControlWord () {
@@ -171,44 +170,44 @@ LIBC_INLINE int raise_except(int excepts) {
171
170
uint32_t toRaise = FEnv::getStatusValueForExcept (excepts);
172
171
int result = 0 ;
173
172
174
- if (toRaise & FEnv::INVALID ) {
173
+ if (toRaise & FEnv::INVALID_F ) {
175
174
divfunc (zero, zero);
176
175
uint32_t statusWord = FEnv::getStatusWord ();
177
176
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
178
- FEnv::INVALID ))
177
+ FEnv::INVALID_F ))
179
178
result = -1 ;
180
179
}
181
180
182
- if (toRaise & FEnv::DIVBYZERO ) {
181
+ if (toRaise & FEnv::DIVBYZERO_F ) {
183
182
divfunc (one, zero);
184
183
uint32_t statusWord = FEnv::getStatusWord ();
185
184
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
186
- FEnv::DIVBYZERO ))
185
+ FEnv::DIVBYZERO_F ))
187
186
result = -1 ;
188
187
}
189
- if (toRaise & FEnv::OVERFLOW ) {
188
+ if (toRaise & FEnv::OVERFLOW_F ) {
190
189
divfunc (largeValue, smallValue);
191
190
uint32_t statusWord = FEnv::getStatusWord ();
192
191
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
193
- FEnv::OVERFLOW ))
192
+ FEnv::OVERFLOW_F ))
194
193
result = -1 ;
195
194
}
196
- if (toRaise & FEnv::UNDERFLOW ) {
195
+ if (toRaise & FEnv::UNDERFLOW_F ) {
197
196
divfunc (smallValue, largeValue);
198
197
uint32_t statusWord = FEnv::getStatusWord ();
199
198
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
200
- FEnv::UNDERFLOW ))
199
+ FEnv::UNDERFLOW_F ))
201
200
result = -1 ;
202
201
}
203
- if (toRaise & FEnv::INEXACT ) {
202
+ if (toRaise & FEnv::INEXACT_F ) {
204
203
float two = 2 .0f ;
205
204
float three = 3 .0f ;
206
205
// 2.0 / 3.0 cannot be represented exactly in any radix 2 floating point
207
206
// format.
208
207
divfunc (two, three);
209
208
uint32_t statusWord = FEnv::getStatusWord ();
210
209
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
211
- FEnv::INEXACT ))
210
+ FEnv::INEXACT_F ))
212
211
result = -1 ;
213
212
}
214
213
return result;
@@ -278,7 +277,6 @@ LIBC_INLINE int set_env(const fenv_t *envp) {
278
277
FEnv::writeStatusWord (state->StatusWord );
279
278
return 0 ;
280
279
}
281
-
282
280
} // namespace fputil
283
281
} // namespace LIBC_NAMESPACE_DECL
284
282
0 commit comments