[libc-commits] [PATCH] D153823: [libc][math] Fix floating-point test support on x86_64 Apple machines

Dominic Chen via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Jul 12 00:38:59 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG50414422ac15: [libc][math] Fix floating-point test support on x86_64 Apple machines (authored by ddcc).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153823/new/

https://reviews.llvm.org/D153823

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


Index: libc/src/__support/FPUtil/x86_64/FEnvImpl.h
===================================================================
--- libc/src/__support/FPUtil/x86_64/FEnvImpl.h
+++ libc/src/__support/FPUtil/x86_64/FEnvImpl.h
@@ -215,10 +215,12 @@
 }
 
 LIBC_INLINE int test_except(int excepts) {
-  uint16_t status_value = internal::get_status_value_for_except(excepts);
+  uint16_t status_word = internal::get_x87_status_word();
+  uint32_t mxcsr = internal::get_mxcsr();
   // Check both x87 status word and MXCSR.
+  uint16_t status_value = internal::get_status_value_for_except(excepts);
   return internal::exception_status_to_macro(
-      static_cast<uint16_t>(status_value & internal::get_mxcsr()));
+      static_cast<uint16_t>(status_value & (status_word | mxcsr)));
 }
 
 // Sets the exception flags but does not trigger the exception handler.
@@ -347,13 +349,20 @@
 
 namespace internal {
 
-#ifdef _WIN32
+#if defined(_WIN32)
 // MSVC fenv.h defines a very simple representation of the floating point state
 // which just consists of control and status words of the x87 unit.
 struct FPState {
   uint32_t control_word;
   uint32_t status_word;
 };
+#elif defined(__APPLE__)
+struct FPState {
+  uint16_t control_word;
+  uint16_t status_word;
+  uint32_t mxcsr;
+  uint8_t reserved[8];
+};
 #else
 struct FPState {
   X87StateDescriptor x87_status;
@@ -557,7 +566,14 @@
 #else
 LIBC_INLINE int get_env(fenv_t *envp) {
   internal::FPState *state = reinterpret_cast<internal::FPState *>(envp);
+#ifdef __APPLE__
+  internal::X87StateDescriptor x87_status;
+  internal::get_x87_state_descriptor(x87_status);
+  state->control_word = x87_status.control_word;
+  state->status_word = x87_status.status_word;
+#else
   internal::get_x87_state_descriptor(state->x87_status);
+#endif // __APPLE__
   state->mxcsr = internal::get_mxcsr();
   return 0;
 }
@@ -605,12 +621,18 @@
 
   // Copy the exception status flags from envp.
   x87_status.status_word &= ~uint16_t(0x3F);
+#ifdef __APPLE__
+  x87_status.status_word |= (fpstate->status_word & 0x3F);
+  // We can set the x87 control word as is as there no sensitive bits.
+  x87_status.control_word = fpstate->control_word;
+#else
   x87_status.status_word |= (fpstate->x87_status.status_word & 0x3F);
   // Copy other non-sensitive parts of the status word.
   for (int i = 0; i < 5; i++)
     x87_status._[i] = fpstate->x87_status._[i];
   // We can set the x87 control word as is as there no sensitive bits.
   x87_status.control_word = fpstate->x87_status.control_word;
+#endif // __APPLE__
   internal::write_x87_state_descriptor(x87_status);
 
   // We can write the MXCSR state as is as there are no sensitive bits.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153823.539417.patch
Type: text/x-patch
Size: 2671 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230712/1f3dbd26/attachment.bin>


More information about the libc-commits mailing list