Levente Uzonyi uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ul.654.mcz ==================== Summary ==================== Name: Collections-ul.654 Author: ul Time: 6 September 2015, 11:42:17.51 pm UUID: 12c2810c-d06f-4ae8-86b4-67ca6f1d6d79 Ancestors: Collections-eem.653 Optimized all integer accessor methods in ByteArray's platform independent access category. =============== Diff against Collections-eem.653 =============== Item was changed: ----- Method: ByteArray>>longAt:bigEndian: (in category 'platform independent access') ----- + longAt: index bigEndian: bigEndian + "Return a 32-bit integer quantity starting from the given byte index. Use #normalize where necessary to ensure compatibility with non-30-bit SmallIntegers." + + | byte result | + bigEndian ifFalse: [ + (byte := self at: index + 3) <= 16r7F ifTrue: [ "Is the result non-negative?" + byte <= 16r3F ifTrue: [ + ^(((byte bitShift: 8) + (self at: index + 2) bitShift: 8) + (self at: index + 1) bitShift: 8) + (self at: index) ]. + ^(LargePositiveInteger new: 4) + replaceFrom: 1 + to: 4 + with: self + startingAt: index; + normalize ]. + "Negative" + byte >= 16rC0 ifTrue: [ + ^-1 - (((((byte bitShift: 8) + (self at: index + 2) bitShift: 8) + (self at: index + 1) bitXor: 16rFFFFFF) bitShift: 8) + ((self at: index) bitXor: 16rFF)) ]. + (result := LargeNegativeInteger new: 4) + digitAt: 4 put: ((self at: index + 3) bitXor: 16rFF); + digitAt: 3 put: ((self at: index + 2) bitXor: 16rFF); + digitAt: 2 put: ((self at: index + 1) bitXor: 16rFF). + (byte := ((self at: index) bitXor: 16rFF) + 1) <= 16rFF ifTrue: [ + ^result + digitAt: 1 put: byte; + normalize ]. + ^result + digitAt: 1 put: 16rFF; + - 1 "It's tempting to do the subtraction in a loop, to avoid the LargeInteger creation, but it's actually slower than this." ]. + (byte := self at: index) <= 16r7F ifTrue: [ "Is the result non-negative?" + byte <= 16r3F ifTrue: [ + ^(((byte bitShift: 8) + (self at: index + 1) bitShift: 8) + (self at: index + 2) bitShift: 8) + (self at: index + 3) ]. + ^(LargePositiveInteger new: 4) + digitAt: 1 put: (self at: index + 3); + digitAt: 2 put: (self at: index + 2); + digitAt: 3 put: (self at: index + 1); + digitAt: 4 put: byte; + normalize ]. + "Negative" + 16rC0 <= byte ifTrue: [ + ^-1 - (((((byte bitShift: 8) + (self at: index + 1) bitShift: 8) + (self at: index + 2) bitXor: 16rFFFFFF) bitShift: 8) + ((self at: index + 3) bitXor: 16rFF)) ]. + (result := LargeNegativeInteger new: 4) + digitAt: 4 put: (byte bitXor: 16rFF); + digitAt: 3 put: ((self at: index + 1) bitXor: 16rFF); + digitAt: 2 put: ((self at: index + 2) bitXor: 16rFF). + (byte := ((self at: index + 3) bitXor: 16rFF) + 1) <= 16rFF ifTrue: [ + ^result + digitAt: 1 put: byte; + normalize ]. + ^result + digitAt: 1 put: 16rFF; + - 1 "It's tempting to do the subtraction in a loop, to avoid the LargeInteger creation, but it's actually slower than this."! - longAt: index bigEndian: aBool - "Return a 32bit integer quantity starting from the given byte index" - | b0 b1 b2 w h | - aBool ifTrue:[ - b0 := self at: index. - b1 := self at: index+1. - b2 := self at: index+2. - w := self at: index+3. - ] ifFalse:[ - w := self at: index. - b2 := self at: index+1. - b1 := self at: index+2. - b0 := self at: index+3. - ]. - "Minimize LargeInteger arithmetic" - h := ((b0 bitAnd: 16r7F) - (b0 bitAnd: 16r80) bitShift: 8) + b1. - b2 = 0 ifFalse:[w := (b2 bitShift: 8) + w]. - h = 0 ifFalse:[w := (h bitShift: 16) + w]. - ^w! Item was changed: ----- Method: ByteArray>>longAt:put:bigEndian: (in category 'platform independent access') ----- + longAt: index put: value bigEndian: bigEndian + "Store a 32-bit signed integer quantity starting from the given byte index" + + | v v2 | + value isLarge ifTrue: [ + bigEndian ifFalse: [ + value positive ifTrue: [ + self + replaceFrom: index + to: index + 3 + with: value + startingAt: 1. + ^value ]. + v := 0. + [ v <= 3 and: [ (v2 := ((value digitAt: v + 1) bitXor: 16rFF) + 1) = 16r100 ] ] whileTrue: [ + self at: index + v put: 0. + v := v + 1 ]. + self at: index + v put: v2. + v := v + 1. + [ v <= 3 ] whileTrue: [ + self at: index + v put: ((value digitAt: (v := v + 1)) bitXor: 16rFF) ]. + ^value ]. + value positive ifTrue: [ + self + at: index put: (value digitAt: 4); + at: index + 1 put: (value digitAt: 3); + at: index + 2 put: (value digitAt: 2); + at: index + 3 put: (value digitAt: 1). + ^value ]. + v := 3. + [ 0 <= v and: [ (v2 := ((value digitAt: 4 - v) bitXor: 16rFF) + 1) = 16r100 ] ] whileTrue: [ + self at: index + v put: 0. + v := v - 1 ]. + self at: index + v put: v2. + [ 0 <= (v := v - 1) ] whileTrue: [ + self at: index + v put: ((value digitAt: 4 - v) bitXor: 16rFF) ]. + ^value ]. + v := value bitShift: -24. + 0 <= (v := (v bitAnd: 16r7F) - (v bitAnd: 16r80)) ifFalse: [ + v := v + 16r100 ]. + bigEndian ifFalse: [ + self + at: index put: (value bitAnd: 16rFF); + at: index + 1 put: ((value bitShift: -8) bitAnd: 16rFF); + at: index + 2 put: ((value bitShift: -16) bitAnd: 16rFF); + at: index + 3 put: v. + ^value ]. + self + at: index put: v; + at: index + 1 put: ((value bitShift: -16) bitAnd: 16rFF); + at: index + 2 put: ((value bitShift: -8) bitAnd: 16rFF); + at: index + 3 put: (value bitAnd: 16rFF). - longAt: index put: value bigEndian: aBool - "Return a 32bit integer quantity starting from the given byte index" - | b0 b1 b2 b3 | - b0 := value bitShift: -24. - b0 := (b0 bitAnd: 16r7F) - (b0 bitAnd: 16r80). - b0 < 0 ifTrue:[b0 := 256 + b0]. - b1 := (value bitShift: -16) bitAnd: 255. - b2 := (value bitShift: -8) bitAnd: 255. - b3 := value bitAnd: 255. - aBool ifTrue:[ - self at: index put: b0. - self at: index+1 put: b1. - self at: index+2 put: b2. - self at: index+3 put: b3. - ] ifFalse:[ - self at: index put: b3. - self at: index+1 put: b2. - self at: index+2 put: b1. - self at: index+3 put: b0. - ]. ^value! Item was changed: ----- Method: ByteArray>>shortAt:bigEndian: (in category 'platform independent access') ----- + shortAt: index bigEndian: bigEndian + "Return a 16-bit signed integer quantity starting from the given byte index" + + | result | + result := bigEndian + ifFalse: [ ((self at: index + 1) bitShift: 8) + (self at: index) ] + ifTrue: [ ((self at: index) bitShift: 8) + (self at: index + 1) ]. + result < 16r8000 ifTrue: [ ^result ]. + ^result - 16r10000! - shortAt: index bigEndian: aBool - "Return a 16 bit integer quantity starting from the given byte index" - | uShort | - uShort := self unsignedShortAt: index bigEndian: aBool. - ^(uShort bitAnd: 16r7FFF) - (uShort bitAnd: 16r8000)! Item was changed: ----- Method: ByteArray>>shortAt:put:bigEndian: (in category 'platform independent access') ----- + shortAt: index put: value bigEndian: bigEndian + "Store a 16-bit signed integer quantity starting from the given byte index" + + | unsignedValue | + (unsignedValue := value) < 0 ifTrue: [ + unsignedValue := unsignedValue + 16r10000 ]. + bigEndian ifFalse: [ + self + at: index + 1 put: (unsignedValue bitShift: -8); + at: index put: (unsignedValue bitAnd: 16rFF). + ^value ]. + self + at: index put: (unsignedValue bitShift: -8); + at: index + 1 put: (unsignedValue bitAnd: 16rFF). - shortAt: index put: value bigEndian: aBool - "Store a 16 bit integer quantity starting from the given byte index" - self unsignedShortAt: index put: (value bitAnd: 16r7FFF) - (value bitAnd: -16r8000) bigEndian: aBool. ^value! Item was changed: ----- Method: ByteArray>>unsignedLong64At:bigEndian: (in category 'platform independent access') ----- + unsignedLong64At: index bigEndian: bigEndian + "Return a 64-bit unsigned integer quantity starting from the given byte index. Use #normalize where necessary to ensure compatibility with non-30-bit SmallIntegers." - unsignedLong64At: index bigEndian: aBool - "Avoid as much largeInteger as we can" - | b0 b2 b3 b5 b6 w n2 n3 | + | v | + bigEndian ifFalse: [ + (v := self at: index + 7) = 0 ifFalse: [ + ^(LargePositiveInteger new: 8) + digitAt: 1 put: (self at: index); + digitAt: 2 put: (self at: index + 1); + digitAt: 3 put: (self at: index + 2); + digitAt: 4 put: (self at: index + 3); + digitAt: 5 put: (self at: index + 4); + digitAt: 6 put: (self at: index + 5); + digitAt: 7 put: (self at: index + 6); + digitAt: 8 put: v; + normalize ]. + (v := self at: index + 6) = 0 ifFalse: [ + ^(LargePositiveInteger new: 7) + digitAt: 1 put: (self at: index); + digitAt: 2 put: (self at: index + 1); + digitAt: 3 put: (self at: index + 2); + digitAt: 4 put: (self at: index + 3); + digitAt: 5 put: (self at: index + 4); + digitAt: 6 put: (self at: index + 5); + digitAt: 7 put: v; + normalize ]. + (v := self at: index + 5) = 0 ifFalse: [ + ^(LargePositiveInteger new: 6) + digitAt: 1 put: (self at: index); + digitAt: 2 put: (self at: index + 1); + digitAt: 3 put: (self at: index + 2); + digitAt: 4 put: (self at: index + 3); + digitAt: 5 put: (self at: index + 4); + digitAt: 6 put: v; + normalize ]. + (v := self at: index + 4) = 0 ifFalse: [ + ^(LargePositiveInteger new: 5) + digitAt: 1 put: (self at: index); + digitAt: 2 put: (self at: index + 1); + digitAt: 3 put: (self at: index + 2); + digitAt: 4 put: (self at: index + 3); + digitAt: 5 put: v; + normalize ]. + (v := self at: index + 3) <= 16r3F ifFalse: [ + ^(LargePositiveInteger new: 4) + digitAt: 1 put: (self at: index); + digitAt: 2 put: (self at: index + 1); + digitAt: 3 put: (self at: index + 2); + digitAt: 4 put: v; + normalize ]. + ^(((v bitShift: 8) + (self at: index + 2) bitShift: 8) + (self at: index + 1) bitShift: 8) + (self at: index) ]. + (v := self at: index) = 0 ifFalse: [ + ^(LargePositiveInteger new: 8) + digitAt: 1 put: (self at: index + 7); + digitAt: 2 put: (self at: index + 6); + digitAt: 3 put: (self at: index + 5); + digitAt: 4 put: (self at: index + 4); + digitAt: 5 put: (self at: index + 3); + digitAt: 6 put: (self at: index + 2); + digitAt: 7 put: (self at: index + 1); + digitAt: 8 put: v; + normalize ]. + (v := self at: index + 1) = 0 ifFalse: [ + ^(LargePositiveInteger new: 7) + digitAt: 1 put: (self at: index + 7); + digitAt: 2 put: (self at: index + 6); + digitAt: 3 put: (self at: index + 5); + digitAt: 4 put: (self at: index + 4); + digitAt: 5 put: (self at: index + 3); + digitAt: 6 put: (self at: index + 2); + digitAt: 7 put: v; + normalize ]. + (v := self at: index + 2) = 0 ifFalse: [ + ^(LargePositiveInteger new: 6) + digitAt: 1 put: (self at: index + 7); + digitAt: 2 put: (self at: index + 6); + digitAt: 3 put: (self at: index + 5); + digitAt: 4 put: (self at: index + 4); + digitAt: 5 put: (self at: index + 3); + digitAt: 6 put: v; + normalize ]. + (v := self at: index + 3) = 0 ifFalse: [ + ^(LargePositiveInteger new: 5) + digitAt: 1 put: (self at: index + 7); + digitAt: 2 put: (self at: index + 6); + digitAt: 3 put: (self at: index + 5); + digitAt: 4 put: (self at: index + 4); + digitAt: 5 put: v; + normalize ]. + (v := self at: index + 4) <= 16r3F ifFalse: [ + ^(LargePositiveInteger new: 4) + digitAt: 1 put: (self at: index + 7); + digitAt: 2 put: (self at: index + 6); + digitAt: 3 put: (self at: index + 5); + digitAt: 4 put: v; + normalize ]. + ^(((v bitShift: 8) + (self at: index + 5) bitShift: 8) + (self at: index + 6) bitShift: 8) + (self at: index + 7)! - aBool ifFalse: [ - w := self at: index. - b6 := self at: index+1. - b5 := self at: index+2. - n2 := self at: index+3. - b3 := self at: index+4. - b2 := self at: index+5. - n3 := self at: index+6. - b0 := self at: index+7. - ] ifTrue: [ - b0 := self at: index. - n3 := self at: index+1. - b2 := self at: index+2. - b3 := self at: index+3. - n2 := self at: index+4. - b5 := self at: index+5. - b6 := self at: index+6. - w := self at: index+7. - ]. - - "Minimize LargeInteger arithmetic" - b6 = 0 ifFalse:[w := (b6 bitShift: 8) + w]. - b5 = 0 ifFalse:[w := (b5 bitShift: 16) + w]. - - b3 = 0 ifFalse:[n2 := (b3 bitShift: 8) + n2]. - b2 = 0 ifFalse:[n2 := (b2 bitShift: 16) + n2]. - n2 == 0 ifFalse: [w := (n2 bitShift: 24) + w]. - - b0 = 0 ifFalse:[n3 := (b0 bitShift: 8) + n3]. - n3 == 0 ifFalse: [w := (n3 bitShift: 48) + w]. - - ^w! Item was changed: ----- Method: ByteArray>>unsignedLong64At:put:bigEndian: (in category 'platform independent access') ----- + unsignedLong64At: index put: value bigEndian: bigEndian + "Store a 64-bit unsigned integer quantity starting from the given byte index" + + value isLarge + ifTrue: [ + | i size | + size := value digitLength. + bigEndian ifFalse: [ + self + replaceFrom: index + to: index + size - 1 + with: value + startingAt: 1; + replaceFrom: index + size + to: index + 7 + with: #[0 0 0 0] + startingAt: 1. + ^value ]. + i := 1. + [ i <= size ] whileTrue: [ + self at: index + 8 - i put: (value digitAt: i). + i := i + 1 ]. + [ i <= 8 ] whileTrue: [ + self at: index + 8 - i put: 0. + i := i + 1 ] ] + ifFalse: [ + bigEndian ifFalse: [ + self + at: index put: (value bitAnd: 16rFF); + at: index + 1 put: ((value bitShift: -8) bitAnd: 16rFF); + at: index + 2 put: ((value bitShift: -16) bitAnd: 16rFF); + at: index + 3 put: (value bitShift: -24); + replaceFrom: index + 4 + to: index + 7 + with: #[0 0 0 0] + startingAt: 1. + ^value ]. + self + replaceFrom: index + to: index + 3 + with: #[0 0 0 0] + startingAt: 1; + at: index + 4 put: (value bitShift: -24); + at: index + 5 put: ((value bitShift: -16) bitAnd: 16rFF); + at: index + 6 put: ((value bitShift: -8) bitAnd: 16rFF); + at: index + 7 put: (value bitAnd: 16rFF) ]. + ^value! - unsignedLong64At: index put: value bigEndian: aBool - "Minimize largeInteger arithmetic" - | ix | - aBool ifFalse: [ - ix := index - 1. - 1 to: 8 do: [:pos| - self at: ix + pos put: (value digitAt: pos) - ]. - ] ifTrue: [ - ix := index + 8. - 1 to: 8 do: [:pos| - self at: ix - pos put: (value digitAt: pos) - ]. - ]. - ^value - ! Item was changed: ----- Method: ByteArray>>unsignedLongAt:bigEndian: (in category 'platform independent access') ----- + unsignedLongAt: index bigEndian: bigEndian + "Return a 32-bit unsigned integer quantity starting from the given byte index. Use #normalize where necessary to ensure compatibility with non-30-bit SmallIntegers." + + | byte | + bigEndian ifTrue: [ + (byte := self at: index) <= 16r3F ifTrue: [ + ^(((byte bitShift: 8) + (self at: index + 1) bitShift: 8) + (self at: index + 2) bitShift: 8) + (self at: index + 3) ]. + ^(LargePositiveInteger new: 4) + digitAt: 1 put: (self at: index + 3); + digitAt: 2 put: (self at: index + 2); + digitAt: 3 put: (self at: index + 1); + digitAt: 4 put: byte; + normalize ]. + (byte := self at: index + 3) <= 16r3F ifTrue: [ + ^(((byte bitShift: 8) + (self at: index + 2) bitShift: 8) + (self at: index + 1) bitShift: 8) + (self at: index) ]. + ^(LargePositiveInteger new: 4) + replaceFrom: 1 + to: 4 + with: self + startingAt: index; + normalize + ! - unsignedLongAt: index bigEndian: aBool - "Return a 32bit unsigned integer quantity starting from the given byte index" - | b0 b1 b2 w | - aBool ifTrue:[ - b0 := self at: index. - b1 := self at: index+1. - b2 := self at: index+2. - w := self at: index+3. - ] ifFalse:[ - w := self at: index. - b2 := self at: index+1. - b1 := self at: index+2. - b0 := self at: index+3. - ]. - "Minimize LargeInteger arithmetic" - b2 = 0 ifFalse:[w := (b2 bitShift: 8) + w]. - b1 = 0 ifFalse:[w := (b1 bitShift: 16) + w]. - b0 = 0 ifFalse:[w := (b0 bitShift: 24) + w]. - ^w! Item was changed: ----- Method: ByteArray>>unsignedLongAt:put:bigEndian: (in category 'platform independent access') ----- + unsignedLongAt: index put: value bigEndian: bigEndian + "Store a 32-bit unsigned integer quantity starting from the given byte index" + + value isLarge + ifTrue: [ + bigEndian ifFalse: [ + self + replaceFrom: index + to: index + 3 + with: value + startingAt: 1. + ^value ]. + self + at: index put: (value digitAt: 4); + at: index + 1 put: (value digitAt: 3); + at: index + 2 put: (value digitAt: 2); + at: index +3 put: (value digitAt: 1) ] + ifFalse: [ + bigEndian ifFalse: [ + self + at: index put: (value bitAnd: 16rFF); + at: index + 1 put: ((value bitShift: -8) bitAnd: 16rFF); + at: index + 2 put: ((value bitShift: -16) bitAnd: 16rFF); + at: index + 3 put: (value bitShift: -24). + ^value ]. + self + at: index put: (value bitShift: -24); + at: index + 1 put: ((value bitShift: -16) bitAnd: 16rFF); + at: index + 2 put: ((value bitShift: -8) bitAnd: 16rFF); + at: index + 3 put: (value bitAnd: 16rFF) ]. - unsignedLongAt: index put: value bigEndian: aBool - "Store a 32bit unsigned integer quantity starting from the given byte index" - | b0 b1 b2 b3 | - b0 := value bitShift: -24. - b1 := (value bitShift: -16) bitAnd: 255. - b2 := (value bitShift: -8) bitAnd: 255. - b3 := value bitAnd: 255. - aBool ifTrue:[ - self at: index put: b0. - self at: index+1 put: b1. - self at: index+2 put: b2. - self at: index+3 put: b3. - ] ifFalse:[ - self at: index put: b3. - self at: index+1 put: b2. - self at: index+2 put: b1. - self at: index+3 put: b0. - ]. ^value! Item was changed: ----- Method: ByteArray>>unsignedShortAt:bigEndian: (in category 'platform independent access') ----- + unsignedShortAt: index bigEndian: bigEndian + "Return a 16-bit unsigned integer quantity starting from the given byte index" + + bigEndian ifFalse: [ ^((self at: index + 1) bitShift: 8) + (self at: index) ]. + ^((self at: index) bitShift: 8) + (self at: index + 1) + ! - unsignedShortAt: index bigEndian: aBool - "Return a 16 bit unsigned integer quantity starting from the given byte index" - ^aBool - ifTrue:[((self at: index) bitShift: 8) + (self at: index+1)] - ifFalse:[((self at: index+1) bitShift: 8) + (self at: index)].! Item was changed: ----- Method: ByteArray>>unsignedShortAt:put:bigEndian: (in category 'platform independent access') ----- + unsignedShortAt: index put: value bigEndian: bigEndian + "Store a 16-bit unsigned integer quantity starting from the given byte index" + + bigEndian ifFalse: [ + self + at: index + 1 put: (value bitShift: -8); + at: index put: (value bitAnd: 16rFF). + ^value ]. + self + at: index put: (value bitShift: -8); + at: index+1 put: (value bitAnd: 16rFF). - unsignedShortAt: index put: value bigEndian: aBool - "Store a 16 bit unsigned integer quantity starting from the given byte index" - aBool ifTrue:[ - self at: index put: (value bitShift: -8). - self at: index+1 put: (value bitAnd: 255). - ] ifFalse:[ - self at: index+1 put: (value bitShift: -8). - self at: index put: (value bitAnd: 255). - ]. ^value! |
Free forum by Nabble | Edit this page |