Skip to content

Commit a80a119

Browse files
committed
saturated sub
1 parent a75ea4e commit a80a119

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/llvm_intrin/intrin_funcs.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

2-
@generated function saturated_add(x::I, y::I) where {I<:IntegerTypesHW}
2+
@generated function saturated(::F, x::I, y::I) where {I<:IntegerTypesHW,F}
33
typ = "i$(8sizeof(I))"
44
s = I <: Signed ? 's' : 'u'
5-
f = "@llvm.$(s)add.sat.$typ"
5+
op = F === typeof(+) ? "add" : "sub"
6+
f = "@llvm.$(s)$(op).sat.$typ"
67
decl = "declare $typ $f($typ, $typ)"
78
instrs = """
89
%res = call $typ $f($typ %0, $typ %1)
@@ -18,11 +19,12 @@
1819
[:x, :y]
1920
)
2021
end
21-
@generated function saturated_add(x::Vec{W,I}, y::Vec{W,I}) where {W,I}
22+
@generated function saturated(::F, x::Vec{W,I}, y::Vec{W,I}) where {W,I,F}
2223
typ = "i$(8sizeof(I))"
2324
vtyp = "<$W x $(typ)>"
2425
s = I <: Signed ? 's' : 'u'
25-
f = "@llvm.$(s)add.sat.$(suffix(W,typ))"
26+
op = F === typeof(+) ? "add" : "sub"
27+
f = "@llvm.$(s)$(op).sat.$(suffix(W,typ))"
2628
decl = "declare $vtyp $f($vtyp, $vtyp)"
2729
instrs = """
2830
%res = call $vtyp $f($vtyp %0, $vtyp %1)
@@ -38,6 +40,8 @@ end
3840
[:(data(x)), :(data(y))]
3941
)
4042
end
43+
@inline saturated_add(x, y) = saturated(+, x, y)
44+
@inline saturated_sub(x, y) = saturated(-, x, y)
4145

4246
@eval @inline function assume(b::Bool)
4347
$(llvmcall_expr(

0 commit comments

Comments
 (0)