
% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
%     - Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%
%     - Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in
%       the documentation and/or other materials provided with the
%       distribution.
%
%     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
%       names of its contributors may be used to endorse or promote products
%       derived from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR
% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



%%
%% SINT.htex
%%
%%
The \spadtype{SingleInteger} domain is intended to provide support
\index{integer!machine}
in \Language{} for machine integer arithmetic.
It is generally much faster than (bignum) \spadtype{Integer} arithmetic
but suffers from a limited range of values.
Since \Language{} can be implemented on top of various
dialects of Lisp, the actual representation of small integers
may not correspond exactly to the host machines integer representation.

The underlying Lisp primitives treat machine-word sized computations
specially.

%Under \spadgloss{AKCL} on the IBM Risc System/6000,
%small integers are restricted
%to the range \texht{$-{2^{26}}$}{\spad{-2^26}} to
%\texht{${2^{26}}-1$}{\spad{2^26 - 1}},
%allowing 1 bit for overflow detection.

\xtc{
You can discover the minimum and maximum values in your implementation
by using \spadfunFrom{min}{SingleInteger} and \spadfunFrom{max}{SingleInteger}.
}{
\spadcommand{min()\$SingleInteger}
}
\xtc{
}{
\spadcommand{max()\$SingleInteger}
}
\xtc{
To avoid confusion with \spadtype{Integer}, which is the default
type for integers, you usually need to work with declared variables
(\spadref{ugTypesDeclare}) \ldots
}{
\spadcommand{a := 1234 :: SingleInteger \bound{a}}
}
\xtc{
or use package calling
(\spadref{ugTypesPkgCall}).
}{
\spadcommand{b := 124\$SingleInteger \bound{b}}
}
\xtc{
You can add, multiply and subtract \spadtype{SingleInteger} objects,
and ask for the greatest common divisor (\spadfun{gcd}).
}{
\spadcommand{gcd(a,b) \free{a}\free{b}}
}
\xtc{
The least common multiple (\spadfun{lcm}) is also available.
}{
\spadcommand{lcm(a,b) \free{a}\free{b}}
}

\xtc{
Operations \spadfunFrom{mulmod}{SingleInteger},
\spadfunFrom{addmod}{SingleInteger},
\spadfunFrom{submod}{SingleInteger}, and
\spadfunFrom{invmod}{SingleInteger} are similar---they provide
arithmetic modulo a given small integer.
Here is \spad{5 * 6 mod 13}.
}{
\spadcommand{mulmod(5,6,13)\$SingleInteger}
}
\xtc{
To reduce a small integer modulo a prime, use \spadfunFrom{positiveRemainder}{SingleInteger}.
}{
\spadcommand{positiveRemainder(37,13)\$SingleInteger}
}
\xtc{
Operations
\spadfunFrom{And}{SingleInteger},
\spadfunFrom{Or}{SingleInteger},
\spadfunFrom{xor}{SingleInteger},
and \spadfunFrom{Not}{SingleInteger}
provide bit level operations on small integers.
}{
\spadcommand{And(3,4)\$SingleInteger}
}
\xtc{
Use
\spad{shift(int,numToShift)} to shift bits, where
\spad{i} is shifted left if \spad{numToShift} is positive, right
if negative.
}{
\spadcommand{shift(1,4)\$SingleInteger}
}
\xtc{
}{
\spadcommand{shift(31,-1)\$SingleInteger}
}

Many other operations are available for small integers, including
many of those provided for \spadtype{Integer}.
To see the other operations, use the Browse \HyperName{} facility
(\spadref{ugBrowse}).
\showBlurb{SingleInteger}.
