aboutsummaryrefslogtreecommitdiff
path: root/docs/history/15-o3/code/examples/mandel.zc
blob: 5810397117fd14a2458378486fb038e031743b75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
}