Skip to content
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
Comment thread
stevengj marked this conversation as resolved.
Base.literal_pow(::typeof(^), ::LitPowTest, ::Val{p}) where {p} = 2
LPT = LitPowTest()
@test (@fastmath LPT^2) == 1
@test LPT^2 == 2