Skip to content

Commit 0d657aa

Browse files
authored
specialize muladd (#949)
* specialize muladd * bump version
1 parent e30448b commit 0d657aa

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.2.10"
3+
version = "1.2.11"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/linalg.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ end
4545
@inline \(a::UniformScaling, b::Union{StaticMatrix,StaticVector}) = a.λ \ b
4646
@inline /(a::StaticMatrix, b::UniformScaling) = a / b.λ
4747

48+
49+
# Ternary ops
50+
@inline Base.muladd(scalar::Number, a::StaticArray, b::StaticArray) = map((ai, bi) -> muladd(scalar, ai, bi), a, b)
51+
@inline Base.muladd(a::StaticArray, scalar::Number, b::StaticArray) = map((ai, bi) -> muladd(ai, scalar, bi), a, b)
52+
4853
#--------------------------------------------------
4954
# Matrix algebra
5055

test/linalg.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,
5151
end
5252
end
5353

54+
@testset "Ternary operators" begin
55+
for T in (Int, Float32, Float64)
56+
c = convert(T, 2)
57+
v1 = @SVector T[2, 4, 6, 8]
58+
v2 = @SVector T[4, 3, 2, 1]
59+
m1 = @SMatrix T[2 4; 6 8]
60+
m2 = @SMatrix T[4 3; 2 1]
61+
62+
# Use that these small integers can be represetnted exactly
63+
# as floating point numbers. In general, the comparison of
64+
# floats should use `≈` instead of `===`.
65+
@test @inferred(muladd(c, v1, v2)) === @SVector T[8, 11, 14, 17]
66+
@test @inferred(muladd(v1, c, v2)) === @SVector T[8, 11, 14, 17]
67+
@test @inferred(muladd(c, m1, m2)) === @SMatrix T[8 11; 14 17]
68+
@test @inferred(muladd(m1, c, m2)) === @SMatrix T[8 11; 14 17]
69+
end
70+
end
71+
5472
@testset "Interaction with `UniformScaling`" begin
5573
@test @inferred(@SMatrix([0 1; 2 3]) + I) === @SMatrix [1 1; 2 4]
5674
@test @inferred(I + @SMatrix([0 1; 2 3])) === @SMatrix [1 1; 2 4]

0 commit comments

Comments
 (0)