I often want to write large integer values such as 10^6 in performance critical functions. I would expect Julia to perform these calculations at compile-time when possible, but it doesn’t appear to do so:
julia> function f(x)
x + 10^6
end
f (generic function with 1 method)
julia> @code_llvm f(1)
; @ REPL[24]:1 within `f'
define i64 @julia_f_346(i64 signext %0) {
top:
; @ REPL[24]:2 within `f'
; ┌ @ none within `literal_pow'
; │┌ @ none within `macro expansion'
; ││┌ @ intfuncs.jl:289 within `^'
%1 = call i64 @j_power_by_squaring_348(i64 signext 10, i64 signext 6)
; └└└
; ┌ @ int.jl:87 within `+'
%2 = add i64 %1, %0
; └
ret i64 %2
}
Yes, defining a const would work, but makes my code more confusing than typing out 1_000_000 or 1_000_000_000 wherever I need it.
I guess I was hoping for something a bit cleaner like CT(10^6). Or even ideally just force evaluate all literals at compile-time. I don’t want to need to remember that 10^4 is optimized to 10000, but 10^6 is not.