<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
<div>implementing div/mod?</div><div><br></div><div>The historical implementation is "ok".</div><div> advantages:</div><div> stable</div><div> doesn't do any div/mod of negative numbers</div><div> disadvantages:</div><div> I believe it gets a few cases wrong, e.g. involving minimal inputs</div><div> relies on silent overflow of signed math</div><div> There are gcc flags that warn about this,</div><div> and I think also optimizations that break it.</div><div><br></div><div><br></div><div>"new" C implementation:</div><div> advantages:</div><div> avoids silent overflow overflow of signed numbers (no warnings from gcc)</div><div> I believe it fixes the incorrect cases in the historical implementation.</div><div> disadvantages:</div><div> one nagging part of it is how it deals with two negative</div><div> inputs; it assumes C matches Modula-3</div><div><br></div><div><br></div><div>INTEGER</div><div>__stdcall</div><div>m3_div(INTEGER b, INTEGER a)</div><div>{</div><div> int aneg = (a < 0);</div><div> int bneg = (b < 0);</div><div> if (aneg == bneg || a == 0 || b == 0)</div><div> return (a / b);</div><div>...</div><div><br></div><div>INTEGER</div><div>__stdcall</div><div>m3_mod(INTEGER b, INTEGER a)</div><div>{</div><div> int aneg = (a < 0);</div><div> int bneg = (b < 0);</div><div> if (aneg == bneg || a == 0 || b == 0)</div><div> return (a % b);</div><div>...</div><div><br></div><div><br></div><div>This makes me a bit nervous regarding the case of aneg && bneg.</div><div>Generally, as was said, everyone defines div/mod the same for positive inputs,</div><div>but once either (or both?) inputs are negative, there are several options.</div><div>But it seems to test out ok. At least on AMD64_DARWIN.</div><div> I could test more platforms.</div><div><br></div><div><br></div><div>gcc "opcodes" (tree codes, there are different ones for</div><div> trunc/round/floor/ceil div/mod)</div><div> presumably fastest option</div><div> and maybe smallest though probably not</div><div> presumably not much used</div><div> 64bit path doesn't work for ARM, for a mostly understood reason,</div><div> compiler goes down path of wanting 128bit integers ("TImode"), though</div><div> I suspect it doesn't really need them, either it ends</div><div> up doing something else, or they are a way of</div><div> moving around a pair of int64</div><div><br></div><div><br></div><div>Could go back to the hybrid I had briefly:</div><div> always use gcc "opcodes", except 64bit operations on ARM</div><div> or could even fix 64bit on ARM?</div><div> </div><div> </div><div> ?</div><div><br></div><div><br></div><div>Notice that m3back has always implemented 32bit div/mod inline.</div><div> But always calls the C for 64bit.</div><div>Also my confidence in gcc "opcodes" was briefly shaken</div><div>by noticing it still uses the "normal" helper functions, but</div><div>upon closer inspection, it does "wrap" them in various forms</div><div>of rounding, depending on the exact operation requested.</div><div><br></div><div> - Jay</div><br> </body>
</html>