Instances of the signature WORD provide a type of unsigned integer
with modular arithmetic and logical operations and conversion
operations. They are also meant to give efficient access to the
primitive machine word types of the underlying hardware, and support
bit-level operations on integers. They are not meant to be a
"larger" int.
In order to provide a more intuitive description of the shift
operators below, we assume a bit ordering in which the most
significant bit is leftmost, and the least significant bit is
rightmost.
structure Word8 : WORD
structure Word31 : WORD
structure Word32 : WORD
structure Word : WORD
structure LargeWord : WORD
signature WORD =
sigeqtype word
valwordSize : int
valtoLarge : word -> word32
valtoLargeX : word -> word32
valtoLargeWord : word -> word32
valtoLargeWordX : word -> word32
valfromLarge : word32 -> word
valfromLargeWord : word32 -> word
valtoLargeInt : word -> intinf
valtoLargeIntX : word -> intinf
valfromLargeInt : intinf -> word
valtoInt : word -> int
valtoIntX : word -> int
valfromInt : int -> word
valandb : word * word -> word
valorb : word * word -> word
valxorb : word * word -> word
valnotb : word -> word
val<< : word * Initial.word0 -> word
val>> : word * Initial.word0 -> word
val~>> : word * Initial.word0 -> word
val+ : word * word -> word
val- : word * word -> word
val* : word * word -> word
valdiv : word * word -> word
valmod : word * word -> word
valcompare : word * word -> order
val< : word * word -> bool
val<= : word * word -> bool
val> : word * word -> bool
val>= : word * word -> bool
val~ : word -> word
valmin : word * word -> word
valmax : word * word -> word
valfmt : StringCvt.radix -> word -> string
valtoString : word -> string
valscan : StringCvt.radix
-> (char, 'a) StringCvt.reader
-> (word, 'a) StringCvt.readervalfromString : string -> word option
end
These convert w to a value of type LargeWord.word. In the
first case, w is converted to its equivalent LargeWord.word value in
the range [0,2(wordSize)-1]. In the second case, w is
``sign-extended,'' i.e., the wordSize low-order bits of w and toLargeX
w are the same, and the remaining bits of toLargeX w are all equal to
the most significant bit of w. toLargeWord and toLargeWordX are
respective synonyms of the first two, and are deprecated.
These functions convert w to the value w(mod
(2(wordSize))) of type word. This has the effect of taking the
low-order wordSize bits of the 2's complement representation of w. The
function fromLargeWord is a deprecated synonym for fromLarge.
These convert w to a value of type LargeInt.int. In
the former case, w is viewed as an integer value in the
range [0,2(wordSize)-1]. In the latter case, w is treated as a 2's
complement signed integer with wordSize precision, thereby having a
value in the range [-2(wordSize-1),2(wordSize-1)-1]. toLargeInt raises
Overflow if the target integer value cannot be represented as a
LargeInt.int. Since the precision of LargeInt.int is always at least
wordSize (see the discussion below), toLargeIntX will never raise an
exception.
converts i of type LargeInt.int to a value of type
word. This has the effect of taking the low-order wordSize bits of the
2's complement representation of i.
These convert w to a value of default integer type. In the
former case, w is viewed as an integer value in the
range [0,2(wordSize)-1]. In the latter case, w is treated as a 2's
complement signed integer with wordSize precision, thereby having a
value in the range [-2(wordSize-1),2(wordSize-1)-1]. They raise
Overflow if the target integer value cannot be represented as an
Int.int.
converts i of the default integer type to a value of type
word. This has the effect of taking the low-order wordSize bits of the
2's complement representation of i. If the precision of Int.int is
less than wordSize, then i is sign-extended to wordSize bits.
shifts i to the left by n bit positions, filling in zeros
from the right. When i and n are interpreted as unsigned binary
numbers, this returns (i* 2(n))(mod (2 (wordSize))). In particular,
shifting by greater than or equal to wordSize results in 0. This
operation is similar to the ``(logical) shift left'' instruction in
many processors.
shifts i to the right by n bit positions, filling in zeros
from the left. When i and n are interpreted as unsigned binary
numbers, it returns floor((i / 2(n))). In particular, shifting by
greater than or equal to wordSize results in 0. This operation is
similar to the ``logical shift right'' instruction in many processors.
shifts i to the right by n bit positions. The value of
the leftmost bit of i remains the same; in a 2's-complement
interpretation, this corresponds to sign extension. When i is
interpreted as a wordSize-bit 2's-complement integer and n is
interpreted as an unsigned binary number, it returns floor((i /
2(n))). In particular, shifting by greater than or equal to wordSize
results in either 0 or all 1's. This operation is similar to the
``arithmetic shift right'' instruction in many processors.
These return a string containing a numeric representation
of i. No prefix "Ow", "OwX", etc. is generated. The version using fmt
creates a representation specified the given radix. The hexadecimal
digits in the range [10,15] are represented by the characters #"A"
through #"F". The version using toString is equivalent to fmt
StringCvt.HEX i.
These functions scan a word from a character source. In
the first version, if an unsigned number in the format denoted by
radix can be parsed from a prefix of the character strm strm using the
character input function getc, the expression evaluates to
SOME(w,rest), where w is the value of the number parsed and rest is
the remainder of the character stream. Initial whitespace is
ignored. NONE is returned otherwise. It raises Overflow when a number
can be parsed, but is too large to fit in type word. The format that
scan accepts depends on the radix argument. Regular expressions
defining these formats are as follows:
Radix Format
StringCvt.BIN (0w)?[0-1]+
StringCvt.OCT (0w)?[0-7]+
StringCvt.DEC (0w)?[0-9]+
The fromString version returns SOME(w) if an unsigned hexadecimal
number in the format (0wx | 0wX | 0x | 0X)?[0-9a-fA-F]+ can be parsed
from a prefix of string s, ignoring initial whitespace, where w is the
value of the number parsed. NONE is returned otherwise. This function
raises Overflow when a hexadecimal numeral can be parsed, but is too
large to be represented by type word. It is equivalent to