Next: , Previous: int16, Up: Top   [Contents]


35 int32

%--------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%--------------------------------------------------%
% Copyright (C) 2017 The Mercury team.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%--------------------------------------------------%
%
% File: int32.m
% Main author: juliensf
% Stability: low.
%
% Predicates and functions for dealing with signed 32-bit integer numbers.
%
%--------------------------------------------------%

:- module int32.
:- interface.

:- import_module pretty_printer.

%--------------------------------------------------%

    % from_int(I, I32):
    % Convert an int to an int32.
    % Fails if not in [-2147483648, 2147483647].
    %
:- pred from_int(int::in, int32::out) is semidet.

    % As above, but throw an exception instead of failing.
    %
:- func det_from_int(int) = int32.

:- func cast_from_int(int) = int32.

:- func cast_from_uint32(uint32) = int32.

:- func to_int(int32) = int.

    % from_bytes_le(Byte0, Byte1, Byte2, Byte3) = I32:
    % I32 is the int32 whose bytes are given in little-endian order by the
    % arguments from left-to-right (i.e. Byte0 is the least significant byte
    % and Byte3 is the most significant byte).
    %
:- func from_bytes_le(uint8, uint8, uint8, uint8) = int32.

    % from_bytes_be(Byte0, Byte1, Byte2, Byte3) = I32:
    % I32 is the int32 whose bytes are given in big-endian order by the
    % arguments in left-to-right order (i.e. Byte0 is the most significant
    % byte and Byte3 is the least significant byte).
    %
:- func from_bytes_be(uint8, uint8, uint8, uint8) = int32.

%--------------------------------------------------%

    % Less than.
    %
:- pred (int32::in) < (int32::in) is semidet.

    % Greater than.
    %
:- pred (int32::in) > (int32::in) is semidet.

    % Less than or equal.
    %
:- pred (int32::in) =< (int32::in) is semidet.

    % Greater than or equal.
    %
:- pred (int32::in) >= (int32::in) is semidet.

%--------------------------------------------------%

    % Unary plus.
    %
:- func + (int32::in) = (int32::uo) is det.

    % Unary minus.
    %
:- func - (int32::in) = (int32::uo) is det.

    % Addition.
    %
:- func int32 + int32 = int32.
:- mode in   + in  = uo is det.
:- mode uo   + in  = in is det.
:- mode in   + uo  = in is det.

:- func plus(int32, int32) = int32.

    % Subtraction.
    %
:- func int32 - int32 = int32.
:- mode in   - in   = uo is det.
:- mode uo   - in   = in is det.
:- mode in   - uo   = in is det.

:- func minus(int32, int32) = int32.

    % Multiplication.
    %
:- func (int32::in) * (int32::in) = (int32::uo) is det.
:- func times(int32, int32) = int32.

    % Maximum.
    %
:- func max(int32, int32) = int32.

    % Minimum.
    %
:- func min(int32, int32) = int32.

    % Flooring integer division.
    % Truncates towards minus infinity, e.g. (-10_i32) div 3_i32 = (-4_i32).
    %
    % Throws a `math.domain_error' exception if the right operand is zero.
    %
:- func (int32::in) div (int32::in) = (int32::uo) is det.

    % Truncating integer division.
    % Truncates towards zero, e.g. (-10_i32) // 3_i32 = (-3_i32).
    % `div' has nicer mathematical properties for negative operands,
    % but `//' is typically more efficient.
    %
    % Throws a `math.domain_error' exception if the right operand is zero.
    %
:- func (int32::in) // (int32::in) = (int32::uo) is det.

    % (/)/2 is a synonym for (//)/2.
    %
:- func (int32::in) / (int32::in) = (int32::uo) is det.

    % unchecked_quotient(X, Y) is the same as X // Y, but the behaviour
    % is undefined if the right operand is zero.
    %
:- func unchecked_quotient(int32::in, int32::in) = (int32::uo) is det.

    % Modulus.
    % X mod Y = X - (X div Y) * Y
    %
    % Throws a `math.domain_error' exception if the right operand is zero.
    %
:- func (int32::in) mod (int32::in) = (int32::uo) is det.

    % Remainder.
    % X rem Y = X - (X // Y) * Y.
    %
    % Throws a `math.domain_error/` exception if the right operand is zero.
    %
:- func (int32::in) rem (int32::in) = (int32::uo) is det.

    % unchecked_rem(X, Y) is the same as X rem Y, but the behaviour is
    % undefined if the right operand is zero.
    %
:- func unchecked_rem(int32::in, int32::in) = (int32::uo) is det.

    % Left shift.
    % X << Y returns X "left shifted" by Y bits.
    % The bit positions vacated by the shift are filled by zeros.
    % Throws an exception if Y is not in [0, 32).
    %
:- func (int32::in) << (int::in) = (int32::uo) is det.

    % unchecked_left_shift(X, Y) is the same as X << Y except that the
    % behaviour is undefined if Y is not in [0, 32).
    % It will typically be be implemented more efficiently than X << Y.
    %
:- func unchecked_left_shift(int32::in, int::in) = (int32::uo) is det.

    % Right shift.
    % X >> Y returns X "right shifted" by Y bits.
    % The bit positions vacated by the shift are filled by the sign bit.
    % Throws an exception if Y is not in [0, 32).
    %
:- func (int32::in) >> (int::in) = (int32::uo) is det.

    % unchecked_right_shift(X, Y) is the same as X >> Y except that the
    % behaviour is undefined if Y is not in [0, bits_per_int32).
    % It will typically be implemented more efficiently than X >> Y.
    %
:- func unchecked_right_shift(int32::in, int::in) = (int32::uo) is det.

    % even(X) is equivalent to (X mod 2 = 0).
    %
:- pred even(int32::in) is semidet.

    % odd(X) is equivalent to (not even(X)), i.e. (X mod 2 = 1).
    %
:- pred odd(int32::in) is semidet.

    % Bitwise and.
    %
:- func (int32::in) /\ (int32::in) = (int32::uo) is det.

    % Bitwise or.
    %
:- func (int32::in) \/ (int32::in) = (int32::uo) is det.

    % Bitwise exclusive or (xor).
    %
:- func xor(int32, int32) = int32.
:- mode xor(in, in) = uo is det.
:- mode xor(in, uo) = in is det.
:- mode xor(uo, in) = in is det.

    % Bitwise complement.
    %
:- func \ (int32::in) = (int32::uo) is det.

    % reverse_bytes(A) = B:
    % B is the value that results from reversing the bytes in the
    % representation of A.
    %
:- func reverse_bytes(int32) = int32.

:- func min_int32 = int32.

:- func max_int32 = int32.

    % Convert a int32 to a pretty_printer.doc for formatting.
    %
:- func int32_to_doc(int32) = pretty_printer.doc.

%--------------------------------------------------%
%--------------------------------------------------%


Next: , Previous: int16, Up: Top   [Contents]