[squeak-dev] little VM fast-up : #bytesSwapped:
Eliot Miranda
eliot.miranda at gmail.com
Fri Jul 4 21:07:52 UTC 2008
Thank you Nicolas! If you send me a method with your date stamp on it it'll
get integrated in my changes...
On Fri, Jul 4, 2008 at 1:22 PM, nicolas cellier <ncellier at ifrance.com>
wrote:
>
> Hello VM gods,
> every little improvment counts, so my 2 pennies:
>
> Interpreter>>byteSwapped: w
> "Answer the given integer with its bytes in the reverse order."
>
> BytesPerWord = 4
> ifTrue:
> [^((w bitShift: Byte3ShiftNegated) bitAnd: Byte0Mask)
> + ((w bitShift: Byte1ShiftNegated) bitAnd: Byte1Mask)
> + ((w bitShift: Byte1Shift ) bitAnd: Byte2Mask)
> + ((w bitShift: Byte3Shift ) bitAnd: Byte3Mask)]
> ifFalse:
> [^((w bitShift: Byte7ShiftNegated) bitAnd: Byte0Mask)
> + ((w bitShift: Byte5ShiftNegated) bitAnd: Byte1Mask)
> + ((w bitShift: Byte3ShiftNegated) bitAnd: Byte2Mask)
> + ((w bitShift: Byte1ShiftNegated) bitAnd: Byte3Mask)
> + ((w bitShift: Byte1Shift ) bitAnd: Byte4Mask)
> + ((w bitShift: Byte3Shift ) bitAnd: Byte5Mask)
> + ((w bitShift: Byte5Shift ) bitAnd: Byte6Mask)
> + ((w bitShift: Byte7Shift ) bitAnd: Byte7Mask)]
>
>
> Can be written with less operations with the classical:
>
> byteSwapped: w
> | x |
> x := w.
> BytesPerWord = 4
> ifTrue: [
> "Note: In C, x unsigned 64 bits, first bitAnd: is not required"
> x = ((x bitAnd: 16rFFFF0000 >> 16)
> + ((x bitAnd: 16r0000FFFF << 16).
> x = ((x bitAnd: 16rFF00FF00 >> 8)
> + ((x bitAnd: 16r00FF00FF << 8)]
> ifFalse: [
> "Note: In C, x unsigned 64 bits, first bitAnd: is not required"
> x = ((x bitAnd: 16rFFFFFFFF00000000 >> 32)
> + ((x bitAnd: 16r00000000FFFFFFFF << 32).
> x = ((x bitAnd: 16rFFFF0000FFFF0000 >> 16)
> + ((x bitAnd: 16r0000FFFF0000FFFF << 16).
> x = ((x bitAnd: 16rFF00FF00FF00FF00 >> 8)
> + ((x bitAnd: 16r00FF00FF00FF00FF << 8)].
> ^x
>
> Yeah, the cost is (BytesPerWord log: 2).
> Of course, you can use named constants, with proper unsigned long
> declarations, that's a detail i let you deal with.
>
> Nicolas
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20080704/c96b16c3/attachment.htm
More information about the Squeak-dev
mailing list
|