[MLton] arith shrinking & bit fiddling

Stephen Weeks MLton@mlton.org
Tue, 21 Oct 2003 08:59:18 -0700


The problematic case is, as you said

	hi = Int.maxInt AND lo <> 0

It seems to me that your two pieces of code do roughly the same work.

> 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.

Isn't it only an extra compare for 1/2^32 of the ints, i.e. those when
hi = Int.maxInt?  So, this piece of code tests the lhs of the AND with
the jo and the rhs with the jne.

> 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.

This seems fine too.  It's testing the rhs of the and with the jz and
the lhs with the jo.

So, the question is whether making the common case be the jo or the jz
is faster.

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