Cuis-Smalltalk / Cuis-Smalltalk-Dev

Active development of Cuis Smalltalk
MIT License
435 stars 70 forks source link

The "SmallFloat64>>exponent" method without the 553 primitive runs in an infinite loop #182

Closed aranega closed 3 years ago

aranega commented 3 years ago

Hi,

While reading and decoding/executing bytecodes of the image without some primitives, I ran into an infinite loop for SmallFloat64 with the exponent method. Where there is not the primitive number 553, the SmallFloat64>>exponent method fallback on this code

exponent
    | temp1 |
    self >= 1.0 ifTrue: [ ^ self floorLog: 2 ].   "<-- Here is the issue"
    self > 0.0 ifTrue: [
        temp1 _ (1.0 / self) exponent.
        self = (1.0 / (1.0 timesTwoPower: temp1)) ifTrue: [ ^ temp1 negated ].
        ^ temp1 negated - 1 ].
    self = 0.0 ifTrue: [ ^ -1 ].
    ^ self negated exponent.

When running 10.0 exponent, the Float>>floorLog: 2 method is executed:

floorLog: arg1
    (arg1 = 2 and: [
        self > 0.0 and: [ self isFinite ]]) ifTrue: [ ^ self exponent ].  "<-- infinite recursive call here"
    ^ (self log: arg1) floor.

askind again for the exponent to 10.0. I know the primitive is not supposed to fail, but just in case. I think I fixed it changing Float>>floorLog: for:

floorLog: arg1
    (arg1 = 2 and: [
        self > 0.0 and: [ self isFinite ]]) ifTrue: [ ^ self exponentPart ].  "<-- exponendPart instead of exponent"
    ^ (self log: arg1) floor.

but I'm unsure it will produce the right result, on some examples it seems it does (I didn't find tests).

EDIT> Nope, the modification I made does not solve nothing, the behavior is different

nicolas-cellier-aka-nice commented 3 years ago

It should be the same, except for the case of denormalized numbers (gradual underflow) - that is when exponentBits is zero. Pick the exponentFromBitPattern from Squeak. In Cuis, use the partBits: method, it will be much nicer.

nicolas-cellier-aka-nice commented 3 years ago

https://source.squeak.org/trunk/Kernel-nice.694.diff

jvuletich commented 3 years ago

Thanks Folks. This is fixed now (in Cuis code style) at #4530

nachoba commented 3 years ago

Hi I see that there are some packages that fail to load due to the fact that "SqueakCompatibility" was deleted from the repo. [image: Capture2.PNG]

For instance, when I try to load Erudite I get: [image: Capture.PNG]

Fortunately I had a copy of that package and now Erudite works. cheers

Ignacio Sniechowski

On Tue, Feb 9, 2021 at 2:33 PM Juan Vuletich @.***> wrote:

Closed #182 https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev/issues/182.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev/issues/182#event-4310470610, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA23RWQUKGBG3TY57MBW7MTS6FWWNANCNFSM4UOS22FA .