[MLton] arith shrinking & bit fiddling

Matthew Fluet fluet@cs.cornell.edu
Tue, 21 Oct 2003 10:52:43 -0400 (EDT)


> 2) Does anyone know an efficient way of doing overflow checking for signed
> negation in assembly?  Note that defining:
>
> gcc compiles
>   long long negate(long long x) { return -x; }
> as
>   negl lo
>   adcl hi
>   negl hi
> which produces the correct result, but sets the overflow flag when the lo
> bits are nonzero (setting the carry flag) and the hi bits are
> Int32.maxInt.  The adcl hi rolls hi to Int32.minInt and the negation
> overflows.  But, the resulting bits are correct.  We only "really"
> overflow when the low bits are (~1: Int32.int).

Sorry, that last sentence is wrong.  The assembly sequence above sets
overflow when
 a) hi = Int.minInt, lo = 0
    * this is correct, as it corresponds to Int64.minInt
 b) hi = Int.maxInt, lo <> 0
    * this is wrong, we need never overflow in these cases

So, I think I can compile it as:

 negl lo
 adcl hi
 negl hi
 jo checkOverflow
 ...
checkOverflow:
 cmp hi,0x80000000
 je overflow

This does one extra comparison for approximately 1/4 of the Int64.ints.
That would seem to be better than checking for Int64.minInt, which would
entail two extra comparisons for all the Int64.ints.

I was hoping to find a way of doing it without jumps, but I don't see one.


_______________________________________________
MLton mailing list
MLton@mlton.org
http://www.mlton.org/mailman/listinfo/mlton