@@ -239,12 +239,6 @@ broadcast_indices
239
239
# special cases defined for performance
240
240
broadcast (f, x:: Number... ) = f (x... )
241
241
@inline broadcast (f, t:: NTuple{N,Any} , ts:: Vararg{NTuple{N,Any}} ) where {N} = map (f, t, ts... )
242
- @inline broadcast! (:: typeof (identity), x:: AbstractArray{T,N} , y:: AbstractArray{S,N} ) where {T,S,N} =
243
- Base. axes (x) == Base. axes (y) ? copyto! (x, y) : _broadcast! (identity, x, y)
244
-
245
- # special cases for "X .= ..." (broadcast!) assignments
246
- broadcast! (:: typeof (identity), X:: AbstractArray , x:: Number ) = fill! (X, x)
247
- broadcast! (f, X:: AbstractArray , x:: Number... ) = (@inbounds for I in eachindex (X); X[I] = f (x... ); end ; X)
248
242
249
243
# # logic for deciding the BroadcastStyle
250
244
# Dimensionality: computing max(M,N) in the type domain so we preserve inferrability
@@ -261,7 +255,7 @@ longest(::Tuple{}, ::Tuple{}) = ()
261
255
# combine_styles operates on values (arbitrarily many)
262
256
combine_styles (c) = result_style (BroadcastStyle (typeof (c)))
263
257
combine_styles (c1, c2) = result_style (combine_styles (c1), combine_styles (c2))
264
- combine_styles (c1, c2, cs... ) = result_style (combine_styles (c1), combine_styles (c2, cs... ))
258
+ @inline combine_styles (c1, c2, cs... ) = result_style (combine_styles (c1), combine_styles (c2, cs... ))
265
259
266
260
# result_style works on types (singletons and pairs), and leverages `BroadcastStyle`
267
261
result_style (s:: BroadcastStyle ) = s
@@ -397,6 +391,7 @@ Base.@propagate_inbounds _broadcast_getindex(::Style{Tuple}, A::Tuple{Any}, I) =
397
391
result = @ncall $ nargs f val
398
392
@inbounds B[I] = result
399
393
end
394
+ return B
400
395
end
401
396
end
402
397
433
428
@inbounds C[ind: bitcache_size] = false
434
429
dumpbitcache (Bc, cind, C)
435
430
end
431
+ return B
436
432
end
437
433
end
438
434
@@ -445,11 +441,38 @@ Note that `dest` is only used to store the result, and does not supply
445
441
arguments to `f` unless it is also listed in the `As`,
446
442
as in `broadcast!(f, A, A, B)` to perform `A[:] = broadcast(f, A, B)`.
447
443
"""
448
- @inline broadcast! (f, C:: AbstractArray , A, Bs:: Vararg{Any,N} ) where {N} =
449
- _broadcast! (f, C, A, Bs... )
444
+ @inline broadcast! (f:: Tf , dest, As:: Vararg{Any,N} ) where {Tf,N} = broadcast! (f, dest, combine_styles (As... ), As... )
445
+ @inline broadcast! (f:: Tf , dest, :: BroadcastStyle , As:: Vararg{Any,N} ) where {Tf,N} = broadcast! (f, dest, nothing , As... )
446
+
447
+ # Default behavior (separated out so that it can be called by users who want to extend broadcast!).
448
+ @inline function broadcast! (f, dest, :: Nothing , As:: Vararg{Any, N} ) where N
449
+ if f isa typeof (identity) && N == 1
450
+ A = As[1 ]
451
+ if A isa AbstractArray && Base. axes (dest) == Base. axes (A)
452
+ return copyto! (dest, A)
453
+ end
454
+ end
455
+ _broadcast! (f, dest, As... )
456
+ return dest
457
+ end
458
+
459
+ # Optimization for the all-Scalar case.
460
+ @inline function broadcast! (f, dest, :: Scalar , As:: Vararg{Any, N} ) where N
461
+ if dest isa AbstractArray
462
+ if f isa typeof (identity) && N == 1
463
+ return fill! (dest, As[1 ])
464
+ else
465
+ @inbounds for I in eachindex (dest)
466
+ dest[I] = f (As... )
467
+ end
468
+ return dest
469
+ end
470
+ end
471
+ _broadcast! (f, dest, As... )
472
+ return dest
473
+ end
450
474
451
- # This indirection allows size-dependent implementations (e.g., see the copying `identity`
452
- # specialization above)
475
+ # This indirection allows size-dependent implementations.
453
476
@inline function _broadcast! (f, C, A, Bs:: Vararg{Any,N} ) where N
454
477
shape = broadcast_indices (C)
455
478
@boundscheck check_broadcast_indices (shape, A, Bs... )
@@ -630,7 +653,7 @@ function broadcast_nonleaf(f, s::NonleafHandlingTypes, ::Type{ElType}, shape::In
630
653
dest = Base. similar (Array{typeof (val)}, shape)
631
654
end
632
655
dest[I] = val
633
- return _broadcast! (f, dest, keeps, Idefaults, As, Val (nargs), iter, st, 1 )
656
+ _broadcast! (f, dest, keeps, Idefaults, As, Val (nargs), iter, st, 1 )
634
657
end
635
658
636
659
broadcast (f, :: Union{Scalar,Unknown} , :: Nothing , :: Nothing , a... ) = f (a... )
0 commit comments