[MLton-devel] Int-inf shifts

Matthew Fluet fluet@CS.Cornell.EDU
Thu, 31 Oct 2002 19:38:19 -0500 (EST)


In the interest of fleshing out the Basis Library.  I added the IntInf
binary operations (andb, orb, xorb, notb, ~>>, <<).  They all seem to work
fine; the ~>> and << could probably be optimized for some small cases.  I
also haven't pushed through the constant-folding in prim.fun.  (I figured
I'd wait until after the merge and avoid bootstrapping problems; or, I
guess I really shoul fill in the stubs.)  Anyways, I was writing a little
regression file and comparing with SML/NJ and we agree everywhere except
on ~>>.  I'm fairly sure that what's coming back from GMP is the right
thing, but sanity check me before I send in a bug report.

The basis says:

 ~>> (i, n)
    shifts i to the right by n bit positions. When i and n are interpreted
as integers, the latter non-negative, this returns Floor (i / 2(n)).

 The bit-wise operations (andb, orb, notb, <<, etc.) treat the integer
arguments as having 2's complement representation. In particular, if we
let bit = pow(2,n), for all sufficiently large values of n we have
andb(i, bit) = 0 if i >= 0
andb(i, bit) = bit if i < 0

MLton produces the following:

A ~>> 0: ~111110011001
      A: ~111110011001
A ~>> 1: ~11111001101
      A: ~111110011001
A ~>> 2: ~1111100111
      A: ~111110011001
A ~>> f: ~1
       A: ~111110011001
A ~>> 10: ~1
       A: ~111110011001
A ~>> 11: ~1
       A: ~111110011001
A ~>> 12: ~1

(The shifts are in HEX and the int-infs are in BIN.)

SML/NJ produces:

      A: ~111110011001
A ~>> 0: ~111110011001
      A: ~111110011001
A ~>> 1: ~11111001100
      A: ~111110011001
A ~>> 2: ~1111100110
      A: ~111110011001
A ~>> f: 0
       A: ~111110011001
A ~>> 10: 0
       A: ~111110011001
A ~>> 11: 0
       A: ~111110011001
A ~>> 12: 0

The A ~>> 12 would seem to clinch it -- a negative 2's complement integer
has an infinite number of leading 1s, so a sufficiently high shift should
yield an infinite word of all 1s, which interpreted in 2's complement
would be ~1, not 0.




-------------------------------------------------------
This sf.net email is sponsored by: Influence the future 
of Java(TM) technology. Join the Java Community 
Process(SM) (JCP(SM)) program now. 
http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0004en
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel