for (int yp = 0; yp < 200; ++yp) { float b = yp*1.5/100 - 1.5; for (int xp = 0; xp < 320; ++xp) { float a = xp*4/320 - 2.5; float x = a; float y = b; int i; for (i = 0; i < 32; ++i) { float u = x*x; float v = y*y; float w = 2*x*y; y = w + b; x = u - v + a; if (u + v >= 4) break; } if (i == 32) c = 0; else c = i%3; plot(x, y, c); } } (x+y)*(x-y) = x*x-y*y (x+y)*(x+y) = x*x+y*y+2*x*y |c|^2(8|c|^2-3) <= 3/32 - Re(c) |c+1| <= 1/4 (x+1)*(x+1) + y*y <= 1/16 (x*x + y*y)*(8*(x*x + y*y) - 3) <= 3/32 - x Use a custom video mode with 202 scanlines. Compute 101, mirror 100, leave 1 blank Initially, largest value is 2.5*2.5 + 1.5*1.5 = 8.5 Bailout condition is |z|>=4, i.e. x*x+y*y >= 16 In square table, put 16 for all values <-4 or >4 x in si y in bx b in dx a in es 0x1c00 in sp loopTop: mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,sp jae escaped mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x loop loopTop Representable values: -16.0 to 15.9990234375 with precision 0.0009765625 0x0002 0.0009765625 0x0004 0.001953125 0x0008 0.00390625 0x0010 0.0078125 0x0020 0.015625 0x0040 0.03125 0x0080 0.0625 0x0100 0.125 0x0200 0.25 0x0400 0.5 0x0800 1 0x1000 2 0x2000 4 0x4000 8 0x8000 16 Problem: We can never escape at y==0. Separate algorithm? loopTopY0: cmp si,0x4000 jge escaped cmp si,-0x4000 jle escaped mov si,[si] ; x*x add si,a ; x*x+a -> new x loop loopTop Divide screen into 8x8 blocks Subdivide and evaluate block a at d,e,f if iteration count at a is different from b or c a...d...b s == 3 ........ ........ ........ e...f... ........ ........ ........ c a.d.b s == 2 .... e.f. .... c adb s == 1 ef c More complicated algorithm: if (a == b) { if (a == c) { // No subdivision, colour entire block a } else { // Evaluate at e and f. Set d==a. Subdivide. } } else { if (a == c) { // Evaluate at d and f. Set e==a. Subdivide. } else { if (b == c) { // Evaluate at d and e. Set f==b. Subdivide. } else { // Evaluate at d, e and f. Subdivide. } } } Need to keep 8 rows == 2560 bytes Plan: See if basic rendering algorithm works on modern machine 0 128 256 384 a.u.b ..... v.w.p ..... c.q.d if (a == b && a == c && a == d) { Fill entire block with a return; } if (a == b) u = a; else compute u if (a == c) v = a; else compute v if (b == d) p = b; else compute p if (c == d) q = c; else compute q if (u == v && u == p && u == q) w = u; else compute w subdivide(a, u, v, w); subdivide(u, b, w, p); subdivide(v, w, c, q); subdivide(w, p, q, d); TODO: Optimization: Use bp-relative addressing in plotting routine to avoid cs: override? Solid-guessing implementation Time Lakes Time Xaos: http://web.mit.edu/kolya/sipb/afs/root.afs/athena/activity/p/peckers/OldFiles/Programs/XaoS-2.2/doc/algorithm.txt Use similar algorithm as mandel_quadtree and re-render from quadtree at each frame rather than trying to shift bits At each frame, compute a horizontal plotting routine for each combination of x-position and grid width Guessing: 100601 -> 100598 with initial grid 32x32 With main lake optimization 2: 99026 With both main lake optimizations: 96961 iterations (saving of 3637 iterations) over 12462 pixels Only faster if lake checks take <0.2918 of an iteration! After avoiding some excess y > 100 points: 94207 iterations over 11819 pixels (Going from maxY = 101 to 96 takes us to 92670 iters over 11273 pixels - not worth it) avoiding excess y == 101 points at level 1: 94051 iterations over 11760 pixels No guessing: 347767 Estimated time for guessing: 4.3 seconds How to deal with incorrect guesses? Divide into 8 instead of 4? Determine how many extra cycles this will take Don't allow adjacent tiles to have sizes differing by 2? Keep a stack of tiles to subdivide Hard code some hacks? Main lake optimization; use different iteration counts for cardioid and largest circle Is this actually faster? center -1, radius 0.25 -> right edge at -0.75 4*y = 2*sin(t) - sin(2*t) d/dt (4*y) = 2*cos(t) - 2*cos(2*t) = 0 cos(t) = -1/2 or 1 t = 2pi/3, -2pi/3, 0 sin(2pi/3) = sqrt(3)/2 sin(4pi/3) = -sqrt(3)/2 4*y = sqrt(3) + sqrt(3)/2 = sqrt(3)*3/2 y = sqrt(3)*3/8 d.p. square: (x0 + 10*x1)^2 == x0^2 + 100*x1^2 + 2*10*x0*x1 = x0^2 + 100*x1^2 + 10*((x0+x1)^2 - (x0-x1)^2)/2 (y0 + 10*y1)^2 == y0^2 + 100*y1^2 + 2*10*x0*y1 = y0^2 + 100*y1^2 + 10*((y0+y1)^2 - (y0-y1)^2)/2 (x0 + 10*x1)*(y0 + 10*y1) = x0*y0 + 10*(x1*y0 + x0*y1) + 100*x1*y1 x1*y0 + x0*y1 = ((x1 + y0)^2 - (x1 - y0)^2 + (x0 + y1)^2 - (x0 - y1)^2)/4 x1*y0 + x0*y1 = (x0 + x1)*(y0 + y1) - x0*y0 - x1*y1 = ((x0 + x1 + y0 + y1)^2 - (x0 + x1 - y0 - y1)^2 - (x0 + y0)^2 + (x0 - y0)^2 - (x1 + y1)^2 + (x1 - y1)^2)/4 1 dp square = 4 sp squares 1 dp Mandel using 3 dp squares = 12 sp squares 1 naive dp multiplication = 4 sp multiplications = 8 sp squares 1 Karatsuba dp multiplication = 3 sp multiplications = 6 sp squares Plotted blocks: 1026 2x2 4104 361 4x4 5776 96 8x8 6144 24 16x16 6144 3 32x32 3072 1510 elided blocks subdivide() calls: 3310 2x2 1200 4x4 396 8x8 130 16x16 40 32x32 5076 total blocks 3566 memory blocks == 28528 bytes subdivide5 y == 96 ? 5 : 32 times 4 stosw subdivide4 y == 96 ? 5 : 16 times 2 stosw subdivide3 y == 96 ? 5 : 8 stosw subdivide2 y == 100 ? 1 : 4 stosb subdivide2 y == 100 ? 1 : 4 stosb subdivide1L - - subdivide1R y == 100 ? 1 : 2 stosb