@@ -12,15 +12,18 @@ const SmallSigned = Union{Int8,Int16,Int32}
12
12
const SmallUnsigned = Union{UInt8,UInt16,UInt32}
13
13
end
14
14
15
- const CommonReduceResult = Union{UInt64,UInt128,Int64,Int128,Float32,Float64}
16
- const WidenReduceResult = Union{SmallSigned, SmallUnsigned, Float16 }
15
+ const CommonReduceResult = Union{UInt64,UInt128,Int64,Int128,Float16, Float32,Float64}
16
+ const WidenReduceResult = Union{SmallSigned, SmallUnsigned}
17
17
18
+ promote_sys_size {T} (:: Type{T} ) = T
19
+ promote_sys_size {T<:SmallSigned} (:: Type{T} ) = Int
20
+ promote_sys_size {T<:SmallUnsigned} (:: Type{T} ) = UInt
18
21
# r_promote_type: promote T to the type of reduce(op, ::Array{T})
19
22
# (some "extra" methods are required here to avoid ambiguity warnings)
20
23
r_promote_type (op, :: Type{T} ) where {T} = T
21
- r_promote_type (op, :: Type{T} ) where {T<: WidenReduceResult } = widen (T)
22
- r_promote_type (:: typeof (+ ), :: Type{T} ) where {T<: WidenReduceResult } = widen (T)
23
- r_promote_type (:: typeof (* ), :: Type{T} ) where {T<: WidenReduceResult } = widen (T)
24
+ r_promote_type (op, :: Type{T} ) where {T<: WidenReduceResult } = promote_sys_size (T)
25
+ r_promote_type (:: typeof (+ ), :: Type{T} ) where {T<: WidenReduceResult } = promote_sys_size (T)
26
+ r_promote_type (:: typeof (* ), :: Type{T} ) where {T<: WidenReduceResult } = promote_sys_size (T)
24
27
r_promote_type (:: typeof (+ ), :: Type{T} ) where {T<: Number } = typeof (zero (T)+ zero (T))
25
28
r_promote_type (:: typeof (* ), :: Type{T} ) where {T<: Number } = typeof (one (T)* one (T))
26
29
r_promote_type (:: typeof (scalarmax), :: Type{T} ) where {T<: WidenReduceResult } = T
@@ -292,21 +295,24 @@ mapreduce(f, op, a::Number) = f(a)
292
295
"""
293
296
reduce(op, v0, itr)
294
297
295
- Reduce the given collection `ìtr ` with the given binary operator `op`. `v0` must be a
298
+ Reduce the given collection `itr ` with the given binary operator `op`. `v0` must be a
296
299
neutral element for `op` that will be returned for empty collections. It is unspecified
297
300
whether `v0` is used for non-empty collections.
298
301
299
- Reductions for certain commonly-used operators have special implementations which should be
300
- used instead: `maximum(itr)`, `minimum(itr)`, `sum(itr)`, `prod(itr)`, `any(itr)`,
301
- `all(itr)`.
302
+ The return type is `Int` (`UInt`) for (un)signed integers of less than system word size.
303
+ For all other arguments, a common return type is found to which all arguments are promoted.
304
+
305
+ Reductions for certain commonly-used operators may have special implementations, and
306
+ should be used instead: `maximum(itr)`, `minimum(itr)`, `sum(itr)`, `prod(itr)`,
307
+ `any(itr)`, `all(itr)`.
302
308
303
309
The associativity of the reduction is implementation dependent. This means that you can't
304
310
use non-associative operations like `-` because it is undefined whether `reduce(-,[1,2,3])`
305
311
should be evaluated as `(1-2)-3` or `1-(2-3)`. Use [`foldl`](@ref) or
306
312
[`foldr`](@ref) instead for guaranteed left or right associativity.
307
313
308
- Some operations accumulate error, and parallelism will also be easier if the reduction can
309
- be executed in groups. Future versions of Julia might change the algorithm. Note that the
314
+ Some operations accumulate error. Parallelism will be easier if the reduction can be
315
+ executed in groups. Future versions of Julia might change the algorithm. Note that the
310
316
elements are not reordered if you use an ordered collection.
311
317
312
318
# Examples
0 commit comments