Int.{quot,rem}

Matthew Fluet fluet@cs.cornell.edu
Mon, 4 Feb 2002 15:45:57 -0500 (EST)


> I am porting the PolySpace changes into our internal version, and they
> replaced our Int_quot and Int_rem with the following macros (for the C
> codegen).
> 
> #define Int_quot(x, y) ((x)/(y))
> #define Int_rem(x, y) ((x)%(y))
> 
> I assume that there was a reason that we used all of the assembly that
> we have in runtime/basis/Int/{quot,rem}.c instead of these, but I
> don't remember it.  I tried their changes on the int.sml regression
> test, and it works fine.

K&R are a little lax on the semantics of / and % on negative operands:
"The direction of truncation for / and the sign of the result for % are
machine-dependent for negative operands, ..." (p. 41)  (See also p. 205.)

The SML Basis is very specific on directions of truncation and signs.

Probably, gcc just maps / and % to their hardware equivalents, which, at
least on x86, correspond to the Basis.  But, nothing in ANSI C prevents it
from doing something else; likewise, Visual C++ might have a different
interpretation.

But, if it works in practice, I've no problems using the #define's.