02-20-2012, 09:07 AM
LCD Wrote:Though if I was going to, I'd say why bother with a new variable? :mrgreen:Code:function mirror(dowedoit as ubyte, number as ubyte) as ubyte
dim result as ubyte
if dowedoit then
result=(number&1)<<7|(number&2)<<5|(number&4)<<3|(number&8)<<1|(number&16)>>1|(number&32)>>3|(number&64)>>5|(number&128)>>7
Else
result=number
end if
return result
end Function
Code:
function mirror(dowedoit as ubyte, number as ubyte) as ubyte
if dowedoit then
number=(number&1)<<7|(number&2)<<5|(number&4)<<3|(number&8)<<1|(number&16)>>1|(number&32)>>3|(number&64)>>5|(number&128)>>7
end if
return number
end Function
That said, if you read the behemoth assembly that makes. Ouch. Hopping on and off the stack like crazy! Not to mention a lot of very expensive reads and writes to (IX+7) - which cost 19 T states each. I wonder if my original beginners algorithm is faster!
Anyway, I think my suggested optimized version is pretty tight:
Code:
function fastcall mirror (dowedoit as uByte, number as uByte) as uByte
asm
pop hl
pop bc
AND A
LD A,B
RET Z
ld b,8
ld c,a
XOR A
mirrorLoop:
RR C
RLA
DJNZ mirrorLoop
jp (hl)
end asm
END FUNCTION