Bit-shift array
BlitzMax Forums/BlitzMax Programming/Bit-shift array
| ||
Is there any way I could shift all the bits in an array say... to the left 23 bits? I have an array of Longs and I need to shift it in order to match a precise position, then make a bitwise AND with another Long array. Obviously, I'm looking for the fastest solution, not the easiest :) |
| ||
Do it in C and include the code. Also.. Longs. Ouch, that will be slow. |
| ||
Well, I was hoping that doing an AND between two long arrays would be faster than two int arraws of double length. Still, I haven't got to the point of actually testing that, since I need the shifting first. |
| ||
A long is 64-bit so won't fit into a 32-bit processor in a single instruction - it needs to fetch half, calculate half, store half, fetch the second half, you get the idea. A 32-bit Int can be in and out in a single shot. As BlitzMax can only compile 32-bit applications, even on a 64-bit processor I think it will be slow. You should test, but I think anything to do with longs will be considerably slower than just doing more with Ints. |
| ||
I recommend mapping the array's storage memory to an int pointer and using that to make the modifications. e.g. something like... local myarrayA:long[1000] local myarrayB:long[1000] 'fill A with some data, then.. local Aint:int ptr=int ptr(varptr(myarrayA[0])) local Bint:int ptr=int ptr(varptr(myarrayB[0])) local b:int=Aint[0] local c:int for local a:int=1 tol 1998 c=Aint[a] Bint[a-1]=(b Shl 23) | (c Shr 9) b=c next Bint[1999]=Aint[1999] Shl 23 Something like that |
| ||
the 32 bit processor has a way of doing 64 bit calculations but it uses two registers instead of 1 for each number edx:eax and ecx:ebx I believe(havent looked in to Assembly in years. I'm not sure what registers it uses anymore) but it does it all in 1 instruction. the reason that it's slow is that it has to load the registers and process the 4 registers and assign the result to 2 or 4 registers dependig on the type of operation it has to do also add to that the method by which the compiler deals with the code and the results. |
| ||
Well, eventually I had to give this up. I was trying to get a fast per-pixel collision checking using bit masks. However there's a point where you stop going for full speed and choose the path that's faster to implement. So I went for checking the pixmaps themselves and it's fast enough for now. Also, that settled longs vs ints. I'm only doing a bit-wise AND between the 2 bytes that represent the alpha component in the two pixmaps. |