[M3devel] div/mod?

Jay K jay.krell at cornell.edu
Sun Jun 27 14:39:45 CEST 2010


implementing div/mod?
The historical implementation is "ok". advantages:   stable   doesn't do any div/mod of negative numbers disadvantages:   I believe it gets a few cases wrong, e.g. involving minimal inputs   relies on silent overflow of signed math     There are gcc flags that warn about this,     and I think also optimizations that break it.

"new" C implementation:  advantages:    avoids silent overflow overflow of signed numbers (no warnings from gcc)    I believe it fixes the incorrect cases in the historical implementation.  disadvantages:    one nagging part of it is how it deals with two negative    inputs; it assumes C matches Modula-3

INTEGER__stdcallm3_div(INTEGER b, INTEGER a){  int aneg = (a < 0);  int bneg = (b < 0);  if (aneg == bneg || a == 0 || b == 0)    return (a / b);...
INTEGER__stdcallm3_mod(INTEGER b, INTEGER a){  int aneg = (a < 0);  int bneg = (b < 0);  if (aneg == bneg || a == 0 || b == 0)    return (a % b);...

This makes me a bit nervous regarding the case of aneg && bneg.Generally, as was said, everyone defines div/mod the same for positive inputs,but once either (or both?) inputs are negative, there are several options.But it seems to test out ok. At least on AMD64_DARWIN. I could test more platforms.

gcc "opcodes" (tree codes, there are different ones for   trunc/round/floor/ceil div/mod)  presumably fastest option    and maybe smallest though probably not  presumably not much used  64bit path doesn't work for ARM, for a mostly understood reason,     compiler goes down path of wanting 128bit integers ("TImode"), though     I suspect it doesn't really need them, either it ends     up doing something else, or they are a way of     moving around a pair of int64

Could go back to the hybrid I had briefly: always use gcc "opcodes", except 64bit operations on ARM or could even fix 64bit on ARM?   ?

Notice that m3back has always implemented 32bit div/mod inline.  But always calls the C for 64bit.Also my confidence in gcc "opcodes" was briefly shakenby noticing it still uses the "normal" helper functions, butupon closer inspection, it does "wrap" them in various formsof rounding, depending on the exact operation requested.
 - Jay
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20100627/22b0ff30/attachment-0001.html>


More information about the M3devel mailing list