Skip to content

Commit 4afd217

Browse files
committed
Use promotion instead of hardcoding types
1 parent a0ecaae commit 4afd217

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

base/reshapedarray.jl

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -136,43 +136,41 @@ reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Integer,Colon}}}) = resh
136136
" may have at most one omitted dimension specified by `Colon()`")))
137137
@noinline throw2(lenA, dims) = throw(DimensionMismatch(string("array size ", lenA,
138138
" must be divisible by the product of the new dimensions ", dims)))
139-
if Int == Int64
140-
const ReshapeIntTypes = Union{Int8, Int16, Int32, Int64}
141-
else
142-
const ReshapeIntTypes = Union{Int8, Int16, Int32}
143-
end
144-
@inline function _reshape_uncolon(A, _dims::Tuple{Vararg{Union{ReshapeIntTypes, Colon}}})
145-
dims = map(x -> x isa Colon ? x : Int(x), _dims)
146-
pre = _before_colon(dims...)::Tuple{Vararg{Int}}
139+
140+
@inline function _reshape_uncolon(A, _dims::Tuple{Vararg{Union{Integer, Colon}}})
141+
# promote the dims to `Int` at least
142+
dims = map(x -> x isa Colon ? x : promote_type(typeof(x), Int)(x), _dims)
143+
pre = _before_colon(dims...)
147144
post = _after_colon(dims...)
148145
_any_colon(post...) && throw1(dims)
149-
post::Tuple{Vararg{Int}}
150146
len = length(A)
151-
sz, is_exact = if iszero(len)
152-
(0, true)
147+
_reshape_uncolon_computesize(len, dims, pre, post)
148+
end
149+
@inline function _reshape_uncolon_computesize(len::Int, dims, pre::Tuple{Vararg{Int}}, post::Tuple{Vararg{Int}})
150+
sz = if iszero(len)
151+
0
153152
else
154153
let pr = Core.checked_dims(pre..., post...) # safe product
155-
if iszero(pr)
156-
throw2(len, dims)
157-
end
158-
(quo, rem) = divrem(len, pr)
159-
(Int(quo), iszero(rem))
154+
quo = _reshape_uncolon_computesize_nonempty(len, dims, pr)
155+
convert(Int, quo)
160156
end
161-
end::Tuple{Int,Bool}
162-
is_exact || throw2(len, dims)
163-
(pre..., sz, post...)::Tuple{Int,Vararg{Int}}
157+
end
158+
(pre..., sz, post...)
164159
end
165-
@inline function _reshape_uncolon(A, dims::Tuple{Vararg{Union{Integer, Colon}}})
166-
pre = _before_colon(dims...)
167-
post = _after_colon(dims...)
168-
_any_colon(post...) && throw1(dims)
169-
len = length(A)
160+
@inline function _reshape_uncolon_computesize(len, dims, pre, post)
170161
pr = prod((pre..., post...))
162+
sz = if iszero(len)
163+
promote(len, pr)[1] # zero of the correct type
164+
else
165+
_reshape_uncolon_computesize_nonempty(len, dims, pr)
166+
end
167+
(pre..., sz, post...)
168+
end
169+
@inline function _reshape_uncolon_computesize_nonempty(len, dims, pr)
171170
iszero(pr) && throw2(len, dims)
172171
(quo, rem) = divrem(len, pr)
173-
sz, is_exact = quo, iszero(rem)
174-
is_exact || throw2(len, dims)
175-
(pre..., sz, post...)
172+
iszero(rem) || throw2(len, dims)
173+
quo
176174
end
177175
@inline _any_colon() = false
178176
@inline _any_colon(dim::Colon, tail...) = true

0 commit comments

Comments
 (0)