[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
Mon Jun 26 17:20:54 PDT 2023


ddcc created this revision.
ddcc added reviewers: lntue, sivachandra, jhuber6.
Herald added projects: libc-project, All.
ddcc requested review of this revision.

Provide platform-specific x87 FPU definitions and operations


Repository:
  rG LLVM Github Monorepo

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,23 @@
 
   // Copy the exception status flags from envp.
   x87_status.status_word &= ~uint16_t(0x3F);
+#ifdef __APPLE__
+  x87_status.status_word |= (fpstate->status_word & 0x3F);
+#else
   x87_status.status_word |= (fpstate->x87_status.status_word & 0x3F);
+#endif // __APPLE__
   // Copy other non-sensitive parts of the status word.
-  for (int i = 0; i < 5; i++)
+#ifndef __APPLE__
+  for (int i = 0; i < 5; i++) {
     x87_status._[i] = fpstate->x87_status._[i];
+  }
+#endif // __APPLE__
   // We can set the x87 control word as is as there no sensitive bits.
+#ifdef __APPLE__
+  x87_status.control_word = fpstate->control_word;
+#else
   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.534793.patch
Type: text/x-patch
Size: 2723 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230627/03291fb8/attachment-0001.bin>


More information about the libc-commits mailing list