<div dir="ltr">Fine by me</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 24, 2016 at 10:10 PM, Eliot Miranda <span dir="ltr">&lt;<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Dave,<br>
<br>
    can we give Nicolas write permission to the svn repository please?  At least the cog branch but I think he&#39;s to be trusted elsewhere too.  Do others agree?<br>
<br>
_,,,^..^,,,_ (phone)<br>
<br>
&gt; On Mar 24, 2016, at 4:28 PM, Nicolas Cellier &lt;<a href="mailto:nicolas.cellier.aka.nice@gmail.com">nicolas.cellier.aka.nice@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hi,<br>
&gt;<br>
&gt; abs(sqInt) is a problem on 64bits system.<br>
&gt; Because sqInt is a long, this should be labs(sqInt).<br>
&gt;<br>
&gt; Well, as long as we only use 32 bits out of the 64, AND restrict ourself to little endian, this wrong code apparently works.<br>
&gt; But it&#39;s so fragile:<br>
&gt;<br>
&gt; #cat &gt; test_abs.c &lt;&lt;END<br>
&gt; #include &lt;stdio.h&gt;<br>
&gt; int main() {<br>
&gt;   long long x=(1LL &lt;&lt; 33) - 1;<br>
&gt;   long long y = abs(x);<br>
&gt;   printf(&quot;x=%lld abs(x)=%lld\n&quot;,x,y);<br>
&gt; }<br>
&gt; END<br>
&gt;<br>
&gt; #clang test_abs.c<br>
&gt; test_abs.c:4:17: warning: implicitly declaring library function &#39;abs&#39; with type &#39;int (int)&#39;<br>
&gt;       [-Wimplicit-function-declaration]<br>
&gt;   long long y = abs(x);<br>
&gt;                 ^<br>
&gt; test_abs.c:4:17: note: include the header &lt;stdlib.h&gt; or explicitly provide a declaration for &#39;abs&#39;<br>
&gt; test_abs.c:4:17: warning: absolute value function &#39;abs&#39; given an argument of type &#39;long long&#39; but has parameter of type<br>
&gt;       &#39;int&#39; which may cause truncation of value [-Wabsolute-value]<br>
&gt;   long long y = abs(x);<br>
&gt;                 ^<br>
&gt; test_abs.c:4:17: note: use function &#39;llabs&#39; instead<br>
&gt;   long long y = abs(x);<br>
&gt;                 ^~~<br>
&gt;                 llabs<br>
&gt; test_abs.c:4:17: note: include the header &lt;stdlib.h&gt; or explicitly provide a declaration for &#39;llabs&#39;<br>
&gt;<br>
&gt; #./a.out<br>
&gt; x=8589934591 abs(x)=1<br>
&gt;<br>
&gt;<br>
&gt; So we can think of adding a #generateAbs:on:indent:<br>
&gt; and handle the #typeFor:in:<br>
&gt;<br>
&gt; BUT: sqInt is either defined as int or long depending on the target:<br>
&gt;<br>
&gt; #if defined(SQ_IMAGE32)<br>
&gt;   typedef int        sqInt;<br>
&gt;   typedef unsigned int    usqInt;<br>
&gt; #elif defined(SQ_HOST64)<br>
&gt;   typedef long        sqInt;<br>
&gt;   typedef unsigned long    usqInt;<br>
&gt; #elif (SIZEOF_LONG_LONG != 8)<br>
&gt; #   error long long integers are not 64-bits wide?<br>
&gt; #else<br>
&gt;   typedef long long        sqInt;<br>
&gt;   typedef unsigned long long    usqInt;<br>
&gt; #endif<br>
&gt;<br>
&gt; Here, I propose 3 solutions:<br>
&gt; 1) since sqInt=int=long in 32bits spur, and sqInt=long in 64bits spur we can cheat:<br>
&gt;     sqLong &#39;long long&#39; int64_t __int64 -&gt; #llabs<br>
&gt;     sqInt long -&gt; #lasb<br>
&gt;     int int32_t __int32 -&gt; #abs<br>
&gt;     it&#39;s not straight and may still generate compiler warnings, which should be avoided<br>
&gt; 2) testing the wordSize, and branching in the generator:<br>
&gt;     is64bitsVM := vmClass notNil and: [vmClass objectMemoryClass wordSize = 8].<br>
&gt;     Ah no, we currently generate single source for src/plugins in 32 and 64bits flavour,<br>
&gt;     that ain&#39;t gonna work, or we should differentiate the plugin generation<br>
&gt; 3) generate a macro SQABS for sqInt and define it in sqMemoryAccess.h, same of sqLong<br>
&gt;     sqInt -&gt; SQABS<br>
&gt;     sqLong -&gt; SQLABS<br>
&gt;<br>
&gt; The correct solution is 3), but I have no commit right to svn, so someone should do it.<br>
&gt; I&#39;m attaching my own sqMemoryAccess.h copy, but please note that it has other improvments like<br>
&gt; - avoiding un-strict pointer aliasing via memcpy for fetching/storing float (except swapper in BIGENDIAN branch)<br>
&gt; - providing some native unsigned memory access<br>
&gt;<br>
&gt; ----------------------------------------------<br>
&gt;<br>
&gt; Now what about unsigned types?<br>
&gt;<br>
&gt; #cat &gt; test_abs.c &lt;&lt;END<br>
&gt; #include &lt;stdio.h&gt;<br>
&gt; int main() {<br>
&gt;   unsigned int x=-1;<br>
&gt;   printf(&quot;abs(x)=%d\n&quot;,abs(x));<br>
&gt;   return 0;<br>
&gt; }<br>
&gt; END<br>
&gt;<br>
&gt; #test_abs.c:4:24: warning: implicitly declaring library function &#39;abs&#39; with type &#39;int (int)&#39;<br>
&gt;       [-Wimplicit-function-declaration]<br>
&gt;   printf(&quot;abs(x)=%d\n&quot;,abs(x));<br>
&gt;                        ^<br>
&gt; test_abs.c:4:24: note: include the header &lt;stdlib.h&gt; or explicitly provide a declaration for &#39;abs&#39;<br>
&gt; test_abs.c:4:24: warning: taking the absolute value of unsigned type &#39;unsigned int&#39; has no effect [-Wabsolute-value]<br>
&gt;   printf(&quot;abs(x)=%d\n&quot;,abs(x));<br>
&gt;                        ^<br>
&gt; test_abs.c:4:24: note: remove the call to &#39;abs&#39; since unsigned values cannot be negative<br>
&gt;   printf(&quot;abs(x)=%d\n&quot;,abs(x));<br>
&gt;                        ^~~<br>
&gt;<br>
&gt; #./a.out<br>
&gt; abs(x)=1<br>
&gt;<br>
&gt; clang first tells that there is no point of taking absolute value of an unsigned int, an unsigned is allways positive.<br>
&gt;<br>
&gt; BUT, then it re-interprets the unsigned as signed, and the program return +1.<br>
&gt; It would return -1 if abs were really a no-op.<br>
&gt;<br>
&gt; So there are several alternatives for the generator here:<br>
&gt; - 1) suppress the generation of abs for an unsigned<br>
&gt;       BEWARE, this will change behaviour vs current state of VMMaker<br>
&gt; - 2) force a signed cast to remove the warning<br>
&gt;       we just enforce current state of VMMaker but tell the compiler to shut up<br>
&gt; - 3) do nothing and leave the problem for later...<br>
&gt;       the compiler will spit more errors<br>
&gt;<br>
&gt; I&#39;m attaching solution 3), have programmed 2) in my own branch.<br>
&gt; 1) is dangerous because of 2 things :<br>
&gt; - unwanted promotion to unsigned type in C - simply put a sizeof(...) in the expression and it turns unsigned...<br>
&gt; - limited confidence about how slang inlining is handling the implicit type conversions...<br>
&gt;<br>
&gt; Currently there is at least 1 abs(unsigned) which is generated in shrinkObjectMemory :<br>
&gt; ../../spurstack64src/vm/gcc3x-interp.c:50178:12: warning: taking the absolute value of unsigned type &#39;unsigned long&#39; has  no effect [-Wabsolute-value]<br>
&gt;                                          &amp;&amp; ((SQABS((((seg-&gt;segSize)) - shrinkage))) &lt; delta1)) {<br>
&gt;<br>
&gt; This is because shrinkage is declared usqInt...<br>
&gt;<br>
&gt; There would be yet another solution<br>
&gt; - 4) raise an exception and let the dear VMMaker user handle the ambiguity<br>
&gt;<br>
&gt; That&#39;s my favourite, but It&#39;s only a comment in attachment, I don&#39;t want to break yet another Jenkins job ;)<br>
&gt; We should force the correct cast where there is ambiguity.<br>
&gt; If we do not, the exception could be proceedable and would proceed to solution 3) -<br>
&gt; (we do not shut up the compiler and give another chance to someone to understand and correct slang source...)<br>
&gt;<br>
&gt; Eliot, David, Tim, others, what do you think ?<br>
&gt;<br>
&gt; &lt;sqMemoryAccess.h&gt;<br>
&gt; &lt;<a href="http://patch_abs_generation.st" rel="noreferrer" target="_blank">patch_abs_generation.st</a>&gt;<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr">===========================================================================<br>John M. McIntosh. Corporate Smalltalk Consulting Ltd <a href="https://www.linkedin.com/in/smalltalk" target="_blank">https://www.linkedin.com/in/smalltalk</a><br>===========================================================================<br></div></div></div></div>
</div>