Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ end
@generated function calc_fma_xyz(x::Dual{T,<:Any,N},
y::Dual{T,<:Any,N},
z::Dual{T,<:Any,N}) where {T,N}
ex = Expr(:tuple, [:(fma(value(x), partials(y)[$i], fma(value(y), partials(x)[$i], partials(z)[$i]))) for i in 1:N]...)
ex = Expr(:tuple, [:(_fma_partial(value(x), partials(y, $i), _fma_partial(value(y), partials(x, $i), partials(z, $i)))) for i in 1:N]...)
return quote
$(Expr(:meta, :inline))
v = fma(value(x), value(y), value(z))
Expand All @@ -642,7 +642,7 @@ end
@generated function calc_fma_xz(x::Dual{T,<:Any,N},
y::Real,
z::Dual{T,<:Any,N}) where {T,N}
ex = Expr(:tuple, [:(fma(partials(x)[$i], y, partials(z)[$i])) for i in 1:N]...)
ex = Expr(:tuple, [:(_fma_partial(y, partials(x, $i), partials(z, $i))) for i in 1:N]...)
return quote
$(Expr(:meta, :inline))
v = fma(value(x), y, value(z))
Expand All @@ -667,7 +667,7 @@ end
@generated function calc_muladd_xyz(x::Dual{T,<:Any,N},
y::Dual{T,<:Any,N},
z::Dual{T,<:Any,N}) where {T,N}
ex = Expr(:tuple, [:(muladd(value(x), partials(y)[$i], muladd(value(y), partials(x)[$i], partials(z)[$i]))) for i in 1:N]...)
ex = Expr(:tuple, [:(_muladd_partial(value(x), partials(y, $i), _muladd_partial(value(y), partials(x, $i), partials(z, $i)))) for i in 1:N]...)
return quote
$(Expr(:meta, :inline))
v = muladd(value(x), value(y), value(z))
Expand All @@ -684,7 +684,7 @@ end
@generated function calc_muladd_xz(x::Dual{T,<:Any,N},
y::Real,
z::Dual{T,<:Any,N}) where {T,N}
ex = Expr(:tuple, [:(muladd(partials(x)[$i], y, partials(z)[$i])) for i in 1:N]...)
ex = Expr(:tuple, [:(_muladd_partial(y, partials(x, $i), partials(z, $i))) for i in 1:N]...)
return quote
$(Expr(:meta, :inline))
v = muladd(value(x), y, value(z))
Expand Down
10 changes: 10 additions & 0 deletions src/partials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,19 @@ if NANSAFE_MODE_ENABLED
y = partial / x
return iszero(partial) ? zero(y) : y
end
@inline function _fma_partial(x::Real, partial::Real, acc::Real)
y = fma(x, partial, acc)
return iszero(partial) ? oftype(y, acc) : y
end
@inline function _muladd_partial(x::Real, partial::Real, acc::Real)
y = muladd(x, partial, acc)
return iszero(partial) ? oftype(y, acc) : y
end
else
@inline _mul_partial(partial::Real, x::Real) = partial * x
@inline _div_partial(partial::Real, x::Real) = partial / x
@inline _fma_partial(x::Real, partial::Real, acc::Real) = fma(x, partial, acc)
@inline _muladd_partial(x::Real, partial::Real, acc::Real) = muladd(x, partial, acc)
end

# edge cases where N == 0 #
Expand Down
14 changes: 14 additions & 0 deletions test/DualTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,4 +761,18 @@ end
end
end

@testset "fma and muladd: consistency with `x * y + z`" begin # issue #860
for f in (fma, muladd)
for (x, y, z) in (
(Dual{TestTag}(Inf, 0.0), Dual{TestTag}(2.0, 0.0), Dual{TestTag}(0.0, 1.0)),
(Dual{TestTag}(1.0, 0.0), Inf, Dual{TestTag}(0.0, 1.0)),
(Inf, Dual{TestTag}(1.0, 0.0), Dual{TestTag}(0.0, 1.0)),
)
d = f(x, y, z)
@test value(d) === Inf
@test isequal(d, x * y + z)
end
end
end

end # module
Loading