aboutsummaryrefslogtreecommitdiff
path: root/docs/history/13-gap-fixes/code/examples/wiring.zc
diff options
context:
space:
mode:
authorGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
committerGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
commitecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch)
tree70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /docs/history/13-gap-fixes/code/examples/wiring.zc
a
Diffstat (limited to 'docs/history/13-gap-fixes/code/examples/wiring.zc')
-rw-r--r--docs/history/13-gap-fixes/code/examples/wiring.zc19
1 files changed, 19 insertions, 0 deletions
diff --git a/docs/history/13-gap-fixes/code/examples/wiring.zc b/docs/history/13-gap-fixes/code/examples/wiring.zc
new file mode 100644
index 0000000..ed30935
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/wiring.zc
@@ -0,0 +1,19 @@
+// virtual enhancements: custom opcodes wired into the cpu while compiling.
+// the recipes run through the core's optimizer; the jit inlines the result.
+enhance popcount(x) micro
+ "r1 = shr x 1; r1 = and r1 0x55555555; x = sub x r1"
+ "; r1 = shr x 2; r1 = and r1 0x33333333; x = and x 0x33333333; x = add x r1"
+ "; r1 = shr x 4; x = add x r1; x = and x 0x0f0f0f0f; x = mul x 0x01010101; x = shr x 24";
+enhance div10(x) micro "x = udiv x 10"; // becomes a multiply-high, no divide
+enhance hypot2(a, b) vm { 0 $ * ~ 0 $ * + }; // lifted from vm code
+enhance minmax(a, b) -> 2 micro "r2 = min a b; r1 = max a b; r0 = r2";
+let lo, hi;
+fn main()
+{
+ putn(popcount(0xff00ff)); putc(' ');
+ putn(div10(12345)); putc(' ');
+ putn(hypot2(3, 4)); putc(' ');
+ asm { 9 4 minmax hi ! lo ! }
+ putn(lo); putc('/'); putn(hi); putc(10);
+ return 0;
+}