<div dir="ltr">Hi All,<div><br></div><div>    I recently eliminated the optimization in Slang that replaces a division by a power of two with a shift, because the code cast the argument to signed, and hence broke unsigned division.  That&#39;s what used to be controlled by the UseRightShiftForDivide class var of CCodeGenerator.</div>
<div><br></div><div>Yesterday I found out that that optimization is the only thing that&#39;s keeping the LargeIntegers plugin afloat.  To whit:</div><div><br></div><div>LargeIntegersPlugin&gt;&gt;cDigitSub: pByteSmall<div>
<span class="" style="white-space:pre">                </span>len: smallLen</div><div><span class="" style="white-space:pre">                </span>with: pByteLarge</div><div><span class="" style="white-space:pre">                </span>len: largeLen</div><div><span class="" style="white-space:pre">                </span>into: pByteRes</div>
<div><span class="" style="white-space:pre">        </span>| z limit |</div><div><span class="" style="white-space:pre">        </span>&lt;var: #pByteSmall type: &#39;unsigned char * &#39;&gt;</div><div><span class="" style="white-space:pre">        </span>&lt;var: #pByteLarge type: &#39;unsigned char * &#39;&gt;</div>
<div><span class="" style="white-space:pre">        </span>&lt;var: #pByteRes type: &#39;unsigned char * &#39;&gt;</div><div><br></div><div><span class="" style="white-space:pre">        </span>z := 0.</div><div><span class="" style="white-space:pre">        </span>&quot;Loop invariant is -1&lt;=z&lt;=1&quot;</div>
<div><span class="" style="white-space:pre">        </span>limit := smallLen - 1.</div><div><span class="" style="white-space:pre">        </span>0 to: limit do: </div><div><span class="" style="white-space:pre">                </span>[:i | </div><div>
<span class="" style="white-space:pre">                </span>z := z + (pByteLarge at: i) - (pByteSmall at: i).</div><div><span class="" style="white-space:pre">                </span>pByteRes at: i put: z - (z // 256 * 256).</div><div><span class="" style="white-space:pre">                </span>&quot;sign-tolerant form of (z bitAnd: 255)&quot;</div>
<div><span class="" style="white-space:pre">                </span>z := z // 256].</div><div><span class="" style="white-space:pre">        </span>limit := largeLen - 1.</div><div><span class="" style="white-space:pre">        </span>smallLen to: limit do: </div>
<div><span class="" style="white-space:pre">                </span>[:i | </div><div><span class="" style="white-space:pre">                </span>z := z + (pByteLarge at: i) .</div><div><span class="" style="white-space:pre">                </span>pByteRes at: i put: z - (z // 256 * 256).</div>
<div><span class="" style="white-space:pre">                </span>&quot;sign-tolerant form of (z bitAnd: 255)&quot;</div><div><span class="" style="white-space:pre">                </span>z := z // 256].</div><div><br></div><div>The &quot;z := z // 256&quot;&#39;s at the end of the loops were being generated as</div>
<div><div>        z = ((sqInt) z) &gt;&gt; 8;</div><div> which is essential for the signed arithmetic implicit in &quot;z := z + (pByteLarge at: i) - (pByteSmall at: i)&quot; to work.</div></div><div><br></div><div>So what&#39;s the right thing to do?</div>
<div><br></div><div>In C -1 // 256 = 0, but in Smalltalk -1 // 256 = -1 (// rounds towards - infinity), whereas  (-1 quo: 256) = 0 (quo: rounds towards 0).</div><div><br></div><div>I could modify the code generator to generate Smalltalk semantics for //, but its not pretty (one has to check signedness, check if there&#39;s a remainder, etc).</div>
<div><br></div><div>What I&#39;d like is to have a signed bitShift:.  Wait you say, bitShift: is signed.  Ah, but the code generator generates unsigned shifts for all bitShift:&#39;s !!!!.</div><div><br></div><div>So some ideas:</div>
<div><br></div><div>1. change bitShift: to obey the type of the receiver (Slang allows one to type variables, defaulting to a singed long). This is my preference, but it risks breaking a good handful of negative bitShift: uses in plugins (which is where I&#39;m worried about regressions).</div>
<div><br></div><div>2. change bitShift: to obey explicit casts, generating a signed shift for </div><div>   foo asInteger bitShift: expr</div><div>   (self cCoerceSimple: #foo to: #sqInt) bitShift: expr</div><div>Seriously?!?! this stinks.</div>
<div><br></div><div>3. write</div><div><span style="white-space:pre">        </span>z := self cCode: [z &gt;&gt;= 8] inSmalltalk: [z // 256]</div><br class="">Seriously?!?! this stinks too.</div><div><br><div>Anything else that makes any sense?</div>
-- <br>best,<div>Eliot</div>
</div></div>