[libc-commits] [libc] [libc] Fix Darwin aarch64 fenv and sigsetjmp build support (PR #192079)

Kacper Fiedorowicz via libc-commits libc-commits at lists.llvm.org
Tue Apr 28 09:57:06 PDT 2026


================
@@ -29,13 +29,47 @@ namespace fputil {
 
 struct FEnv {
   struct FPState {
-    uint64_t StatusWord;
-    uint64_t ControlWord;
+    struct Word {
+      unsigned char Bytes[sizeof(uint32_t)];
+
+      LIBC_INLINE operator uint32_t() const {
+        uint32_t value;
+        __builtin_memcpy(&value, Bytes, sizeof(value));
+        return value;
+      }
+
+      LIBC_INLINE Word &operator=(uint32_t value) {
+        __builtin_memcpy(Bytes, &value, sizeof(value));
+        return *this;
+      }
+    };
+
+    Word ControlWord;
+    Word StatusWord;
   };
 
   static_assert(
       sizeof(fenv_t) == sizeof(FPState),
       "Internal floating point state does not match the public fenv_t type.");
+  static_assert(
+      alignof(fenv_t) == alignof(FPState),
+      "Internal floating point state alignment does not match the public "
+      "fenv_t type.");
+
+#ifndef FE_FLUSHTOZERO
+#ifdef FE_DENORM
+  static constexpr uint32_t FE_FLUSHTOZERO = FE_DENORM;
+#else
+  static constexpr uint32_t FE_FLUSHTOZERO = 0;
+#endif
+#endif
+
+  static constexpr uint32_t __fpcr_trap_invalid = 0x100;
+  static constexpr uint32_t __fpcr_trap_overflow = 0x200;
+  static constexpr uint32_t __fpcr_trap_underflow = 0x400;
+  static constexpr uint32_t __fpcr_trap_divbyzero = 0x800;
+  static constexpr uint32_t __fpcr_trap_inexact = 0x1000;
+  static constexpr uint32_t __fpcr_flush_to_zero = 0x1000000;
----------------
fenze wrote:

Yes, I checked these against Darwin's arm64 <fenv.h>. That header exposes an enum for the controllable FPCR bits:

    __fpcr_trap_invalid   = 0x00000100
    __fpcr_trap_divbyzero = 0x00000200
    __fpcr_trap_overflow  = 0x00000400
    __fpcr_trap_underflow = 0x00000800
    __fpcr_trap_inexact   = 0x00001000
    __fpcr_trap_denormal  = 0x00008000
    __fpcr_flush_to_zero  = 0x01000000

  I updated the patch to match those values and added a comment pointing back to Darwin's arm64
  <fenv.h> as the source.

  While checking this, I also noticed that FE_FLUSHTOZERO should use __fpcr_trap_denormal for the
  exception trap-enable path. __fpcr_flush_to_zero is a separate FPCR mode bit for FZ, not the
  trap-enable bit.

https://github.com/llvm/llvm-project/pull/192079


More information about the libc-commits mailing list