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


115 uint8

%--------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%--------------------------------------------------%
% Copyright (C) 2017-2021 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%--------------------------------------------------%
%
% File: uint8.m
% Main author: juliensf
% Stability: low.
%
% Predicates and functions for dealing with unsigned 8-bit integer numbers.
%
%--------------------------------------------------%

:- module uint8.
:- interface.

:- import_module pretty_printer.

%--------------------------------------------------%
%
% Conversion from int.
%

    % from_int(I, U8):
    %
    % Convert an int to a uint8.
    % Fails if I is not in [0, 2^8 - 1].
    %
:- pred from_int(int::in, uint8::out) is semidet.

    % det_from_int(I) = U8:
    %
    % Convert an int to a uint8.
    % Throws an exception if I is not in [0, 2^8 - 1].
    %
:- func det_from_int(int) = uint8.

    % cast_from_int(I) = U8:
    %
    % Convert an int to a uint8.
    % Always succeeds, but will yield a result that is mathematically equal
    % to I only if I is in [0, 2^8 - 1].
    %
:- func cast_from_int(int) = uint8.

%--------------------------------------------------%
%
% Conversion from uint.
%

    % from_uint(U, U8):
    %
    % Convert a uint to a uint8.
    % Fails if U is not in [0, 2^8 - 1].
    %
:- pred from_uint(uint::in, uint8::out) is semidet.

    % det_from_uint(U) = U8:
    %
    % Convert a uint to a uint8.
    % Throws an exception if U is not in [0, 2^8 - 1].
    %
:- func det_from_uint(uint) = uint8.

    % cast_from_uint(U) = U8:
    %
    % Convert a uint to a uint8.
    % Always succeeds, but will yield a result that is mathematically equal
    % to U only if U is in [0, 2^8 - 1].
    %
:- func cast_from_uint(uint) = uint8.

%--------------------------------------------------%
%
% Conversion to int.
%

    % to_int(U8) = I:
    %
    % Convert a uint8 to an int.
    % Always succeeds, and yields a result that is mathematically equal
    % to U8.
    %
:- func to_int(uint8) = int.

    % cast_to_int(U8) = I:
    %
    % Convert a uint8 to an int.
    % Always succeeds, and yields a result that is mathematically equal
    % to U8.
    %
:- func cast_to_int(uint8) = int.

%--------------------------------------------------%
%
% Conversion to uint.
%

    % cast_to_uint(U8) = U:
    %
    % Convert a uint8 to a uint.
    % Always succeeds, and yields a result that is mathematically equal
    % to U8.
    %
:- func cast_to_uint(uint8) = uint.

%--------------------------------------------------%
%
% Change of signedness.
%

    % cast_from_int8(I8) = U8:
    %
    % Convert an int8 to a uint8. This will yield a result that is
    % mathematically equal to I8 only if I8 is in [0, 2^7 - 1].
    %
:- func cast_from_int8(int8) = uint8.

%--------------------------------------------------%
%
% Comparisons and related operations.
%

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

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

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

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

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

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

%--------------------------------------------------%
%
% Arithmetic operations.
%

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

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

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

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

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

    % Truncating integer division.
    %
    % Throws a `domain_error' exception if the right operand is zero.
    %
:- func (uint8::in) div (uint8::in) = (uint8::uo) is det.

    % Truncating integer division.
    %
    % Throws a `domain_error' exception if the right operand is zero.
    %
:- func (uint8::in) // (uint8::in) = (uint8::uo) is det.

    % (/)/2 is a synonym for (//)/2.
    %
:- func (uint8::in) / (uint8::in) = (uint8::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(uint8::in, uint8::in) = (uint8::uo) is det.

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

    % Remainder.
    % X rem Y = X - (X // Y) * Y.
    %
    % Throws a `domain_error/` exception if the right operand is zero.
    %
:- func (uint8::in) rem (uint8::in) = (uint8::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(uint8::in, uint8::in) = (uint8::uo) is det.

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

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

%--------------------------------------------------%
%
% Shift operations.
%

    % 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, 8).
    %
:- func (uint8::in) << (int::in) = (uint8::uo) is det.
:- func (uint8::in) <<u (uint::in) = (uint8::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, 8).
    % It will typically be implemented more efficiently than X << Y.
    %
:- func unchecked_left_shift(uint8::in, int::in) = (uint8::uo) is det.
:- func unchecked_left_ushift(uint8::in, uint::in) = (uint8::uo) is det.

    % Right shift.
    % X >> Y returns X "right shifted" by Y bits.
    % The bit positions vacated by the shift are filled by zeros.
    % Throws an exception if Y is not in [0, 8).
    %
:- func (uint8::in) >> (int::in) = (uint8::uo) is det.
:- func (uint8::in) >>u (uint::in) = (uint8::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, 8).
    % It will typically be implemented more efficiently than X >> Y.
    %
:- func unchecked_right_shift(uint8::in, int::in) = (uint8::uo) is det.
:- func unchecked_right_ushift(uint8::in, uint::in) = (uint8::uo) is det.

%--------------------------------------------------%
%
% Logical operations.
%

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

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

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

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

%--------------------------------------------------%
%
% Operations on bits and bytes.
%

    % num_zeros(U) = N:
    %
    % N is the number of zeros in the binary representation of U.
    %
:- func num_zeros(uint8) = int.

    % num_ones(U) = N:
    %
    % N is the number of ones in the binary representation of U.
    %
:- func num_ones(uint8) = int.

    % num_leading_zeros(U) = N:
    %
    % N is the number of leading zeros in the binary representation of U,
    % starting at the most significant bit position.
    % Note that num_leading_zeros(0u8) = 8.
    %
:- func num_leading_zeros(uint8) = int.

    % num_trailing_zeros(U) = N:
    %
    % N is the number of trailing zeros in the binary representation of U,
    % starting at the least significant bit position.
    % Note that num_trailing_zeros(0u8) = 8.
    %
:- func num_trailing_zeros(uint8) = int.

    % reverse_bits(A) = B:
    %
    % B is the is value that results from reversing the bits in the binary
    % representation of A.
    %
:- func reverse_bits(uint8) = uint8.

    % rotate_left(U, D) = N:
    %
    % N is the value obtained by rotating the binary representation of U
    % left by D bits. Throws an exception if D is not in the range [0, 7].
    %
:- func rotate_left(uint8, uint) = uint8.

    % unchecked_rotate_left(U, D) = N:
    %
    % N is the value obtained by rotating the binary representation of U
    % left by an amount given by the lowest 3 bits of D.
    %
:- func unchecked_rotate_left(uint8, uint) = uint8.

    % rotate_right(U, D) = N:
    %
    % N is the value obtained by rotating the binary representation of U
    % right by D bits. Throws an exception if D is not in the range [0, 7].
    %
:- func rotate_right(uint8, uint) = uint8.

    % unchecked_rotate_left(U, D) = N:
    %
    % N is the value obtained by rotating the binary representation of U
    % right by an amount given by the lowest 3 bits of D.
    %
:- func unchecked_rotate_right(uint8, uint) = uint8.

    % set_bit(U, I) = N:
    % N is the value obtained by setting the I'th bit (the bit worth 2^I) of U
    % to one. An exception is thrown if I is not in the range [0, 7].
    %
:- func set_bit(uint8, uint) = uint8.

    % unchecked_set_bit(U, I) = N:
    % As above, but the behaviour is undefined if I is not in the range
    % [0, 7].
    %
:- func unchecked_set_bit(uint8, uint) = uint8.

    % clear_bit(U, I) = N:
    % N is the value obtained by setting the I'th bit (the bit worth 2^I) of U
    % to zero. An exception is thrown if I is not in the range [0, 7].
    %
:- func clear_bit(uint8, uint) = uint8.

    % unchecked_clear_bit(U, I) = N:
    % As above, but the behaviour is undefined if I is not in the range
    % [0, 7].
    %
:- func unchecked_clear_bit(uint8, uint) = uint8.

    % flip_bit(U, I) = N:
    % N is the value obtained by flipping the I'th bit (the bit worth 2^I) of
    % U. An exception is thrown if I is not in the range [0, 7].
    %
:- func flip_bit(uint8, uint) = uint8.

    % unchecked_flip_bit(U, I) = N:
    % As above, but the behaviour is undefined if I is not in the range
    % [0, 7].
    %
:- func unchecked_flip_bit(uint8, uint) = uint8.

    % bit_is_set(U, I):
    % True iff the I'th bit (the bit worth 2^I) of U is one.
    % An exception is thrown if I is not in the range [0, 7].
    %
:- pred bit_is_set(uint8::in, uint::in) is semidet.

    % unchecked_bit_is_set(U, I):
    % As above, but the behaviour is undefined if I is not in the range
    % [0, 7].
    %
:- pred unchecked_bit_is_set(uint8::in, uint::in) is semidet.

    % bit_is_clear(U, I):
    % True iff the I'th bit (the bit worth 2^I) of U is zero.
    % An exception is thrown if I is not in the range [0, 7].
    %
:- pred bit_is_clear(uint8::in, uint::in) is semidet.

    % unchecked_bit_is_clear(U, I):
    % As above, but the behaviour is undefined if I is not in the range
    % [0, 7].
    %
:- pred unchecked_bit_is_clear(uint8::in, uint::in) is semidet.

%--------------------------------------------------%
%
% Limits.
%

:- func max_uint8 = uint8.

%--------------------------------------------------%
%
% Prettyprinting.
%

    % Convert an uint8 to a pretty_printer.doc for formatting.
    %
:- func uint8_to_doc(uint8) = pretty_printer.doc.
:- pragma obsolete(func(uint8_to_doc/1), [pretty_printer.uint8_to_doc/1]).

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


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