Skip to content

Commit 073534c

Browse files
[libc][windows] rename fenv internals for windows
On windows, including math.h causes macros for "OVERFLOW" and "UNDERFLOW" to be defined. This patch renames some variables internal to FEnvImpl.h to avoid colliding with those. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D134775
1 parent 1c835b5 commit 073534c

File tree

1 file changed

+56
-57
lines changed

1 file changed

+56
-57
lines changed

libc/src/__support/FPUtil/x86_64/FEnvImpl.h

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ static constexpr uint16_t MXCSR_ROUNDING_CONTROL_BIT_POSITION = 13;
4848
// The exception flags in the x87 status register and the MXCSR have the same
4949
// encoding as well as the same bit positions.
5050
struct ExceptionFlags {
51-
static constexpr uint16_t INVALID = 0x1;
51+
static constexpr uint16_t INVALID_F = 0x1;
5252
// Some libcs define __FE_DENORM corresponding to the denormal input
5353
// exception and include it in FE_ALL_EXCEPTS. We define and use it to
5454
// support compiling against headers provided by such libcs.
55-
static constexpr uint16_t DENORMAL = 0x2;
56-
static constexpr uint16_t DIV_BY_ZERO = 0x4;
57-
static constexpr uint16_t OVERFLOW = 0x8;
58-
static constexpr uint16_t UNDERFLOW = 0x10;
59-
static constexpr uint16_t INEXACT = 0x20;
55+
static constexpr uint16_t DENORMAL_F = 0x2;
56+
static constexpr uint16_t DIV_BY_ZERO_F = 0x4;
57+
static constexpr uint16_t OVERFLOW_F = 0x8;
58+
static constexpr uint16_t UNDERFLOW_F = 0x10;
59+
static constexpr uint16_t INEXACT_F = 0x20;
6060
};
6161

6262
// The exception control bits occupy six bits, one bit for each exception.
@@ -71,25 +71,25 @@ static constexpr uint16_t MXCSR_EXCEPTION_CONTOL_BIT_POISTION = 7;
7171
static inline uint16_t get_status_value_for_except(int excepts) {
7272
// We will make use of the fact that exception control bits are single
7373
// bit flags in the control registers.
74-
return (excepts & FE_INVALID ? ExceptionFlags::INVALID : 0) |
74+
return (excepts & FE_INVALID ? ExceptionFlags::INVALID_F : 0) |
7575
#ifdef __FE_DENORM
76-
(excepts & __FE_DENORM ? ExceptionFlags::DENORMAL : 0) |
76+
(excepts & __FE_DENORM ? ExceptionFlags::DENORMAL_F : 0) |
7777
#endif // __FE_DENORM
78-
(excepts & FE_DIVBYZERO ? ExceptionFlags::DIV_BY_ZERO : 0) |
79-
(excepts & FE_OVERFLOW ? ExceptionFlags::OVERFLOW : 0) |
80-
(excepts & FE_UNDERFLOW ? ExceptionFlags::UNDERFLOW : 0) |
81-
(excepts & FE_INEXACT ? ExceptionFlags::INEXACT : 0);
78+
(excepts & FE_DIVBYZERO ? ExceptionFlags::DIV_BY_ZERO_F : 0) |
79+
(excepts & FE_OVERFLOW ? ExceptionFlags::OVERFLOW_F : 0) |
80+
(excepts & FE_UNDERFLOW ? ExceptionFlags::UNDERFLOW_F : 0) |
81+
(excepts & FE_INEXACT ? ExceptionFlags::INEXACT_F : 0);
8282
}
8383

8484
static inline int exception_status_to_macro(uint16_t status) {
85-
return (status & ExceptionFlags::INVALID ? FE_INVALID : 0) |
85+
return (status & ExceptionFlags::INVALID_F ? FE_INVALID : 0) |
8686
#ifdef __FE_DENORM
87-
(status & ExceptionFlags::DENORMAL ? __FE_DENORM : 0) |
87+
(status & ExceptionFlags::DENORMAL_F ? __FE_DENORM : 0) |
8888
#endif // __FE_DENORM
89-
(status & ExceptionFlags::DIV_BY_ZERO ? FE_DIVBYZERO : 0) |
90-
(status & ExceptionFlags::OVERFLOW ? FE_OVERFLOW : 0) |
91-
(status & ExceptionFlags::UNDERFLOW ? FE_UNDERFLOW : 0) |
92-
(status & ExceptionFlags::INEXACT ? FE_INEXACT : 0);
89+
(status & ExceptionFlags::DIV_BY_ZERO_F ? FE_DIVBYZERO : 0) |
90+
(status & ExceptionFlags::OVERFLOW_F ? FE_OVERFLOW : 0) |
91+
(status & ExceptionFlags::UNDERFLOW_F ? FE_UNDERFLOW : 0) |
92+
(status & ExceptionFlags::INEXACT_F ? FE_INEXACT : 0);
9393
}
9494

9595
struct X87StateDescriptor {
@@ -263,19 +263,19 @@ static inline int raise_except(int excepts) {
263263
internal::fwait();
264264
};
265265

266-
if (status_value & internal::ExceptionFlags::INVALID)
267-
raise_helper(internal::ExceptionFlags::INVALID);
268-
if (status_value & internal::ExceptionFlags::DIV_BY_ZERO)
269-
raise_helper(internal::ExceptionFlags::DIV_BY_ZERO);
270-
if (status_value & internal::ExceptionFlags::OVERFLOW)
271-
raise_helper(internal::ExceptionFlags::OVERFLOW);
272-
if (status_value & internal::ExceptionFlags::UNDERFLOW)
273-
raise_helper(internal::ExceptionFlags::UNDERFLOW);
274-
if (status_value & internal::ExceptionFlags::INEXACT)
275-
raise_helper(internal::ExceptionFlags::INEXACT);
266+
if (status_value & internal::ExceptionFlags::INVALID_F)
267+
raise_helper(internal::ExceptionFlags::INVALID_F);
268+
if (status_value & internal::ExceptionFlags::DIV_BY_ZERO_F)
269+
raise_helper(internal::ExceptionFlags::DIV_BY_ZERO_F);
270+
if (status_value & internal::ExceptionFlags::OVERFLOW_F)
271+
raise_helper(internal::ExceptionFlags::OVERFLOW_F);
272+
if (status_value & internal::ExceptionFlags::UNDERFLOW_F)
273+
raise_helper(internal::ExceptionFlags::UNDERFLOW_F);
274+
if (status_value & internal::ExceptionFlags::INEXACT_F)
275+
raise_helper(internal::ExceptionFlags::INEXACT_F);
276276
#ifdef __FE_DENORM
277-
if (status_value & internal::ExceptionFlags::DENORMAL) {
278-
raise_helper(internal::ExceptionFlags::DENORMAL);
277+
if (status_value & internal::ExceptionFlags::DENORMAL_F) {
278+
raise_helper(internal::ExceptionFlags::DENORMAL_F);
279279
}
280280
#endif // __FE_DENORM
281281

@@ -371,24 +371,24 @@ static_assert(
371371
// The exception flags in the Windows FEnv struct and the MXCSR have almost
372372
// reversed bit positions.
373373
struct WinExceptionFlags {
374-
static constexpr uint32_t INEXACT = 0x01;
375-
static constexpr uint32_t UNDERFLOW = 0x02;
376-
static constexpr uint32_t OVERFLOW = 0x04;
377-
static constexpr uint32_t DIV_BY_ZERO = 0x08;
378-
static constexpr uint32_t INVALID = 0x10;
379-
static constexpr uint32_t DENORMAL = 0x20;
374+
static constexpr uint32_t INEXACT_WIN = 0x01;
375+
static constexpr uint32_t UNDERFLOW_WIN = 0x02;
376+
static constexpr uint32_t OVERFLOW_WIN = 0x04;
377+
static constexpr uint32_t DIV_BY_ZERO_WIN = 0x08;
378+
static constexpr uint32_t INVALID_WIN = 0x10;
379+
static constexpr uint32_t DENORMAL_WIN = 0x20;
380380

381381
// The Windows FEnv struct has a second copy of all of these bits in the high
382382
// byte of the 32 bit control word. These are used as the source of truth when
383383
// calling fesetenv.
384384
static constexpr uint32_t HIGH_OFFSET = 24;
385385

386-
static constexpr uint32_t HIGH_INEXACT = INEXACT << HIGH_OFFSET;
387-
static constexpr uint32_t HIGH_UNDERFLOW = UNDERFLOW << HIGH_OFFSET;
388-
static constexpr uint32_t HIGH_OVERFLOW = OVERFLOW << HIGH_OFFSET;
389-
static constexpr uint32_t HIGH_DIV_BY_ZERO = DIV_BY_ZERO << HIGH_OFFSET;
390-
static constexpr uint32_t HIGH_INVALID = INVALID << HIGH_OFFSET;
391-
static constexpr uint32_t HIGH_DENORMAL = DENORMAL << HIGH_OFFSET;
386+
static constexpr uint32_t HIGH_INEXACT = INEXACT_WIN << HIGH_OFFSET;
387+
static constexpr uint32_t HIGH_UNDERFLOW = UNDERFLOW_WIN << HIGH_OFFSET;
388+
static constexpr uint32_t HIGH_OVERFLOW = OVERFLOW_WIN << HIGH_OFFSET;
389+
static constexpr uint32_t HIGH_DIV_BY_ZERO = DIV_BY_ZERO_WIN << HIGH_OFFSET;
390+
static constexpr uint32_t HIGH_INVALID = INVALID_WIN << HIGH_OFFSET;
391+
static constexpr uint32_t HIGH_DENORMAL = DENORMAL_WIN << HIGH_OFFSET;
392392
};
393393

394394
/*
@@ -470,25 +470,24 @@ static inline int get_env(fenv_t *envp) {
470470
uint32_t mxcsr = internal::get_mxcsr();
471471

472472
// Set exception flags in the status word
473-
status_word |= (mxcsr & (internal::ExceptionFlags::INVALID |
474-
internal::ExceptionFlags::DENORMAL))
473+
status_word |= (mxcsr & (internal::ExceptionFlags::INVALID_F |
474+
internal::ExceptionFlags::DENORMAL_F))
475475
<< 4;
476-
status_word |= (mxcsr & internal::ExceptionFlags::DIV_BY_ZERO) << 1;
477-
status_word |= (mxcsr & internal::ExceptionFlags::OVERFLOW) >> 1;
478-
status_word |= (mxcsr & internal::ExceptionFlags::UNDERFLOW) >> 3;
479-
status_word |= (mxcsr & internal::ExceptionFlags::INEXACT) >> 5;
476+
status_word |= (mxcsr & internal::ExceptionFlags::DIV_BY_ZERO_F) << 1;
477+
status_word |= (mxcsr & internal::ExceptionFlags::OVERFLOW_F) >> 1;
478+
status_word |= (mxcsr & internal::ExceptionFlags::UNDERFLOW_F) >> 3;
479+
status_word |= (mxcsr & internal::ExceptionFlags::INEXACT_F) >> 5;
480480
status_word |= status_word << WinExceptionFlags::HIGH_OFFSET;
481481

482482
// Set exception masks in bits 0-5 and 24-29
483-
control_word |=
484-
(mxcsr &
485-
((internal::ExceptionFlags::INVALID | internal::ExceptionFlags::DENORMAL)
486-
<< 7)) >>
487-
3;
488-
control_word |= (mxcsr & (internal::ExceptionFlags::DIV_BY_ZERO << 7)) >> 6;
489-
control_word |= (mxcsr & (internal::ExceptionFlags::OVERFLOW << 7)) >> 8;
490-
control_word |= (mxcsr & (internal::ExceptionFlags::UNDERFLOW << 7)) >> 10;
491-
control_word |= (mxcsr & (internal::ExceptionFlags::INEXACT << 7)) >> 12;
483+
control_word |= (mxcsr & ((internal::ExceptionFlags::INVALID_F |
484+
internal::ExceptionFlags::DENORMAL_F)
485+
<< 7)) >>
486+
3;
487+
control_word |= (mxcsr & (internal::ExceptionFlags::DIV_BY_ZERO_F << 7)) >> 6;
488+
control_word |= (mxcsr & (internal::ExceptionFlags::OVERFLOW_F << 7)) >> 8;
489+
control_word |= (mxcsr & (internal::ExceptionFlags::UNDERFLOW_F << 7)) >> 10;
490+
control_word |= (mxcsr & (internal::ExceptionFlags::INEXACT_F << 7)) >> 12;
492491
control_word |= control_word << WinExceptionFlags::HIGH_OFFSET;
493492

494493
// Set rounding in bits 8-9 and 30-31

0 commit comments

Comments
 (0)