Skip to content

Commit 77d051f

Browse files
authored
Merge pull request #429 from tkoolen/tk/fix-zeros-ones
Fix #428, zeros and ones shouldn't use _fill
2 parents 8a2ea49 + e30b27b commit 77d051f

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/arraymath.jl

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
@inline zeros(::SA) where {SA <: StaticArray} = zeros(SA)
2-
@generated function zeros(::Type{SA}) where {SA <: StaticArray}
2+
@inline zeros(::Type{SA}) where {SA <: StaticArray} = _zeros(Size(SA), SA)
3+
@generated function _zeros(::Size{s}, ::Type{SA}) where {s, SA <: StaticArray}
34
T = eltype(SA)
4-
if T === Any
5-
return quote
6-
@_inline_meta
7-
_fill(zero(Float64), Size(SA), SA)
8-
end
9-
else
10-
return quote
11-
@_inline_meta
12-
_fill(zero($T), Size(SA), SA)
13-
end
5+
if T == Any
6+
T = Float64
7+
end
8+
v = [:(zero($T)) for i = 1:prod(s)]
9+
return quote
10+
@_inline_meta
11+
$SA(tuple($(v...)))
1412
end
1513
end
1614

1715
@inline ones(::SA) where {SA <: StaticArray} = ones(SA)
18-
@generated function ones(::Type{SA}) where {SA <: StaticArray}
16+
@inline ones(::Type{SA}) where {SA <: StaticArray} = _ones(Size(SA), SA)
17+
@generated function _ones(::Size{s}, ::Type{SA}) where {s, SA <: StaticArray}
1918
T = eltype(SA)
20-
if T === Any
21-
return quote
22-
@_inline_meta
23-
_fill(one(Float64), Size(SA), SA)
24-
end
25-
else
26-
return quote
27-
@_inline_meta
28-
_fill(one($T), Size(SA), SA)
29-
end
19+
if T == Any
20+
T = Float64
21+
end
22+
v = [:(one($T)) for i = 1:prod(s)]
23+
return quote
24+
@_inline_meta
25+
$SA(tuple($(v...)))
3026
end
3127
end
3228

test/arraymath.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ import StaticArrays.arithmetic_closure
1919
@test @inferred(ones(MVector{3}))::MVector == @MVector [1.0, 1.0, 1.0]
2020
@test @inferred(ones(MMatrix{2,2}))::MMatrix == @MMatrix [1.0 1.0; 1.0 1.0]
2121
@test @inferred(ones(MArray{Tuple{1,1,1}}))::MArray == MArray{Tuple{1,1,1}}((1.0,))
22+
23+
# https://github.com/JuliaArrays/StaticArrays.jl/issues/428
24+
bigzeros = zeros(SVector{2, BigInt})
25+
@test bigzeros == @SVector [big(0), big(0)]
26+
@test bigzeros[1] !== bigzeros[2]
27+
28+
bigones = ones(SVector{2, BigInt})
29+
@test bigones == @SVector [big(1), big(1)]
30+
@test bigones[1] !== bigones[2]
2231
end
2332

2433
@testset "zero()" begin

0 commit comments

Comments
 (0)