[libc-commits] [libc] a450c38 - [libc][fenv] Fix performance regression for x86 mxcsr utilities. (#172717)

via libc-commits libc-commits at lists.llvm.org
Wed Dec 17 12:07:15 PST 2025


Author: lntue
Date: 2025-12-17T15:07:08-05:00
New Revision: a450c38012419bbe6109e2c4a07b89d2f259eac4

URL: https://github.com/llvm/llvm-project/commit/a450c38012419bbe6109e2c4a07b89d2f259eac4
DIFF: https://github.com/llvm/llvm-project/commit/a450c38012419bbe6109e2c4a07b89d2f259eac4.diff

LOG: [libc][fenv] Fix performance regression for x86 mxcsr utilities. (#172717)

In a recent update to x86's fenv, we use `_MM_GET/SET_EXCEPTION_STATE`
macros from `<immintrin.h>` which turn out to do a bit more work than
just read and write the mxcsr register. In this PR we change those to a
direct read and write mxcsr intrinsics.

Added: 
    

Modified: 
    libc/src/__support/FPUtil/x86_64/fenv_mxcsr_utils.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/x86_64/fenv_mxcsr_utils.h b/libc/src/__support/FPUtil/x86_64/fenv_mxcsr_utils.h
index 421cf67fc90bd..e28aa3a9411e7 100644
--- a/libc/src/__support/FPUtil/x86_64/fenv_mxcsr_utils.h
+++ b/libc/src/__support/FPUtil/x86_64/fenv_mxcsr_utils.h
@@ -61,14 +61,14 @@ LIBC_INLINE static uint32_t get_mxcsr() { return _mm_getcsr(); }
 LIBC_INLINE static void write_mxcsr(uint32_t w) { _mm_setcsr(w); }
 
 LIBC_INLINE static void clear_except(uint16_t excepts) {
-  uint32_t mxcsr = _MM_GET_EXCEPTION_STATE();
+  uint32_t mxcsr = get_mxcsr();
   mxcsr &= ~static_cast<uint32_t>(excepts);
-  _MM_SET_EXCEPTION_STATE(mxcsr);
+  write_mxcsr(mxcsr);
 }
 
 LIBC_INLINE static uint16_t test_except(uint16_t excepts) {
   uint32_t mxcsr = get_mxcsr();
-  return static_cast<uint16_t>(excepts & mxcsr);
+  return static_cast<uint16_t>(excepts & ExceptionFlags::ALL_F & mxcsr);
 }
 
 LIBC_INLINE static uint16_t get_except() {
@@ -83,9 +83,9 @@ LIBC_INLINE static void set_except(uint16_t excepts) {
 }
 
 LIBC_INLINE static void raise_except(uint16_t excepts) {
-  uint32_t mxcsr = _MM_GET_EXCEPTION_STATE();
-  mxcsr |= excepts;
-  _MM_SET_EXCEPTION_STATE(mxcsr);
+  uint32_t mxcsr = get_mxcsr();
+  mxcsr |= excepts & ExceptionFlags::ALL_F;
+  write_mxcsr(mxcsr);
 #ifdef LIBC_TRAP_ON_RAISE_FP_EXCEPT
   // We will try to trigger the SIGFPE if floating point exceptions are not
   // masked.  Since we already set all the floating point exception flags, we


        


More information about the libc-commits mailing list