From 69890e363972ecd450ff3275c8ac63d302fc20f7 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Thu, 8 Oct 2026 11:24:58 -0400 Subject: [PATCH] Use BuiltinRelaxedFP ops instead of C shims when they're available. There used to not be any way to attach reassociate or contract flags to fadd/fmul from Swift, which meant that we had to use inline C shims to accomplish this. But I added some Builtins to Swift, so now we don't need C to do this any longer. --- Sources/RealModule/Double+Real.swift | 8 ++++++++ Sources/RealModule/Float+Real.swift | 8 ++++++++ Sources/RealModule/Float16+Real.swift | 13 +++++++++++-- Sources/RealModule/Float80+Real.swift | 8 ++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Sources/RealModule/Double+Real.swift b/Sources/RealModule/Double+Real.swift index 1a4138d..388cefa 100644 --- a/Sources/RealModule/Double+Real.swift +++ b/Sources/RealModule/Double+Real.swift @@ -222,11 +222,19 @@ extension Double: Real { @_transparent public static func _relaxedAdd(_ a: Double, _ b: Double) -> Double { +#if hasFeature(BuiltinRelaxedFP) // Swift 6.5 and later + Double(Builtin.fadd_relaxed_FPIEEE64(a._value, b._value)) +#else _numerics_relaxed_add(a, b) +#endif } @_transparent public static func _relaxedMul(_ a: Double, _ b: Double) -> Double { +#if hasFeature(BuiltinRelaxedFP) // Swift 6.5 and later + Double(Builtin.fmul_relaxed_FPIEEE64(a._value, b._value)) +#else _numerics_relaxed_mul(a, b) +#endif } } diff --git a/Sources/RealModule/Float+Real.swift b/Sources/RealModule/Float+Real.swift index ab459bb..f1982ac 100644 --- a/Sources/RealModule/Float+Real.swift +++ b/Sources/RealModule/Float+Real.swift @@ -195,11 +195,19 @@ extension Float: Real { @_transparent public static func _relaxedAdd(_ a: Float, _ b: Float) -> Float { +#if hasFeature(BuiltinRelaxedFP) // Swift 6.5 and later + Float(Builtin.fadd_relaxed_FPIEEE32(a._value, b._value)) +#else _numerics_relaxed_addf(a, b) +#endif } @_transparent public static func _relaxedMul(_ a: Float, _ b: Float) -> Float { +#if hasFeature(BuiltinRelaxedFP) // Swift 6.5 and later + Float(Builtin.fmul_relaxed_FPIEEE32(a._value, b._value)) +#else _numerics_relaxed_mulf(a, b) +#endif } } diff --git a/Sources/RealModule/Float16+Real.swift b/Sources/RealModule/Float16+Real.swift index 98e97e8..02debd5 100644 --- a/Sources/RealModule/Float16+Real.swift +++ b/Sources/RealModule/Float16+Real.swift @@ -173,18 +173,27 @@ extension Float16: Real { } #endif - #if !arch(wasm32) + #if !arch(wasm32) || hasFeature(BuiltinRelaxedFP) // WASM doesn't have _Float16 on the C side, so we can't define the C hooks - // that these use. TODO: implement these as Swift builtins instead. + // that these use. However, if we have BuiltinRelaxedFP we can use builtins + // to do this directly without needing the C hooks. @_transparent public static func _relaxedAdd(_ a: Float16, _ b: Float16) -> Float16 { + #if hasFeature(BuiltinRelaxedFP) // Swift 6.5 and later + Float16(Builtin.fadd_relaxed_FPIEEE16(a._value, b._value)) + #else _numerics_relaxed_addf16(a, b) + #endif } @_transparent public static func _relaxedMul(_ a: Float16, _ b: Float16) -> Float16 { + #if hasFeature(BuiltinRelaxedFP) // Swift 6.5 and later + Float16(Builtin.fmul_relaxed_FPIEEE16(a._value, b._value)) + #else _numerics_relaxed_mulf16(a, b) + #endif } #endif } diff --git a/Sources/RealModule/Float80+Real.swift b/Sources/RealModule/Float80+Real.swift index b4ca39b..fc63524 100644 --- a/Sources/RealModule/Float80+Real.swift +++ b/Sources/RealModule/Float80+Real.swift @@ -168,12 +168,20 @@ extension Float80: Real { @_transparent public static func _relaxedAdd(_ a: Float80, _ b: Float80) -> Float80 { +#if hasFeature(BuiltinRelaxedFP) // Swift 6.5 and later + Float80(Builtin.fadd_relaxed_FPIEEE80(a._value, b._value)) +#else _numerics_relaxed_addl(a, b) +#endif } @_transparent public static func _relaxedMul(_ a: Float80, _ b: Float80) -> Float80 { +#if hasFeature(BuiltinRelaxedFP) // Swift 6.5 and later + Float80(Builtin.fmul_relaxed_FPIEEE80(a._value, b._value)) +#else _numerics_relaxed_mull(a, b) +#endif } } #endif