Slow fixed point math
Posted: Mon Oct 29, 2007 4:26 pm
I wrote a Julia animator a while ago, and yesterday I decided to optimize it with fixed point math. But when I used ffix with the floating point math, it was much faster then when I used integer or long integer fixed point math. I though that this was maybe because of the extra multipications and (integer) divides that you have to use to keep the iteration in range, but I was still suprised. Am I doing something wrong? I read in a tutorial that with fractals, fixed point math is usually much faster. You can have my code, and try it yourself, I commented the relevant parts:
Code: Select all
'When you compile, dont forget to uncomment
'either the fixed point version or the floating point version
'(scroll down till you see it)
DECLARE SUB ffix ()
CLS
SCREEN 13
DEFINT A-Z
CONST max = 25
ffix
FOR c = 0 TO max
OUT &H3C8, c
OUT &H3C9, c / max * 63
OUT &H3C9, c / max * 63
OUT &H3C9, c / max * 63
NEXT
DIM p AS LONG, p2 AS LONG
'Make this line a comment if you want to use floating point math.
DIM zr AS LONG, zi AS LONG, cr AS LONG, ci AS LONG, zr2 AS LONG, zi2 AS LONG
LINE (21, 13)-(298, 186), max, B
DEF SEG = &HA000
t1! = TIMER
DO UNTIL LEN(INKEY$)
p = 4502
p2 = 59497
a! = a! + .05
scr! = COS(a!)
sci! = SIN(a!)
ci = sci! * 256
cr = scr! * 256
FOR y = 14 TO 100
FOR x = 22 TO 297
''''Floating point version
'zr! = (x / 319) * 4 - 2
'zi! = (y / 199) * 4 - 2
'c = 0
'zr2! = zr! * zr!: zi2! = zi! * zi!
'DO
' zi! = (2 * zr! * zi!) + sci!
' zr! = zr2! - zi2! + scr!
' zr2! = zr! * zr!
' zi2! = zi! * zi!
' c = c + 1
'LOOP UNTIL (zr2! + zi2!) >= 4 OR c = max
''End
'''Fixed point version
'zr = (x * 3.2 - 512)
'zi = (y * 5.12 - 512)
'c = 0
'zr2 = (zr * zr) \ 256: zi2 = (zi * zi) \ 256
'DO
' zi = (zr * zi) \ 128 + ci
' zr = zr2 - zi2 + cr
' zr2 = (zr * zr) \ 256
' zi2 = (zi * zi) \ 256
' c = c + 1
'LOOP UNTIL (zr2 + zi2) >= 1024 OR c = max
'''End
POKE p, c
POKE p2, c
p = p + 1
p2 = p2 - 1
NEXT
p = p + 44
p2 = p2 - 44
NEXT
LOOP
t2! = TIMER
DEF SEG
PRINT "Fps:" + STR$((a! / .05) / (t2! - t1!))
SLEEP
END