From a80a1194d9740efb9c08e103c8d848061f2dc017 Mon Sep 17 00:00:00 2001 From: chriselrod Date: Fri, 17 Nov 2023 12:11:19 -0500 Subject: [PATCH] saturated sub --- src/llvm_intrin/intrin_funcs.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/llvm_intrin/intrin_funcs.jl b/src/llvm_intrin/intrin_funcs.jl index 04410133..8307c2b3 100644 --- a/src/llvm_intrin/intrin_funcs.jl +++ b/src/llvm_intrin/intrin_funcs.jl @@ -1,8 +1,9 @@ -@generated function saturated_add(x::I, y::I) where {I<:IntegerTypesHW} +@generated function saturated(::F, x::I, y::I) where {I<:IntegerTypesHW,F} typ = "i$(8sizeof(I))" s = I <: Signed ? 's' : 'u' - f = "@llvm.$(s)add.sat.$typ" + op = F === typeof(+) ? "add" : "sub" + f = "@llvm.$(s)$(op).sat.$typ" decl = "declare $typ $f($typ, $typ)" instrs = """ %res = call $typ $f($typ %0, $typ %1) @@ -18,11 +19,12 @@ [:x, :y] ) end -@generated function saturated_add(x::Vec{W,I}, y::Vec{W,I}) where {W,I} +@generated function saturated(::F, x::Vec{W,I}, y::Vec{W,I}) where {W,I,F} typ = "i$(8sizeof(I))" vtyp = "<$W x $(typ)>" s = I <: Signed ? 's' : 'u' - f = "@llvm.$(s)add.sat.$(suffix(W,typ))" + op = F === typeof(+) ? "add" : "sub" + f = "@llvm.$(s)$(op).sat.$(suffix(W,typ))" decl = "declare $vtyp $f($vtyp, $vtyp)" instrs = """ %res = call $vtyp $f($vtyp %0, $vtyp %1) @@ -38,6 +40,8 @@ end [:(data(x)), :(data(y))] ) end +@inline saturated_add(x, y) = saturated(+, x, y) +@inline saturated_sub(x, y) = saturated(-, x, y) @eval @inline function assume(b::Bool) $(llvmcall_expr(