Re: fast bit rotation
- Posted by Tommy Carlier <tommy.carlier at pandora.be> Dec 31, 2003
- 432 views
------- <code> include machine.e constant rotate_left = allocate(15), rotate_left_param = rotate_left + 2 poke(rotate_left, { #60, -- 0: pusha #B8,#00,#00,#00,#00, -- 1: mov eax, dword param (2) #D1,#C0, -- 6: rol eax, 1 #A3,#6A,#0A,#82,#82, -- 8: mov [@param], eax #61, -- D: popa #C3}) -- E: ret poke4(rotate_left + 9, rotate_left_param) -- @param constant rotate_right = allocate(15), rotate_right_param = rotate_right + 2 poke(rotate_right, { #60, -- 0: pusha #B8,#00,#00,#00,#00, -- 1: mov eax, dword param (2) #D1,#C8, -- 6: ror eax, 1 #A3,#6A,#3A,#C2,#83, -- 8: mov [@param], eax #61, -- D: popa #C3}) -- E: ret poke4(rotate_right + 9, rotate_right_param) -- @param -- To use: poke4(rotate_left_param, i) -- i = an integer call(rotate_left) i = peek4s(rotate_left_param) poke4(rotate_right_param, j) -- j = an integer call(rotate_right) j = peek4s(rotate_right_param) -- Before closing your program, you should call: free(rotate_left) free(rotate_right) ------- </code> Perhaps there are faster methods than this, but on my laptop (Pentium II 433MHz, 64MB RAM), I can do 10_000_000 (ten million) rotations in 19.11 seconds, which is more than 500_000 rotations per second. -- Tommy Carlier tommy online: http://users.pandora.be/tommycarlier