Skip to content

Commit 36c179f

Browse files
committed
Make fastmath aware of Base.literal_pow.
1 parent c6e51d0 commit 36c179f

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

base/fastmath.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ function make_fastmath(expr::Expr)
113113
$arrvar[$(indvars...)] = $op($arrvar[$(indvars...)], $rhs)
114114
end
115115
end
116+
elseif is_literal_int_pow(expr)
117+
expr = Expr(expr.head, :(Base.literal_pow),
118+
expr.args[1], expr.args[2], Val(expr.args[3]))
116119
end
117120
Expr(make_fastmath(expr.head), map(make_fastmath, expr.args)...)
118121
end
@@ -150,6 +153,10 @@ macro fastmath(expr)
150153
make_fastmath(esc(expr))
151154
end
152155

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

154161
# Basic arithmetic
155162

test/fastmath.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,10 @@ let a = ones(2,2), b = ones(2,2)
202202
local c = 0
203203
@test @fastmath(c |= 1) == 1
204204
end
205+
206+
struct LitPowTest end
207+
Base.literal_pow(::typeof(Base.FastMath.pow_fast), ::LitPowTest, ::Val{p}) where {p} = 1
208+
Base.literal_pow(::typeof(^), ::LitPowTest, ::Val{p}) where {p} = 2
209+
LPT = LitPowTest()
210+
@test (@fastmath LPT^2) == 1
211+
@test LPT^2 == 2

0 commit comments

Comments
 (0)