<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    On 6/24/2020 11:34 AM, Luciano Notarfrancesco via Cuis-dev wrote:
    <blockquote
cite="mid:CAL5GDyou3Lo-2xEG1N+9NxBVnbh0SoQnW6v1yrTdhGN2fnFnwg@mail.gmail.com"
      type="cite">
      <div>
        <div dir="auto">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.</div>
      </div>
      <div><br>
        <div class="gmail_quote">
          <div dir="ltr" class="gmail_attr">On Wed, 24 Jun 2020 at 9:28
            PM, Juan Vuletich <<a moz-do-not-send="true"
              href="mailto:juan@jvuletich.org">juan@jvuletich.org</a>>
            wrote:<br>
          </div>
          <blockquote class="gmail_quote" style="margin: 0px 0px 0px
            0.8ex; border-left: 1px solid rgb(204, 204, 204);
            padding-left: 1ex;">
            <div bgcolor="#ffffff" text="#000000"> On 6/23/2020 10:58
              AM, Luciano Notarfrancesco via Cuis-dev wrote:
              <blockquote type="cite">
                <div>
                  <div dir="auto">Sorry, should be whileTrue there...</div>
                </div>
                <div><br>
                  <div class="gmail_quote">
                    <div dir="ltr" class="gmail_attr">On Tue, 23 Jun
                      2020 at 8:57 PM, Luciano Notarfrancesco <<a
                        moz-do-not-send="true"
                        href="mailto:luchiano@gmail.com" target="_blank">luchiano@gmail.com</a>>

                      wrote:<br>
                    </div>
                    <blockquote class="gmail_quote" style="margin: 0px
                      0px 0px 0.8ex; border-left: 1px solid rgb(204,
                      204, 204); padding-left: 1ex;">
                      <div dir="auto">Try this in a workspace:</div>
                      <div dir="auto"><br>
                      </div>
                      <div dir="auto">[a _ (1 << 60) atRandom.</div>
                      <div dir="auto">b _ (a bitReverse: 64) bitReverse:
                        64.</div>
                      <div dir="auto">a = b] whileFalse</div>
                      <div dir="auto"><br>
                      </div>
                      <div dir="auto">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.</div>
                    </blockquote>
                  </div>
                </div>
              </blockquote>
              <br>
            </div>
            <div bgcolor="#ffffff" text="#000000"> Well, this was fun!<br>
              <br>
              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!<br>
              <br>
              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.<br>
              <br>
              Please take a look.<br>
              <br>
              Cheers,<br>
              <pre style="font-family: monospace;" cols="72">-- 
Juan Vuletich
<a moz-do-not-send="true" href="http://www.cuis-smalltalk.org" target="_blank" style="font-family: monospace;">www.cuis-smalltalk.org</a>
<a moz-do-not-send="true" href="https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev" target="_blank" style="font-family: monospace;">https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev</a>
<a moz-do-not-send="true" href="https://github.com/jvuletich" target="_blank" style="font-family: monospace;">https://github.com/jvuletich</a>
<a moz-do-not-send="true" href="https://www.linkedin.com/in/juan-vuletich-75611b3" target="_blank" style="font-family: monospace;">https://www.linkedin.com/in/juan-vuletich-75611b3</a>
@JuanVuletich</pre>
            </div>
          </blockquote>
        </div>
      </div>
    </blockquote>
    <br>
    Take a look at the code!<br>
    <br>
    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.<br>
    <br>
    <pre class="moz-signature" cols="72">-- 
Juan Vuletich
<a class="moz-txt-link-abbreviated" href="http://www.cuis-smalltalk.org">www.cuis-smalltalk.org</a>
<a class="moz-txt-link-freetext" href="https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev">https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev</a>
<a class="moz-txt-link-freetext" href="https://github.com/jvuletich">https://github.com/jvuletich</a>
<a class="moz-txt-link-freetext" href="https://www.linkedin.com/in/juan-vuletich-75611b3">https://www.linkedin.com/in/juan-vuletich-75611b3</a>
@JuanVuletich</pre>
  </body>
</html>