-
Notifications
You must be signed in to change notification settings - Fork 65
/
LittleEndianBitmap.class.st
381 lines (358 loc) · 14 KB
/
LittleEndianBitmap.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
Class {
#name : #LittleEndianBitmap,
#superclass : #Bitmap,
#type : #words,
#category : #'VMMaker-JITSimulation'
}
{ #category : #converting }
LittleEndianBitmap >> asByteArray [
"Faster way to make a byte array from me.
copyFromByteArray: makes equal Bitmap."
| f bytes hack |
EndianDetector isLittleEndian ifTrue:
[bytes := self shallowCopy.
ByteArray adoptInstance: bytes.
^bytes].
f := Form extent: 4@self size depth: 8 bits: self.
bytes := ByteArray new: self size * 4.
hack := Form new hackBits: bytes.
hack swapEndianness.
hack copyBits: f boundingBox
from: f
at: 0@0
clippingBox: hack boundingBox
rule: Form over
fillColor: nil
map: nil.
"f displayOn: hack."
^bytes
"| v | v := LittleEndianBitmap withAll: (1 to: 4).
[v asByteArray] bench '19,700,000 per second. 50.8 nanoseconds per run.'"
"| v | v := Bitmap withAll: (1 to: 4).
[v asByteArray] bench '977,000 per second. 1.02 microseconds per run.'"
]
{ #category : #accessing }
LittleEndianBitmap >> byteAt: byteAddress [
"Extract a byte from a Bitmap (little-endian version)"
| lowBits |
lowBits := byteAddress - 1 bitAnd: 3.
^((self at: byteAddress - 1 - lowBits // 4 + 1)
bitShift: lowBits * -8)
bitAnd: 16rFF
]
{ #category : #accessing }
LittleEndianBitmap >> byteAt: byteAddress put: byte [
"Insert a byte into a Bitmap (little-endian version). N.B. Signedness will be lost since byteAt: answers non-negative values.
We can add a signedByteAt: if needed."
| value longWord shift lowBits longAddr |
(byte < 0 or:[byte > 255]) ifTrue:[^self errorImproperStore].
value := byte < 0
ifTrue: [byte < 128 ifTrue:
[self errorImproperStore].
16rFF bitAnd: byte]
ifFalse: [16rFF < byte ifTrue:
[self errorImproperStore].
byte].
lowBits := byteAddress - 1 bitAnd: 3.
longWord := self at: (longAddr := (byteAddress - 1 - lowBits) // 4 + 1).
shift := lowBits * 8.
longWord := longWord
- (longWord bitAnd: (16rFF bitShift: shift))
+ (value bitShift: shift).
self at: longAddr put: longWord.
^byte
]
{ #category : #accessing }
LittleEndianBitmap >> long64At: byteAddress [
| lowBits hiWord loWord midWord mask wordIndex result signBit |
wordIndex := byteAddress - 1 // 4 + 1.
(lowBits := byteAddress - 1 \\ 4) = 0 ifTrue:
[loWord := self at: wordIndex.
hiWord := self at: wordIndex + 1.
^hiWord = 0
ifTrue: [loWord]
ifFalse: [(hiWord signedIntFromLong bitShift: 32) + loWord]].
"There will always be three accesses; two partial words and a full word in the middle"
mask := 16rFFFFFFFF bitShift: 4 - lowBits * -8.
loWord := (self at: wordIndex) bitAnd: mask bitInvert32.
midWord := self at: wordIndex + 1.
hiWord := (self at: wordIndex + 2) bitAnd: mask.
result := loWord bitShift: lowBits * -8.
midWord ~= 0 ifTrue:
[result := result + (midWord bitShift: (4 - lowBits * 8))].
hiWord ~= 0 ifTrue:
[signBit := 1 << (lowBits * 8 - 1).
(signBit anyMask: hiWord) ifTrue:
[hiWord := hiWord - signBit - signBit].
result := result + (hiWord bitShift: (4 - lowBits + 4 * 8))].
^result
]
{ #category : #accessing }
LittleEndianBitmap >> long64At: byteIndex put: aValue [
| lowBits mask wordIndex |
(lowBits := byteIndex - 1 \\ 4) = 0 ifTrue:
[self "N.B. Do the access that can fail first, before altering the receiver"
longAt: byteIndex + 4 put: (aValue bitShift: -32);
unsignedLongAt: byteIndex put: (aValue bitAnd: 16rffffffff).
^aValue].
"There will always be three accesses; two partial words and a full word in the middle"
wordIndex := byteIndex - 1 // 4 + 1.
aValue < 0
ifTrue:
[(aValue bitShift: -32) < -2147483648 ifTrue:
[^self errorImproperStore]]
ifFalse:
[16r7FFFFFFF < (aValue bitShift: -32) ifTrue:
[^self errorImproperStore]].
mask := 16rFFFFFFFF bitShift: 4 - lowBits * -8.
self at: wordIndex put: (((self at: wordIndex) bitAnd: mask) bitXor: ((aValue bitShift: lowBits * 8) bitAnd: mask bitInvert32)).
self at: wordIndex + 1 put: ((aValue bitShift: 4 - lowBits * -8) bitAnd: 16rFFFFFFFF).
self at: wordIndex + 2 put: (((self at: wordIndex + 2) bitAnd: mask bitInvert32) bitXor: ((aValue bitShift: 4 - lowBits + 4 * -8) bitAnd: mask)).
^aValue
]
{ #category : #accessing }
LittleEndianBitmap >> longAt: byteIndex [
^self longAt: byteIndex bigEndian: false
]
{ #category : #accessing }
LittleEndianBitmap >> longAt: byteIndex bigEndian: bigEndian [
"Compatibility with the ByteArray method of the same name."
| lowBits wordIndex value word0 word1 |
lowBits := byteIndex - 1 bitAnd: 3.
wordIndex := byteIndex - 1 // 4 + 1.
lowBits = 0
ifTrue:
[value := self at: wordIndex]
ifFalse:
[word0 := self at: wordIndex.
word1 := self at: wordIndex + 1.
value := 16rFFFFFFFF bitAnd: (word0 bitShift: lowBits * -8) + (word1 bitShift: 4 - lowBits * 8)].
bigEndian ifTrue:
[value := ((value bitShift: -24) bitAnd: 16rFF)
+ ((value bitShift: -8) bitAnd: 16rFF00)
+ ((value bitAnd: 16rFF00) bitShift: 8)
+ ((value bitAnd: 16rFF) bitShift: 24)].
(16r80000000 bitAnd: value) ~= 0 ifTrue:
[value := (16r7FFFFFFF bitAnd: value) - 16r80000000].
^value
"| bm ba |
(bm := LittleEndianBitmap new: 4)
at: 1 put: 16r01234567;
at: 2 put: 16r89ABCDEF;
at: 3 put: 16r89ABCDEF;
at: 4 put: 16r01234567.
ba := bm asByteArray.
(((1 to: 5), (9 to: 13) select:
[:bi| (bm longAt: bi bigEndian: true) ~= (ba longAt: bi bigEndian: true)]) collect:
[:i| { i. (bm longAt: i bigEndian: true) hex. (ba longAt: i bigEndian: true) hex}]),
(((1 to: 5), (9 to: 13) select:
[:bi| (bm longAt: bi bigEndian: false) ~= (ba longAt: bi bigEndian: false)]) collect:
[:i| { i. (bm longAt: i bigEndian: false) hex. (ba longAt: i bigEndian: false) hex}])"
]
{ #category : #accessing }
LittleEndianBitmap >> longAt: byteIndex put: aValue [
"Compatibility with the ByteArray method of the same name."
| lowBits wordIndex mask |
lowBits := byteIndex - 1 bitAnd: 3.
wordIndex := byteIndex - 1 // 4 + 1.
aValue < 0
ifTrue:
[aValue < -2147483648 ifTrue:
[^self errorImproperStore]]
ifFalse:
[16r7FFFFFFF < aValue ifTrue:
[^self errorImproperStore]].
lowBits = 0 ifTrue:
[self at: wordIndex put: (16rFFFFFFFF bitAnd: aValue).
^aValue].
mask := 16rFFFFFFFF bitShift: 4 - lowBits * -8.
self at: wordIndex put: (((self at: wordIndex) bitAnd: mask) bitXor: ((aValue bitShift: lowBits * 8) bitAnd: mask bitInvert32)).
self at: wordIndex + 1 put: (((self at: wordIndex + 1) bitAnd: mask bitInvert32) bitXor: ((aValue bitShift: 4 - lowBits * -8) bitAnd: mask)).
^aValue
"(1 to: 8) collect:
[:ba| | bm |
bm := LittleEndianBitmap new: 4.
bm at: 1 put: 16r55555555.
bm at: 2 put: 16rAAAAAAAA.
bm longAt: ba put: 16r04030201.
{ (bm at: 1) hex. (bm at: 2) hex }]"
]
{ #category : #accessing }
LittleEndianBitmap >> longAt: byteIndex put: aValue bigEndian: bigEndian [
"Compatibility with the ByteArray method of the same name."
| lowBits wordIndex value mask |
lowBits := byteIndex - 1 bitAnd: 3.
wordIndex := byteIndex - 1 // 4 + 1.
value := aValue < 0
ifTrue: [16rFFFFFFFF bitAnd: aValue]
ifFalse: [16rFFFFFFFF < aValue ifTrue:
[self errorImproperStore].
aValue].
bigEndian ifTrue:
[value := ((value bitShift: -24) bitAnd: 16rFF)
+ ((value bitShift: -8) bitAnd: 16rFF00)
+ ((value bitAnd: 16rFF00) bitShift: 8)
+ ((value bitAnd: 16rFF) bitShift: 24)].
lowBits = 0 ifTrue:
[self at: wordIndex put: value.
^aValue].
mask := 16rFFFFFFFF bitShift: lowBits * -8.
self at: wordIndex put: (((self at: wordIndex) bitAnd: mask) bitXor: ((value bitShift: lowBits * 8) bitAnd: mask bitInvert32)).
self at: wordIndex + 1 put: (((self at: wordIndex + 1) bitAnd: mask bitInvert32) bitXor: ((value bitShift: 4 - lowBits * -8) bitAnd: mask)).
^aValue
"(1 to: 8) collect:
[:ba| | bm |
bm := LittleEndianBitmap new: 4.
bm at: 1 put: 16r55555555.
bm at: 2 put: 16rAAAAAAAA.
bm longAt: ba put: 16r04030201 bigEndian: false.
{ (bm at: 1) hex. (bm at: 2) hex }]"
"(1 to: 8) collect:
[:ba| | bm |
bm := LittleEndianBitmap new: 4.
bm at: 1 put: 16r55555555.
bm at: 2 put: 16rAAAAAAAA.
bm longAt: ba put: 16r01020304 bigEndian: true.
{ (bm at: 1) hex. (bm at: 2) hex }]"
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedByteAt: byteAddress [
"Extract a byte from a Bitmap (little-endian version)"
| lowBits |
lowBits := byteAddress - 1 bitAnd: 3.
^((self at: byteAddress - 1 - lowBits // 4 + 1)
bitShift: lowBits * -8)
bitAnd: 16rFF
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedByteAt: byteAddress put: byte [
"Insert a byte into a Bitmap (little-endian version)"
| longWord shift lowBits longAddr |
(byte < 0 or:[byte > 255]) ifTrue:[^self errorImproperStore].
lowBits := byteAddress - 1 bitAnd: 3.
longWord := self at: (longAddr := (byteAddress - 1 - lowBits) // 4 + 1).
shift := lowBits * 8.
longWord := longWord
- (longWord bitAnd: (16rFF bitShift: shift))
+ (byte bitShift: shift).
self at: longAddr put: longWord.
^byte
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedLong64At: byteAddress [
"memory is a Bitmap, a 32-bit indexable array of bits"
| lowBits hiWord loWord midWord mask wordIndex result |
wordIndex := byteAddress - 1 // 4 + 1.
(lowBits := byteAddress - 1 \\ 4) = 0 ifTrue:
[loWord := self at: wordIndex.
hiWord := self at: wordIndex + 1.
^hiWord = 0
ifTrue: [loWord]
ifFalse: [(hiWord bitShift: 32) + loWord]].
"There will always be three accesses; two partial words and a full word in the middle"
mask := 16rFFFFFFFF bitShift: 4 - lowBits * -8.
loWord := (self at: wordIndex) bitAnd: mask bitInvert32.
midWord := self at: wordIndex + 1.
hiWord := (self at: wordIndex + 2) bitAnd: mask.
result := loWord bitShift: lowBits * -8.
midWord ~= 0 ifTrue:
[result := result + (midWord bitShift: (4 - lowBits * 8))].
hiWord ~= 0 ifTrue:
[result := result + (hiWord bitShift: (4 - lowBits + 4 * 8))].
^result
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedLong64At: byteIndex put: aValue [
| lowBits mask wordIndex |
(lowBits := byteIndex - 1 \\ 4) = 0 ifTrue:
[self "N.B. Do the access that can fail first, before altering the receiver"
unsignedLongAt: byteIndex + 4 put: (aValue bitShift: -32);
unsignedLongAt: byteIndex put: (aValue bitAnd: 16rffffffff).
^aValue].
"There will always be three accesses; two partial words and a full word in the middle"
wordIndex := byteIndex - 1 // 4 + 1.
(aValue bitShift: -64) ~= 0 ifTrue:
[^self errorImproperStore].
mask := 16rFFFFFFFF bitShift: 4 - lowBits * -8.
self at: wordIndex put: (((self at: wordIndex) bitAnd: mask) bitXor: ((aValue bitShift: lowBits * 8) bitAnd: mask bitInvert32)).
self at: wordIndex + 1 put: ((aValue bitShift: 4 - lowBits * -8) bitAnd: 16rFFFFFFFF).
self at: wordIndex + 2 put: (((self at: wordIndex + 2) bitAnd: mask bitInvert32) bitXor: ((aValue bitShift: 4 - lowBits + 4 * -8) bitAnd: mask)).
^aValue
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedLongAt: byteIndex [
"Compatiblity with the ByteArray & Alien methods of the same name."
^(byteIndex - 1 bitAnd: 3) = 0
ifTrue: [self at: byteIndex - 1 // 4 + 1]
ifFalse: [self unsignedLongAt: byteIndex bigEndian: false]
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedLongAt: byteIndex bigEndian: bigEndian [
"Compatiblity with the ByteArray method of the same name."
| lowBits wordIndex value word0 word1 |
lowBits := byteIndex - 1 bitAnd: 3.
wordIndex := byteIndex - 1 // 4 + 1.
lowBits = 0
ifTrue:
[value := self at: wordIndex]
ifFalse:
[word0 := self at: wordIndex.
word1 := self at: wordIndex + 1.
value := 16rFFFFFFFF bitAnd: (word0 bitShift: lowBits * -8) + (word1 bitShift: 4 - lowBits * 8)].
bigEndian ifTrue:
[value := ((value bitShift: -24) bitAnd: 16rFF)
+ ((value bitShift: -8) bitAnd: 16rFF00)
+ ((value bitAnd: 16rFF00) bitShift: 8)
+ ((value bitAnd: 16rFF) bitShift: 24)].
^value
"| bm ba |
(bm := LittleEndianBitmap new: 4)
at: 1 put: 16r01234567;
at: 2 put: 16r89ABCDEF;
at: 3 put: 16r89ABCDEF;
at: 4 put: 16r01234567.
ba := bm asByteArray.
(((1 to: 5), (9 to: 13) select:
[:bi| (bm unsignedLongAt: bi bigEndian: true) ~= (ba unsignedLongAt: bi bigEndian: true)]) collect:
[:i| { i. (bm unsignedLongAt: i bigEndian: true) hex. (ba unsignedLongAt: i bigEndian: true) hex}]),
(((1 to: 5), (9 to: 13) select:
[:bi| (bm unsignedLongAt: bi bigEndian: false) ~= (ba unsignedLongAt: bi bigEndian: false)]) collect:
[:i| { i. (bm unsignedLongAt: i bigEndian: false) hex. (ba unsignedLongAt: i bigEndian: false) hex}])"
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedLongAt: byteIndex put: aValue [
"Compatiblity with the ByteArray & Alien methods of the same name."
| lowBits mask wordIndex |
wordIndex := byteIndex - 1 // 4 + 1.
(lowBits := byteIndex - 1 bitAnd: 3) = 0 ifTrue:
[^self at: wordIndex put: aValue].
(aValue bitShift: -32) = 0 ifFalse:
[^self errorImproperStore].
mask := 16rFFFFFFFF bitShift: 4 - lowBits * -8.
self at: wordIndex put: (((self at: wordIndex) bitAnd: mask) bitXor: ((aValue bitShift: lowBits * 8) bitAnd: mask bitInvert32)).
self at: wordIndex + 1 put: (((self at: wordIndex + 1) bitAnd: mask bitInvert32) bitXor: (16rFFFFFFFF bitAnd: ((aValue bitShift: 4 - lowBits * -8) bitAnd: mask))).
^aValue
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedShortAt: byteIndex [
"Compatiblity with the ByteArray & Alien methods of the same name."
| zi |
^((zi := byteIndex - 1) bitAnd: 1) = 0
ifTrue: [(zi bitAnd: 2) = 0
ifTrue: [(self at: zi // 4 + 1) bitAnd: 16rFFFF]
ifFalse: [(self at: zi - 2 // 4 + 1) bitShift: -16]]
ifFalse: [(self unsignedLongAt: byteIndex bigEndian: false) bitAnd: 16rFFFF]
]
{ #category : #accessing }
LittleEndianBitmap >> unsignedShortAt: byteIndex put: aValue [
"Compatiblity with the ByteArray & Alien methods of the same name."
| zi |
(aValue < 0 or: [aValue > 16rFFFF]) ifTrue:
[^self errorImproperStore].
((zi := byteIndex - 1) bitAnd: 1) = 0
ifTrue: [(zi bitAnd: 2) = 0
ifTrue: [self at: (zi := zi // 4 + 1) put: (16rFFFF0000 bitAnd: (self at: zi)) + aValue]
ifFalse: [self at: (zi := zi - 2 // 4 + 1) put: (16rFFFF bitAnd: (self at: zi)) + (aValue bitShift: 16)]]
ifFalse: [self notYetImplemented].
^aValue
]