[MLton] arith shrinking & bit fiddling

Matthew Fluet fluet@cs.cornell.edu
Tue, 21 Oct 2003 11:07:14 -0400 (EDT)


> 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

Grr... wrong again.  It should be

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

This still only does one extra comparison for 1/4 of the ints.  But, it's
annoying, because the zeroness of lo was known at negl lo.  So, an
alternative would be:

  negl lo
  jz maybeMin
  incl hi      # since lo is nonzero, carry was set
  negl hi
  jmp success
 maybeMin:     # since lo is zero, carry was not set
  negl hi
  jo overflow

Now there are no extra comparisons, just one rarely taken conditional
branch.  That might be o.k.


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