Maybe that’s true when the exponent is a literal, or a small (< 4) integer, but if you’re using a variable that is larger than 3, the exponent is converted to a float anyway
I’m on my phone, and can’t check. It’s weird that accuracy crashed and burned exactly for ^7, and was suddenly off by 8 orders of magnitude, but was ok for ^6, so I assumed overflow.
I believe we’re getting lost in translation. I was talking about difference between a ^ b when b is an integer or a float (and b is not an integer literal). @lmiq asked about LoopVectorization when I was talking about the Base function ^. Now you’re comparing pow(x, 7) (which is different from x ^ 7) for vectors, between Base.map and LoopVectorization.vmap, which doesn’t have anything to do with what I was talking about above?
How so? I pointed out that ρ0 ^ γ overflows when ρ0 = 1000 and γ = 7, and there is no performance benefit in using integer values in this case (at least in Base)
I said so because everyone else was talking about LoopVectorization, and you came in saying that this was was not relevant, and insisted on talking about the Base versions.
If you do this in a loop, LoopVectorization will automatically do this for you:
using LoopVectorization, BenchmarkTools
function pow7turbo!(y, x)
@turbo for i in eachindex(y,x)
y[i] = x[i]^7
end
end
pow7(x) = begin x3 = x^3; x * x3^2 end;
@btime pow7turbo!(y, x) setup=(x=rand(1000); y=similar(x));
@btime y .= pow7.(x) setup=(x=rand(1000); y=similar(x));
I guess I could apply the same transform earlier, at a time that it’d also apply to broadcast statements.
This happens at macro-expand time given literals, otherwise LoopVectorization currently can’t read the constant values.
and if possible staying in log space for the whole calculation. This occasionally improves speed to, but I did not benchmark for this particular problem.