Skip to main content

Int16

Reference for mo:core/Int16 in the core library.

Int16 is a checked signed 16-bit surface. Narrowing conversions require range preconditions, and abs/neg reject the minimum value because its positive counterpart is not representable.

Import

mo:core/Int16

Status

  • Runtime module

Public API

Types

  • Int16

Values

  • minValue
  • maxValue

Functions

toBitsNatSpec(x : Int16) : Nat

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

toInt(x : Int16) : Int

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

fromInt(x : Int) : Int16

Converts between the module type and the target representation.

Contract

requires x >= -32_768;
requires x <= 32_767;

Use when: crossing between this module's value and another representation.

fromIntWrap(x : Int) : Int16

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

fromInt8(x : Int8) : Int16

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

toInt8(x : Int16) : Int8

Converts between the module type and the target representation.

Contract

requires x >= (-128 : Int16);
requires x <= (127 : Int16);

Use when: crossing between this module's value and another representation.

fromInt32(x : Int32) : Int16

Converts between the module type and the target representation.

Contract

requires x >= (-32_768 : Int32);
requires x <= (32_767 : Int32);

Use when: crossing between this module's value and another representation.

toInt32(x : Int16) : Int32

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

fromNat16(x : Nat16) : Int16

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

toNat16(x : Int16) : Nat16

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

toText(x : Int16) : Text

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

abs(x : Int16) : Int16

Returns the absolute value.

Contract

requires x != minValue;
ensures result == (if (x < 0) { -x } else { x });

Use when: code or specifications need this operation with the documented contract.

min(x : Int16, y : Int16) : Int16

Returns the selected bound according to the module's ordering.

Contract

ensures result == (if (x < y) { x } else { y });

Use when: code or specifications need this operation with the documented contract.

max(x : Int16, y : Int16) : Int16

Returns the selected bound according to the module's ordering.

Contract

ensures result == (if (x < y) { y } else { x });

Use when: code or specifications need this operation with the documented contract.

equal(x : Int16, y : Int16) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x == y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

notEqual(x : Int16, y : Int16) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x != y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

less(x : Int16, y : Int16) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x < y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

lessOrEqual(x : Int16, y : Int16) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x <= y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

greater(x : Int16, y : Int16) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x > y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

greaterOrEqual(x : Int16, y : Int16) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x >= y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

compare(x : Int16, y : Int16) : Types.Order

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == #less ==> x < y;
ensures result == #equal ==> x == y;
ensures result == #greater ==> x > y;
ensures x < y ==> result == #less;
ensures x == y ==> result == #equal;
ensures x > y ==> result == #greater;

Use when: code or contracts need an explicit comparison helper instead of an operator.

neg(x : Int16) : Int16

Returns the arithmetic negation.

Contract

requires x != (-32_768 : Int16);
ensures result == -x;
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

add(x : Int16, y : Int16) : Int16

Returns the sum of the two operands.

Contract

requires (-32_768 : Int16) <= x + y;
requires x + y <= (32_767 : Int16);
ensures result == x + y;
ensures inRange(result);

Use when: the postcondition must relate the updated value to the previous one.

sub(x : Int16, y : Int16) : Int16

Returns the left operand minus the right operand.

Contract

requires (-32_768 : Int16) <= x - y;
requires x - y <= (32_767 : Int16);
ensures result == x - y;
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

mul(x : Int16, y : Int16) : Int16

Returns the product of the two operands.

Contract

requires (-32_768 : Int16) <= x * y;
requires x * y <= (32_767 : Int16);
ensures result == x * y;
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

div(x : Int16, y : Int16) : Int16

Returns the integer quotient, subject to the divisor precondition.

Contract

requires y != 0;
requires not (x == (-32_768 : Int16) and y == (-1 : Int16));
ensures y != 0 ==> result == x / y;
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

rem(x : Int16, y : Int16) : Int16

Returns the integer remainder, subject to the divisor precondition.

Contract

requires y != 0;
ensures y != 0 ==> result == x % y;
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

pow(x : Int16, y : Int16) : Int16

Returns the left operand raised to the supplied exponent.

Contract

requires y >= (0 : Int16);
requires y < (16 : Int16);
requires y <= (1 : Int16) or
(-32_768 <= Int.pow(toInt(x), toInt(y)) and Int.pow(toInt(x), toInt(y)) <= 32_767);
ensures (y >= 0 and y < (16 : Int16)) ==>
toBitsNatSpec(result) == Nat.pow(toBitsNatSpec(x), toBitsNatSpec(y)) % 65_536;
ensures y == 0 ==> result == 1;
ensures y == 1 ==> result == x;
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

bitnot(x : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

bitand(x : Int16, y : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

bitor(x : Int16, y : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

bitxor(x : Int16, y : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

bitshiftLeft(x : Int16, y : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures y == 0 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitshiftRight(x : Int16, y : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures y == 0 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitrotLeft(x : Int16, y : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures y == 0 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitrotRight(x : Int16, y : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures y == 0 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bittest(x : Int16, p : Nat) : Bool

Performs the corresponding bit-level operation.

Contract

ensures result == Prim.btstInt16(x, Prim.intToInt16(p));

Use when: code or specifications need this operation with the documented contract.

bitset(x : Int16, p : Nat) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures p >= 16 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitclear(x : Int16, p : Nat) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures p >= 16 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitflip(x : Int16, p : Nat) : Int16

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures p >= 16 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitcountNonZero(x : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures result == Prim.popcntInt16(x);
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

bitcountLeadingZero(x : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures result == Prim.clzInt16(x);
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

bitcountTrailingZero(x : Int16) : Int16

Performs the corresponding bit-level operation.

Contract

ensures result == Prim.ctzInt16(x);
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

explode(x : Int16) : (Nat8, Nat8)

Breaks the fixed-width integer into its byte components.

Contract

ensures result == Prim.explodeInt16(x);

Use when: code or specifications need this operation with the documented contract.

addWrap(x : Int16, y : Int16) : Int16

Returns wrapping addition for the fixed-width type.

Contract

ensures inRange(result);

Use when: the postcondition must relate the updated value to the previous one.

subWrap(x : Int16, y : Int16) : Int16

Returns wrapping subtraction for the fixed-width type.

Contract

ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

mulWrap(x : Int16, y : Int16) : Int16

Returns wrapping multiplication for the fixed-width type.

Contract

ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

powWrap(x : Int16, y : Int16) : Int16

Returns wrapping exponentiation for the fixed-width type.

Contract

ensures (y >= 0 and y < (16 : Int16)) ==>
toBitsNatSpec(result) == Nat.pow(toBitsNatSpec(x), toBitsNatSpec(y)) % 65_536;
ensures y == 0 ==> result == 1;
ensures y == 1 ==> result == x;
ensures inRange(result);

Use when: code or specifications need this operation with the documented contract.

range(fromInclusive : Int16, toExclusive : Int16) : Iter.Iter<Int16>

Creates an iterator over the requested numeric range or domain.

Contract

ensures Iter.Spec.forall<Int16>(pure func (k : Int16) : Bool =
Iter.Spec.contains<Int16>(result, k) ==> (k >= fromInclusive and k < toExclusive));
ensures Iter.Spec.forall<Int16>(pure func (k : Int16) : Bool =
(k >= fromInclusive and k < toExclusive) ==> Iter.Spec.contains<Int16>(result, k));

Use when: iteration boundaries should be explicit in code and examples.

rangeInclusive(from : Int16, to : Int16) : Iter.Iter<Int16>

Creates an iterator over the requested numeric range or domain.

Contract

ensures (from > to) ==> Iter.Spec.forall<Int16>(pure func (k : Int16) : Bool = not Iter.Spec.contains<Int16>(result, k));
ensures (from <= to) ==> Iter.Spec.contains<Int16>(result, from);
ensures Iter.Spec.forall<Int16>(pure func (k : Int16) : Bool =
Iter.Spec.contains<Int16>(result, k) ==> (k >= from and k <= to));
ensures Iter.Spec.forall<Int16>(pure func (k : Int16) : Bool =
(k >= from and k <= to) ==> Iter.Spec.contains<Int16>(result, k));

Use when: iteration boundaries should be explicit in code and examples.

allValues() : Iter.Iter<Int16>

Creates an iterator over the requested numeric range or domain.

Contract

ensures Iter.Spec.forall<Int16>(pure func (k : Int16) : Bool =
Iter.Spec.contains<Int16>(result, k) ==> (k >= minValue and k <= maxValue));
ensures Iter.Spec.forall<Int16>(pure func (k : Int16) : Bool =
(k >= minValue and k <= maxValue) ==> Iter.Spec.contains<Int16>(result, k));

Use when: iteration boundaries should be explicit in code and examples.

Summary

  • Runtime module under mo:core/Int16.
  • Exposes 51 public functions.