Previous Table of Contents Next


11.10.3. The Word Interface

The Word interface describes operations for unsigned arithmetic.

   INTERFACE Word;
   TYPE T = INTEGER;
   CONST Size = BITSIZE(T);

A Word.T w represents a sequence of Word.Size bits w0, …, w(Word.Size-1). It also represents the unsigned number SUM (wi * 2i). 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 *)

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 *)

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

Because Modula-3 has no exponentiation operator, Word.Shift(1, n) is the usual way of writing 2n 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.

11.10.4. The Real, LongReal, and Extended Interfaces

For definitions of the terms used in these interfaces, see the ANSI/IEEE Standard 754-1985 for floating-point arithmetic.

These interfaces define constant attributes of the three built-in floating-point types:

   INTERFACE Real; TYPE T = REAL;
   CONST
     Base: INTEGER = ...;
     Precision: INTEGER = ...;
     MaxFinite: T = ...;
     MinPos: T = ...;
     MinPosNormal: T = ...;
   END Real.
   INTERFACE LongReal; TYPE T = LONGREAL;
   CONST
     Base: INTEGER = ...;
     Precision: INTEGER = ...;
     MaxFinite: T = ...;
     MinPos: T = ...;
     MinPosNormal: T = ...;
   END LongReal.
   INTERFACE Extended; TYPE T = EXTENDED;
   CONST
     Base: INTEGER = ...;
     Precision: INTEGER = ...;
     MaxFinite: T = ...;
     MinPos: T = ...;
     MinPosNormal: T = ...;
   END Extended.

The specification is the same for all three interfaces:

  Base is the radix of the floating-point representation for T.
  Precision is the number of digits of precision for T.
  MaxFinite is the maximum finite value in T. For non-IEEE implementations, this is the same as LAST(T).
  MinPos is the minimum positive value in T.
  MinPosNormal is the minimum positive normal value in T; it differs from MinPos only for implementations (such as IEEE) with denormalized numbers.

11.10.5. The RealFloat, LongFloat, and ExtendedFloat Interfaces

For definitions of the terms used in these interfaces, see the ANSI/IEEE Standard 754-1985 for floating-point arithmetic.

These interfaces define operations that depend on the floating-point representation. They are all are instances of a generic interface Float:

   INTERFACE RealFloat = Float(Real) END RealFloat.
   INTERFACE LongFloat = Float(LongReal) END LongFloat.
   INTERFACE ExtendedFloat = Float(Extended) END ExtendedFloat.
   GENERIC INTERFACE Float(R); TYPE T = R.T;

This generic interface provides access to the floating-point operations required or recommended by the IEEE floating-point standard. Consult the standard for the precise specifications of the procedures, including when their arguments are NaNs, infinities, and signed zeros and including what exceptions they can raise. The comments here specify their effect when the arguments are ordinary numbers and no exception is raised. Implementations on non-IEEE machines that have values similar to NaNs and infinities should explain how those values behave in an implementation guide.

   PROCEDURE Scalb(x: T; n: INTEGER): T;

Return x * 2n.

   PROCEDURE Logb(x: T): T;

Return the exponent of x. More precisely, return the unique n such that the ratio ABS(x) / Basen is in the range[1..Base-1], unless x is denormalized, in which case return the minimum exponent value for T.

   PROCEDURE ILogb(x: T): INTEGER;

Like Logb, but returns an integer, never raises an exception, and always returns the n such that ABS(x) / Basen is in the range [1..Base-1], even for denormalized numbers.

   PROCEDURE NextAfter(x, y: T): T;

Return the next representable neighbor of x in the direction towards y. If x = y, return x.

   PROCEDURE CopySign(x, y: T): T;

Return x with the sign of y.

   PROCEDURE Finite(x: T): BOOLEAN;

Return TRUE if x is strictly between minus infinity and plus infinity. This always returns TRUE on non-IEEE machines.

   PROCEDURE IsNaN(x: T): BOOLEAN;


Previous Table of Contents Next