@@ -600,90 +600,10 @@ The magic:
600600
601601**Write once, works everywhere™**
602602
603-
604- ---
605-
606- # Runtime dispatching
607-
608- - One function semantically
609- - Several implementations
610- - Select the best one at runtime for performance.
611-
612-
613-
614- ---
615-
616- # Issue: x64 processors support different instructions
617-
618- A Zen 5 CPU and a Pentium 4 CPU can be quite different.
619-
620- ```cpp
621- bool has_sse2() { /* query the CPU */ }
622- bool has_avx2() { /* query the CPU */ }
623- bool has_avx512() { /* query the CPU */ }
624- ```
625-
626- These functions cannot be ` consteval ` .
627-
628-
629- ---
630-
631- <img src="images/dispatching.svg" width="50%">
632-
633- ---
634-
635- # Example: Sum function
636-
637- ``` cpp
638- using SumFunc = float (*)(const float *, size_t );
639- ```
640-
641- ---
642-
643- # Setup a reassignable implementation
644-
645-
646- ``` cpp
647- SumFunc &get_sum_fnc () {
648- static SumFunc sum_impl = sum_init;
649- return sum_impl;
650- }
651- ```
652-
653- We initialize it with some special initialization function.
654-
655-
656-
657- ---
658-
659- ``` cpp
660- float sum_init (const float * data, size_t n) {
661- SumFunc &sum_impl = get_sum_fnc();
662- if (has_avx2()) {
663- sum_impl = sum_avx2;
664- } else if (has_sse2()) {
665- sum_impl = sum_sse2;
666- } else {
667- sum_impl = sum_generic;
668- }
669- return sum_impl(data, n);
670- }
671- ```
672-
673- On first call, `get_sum_fnc()` is modified, and then it will remain constant.
674-
675- ---
676-
677- # Runtime dispatching and metaprogramming
678-
679- - Metaprogramming is at compile-time.
680- - Runtime dispatching is fundamentally at runtime.
681-
682603---
683604
684605# Does your string need escaping?
685606
686-
687607- In JSON, you must escape control characters, quotes.
688608- Most strings in practice do not need escaping.
689609
0 commit comments