[squeak-dev] little VM fast-up : #bytesSwapped:
nicolas cellier
ncellier at ifrance.com
Fri Jul 4 20:22:39 UTC 2008
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
More information about the Squeak-dev
mailing list
|