Overview
Completing our tour of the standard Modula-3 interfaces, we come to the
Word interface. It has some significant differences from the others.
First, the interface is hard-wired into the compiler so that the
simpler Word operations can be performed inline instead of through
function calls. Second, many procedures here are declared EPHEMERAL
which is a SPIN invention meaning that they can be interrupted at any
time without serious ill-effects.
Using the Word interface
It is worth remembering that declaring a variable to be of type Word.T
does not force the compiler to perform unsigned operations on it. If
you want the unsigned operations, you must use the following procedures
explicitly.
(* A Word.T w represents a sequence of Word.Size bits
w(0), ..., w(Word.Size-1).
It also represents the unsigned number
sum of 2^(i) * w(i) for i in 0, ..., Word.Size-1. *)
TYPE
T = INTEGER;
(* encoding is implementation-dependent; e.g., 2's complement. *)
CONST
Size : INTEGER = BITSIZE (T); (* implementation-dependent *)
EPHEMERAL PROCEDURE Plus (x, y: T): T; (* (x + y) MOD 2^[Word.Size] *)
EPHEMERAL PROCEDURE Times (x, y: T): T; (* (x * y) MOD 2^[Word.Size] *)
EPHEMERAL PROCEDURE Minus (x, y: T): T; (* (x - y) MOD 2^[Word.Size] *)
EPHEMERAL PROCEDURE Divide (x, y: T): T; (* x divided by y *)
EPHEMERAL PROCEDURE Mod (x, y: T): T; (* x MOD y *)
EPHEMERAL PROCEDURE LT (x, y: T): BOOLEAN; (* x < y *)
EPHEMERAL PROCEDURE LE (x, y: T): BOOLEAN; (* x <= y *)
EPHEMERAL PROCEDURE GT (x, y: T): BOOLEAN; (* x > y *)
EPHEMERAL PROCEDURE GE (x, y: T): BOOLEAN; (* x >= y *)
EPHEMERAL PROCEDURE And (x, y: T): T; (* Bitwise AND of x and y *)
EPHEMERAL PROCEDURE Or (x, y: T): T; (* Bitwise OR of x and y *)
EPHEMERAL PROCEDURE Xor (x, y: T): T; (* Bitwise XOR of x and y *)
EPHEMERAL PROCEDURE Not (x: T): T; (* Bitwise complement of x *)
EPHEMERAL 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) *)
EPHEMERAL PROCEDURE LeftShift (x: T; n: [0..Size-1]): T;
(* = Shift (x, n) *)
EPHEMERAL PROCEDURE RightShift (x: T; n: [0..Size-1]): T;
(* = Shift (x, -n) *)
EPHEMERAL PROCEDURE Rotate (x: T; n: INTEGER): T;
(* Bit i of the result equals bit (i - n) MOD Word.Size of x. *)
EPHEMERAL PROCEDURE LeftRotate (x: T; n: [0..Size-1]): T;
(* = Rotate (x, n) *)
EPHEMERAL PROCEDURE RightRotate (x: T; n: [0..Size-1]): T;
(* = Rotate (x, -n) *)
EPHEMERAL 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. *)
EPHEMERAL PROCEDURE Insert (x, y: T; i, n: CARDINAL): T;
(* Return x with n bits replaced, 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. *)