'From Pharo1.1a of ''19 October 2009'' [Latest update: #11010] on 21 October 2009 at 2:13:43 am'! !BitBltSimulation methodsFor: 'combination rules' stamp: 'HenrikSperreJohansen 10/21/2009 02:13'! partitionedAdd: word1 to: word2 nBits: nBits nPartitions: nParts "Add word1 to word2 as nParts partitions of nBits each. This is useful for packed pixels, or packed colors" | mask sum result | mask := maskTable at: nBits. "partition mask starts at the right" result := 0. 1 to: nParts do: [:i | i = nParts ifFalse: [ sum := (word1 bitAnd: mask) + (word2 bitAnd: mask). sum <= mask "result must not carry out of partition" ifTrue: [result := result bitOr: sum] ifFalse: [result := result bitOr: mask]. mask := mask << nBits "slide left to next partition"] "Avoid overflow when nBits*nParts = sizeOf: Integer" ifTrue: [|w1 w2| w1 := (word1 bitAnd: mask) >> nBits. w2 := (word2 bitAnd: mask) >> nBits. sum := w1 + w2. sum <= (mask >> nBits) "result must not carry out of partition" ifTrue: [result := result bitOr: (sum <