From 7739d917f5f6209ebd18b5f248662c0d5fc59bb6 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Mon, 11 Nov 2024 13:16:37 +0100 Subject: [PATCH] fix IEEE compliancy in floating point min/max reductions on LLVM version that supports it (#130) Co-authored-by: KristofferC --- src/LLVM_intrinsics.jl | 6 ++++-- src/simdvec.jl | 4 ++-- test/runtests.jl | 9 +++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/LLVM_intrinsics.jl b/src/LLVM_intrinsics.jl index f7eebab..26466b9 100644 --- a/src/LLVM_intrinsics.jl +++ b/src/LLVM_intrinsics.jl @@ -806,9 +806,11 @@ end # Horizontal reductions (LLVM 9) # ################################## +const SUPPORTS_FMAXIMUM_FMINIMUM = Base.libllvm_version >= v"18" + const HORZ_REDUCTION_OPS_FLOAT = [ - :fmax - :fmin + SUPPORTS_FMAXIMUM_FMINIMUM ? :fmaximum : :fmax + SUPPORTS_FMAXIMUM_FMINIMUM ? :fminimum : :fmin ] const HORZ_REDUCTION_OPS_INT = [ diff --git a/src/simdvec.jl b/src/simdvec.jl index fe44af0..cb2c8cd 100644 --- a/src/simdvec.jl +++ b/src/simdvec.jl @@ -458,10 +458,10 @@ const HORZ_REDUCTION_OPS = [ (| , Union{IntegerTypes, Bool} , Intrinsics.reduce_or) (max , IntTypes , Intrinsics.reduce_smax) (max , UIntTypes , Intrinsics.reduce_umax) - (max , FloatingTypes , Intrinsics.reduce_fmax) + (max , FloatingTypes , Intrinsics.SUPPORTS_FMAXIMUM_FMINIMUM ? Intrinsics.reduce_fmaximum : Intrinsics.reduce_fmax) (min , IntTypes , Intrinsics.reduce_smin) (min , UIntTypes , Intrinsics.reduce_umin) - (min , FloatingTypes , Intrinsics.reduce_fmin) + (min , FloatingTypes , Intrinsics.SUPPORTS_FMAXIMUM_FMINIMUM ? Intrinsics.reduce_fminimum : Intrinsics.reduce_fmin) (+ , Union{IntegerTypes, Bool} , Intrinsics.reduce_add) (* , IntegerTypes , Intrinsics.reduce_mul) (+ , FloatingTypes , Intrinsics.reduce_fadd) diff --git a/test/runtests.jl b/test/runtests.jl index dbf9880..6ba4c3c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -354,6 +354,15 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) @test sum(Vec{3,Float64}(1)) === 3.0 @test prod(Vec{5,Float64}(2)) === 32.0 + + # Floating point reduce IEEEE # https://github.com/eschnett/SIMD.jl/issues/129 + if SIMD.Intrinsics.SUPPORTS_FMAXIMUM_FMINIMUM + t = (1.0, 2.0, NaN, 0.5) + tv = SIMD.Vec(t) + + @test isnan(reduce(min, t)) + @test isnan(reduce(min, tv)) + end end @testset "Load and store functions" begin