[Cuis-dev] Curious bug

Juan Vuletich juan at jvuletich.org
Wed Jun 24 08:36:10 PDT 2020


On 6/24/2020 11:34 AM, Luciano Notarfrancesco via Cuis-dev wrote:
> But that means that at some point a bit operation (bitShift, bitOr, 
> bitAnd) answers a LargeInteger that actually fits in a SmallInteger, 
> right? That should never happen, because for example equality fails.
>
> On Wed, 24 Jun 2020 at 9:28 PM, Juan Vuletich <juan at jvuletich.org 
> <mailto:juan at jvuletich.org>> wrote:
>
>     On 6/23/2020 10:58 AM, Luciano Notarfrancesco via Cuis-dev wrote:
>>     Sorry, should be whileTrue there...
>>
>>     On Tue, 23 Jun 2020 at 8:57 PM, Luciano Notarfrancesco
>>     <luchiano at gmail.com <mailto:luchiano at gmail.com>> wrote:
>>
>>         Try this in a workspace:
>>
>>         [a _ (1 << 60) atRandom.
>>         b _ (a bitReverse: 64) bitReverse: 64.
>>         a = b] whileFalse
>>
>>         I tested it in the latest image with a recent cog spur x64
>>         VM. The loop stops with a ‘b’ that is a LargePositiveInteger
>>         but it actually fits in a SmallInteger and it is equal to
>>         ‘a’. I don’t have time to debug it until the weekend but I
>>         thought someone might find it amusing.
>>
>
>     Well, this was fun!
>
>     When you find the problem (the primitive in #bitShiftMagnitude:
>     answers the receiver if the argument is zero, without reducing it
>     to SmallInteger as appropriate), and you try to fix it, then, when
>     you want to write a test that fails without the fix, you can not
>     make it fail!
>
>     The problem only happens when #bitReverse: gets jitted by the Cog
>     VM, and that requires a few runs to happen. Hence, the test loops
>     a few times, doing exactly the same.
>
>     Please take a look.
>
>     Cheers,
>
>     -- 
>     Juan Vuletich
>     www.cuis-smalltalk.org  <http://www.cuis-smalltalk.org>
>     https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev
>     https://github.com/jvuletich
>     https://www.linkedin.com/in/juan-vuletich-75611b3
>     @JuanVuletich
>

Take a look at the code!

In LargePositiveInteger >> bitReverse: it builds a LargePositiveInteger. 
Before answering the result, it calls #bitShiftMagnitude: . As I told 
above, the primitive in #bitShiftMagnitude: answers the receiver if the 
argument is zero, without reducing it to SmallInteger as appropriate. 
That's not unreasonable. #bitShiftMagnitude: can assume the receiver is 
a sane instance. The fix I included is to call #normalize as 
appropriate, instead of calling #bitShift: when the argument is zero.

-- 
Juan Vuletich
www.cuis-smalltalk.org
https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev
https://github.com/jvuletich
https://www.linkedin.com/in/juan-vuletich-75611b3
@JuanVuletich

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20200624/19519e06/attachment-0001.htm>


More information about the Cuis-dev mailing list