[M3devel] Sanity check request regarding CT arithmetic

Rodney M. Bates rodney.bates at wichita.edu
Wed Jan 30 04:15:54 CET 2008


Whenever I run across something this unbelievable, I figure I must
be missing something.  Can anybody tell me what?

While working on making Pickles work on LONGINT, I have noted the
following code snippets for doing target arithmetic at compile time:

 From m3-sys/m3middle/src/Target.i3:

(* The bits of a target INTEGER (in 2's complement) are stored in
    an array of small host values, with the low order bits in the first
    element of the array. *)

TYPE
   Int = (* OPAQUE *) RECORD
     n: CARDINAL;          (* only bytes [0..n-1] contain valid bits *)
     x := IBytes{0,..};    (* default is Zero *)
   END;
   IBytes = ARRAY [0..7] OF IByte;
   IByte = BITS 8 FOR [0..16_ff];

 From m3-sys/m3middle/src/TWord.m3:

PROCEDURE Subtract (READONLY a, b: Int;  VAR r: Int) =
   VAR borrow := 0;  n := MIN (a.n, b.n);
   BEGIN
     <*ASSERT n # 0*>
     r.n := n;
     FOR i := 0 TO n-1 DO
       borrow := a.x[i] - b.x[i] - borrow;
       r.x[i] := Word.And (borrow, Mask);
       borrow := Word.And (RShift (borrow, BITSIZE (IByte)), 1);
     END;
   END Subtract;

Unless the two values have equal numbers of bytes, Subtract just
throws away the high bytes of the longer operand.  It looks like
pretty much all the CT target arithmetic in TWord.m3 and TInt.m3
does this.

It seems just about inconceivable that the arithmetic could be this
broken and not have created many impossible-to-ignore bugs.  SRC
and pm3 do it differently.  They have a single global variable that
gives the used size of all CT target values.

The only possible explanation I can think of is that the cm3 compiler
happens never to pass two Target.Int values with different values of n.
The relevant declarations give no hint of such a counter-intuitive
requirement.  If this is an essential precondition, it should be
documented in the interfaces.

-- 
-------------------------------------------------------------
Rodney M. Bates, retired assistant professor
Dept. of Computer Science, Wichita State University
Wichita, KS 67260-0083
316-978-3922
rodney.bates at wichita.edu



More information about the M3devel mailing list