From 9ad9bf96bbb4502209d7c2e8b1e3c729bdfbcfc4 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sun, 9 May 2021 08:03:48 -0700 Subject: mandelbrot: streamline exposition --- html/mandelbrot-fixed.mu.html | 214 ++++++++++++++++++++---------------------- 1 file changed, 104 insertions(+), 110 deletions(-) (limited to 'html/mandelbrot-fixed.mu.html') diff --git a/html/mandelbrot-fixed.mu.html b/html/mandelbrot-fixed.mu.html index 6f87111e..3a09bf53 100644 --- a/html/mandelbrot-fixed.mu.html +++ b/html/mandelbrot-fixed.mu.html @@ -170,118 +170,112 @@ if ('onhashchange' in window) { 111 width-f <- shift-left 0xb/log2-font-width-and-fixed-precision # 3 + 8 = 11 112 var height-f/edi: int <- copy b 113 height-f <- shift-left 0xc/log2-font-height-and-fixed-precision # 4 + 8 = 12 -114 # it might make sense to keep x and y as regular ints -115 # treating them as fixed-point for demonstration purposes -116 var y-f/ecx: int <- copy 0 -117 { -118 compare y-f, height-f -119 break-if->= -120 var imaginary-f/ebx: int <- mandelbrot-min-y y-f, width-f, height-f -121 var x-f/eax: int <- copy 0 -122 { -123 compare x-f, width-f -124 break-if->= -125 var real-f/edx: int <- mandelbrot-min-x x-f, width-f -126 var iterations/esi: int <- mandelbrot-pixel real-f, imaginary-f, 0x400/max -127 { -128 var x: int -129 var y: int -130 var tmp/eax: int <- fixed-to-int x-f -131 copy-to x, tmp -132 tmp <- fixed-to-int y-f -133 copy-to y, tmp -134 compare iterations, 0x400/max -135 { -136 break-if->= -137 pixel screen, x, y, 0xf/white -138 } -139 compare iterations, 0x400/max -140 { -141 break-if-< -142 pixel screen, x, y, 0/black -143 } -144 } -145 x-f <- add 0x100/1 -146 loop -147 } -148 y-f <- add 0x100/1 -149 loop -150 } -151 } -152 -153 fn mandelbrot-pixel real-f: int, imaginary-f: int, max: int -> _/esi: int { -154 var x-f/esi: int <- copy 0 -155 var y-f/edi: int <- copy 0 -156 var iterations/ecx: int <- copy 0 -157 { -158 var done?/eax: boolean <- mandelbrot-done? x-f, y-f -159 compare done?, 0/false -160 break-if-!= -161 compare iterations, max -162 break-if->= -163 var x2-f/edx: int <- mandelbrot-x x-f, y-f, real-f -164 var y2-f/ebx: int <- mandelbrot-y x-f, y-f, imaginary-f -165 x-f <- copy x2-f -166 y-f <- copy y2-f -167 iterations <- increment -168 loop -169 } -170 return iterations -171 } -172 -173 fn mandelbrot-done? x-f: int, y-f: int -> _/eax: boolean { -174 # x*x + y*y > 4 -175 var tmp-f/eax: int <- multiply-fixed x-f, x-f -176 var result-f/ecx: int <- copy tmp-f -177 tmp-f <- multiply-fixed y-f, y-f -178 result-f <- add tmp-f -179 compare result-f, 0x400/4 -180 { -181 break-if-> -182 return 0/false -183 } -184 return 1/true +114 var y/ecx: int <- copy 0 +115 { +116 compare y, height-f +117 break-if->= +118 var imaginary-f/ebx: int <- viewport-to-imaginary-f y, width-f, height-f +119 var x/eax: int <- copy 0 +120 { +121 compare x, width-f +122 break-if->= +123 var real-f/edx: int <- viewport-to-real-f x, width-f +124 var iterations/esi: int <- mandelbrot-iterations-for-point real-f, imaginary-f, 0x400/max +125 compare iterations, 0x400/max +126 { +127 break-if->= +128 pixel screen, x, y, 0xf/white +129 } +130 compare iterations, 0x400/max +131 { +132 break-if-< +133 pixel screen, x, y, 0/black +134 } +135 x <- increment +136 loop +137 } +138 y <- increment +139 loop +140 } +141 } +142 +143 fn mandelbrot-iterations-for-point real-f: int, imaginary-f: int, max: int -> _/esi: int { +144 var x-f/esi: int <- copy 0 +145 var y-f/edi: int <- copy 0 +146 var iterations/ecx: int <- copy 0 +147 { +148 var done?/eax: boolean <- mandelbrot-done? x-f, y-f +149 compare done?, 0/false +150 break-if-!= +151 compare iterations, max +152 break-if->= +153 var x2-f/edx: int <- mandelbrot-x x-f, y-f, real-f +154 var y2-f/ebx: int <- mandelbrot-y x-f, y-f, imaginary-f +155 x-f <- copy x2-f +156 y-f <- copy y2-f +157 iterations <- increment +158 loop +159 } +160 return iterations +161 } +162 +163 fn mandelbrot-done? x-f: int, y-f: int -> _/eax: boolean { +164 # x*x + y*y > 4 +165 var tmp-f/eax: int <- multiply-fixed x-f, x-f +166 var result-f/ecx: int <- copy tmp-f +167 tmp-f <- multiply-fixed y-f, y-f +168 result-f <- add tmp-f +169 compare result-f, 0x400/4 +170 { +171 break-if-> +172 return 0/false +173 } +174 return 1/true +175 } +176 +177 fn mandelbrot-x x-f: int, y-f: int, real-f: int -> _/edx: int { +178 # x*x - y*y + real +179 var tmp-f/eax: int <- multiply-fixed x-f, x-f +180 var result-f/ecx: int <- copy tmp-f +181 tmp-f <- multiply-fixed y-f, y-f +182 result-f <- subtract tmp-f +183 result-f <- add real-f +184 return result-f 185 } 186 -187 fn mandelbrot-x x-f: int, y-f: int, real-f: int -> _/edx: int { -188 # x*x - y*y + real -189 var tmp-f/eax: int <- multiply-fixed x-f, x-f -190 var result-f/ecx: int <- copy tmp-f -191 tmp-f <- multiply-fixed y-f, y-f -192 result-f <- subtract tmp-f -193 result-f <- add real-f -194 return result-f -195 } -196 -197 fn mandelbrot-y x-f: int, y-f: int, imaginary-f: int -> _/ebx: int { -198 # 2*x*y + imaginary -199 var result-f/eax: int <- copy x-f -200 result-f <- shift-left 1/log2 -201 result-f <- multiply-fixed result-f, y-f -202 result-f <- add imaginary-f -203 return result-f -204 } -205 -206 fn mandelbrot-min-x x-f: int, width-f: int -> _/edx: int { -207 # (x - width/2)*4/width -208 var result-f/eax: int <- copy x-f -209 var half-width-f/ecx: int <- copy width-f -210 half-width-f <- shift-right-signed 1/log2 -211 result-f <- subtract half-width-f -212 result-f <- shift-left 2/log4 -213 result-f <- divide-fixed result-f, width-f -214 return result-f -215 } -216 -217 fn mandelbrot-min-y y-f: int, width-f: int, height-f: int -> _/ebx: int { -218 # (y - height/2)*4/width -219 var result-f/eax: int <- copy y-f -220 shift-right-signed height-f, 1/log2 -221 result-f <- subtract height-f -222 result-f <- shift-left 2/log4 -223 result-f <- divide-fixed result-f, width-f -224 return result-f -225 } +187 fn mandelbrot-y x-f: int, y-f: int, imaginary-f: int -> _/ebx: int { +188 # 2*x*y + imaginary +189 var result-f/eax: int <- copy x-f +190 result-f <- shift-left 1/log2 +191 result-f <- multiply-fixed result-f, y-f +192 result-f <- add imaginary-f +193 return result-f +194 } +195 +196 # Scale (x, y) pixel coordinates to a complex plane where the viewport width +197 # ranges from -2 to +2. Viewport height just follows the viewport's aspect +198 # ratio. +199 +200 fn viewport-to-real-f x: int, width-f: int -> _/edx: int { +201 # (x - width/2)*4/width +202 var result-f/eax: int <- int-to-fixed x +203 var half-width-f/ecx: int <- copy width-f +204 half-width-f <- shift-right-signed 1/log2 +205 result-f <- subtract half-width-f +206 result-f <- shift-left 2/log4 +207 result-f <- divide-fixed result-f, width-f +208 return result-f +209 } +210 +211 fn viewport-to-imaginary-f y: int, width-f: int, height-f: int -> _/ebx: int { +212 # (y - height/2)*4/width +213 var result-f/eax: int <- int-to-fixed y +214 shift-right-signed height-f, 1/log2 +215 result-f <- subtract height-f +216 result-f <- shift-left 2/log4 +217 result-f <- divide-fixed result-f, width-f +218 return result-f +219 } -- cgit 1.4.1-2-gfad0