aboutsummaryrefslogtreecommitdiff
path: root/examples/mandel.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 /examples/mandel.zc
a
Diffstat (limited to 'examples/mandel.zc')
-rw-r--r--examples/mandel.zc32
1 files changed, 32 insertions, 0 deletions
diff --git a/examples/mandel.zc b/examples/mandel.zc
new file mode 100644
index 0000000..0a8f69e
--- /dev/null
+++ b/examples/mandel.zc
@@ -0,0 +1,32 @@
+// the mandelbrot set in 16.16 fixed point. a word is 64 bits, so the product
+// fits and the point sits 16 bits down. the jit inlines it as a multiply and a shift.
+enhance fmul(a, b) micro "a = mul a b; a = sar a 16";
+
+const ONE = 65536, W = 72, H = 28, MAXIT = 40;
+let shades = " .,:;-=+*%#@";
+
+int escape(int cx, int cy) {
+ int x = 0, y = 0, i = 0;
+ while (i < MAXIT) {
+ int xx = fmul(x, x), yy = fmul(y, y);
+ if (xx + yy > 4 * ONE) return i;
+ y = 2 * fmul(x, y) + cy;
+ x = xx - yy + cx;
+ i++;
+ }
+ return MAXIT;
+}
+fn main() {
+ int row = 0;
+ while (row < H) {
+ int col = 0, cy = (row * 2400 / (H - 1) - 1200) * ONE / 1000;
+ while (col < W) {
+ int cx = (col * 3200 / (W - 1) - 2200) * ONE / 1000, it = escape(cx, cy);
+ put_c(it == MAXIT ? '@' : shades[it * 11 / MAXIT]);
+ col++;
+ }
+ put_c(10);
+ row++;
+ }
+ return 0;
+}