[top] [prev] [next]

The Word interface

INTERFACE Word;

TYPE T = INTEGER;

CONST Size = BITSIZE(T);
A Word.T w represents a sequence of Word.Size bits w_0, ..., w_(Word.Size-1). It also represents the unsigned number SUM (w_i * 2^i). Finally, it also represents a signed INTEGER by some implementation-dependent encoding (for example, two's complement). The built-in operations of the language deal with the signed value; the operations in this interface deal with the unsigned value or with the bit sequence.

Here are the arithmetic operations on unsigned words:

PROCEDURE Plus  (x,y: T): T;     (* (x + y) MOD 2^Word.Size *)
PROCEDURE Times (x,y: T): T;     (* (x * y) MOD 2^Word.Size *)
PROCEDURE Minus (x,y: T): T;     (* (x - y) MOD 2^Word.Size *)
PROCEDURE Divide(x,y: T): T;     (* x DIV y *)
PROCEDURE Mod(x,y: T): T;        (* x MOD y *)
PROCEDURE LT(x,y: T): BOOLEAN;   (* x < y *)
PROCEDURE LE(x,y: T): BOOLEAN;   (* x <= y *)
PROCEDURE GT(x,y: T): BOOLEAN;   (* x > y *)
PROCEDURE GE(x,y: T): BOOLEAN;   (* x >= y *)

And here are the logical operations on bit sequences:

PROCEDURE And(x,y: T): T;        (* Bitwise AND of x and y *)
PROCEDURE Or (x,y: T): T;        (* Bitwise OR of x and y *)
PROCEDURE Xor(x,y: T): T;        (* Bitwise XOR of x and y *)
PROCEDURE Not  (x: T):  T;       (* Bitwise complement of x *)

And here are additional operations on bit sequences:

PROCEDURE Shift(x: T; n: INTEGER): T;
For all i such that both i and i - n are in the range [0..Word.Size - 1], bit i of the result equals bit i - n of x. The other bits of the result are 0. Thus shifting by n > 0 is like multiplying by 2^n.

Since Modula-3 has no exponentiation operator, Word.Shift(1, n) is the usual way of writing 2^n in a constant expression.

PROCEDURE Rotate(x: T; n: INTEGER): T;
Bit i of the result is bit ((i - n) MOD Word.Size) of x.

PROCEDURE Extract(x: T; i, n: CARDINAL): T;
Take n bits from x, with bit i as the least significant bit, and return them as the least significant n bits of a word whose other bits are 0. A checked runtime error if n + i > Word.Size.

PROCEDURE Insert(x: T; y: T; i, n: CARDINAL): T;
Result of replacing n bits of x, with bit i as the least significant bit, by the least significant n bits of y. The other bits of x are unchanged. A checked runtime error if n + i > Word.Size.

END Word.

The Long interface

The Long interface is identical to Word, except in the preamble:

INTERFACE Long;

TYPE T = LONGINT;

...

END Long.
The operations are the same except that they operate on T = LONGINT instead of T = INTEGER.

The CM Modula-3 version of Word is here.

The CM Modula-3 version of Long is here.

[top] [prev] [next]