# How to force compile-time evaluation of literal calculations?

**URL:** https://discourse.julialang.org/t/how-to-force-compile-time-evaluation-of-literal-calculations/56822
**Category:** Performance
**Created:** [March 9, 2021, 4:37pm UTC](https://discourse.julialang.org/t/how-to-force-compile-time-evaluation-of-literal-calculations/56822 "2021-03-09T16:37:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)
#### Post date: [March 9, 2021, 4:37pm UTC](https://discourse.julialang.org/t/how-to-force-compile-time-evaluation-of-literal-calculations/56822/1 "2021-03-09T16:37:36Z")

</div>

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
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
}

```

---

<div class="post-metadata">

### Author: ![eliassno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliassno/32/18917_2.png) [@eliassno](https://discourse.julialang.org/u/eliassno)
#### Post date: [March 9, 2021, 4:50pm UTC](https://discourse.julialang.org/t/how-to-force-compile-time-evaluation-of-literal-calculations/56822/2 "2021-03-09T16:50:54Z")

</div>

Not sure what kinds of calculations you’re dealing with, but would it not suffice to set the literal as a constant?

```julia
const A = 10^6
function f(x)
    x + A
end
@code_llvm f(1)

```

---

<div class="post-metadata">

### Author: ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)
#### Post date: [March 9, 2021, 5:01pm UTC](https://discourse.julialang.org/t/how-to-force-compile-time-evaluation-of-literal-calculations/56822/3 "2021-03-09T17:01:04Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)
#### Post date: [March 9, 2021, 5:10pm UTC](https://discourse.julialang.org/t/how-to-force-compile-time-evaluation-of-literal-calculations/56822/4 "2021-03-09T17:10:41Z")

</div>

This seems to work (tested on Julia 1.6):

```julia
julia> f(x) = x + Int(1e6)
f (generic function with 1 method)

julia> @code_llvm f(1)
; @ REPL[18]:1 within `f'
define i64 @julia_f_277(i64 signext %0) {
top:
; ┌ @ int.jl:87 within `+'
   %1 = add i64 %0, 1000000
; └
  ret i64 %1
}

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 9, 2021, 5:18pm UTC](https://discourse.julialang.org/t/how-to-force-compile-time-evaluation-of-literal-calculations/56822/5 "2021-03-09T17:18:16Z")

</div>

I am not sure if I can suggest this, but this most simple macro ever seems to work:

```julia
julia> macro CT(expr)
         eval(expr)
       end
@CT (macro with 1 method)

julia> f(x) = x + @CT(10^6)
f (generic function with 1 method)

julia> @code_llvm f(1)

; @ REPL[32]:1 within `f'
define i64 @julia_f_902(i64) {
top:
; ┌ @ int.jl:86 within `+'
   %1 = add i64 %0, 1000000
; └
  ret i64 %1
}

```

In principle it can be use to make any function call with constant arguments constant, like

```julia
julia> f(x) = x + @CT(sin(π/4) + sqrt(10) + 10^6)

```
