Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alternative fix for mul! #34394

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@ julia> MulAddMul(12, 34)(56, 78) == 56 * 12 + 78 * 34
true
```
"""
struct MulAddMul{ais1, bis0, TA, TB}
struct MulAddMul{TA, TB}
alpha::TA
beta::TB
ais1::Bool
bis0::Bool
end

MulAddMul(alpha::TA, beta::TB) where {TA, TB} =
MulAddMul{isone(alpha), iszero(beta), TA, TB}(alpha, beta)
MulAddMul{TA, TB}(alpha, beta, isone(alpha), iszero(beta))

MulAddMul() = MulAddMul(true, false)

@inline (::MulAddMul{true})(x) = x
@inline (p::MulAddMul{false})(x) = x * p.alpha
@inline (::MulAddMul{true, true})(x, _) = x
@inline (p::MulAddMul{false, true})(x, _) = x * p.alpha
@inline (p::MulAddMul{true, false})(x, y) = x + y * p.beta
@inline (p::MulAddMul{false, false})(x, y) = x * p.alpha + y * p.beta
@inline (p::MulAddMul)(x) = p.ais1 ? x : x * p.alpha

@inline function (p::MulAddMul)(x, y)
x_mul_a = p.ais1 ? x : x * p.alpha
if p.bis0
return x_mul_a
else
return x_mul_a + y * p.beta
end
end

"""
_modify!(_add::MulAddMul, x, C, idx)
Expand All @@ -59,14 +65,13 @@ julia> C
123.0
```
"""
@inline @propagate_inbounds function _modify!(p::MulAddMul{ais1, bis0},
x, C, idx′) where {ais1, bis0}
@inline @propagate_inbounds function _modify!(p::MulAddMul, x, C, idx′)
# `idx′` may be an integer, a tuple of integer, or a `CartesianIndex`.
# Let `CartesianIndex` constructor normalize them so that it can be
# used uniformly. It also acts as a workaround for performance penalty
# of splatting a number (#29114):
idx = CartesianIndex(idx′)
if bis0
if p.bis0
C[idx] = p(x)
else
C[idx] = p(x, C[idx])
Expand Down