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

Make @fastmath aware of Base.literal_pow #21099

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ function make_fastmath(expr::Expr)
$arrvar[$(indvars...)] = $op($arrvar[$(indvars...)], $rhs)
end
end
elseif is_literal_int_pow(expr)
expr = Expr(expr.head, :(Base.literal_pow),
expr.args[1], expr.args[2], Val(expr.args[3]))
end
Base.exprarray(make_fastmath(expr.head), Base.mapany(make_fastmath, expr.args))
end
Expand Down Expand Up @@ -155,6 +158,10 @@ macro fastmath(expr)
make_fastmath(esc(expr))
end

function is_literal_int_pow(ex::Expr)
return (ex.head === :call && length(ex.args) == 3 &&
ex.args[1] === :^ && isa(ex.args[3], Integer))
end

# Basic arithmetic

Expand Down
7 changes: 7 additions & 0 deletions test/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,10 @@ end
@test (@fastmath "a" * "b") == "ab"
@test (@fastmath "a" ^ 2) == "aa"
end

struct LitPowTest end
Base.literal_pow(::typeof(Base.FastMath.pow_fast), ::LitPowTest, ::Val{p}) where {p} = 1
stevengj marked this conversation as resolved.
Show resolved Hide resolved
Base.literal_pow(::typeof(^), ::LitPowTest, ::Val{p}) where {p} = 2
LPT = LitPowTest()
@test (@fastmath LPT^2) == 1
@test LPT^2 == 2