Skip to content

Commit 808d494

Browse files
janvranyshingarov
authored andcommitted
[Z3] Update Float >> toBitVector: to work also on Smalltalk/X
This commit updates `Float >> toBitVector:` to work on both Pharo and Smalltalk/X. The code is arguably ugly but I did not find a nicer way to do it. One cannot simply force "Pharo way" on Smalltalk/X and make `#basicAt:` behaving the same - this would break existing code. Another way would be to have some "new" compatibility method, but then, where to place it? To `Z3-FFI-Pharo` and `Z3-FFI-SmalltalkX`? Did not feel right - this has nothing to do with FFI. Yet another way would be revert back and use of `asIEEE64BitWord`, and implement this in both Smalltalk/X and "our" Pharo 8. This would break `FloatingPoint` on stock Pharo 8 (but perhaps that's okay). So in the end, I opted for this ugly "dialect" test and keep the ugliness local to this method.
1 parent 33e3380 commit 808d494

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Z3/Float.extension.st

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,29 @@ Extension { #name : #Float }
44
Float >> toBitVector: length [
55
"Return the receiver as IEEE 754 bits"
66

7-
^ ((self basicAt: 1) bitShift: 32) + (self basicAt: 2)
8-
toBitVector: length.
7+
| bits |
8+
9+
length = 64 ifFalse: [ self error: 'bit length mismatch' ].
10+
11+
"Following code is arguably ugly - but that's the cost of supporting
12+
multiple Pharo versions and Smalltalk/X."
13+
14+
((Smalltalk respondsTo: #isSmalltalkX) and: [ Smalltalk isSmalltalkX ]) ifTrue: [
15+
"This is Smalltalk/X:"
16+
bits := 0.
17+
UninterpretedBytes isBigEndian ifTrue: [
18+
1 to: (length // 8) do: [:i | bits := (bits << 8) | (self basicAt: i) ]
19+
] ifFalse: [
20+
(length // 8) downTo: 1 do: [:i | bits := (bits << 8) | (self basicAt: i) ]
21+
].
22+
] ifFalse: [
23+
"Assuming some version of Pharo:"
24+
bits := ((self basicAt: 1) bitShift: 32) + (self basicAt: 2)
25+
].
26+
27+
^ bits toBitVector: length
28+
29+
"Created: / 17-09-2025 / 12:21:38 / Jan Vrany <jan.vrany@labware.com>"
930
]
1031

1132
{ #category : #'*Z3' }

0 commit comments

Comments
 (0)