You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2025-11-30-comptime-c-functions.md
+8-12Lines changed: 8 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,17 +20,7 @@ macro_version:
20
20
21
21
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.
22
22
23
-
# Optimization tricks
24
-
25
-
-`static inline` allows inlining across compilation boundaries.
26
-
-`__attribute__((always_inline))`*strongly* urges compilers to inline functions.
27
-
-`__builtin_unreachable()` is used to teach the optimizer which assumptions it can make about input arguments.
28
-
- Passing `-O3` to the compiler tells it to optimize the code very hard.
29
-
- Passing `-march=native` to the compiler tells it to make optimizations based on your specific CPU.
30
-
- Constant buffer addresses + sizes let the optimizer trace through `memcpy()` calls.
31
-
- All operations become statically analyzable, reducing to constants.
32
-
-`assert()` calls get eliminated when conditions are provably true.
33
-
-[Link-time optimization](https://en.wikipedia.org/wiki/Interprocedural_optimization) with `-flto` should allow Clang and GCC to perform these optimizations even when the code is split across several object files.
23
+
[Link-time optimization](https://en.wikipedia.org/wiki/Interprocedural_optimization) with `-flto` should allow Clang and GCC to perform these optimizations even when the code is split across several object files.
34
24
35
25
# Generic stack
36
26
@@ -40,6 +30,10 @@ I added a `main()` function to the program to prove that it doesn't crash on any
40
30
41
31
It's important to note that `fn_version()` and `macro_version()` get optimized just as hard, even when you remove the `main()`.
42
32
33
+
Here are the used optimization flags:
34
+
- Passing `-O3` to the compiler tells it to optimize the code very hard.
35
+
- Passing `-march=native` to the compiler tells it to make optimizations based on your specific CPU.
36
+
43
37
Copy of the code on [Compiler Explorer](https://godbolt.org/z/fdf5acdcn):
44
38
45
39
```c
@@ -65,7 +59,7 @@ typedef struct stack {
65
59
size_t element_size;
66
60
} stack;
67
61
68
-
__attribute__((always_inline))
62
+
__attribute__((always_inline)) // Strongly urges compilers to inline functions
0 commit comments