Skip to content

Commit 12086eb

Browse files
authored
Update 2025-11-30-comptime-c-functions.md
1 parent e9b0534 commit 12086eb

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

_posts/2025-11-30-comptime-c-functions.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Compile-time function execution is great, as it means your program has to run le
1212
The below data structure showcase programs get optimized away at compile time by Clang and GCC, using [constant folding](https://en.wikipedia.org/wiki/Constant_folding), [inlining](https://en.wikipedia.org/wiki/Inline_expansion), and [dead code elimination](https://en.wikipedia.org/wiki/Dead-code_elimination):
1313
```nasm
1414
fn_version:
15-
ret
15+
ret
1616
1717
macro_version:
18-
ret
18+
ret
1919
```
2020

2121
The best use-case I can think of for this technique is generating lookup tables at compile-time, as math functions like `sin()` *also* get optimized away.
@@ -204,18 +204,18 @@ GCC doesn't on the other hand, even when passed these extra flags:
204204
It still keeps the `calloc()` and `free()` around:
205205
```nasm
206206
"main":
207-
sub rsp, 8
208-
mov esi, 13
209-
mov edi, 2
210-
call "calloc"
211-
mov QWORD PTR [rax+5], OFFSET FLAT:.LC0
212-
mov rdi, rax
213-
call "free"
214-
mov edi, OFFSET FLAT:.LC1
215-
call "puts"
216-
xor eax, eax
217-
add rsp, 8
218-
ret
207+
sub rsp, 8
208+
mov esi, 13
209+
mov edi, 2
210+
call "calloc"
211+
mov QWORD PTR [rax+5], OFFSET FLAT:.LC0
212+
mov rdi, rax
213+
call "free"
214+
mov edi, OFFSET FLAT:.LC1
215+
call "puts"
216+
xor eax, eax
217+
add rsp, 8
218+
ret
219219
```
220220

221221
But GCC manages to optimize them away when the `printf("All tests passed.\n");` at the end of `main()` is removed, for some unknown reason.

0 commit comments

Comments
 (0)