aboutsummaryrefslogtreecommitdiff
path: root/docs/history/17-o3-boundaries/code/examples/mandel.zc
diff options
context:
space:
mode:
Diffstat (limited to 'docs/history/17-o3-boundaries/code/examples/mandel.zc')
-rw-r--r--docs/history/17-o3-boundaries/code/examples/mandel.zc36
1 files changed, 36 insertions, 0 deletions
diff --git a/docs/history/17-o3-boundaries/code/examples/mandel.zc b/docs/history/17-o3-boundaries/code/examples/mandel.zc
new file mode 100644
index 0000000..5810397
--- /dev/null
+++ b/docs/history/17-o3-boundaries/code/examples/mandel.zc
@@ -0,0 +1,36 @@
+// the mandelbrot set in 16.16 fixed point. the cpu has no fixed-point
+// multiply, so one gets wired in: a 32x32 -> 64 bit multiply, keeping the
+// middle 32 bits. the jit inlines it as two native multiplies and a shift.
+enhance fmul(a, b) micro
+ "r2 = mul a b; r3 = mulhs a b"
+ "; r3 = shl r3 16; r2 = shr r2 16; a = or r3 r2";
+
+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);
+ putc(it == MAXIT ? '@' : shades[it * 11 / MAXIT]);
+ col++;
+ }
+ putc(10);
+ row++;
+ }
+ return 0;
+}