abla/int128

Signed 128-bit integers, in two's complement. int128 is a type of the language, like i64; what's done with it comes with the import. It is the sibling of uint256, and works the same way: an LLVM i128 in a function's scalar plan, and 16 bytes of string data where a value goes boxed (a field, an array element, a capture, a generic, a T?). A program that doesn't import it has none of it.

import "abla/io"
import "abla/int128"

fun main {
    val a = int128(-7)
    println("${a / int128(2)} ${a % int128(2)} ${a >> 1} ${a.toHex()} ${a < int128(0)}")
    println("${int128Min() / int128(-1) == int128Min()} ${int128Max().addChecked(int128(1)) == null} ${int128FromDecimal("-5") == int128(-5)}")
    println("${int128FromBytes(a.toBytes()) == a} ${int128FromBytesLittleEndian(a.toBytesLittleEndian()) == a} ${a.toBytes().size}")
}
// output:
// -3 -1 -4 -0x7 true
// true true true
// true true 16

What it does

+ - * & | ^ ~ -xwrap at 128 bits, as every fixed width's do; both sides must be int128
/ %signed, truncating toward zero (-7 / 2 is -3, -7 % 2 is -1); by zero is the language's panic, division by zero, exit 101. The one quotient too large, int128Min() / int128(-1), wraps to itself and its remainder is 0, as for every signed width
<< >>a count of any integer type; negative or at least 128 gives zero, as for every width, >> included: -1 >> 127 is -1 and -1 >> 128 is 0, the language's rule (language.md). >> is arithmetic below that. a.ushr(count) is the logical shift, >>>
== != < <= > >=both sides int128, signed
"$x"decimal with a minus, as any integer
int128(x)from any integer width; a signed source extends its sign, an unsigned one its zeros
int128Max(), int128Min(), int128Words(...)2^127 - 1 and -2^127; 4 u32s of the two's complement, most significant first, for a constant too big for a literal
a.addChecked(b), subChecked, mulCheckedint128?: null if it doesn't fit
a.toU64(), toInt(), toI32(), toU32(), toU16(), toU8()the low bits, as every narrowing
a.toU64OrNull(), toIntOrNull(), toI32OrNull(), toU32OrNull(), ...null if it doesn't fit: toU64OrNull fits 0 to 2^64 - 1, toIntOrNull fits an int
a.toDecimal(), int128FromDecimal(text)an optional leading -, then digits only: no +, space or point; leading zeros are allowed; null otherwise, or out of range
a.toHex(), int128FromHex(text)0x, lower case, no leading zeros, and a - before the 0x for a negative one; reads an optional -, then with or without 0x, in either case
a.toBytes(), int128FromBytes(bytes)16 bytes of the two's complement, big-endian; reads up to 16, extending the sign of the first byte
a.toBytesLittleEndian(), int128FromBytesLittleEndian(bytes)the same little-endian: the least significant byte first, extending the sign of the last
a.isZero(), isNegative(), signum(), abs(), bit(i)abs() of the smallest value is itself; bit(i) is the two's complement's, and beyond the width the sign

What it doesn't do

  • Mix with other types. x + 1 and x == 0 are arithmetic.type and comparison.type, as for vectors: x + int128(1).

A literal too big for an int comes through int128Words or int128FromHex.

  • Keep secrets: it is not constant-time. Its comparison and division take a time that depends on the value, as

uint256's do, and tools/test-ct.sh does not read it. A key belongs in abla/ct (ct.md: secretU32, secretU64) and the bignum helpers.

  • Cross an `@export`, be the subject of a when, be a Map key (use .toBytes()), or go through u64(x) (use

x.toU64()). All of these are as uint256's (uint256.md).

  • Convert to the other wide types. Go through a u64, or bytes.

At compile time and on each target

# runs all of it with the same results as a program's. WIDE_FULL=1 tools/test-wide-integers.sh COMPILER int128 (make test-wide-integers-full; make test runs a quick version) runs it against JavaScript's BigInt on fresh random values and edge values each time (signed values included: the smallest, the largest, -1, and every carry), on x86-64 (release, --fast, -g and ablac run), Wasm under Node, AArch64, RISC-V 32 and 64 and Xtensa, as uint256 does, with no C library.

A 128-bit division, and a variable shift off x86-64 and AArch64, is done at 256 bits in a small function of its own and narrowed: at 128 bits they are calls to __udivti3 and __lshrti3, which nothing here links. It is slower than a native 64-bit divide. Multiplication is native on x86-64 and AArch64 and words elsewhere.

A signed division is LLVM's sdiv at 256 bits, where Min / -1 is an ordinary quotient that narrows to Min.

Built from compiler source ba5d6b7d. Examples and generated signatures follow that source revision.