abla/int256
Signed 256-bit integers, in two's complement. int256 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 i256 in a function's scalar plan, and 32 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/int256"
fun main {
val a = int256(-7)
println("${a / int256(2)} ${a % int256(2)} ${a >> 1} ${a.toHex()} ${a < int256(0)}")
println("${int256Min() / int256(-1) == int256Min()} ${int256Max().addChecked(int256(1)) == null} ${int256FromDecimal("-5") == int256(-5)}")
println("${int256FromBytes(a.toBytes()) == a} ${int256FromBytesLittleEndian(a.toBytesLittleEndian()) == a} ${a.toBytes().size}")
}
// output:
// -3 -1 -4 -0x7 true
// true true true
// true true 32
What it does
+ - * & | ^ ~ -x | wrap at 256 bits, as every fixed width's do; both sides must be int256 |
|---|---|
/ % | 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, int256Min() / int256(-1), wraps to itself and its remainder is 0, as for every signed width |
<< >> | a count of any integer type; negative or at least 256 gives zero, as for every width, >> included: -1 >> 255 is -1 and -1 >> 256 is 0, the language's rule (language.md). >> is arithmetic below that. a.ushr(count) is the logical shift, >>> |
== != < <= > >= | both sides int256, signed |
"$x" | decimal with a minus, as any integer |
int256(x) | from any integer width; a signed source extends its sign, an unsigned one its zeros |
int256Max(), int256Min(), int256Words(...) | 2^255 - 1 and -2^255; 8 u32s of the two's complement, most significant first, for a constant too big for a literal |
a.addChecked(b), subChecked, mulChecked | int256?: 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(), int256FromDecimal(text) | an optional leading -, then digits only: no +, space or point; leading zeros are allowed; null otherwise, or out of range |
a.toHex(), int256FromHex(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(), int256FromBytes(bytes) | 32 bytes of the two's complement, big-endian; reads up to 32, extending the sign of the first byte |
a.toBytesLittleEndian(), int256FromBytesLittleEndian(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 + 1andx == 0arearithmetic.typeandcomparison.type, as for vectors:x + int256(1).
A literal too big for an int comes through int256Words or int256FromHex.
- 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 aMapkey (use.toBytes()), or go throughu64(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 int256 (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 signed division is LLVM's sdiv, and Min / -1 is dealt with before it, since it is undefined there.
Built from compiler source ba5d6b7d. Examples and generated signatures follow that source revision.