|
From: Dean B. <dea...@bt...> - 2000-06-30 17:06:49
|
I've been playing with assembly speeding up my BSP compiler over the last few days and I came accross a problem where I had to flip BCD bytes in a register. This is similar to the Endian Swap you have to do for the mod's. I basically had to Endian Swap 32-bit integers. The way I saw it there were 2 viable methods for the 16 bit integer: mov ax, Value ; 1 clock xor ah, al ; 1 clock xor al, ah ; 1 clock xor ah, al ; 1 clock and mov ax, Value ; 1 clock xchg ah, al ; 3 clocks so a total of 4 cycles for each. I needed to do this with 32-bit registers though. :( And as you well know there are only al, ah, ax, and eax so you can't really use the 2 methods above. So I used the following slow method for the meantime and was gonna come back and speed the bugger up at a later date. mov eax, InValue ; 1 clock mov edx, InValue ; 1 clock shr edx, 16 ; 3 clocks xchg al, ah ; 3 clocks xchg dl, dh ; 3 clocks shl eax, 16 ; 3 clocks mov ax, dx ; 1 clock which is a total of 15 cycles. :((((( When I came back to speed it up I stumbled accross a beautifull little instruction that is available on 486+ processors called bswap. :) It allowed me to do this: mov eax, InValue ; 1 clock bswap eax ; 1 clock This took the clock cycles for this code from 15 to 2!!!!!!!! And as this is in one of the main loops involved in the compliation process you wouldn't believe the speed increase that this bad boy has given me. :) I was thinking that we should be using this sucker in the mod loader but then I discovered that it only works with the entire 32-bits of the register. :( So for it to work for 16 bit registers you have to: mov eax, InValue ; 1 clock bswap eax ; 1 clock shr eax, 16 ; 3 clocks which is 5 clocks in total so it's best sticking with xchg. :( So it's faster to Endian swap 32-bit values than 16-bit values. Just thought you guys might be interested in this stuff. :) laterz. |