293 lines
9.3 KiB
Plaintext
293 lines
9.3 KiB
Plaintext
#! @file integers.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the operations of integers processors give but the operators do not:
|
||
|
|
#! rotating; adding, subtracting, and multiplying with a check or with
|
||
|
|
#! saturation; powers, greatest common divisors, square roots, and dividing
|
||
|
|
#! toward negative infinity. Each works for every integer type, signed or
|
||
|
|
#! unsigned, of any width. Counting bits and swapping bytes are `core`'s.
|
||
|
|
|
||
|
|
import core::intrinsics;
|
||
|
|
module std::math;
|
||
|
|
|
||
|
|
|
||
|
|
#! An integer's bits turned toward its top, those leaving the top coming back in
|
||
|
|
#! at the bottom.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @param by How many places, of which only the remainder dividing by the
|
||
|
|
#! type's width counts.
|
||
|
|
#! @returns The integer turned.
|
||
|
|
pub def rotate_left<T>(x: T, by: u32): T where T: integer {
|
||
|
|
val width := size_of::<T>() * 8;
|
||
|
|
val n := by as u64 % width;
|
||
|
|
return x if n == 0;
|
||
|
|
val mask := width_mask::<T>();
|
||
|
|
val bits := x as u64 & mask;
|
||
|
|
return (((bits << n) | (bits >> (width - n))) & mask) as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! An integer's bits turned toward its bottom, those leaving the bottom coming
|
||
|
|
#! back in at the top.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @param by How many places, of which only the remainder dividing by the
|
||
|
|
#! type's width counts.
|
||
|
|
#! @returns The integer turned.
|
||
|
|
pub def rotate_right<T>(x: T, by: u32): T where T: integer {
|
||
|
|
val width := size_of::<T>() * 8;
|
||
|
|
val n := by as u64 % width;
|
||
|
|
return x if n == 0;
|
||
|
|
return rotate_left(x, (width - n) as u32);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a + b`, where it fits the type.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a One integer.
|
||
|
|
#! @param b The other.
|
||
|
|
#! @returns The sum, or `nothing` where it overflows.
|
||
|
|
pub def checked_add<T>(a: T, b: T): maybe<T> where T: integer {
|
||
|
|
val sum := a + b;
|
||
|
|
if is_signed::<T>() {
|
||
|
|
return nothing {} if (a < 0) == (b < 0) && (sum < 0) != (a < 0);
|
||
|
|
return sum;
|
||
|
|
}
|
||
|
|
return nothing {} if sum < a;
|
||
|
|
return sum;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a - b`, where it fits the type.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a The integer subtracted from.
|
||
|
|
#! @param b The integer subtracted.
|
||
|
|
#! @returns The difference, or `nothing` where it overflows.
|
||
|
|
pub def checked_sub<T>(a: T, b: T): maybe<T> where T: integer {
|
||
|
|
val difference := a - b;
|
||
|
|
if is_signed::<T>() {
|
||
|
|
return nothing {} if (a < 0) != (b < 0) && (difference < 0) != (a < 0);
|
||
|
|
return difference;
|
||
|
|
}
|
||
|
|
return nothing {} if a < b;
|
||
|
|
return difference;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a * b`, where it fits the type.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a One integer.
|
||
|
|
#! @param b The other.
|
||
|
|
#! @returns The product, or `nothing` where it overflows.
|
||
|
|
pub def checked_mul<T>(a: T, b: T): maybe<T> where T: integer {
|
||
|
|
return 0 as T if a == 0;
|
||
|
|
# -1 times the most negative integer is the one product dividing back cannot
|
||
|
|
# check, since that division overflows too.
|
||
|
|
if is_signed::<T>() && a == ~(0 as T) {
|
||
|
|
return nothing {} if b == least_of::<T>();
|
||
|
|
return -b;
|
||
|
|
}
|
||
|
|
# The product wrapped, which divides back to `b` only where it did not wrap.
|
||
|
|
val product := a * b;
|
||
|
|
return nothing {} if product / a != b;
|
||
|
|
return product;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! An integer raised to a power, where it fits the type.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @param exponent The power.
|
||
|
|
#! @returns x^exponent, 1 where the power is 0, or `nothing` where it overflows.
|
||
|
|
pub def checked_pow<T>(x: T, exponent: u32): maybe<T> where T: integer {
|
||
|
|
var result := 1 as T;
|
||
|
|
var base := x;
|
||
|
|
var left := exponent;
|
||
|
|
while left != 0 {
|
||
|
|
if (left & 1) != 0 {
|
||
|
|
result = checked_mul(result, base) else { return nothing {}; };
|
||
|
|
}
|
||
|
|
left >>= 1;
|
||
|
|
if left != 0 {
|
||
|
|
base = checked_mul(base, base) else { return nothing {}; };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a + b`, held at the type's greatest or least value where it would pass it.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a One integer.
|
||
|
|
#! @param b The other.
|
||
|
|
#! @returns The sum, saturated.
|
||
|
|
pub def saturating_add<T>(a: T, b: T): T where T: integer {
|
||
|
|
if checked_add(a, b) is some(sum) {
|
||
|
|
return sum;
|
||
|
|
}
|
||
|
|
return is_signed::<T>() && b < 0 ? least_of::<T>() : greatest_of::<T>();
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a - b`, held at the type's greatest or least value where it would pass it.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a The integer subtracted from.
|
||
|
|
#! @param b The integer subtracted.
|
||
|
|
#! @returns The difference, saturated.
|
||
|
|
pub def saturating_sub<T>(a: T, b: T): T where T: integer {
|
||
|
|
if checked_sub(a, b) is some(difference) {
|
||
|
|
return difference;
|
||
|
|
}
|
||
|
|
return is_signed::<T>() && b < 0 ? greatest_of::<T>() : least_of::<T>();
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a * b`, held at the type's greatest or least value where it would pass it.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a One integer.
|
||
|
|
#! @param b The other.
|
||
|
|
#! @returns The product, saturated.
|
||
|
|
pub def saturating_mul<T>(a: T, b: T): T where T: integer {
|
||
|
|
if checked_mul(a, b) is some(product) {
|
||
|
|
return product;
|
||
|
|
}
|
||
|
|
return is_signed::<T>() && (a < 0) != (b < 0) ? least_of::<T>() : greatest_of::<T>();
|
||
|
|
}
|
||
|
|
|
||
|
|
#! An integer raised to a power, wrapping where it overflows, as `*` does.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @param exponent The power.
|
||
|
|
#! @returns x^exponent, as the type holds it; 1 where the power is 0.
|
||
|
|
pub def int_pow<T>(x: T, exponent: u32): T where T: integer {
|
||
|
|
var result := 1 as T;
|
||
|
|
var base := x;
|
||
|
|
var left := exponent;
|
||
|
|
while left != 0 {
|
||
|
|
if (left & 1) != 0 {
|
||
|
|
result = result * base;
|
||
|
|
}
|
||
|
|
left >>= 1;
|
||
|
|
if left != 0 {
|
||
|
|
base = base * base;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The greatest common divisor of two integers: the greatest integer dividing
|
||
|
|
#! both, whatever their signs.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a One integer.
|
||
|
|
#! @param b The other.
|
||
|
|
#! @returns The divisor, never below zero, and 0 where both are 0. Where it is
|
||
|
|
#! the magnitude of a signed type's most negative integer, which the
|
||
|
|
#! type cannot hold, it wraps to that integer.
|
||
|
|
pub def gcd<T>(a: T, b: T): T where T: integer {
|
||
|
|
return word_gcd(magnitude_of(a), magnitude_of(b)) as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The least common multiple of two integers: the least integer above zero both
|
||
|
|
#! divide.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a One integer.
|
||
|
|
#! @param b The other.
|
||
|
|
#! @returns The multiple, never below zero, and 0 where either is 0. It wraps
|
||
|
|
#! where the type cannot hold it, as `*` does.
|
||
|
|
pub def lcm<T>(a: T, b: T): T where T: integer {
|
||
|
|
val x := magnitude_of(a);
|
||
|
|
val y := magnitude_of(b);
|
||
|
|
return 0 as T if x == 0 || y == 0;
|
||
|
|
return ((x / word_gcd(x, y)) * y) as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The integer square root: the greatest integer whose square is not above an
|
||
|
|
#! integer.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @returns The root, and 0 below zero.
|
||
|
|
pub def isqrt<T>(x: T): T where T: integer {
|
||
|
|
return 0 as T if x <= 0;
|
||
|
|
val n := x as u64 & width_mask::<T>();
|
||
|
|
# The root of the nearest double is within one or two of the root.
|
||
|
|
var root := square_root(n as f64) as u64;
|
||
|
|
while root > 0 && root > n / root {
|
||
|
|
root--;
|
||
|
|
}
|
||
|
|
while root + 1 <= n / (root + 1) {
|
||
|
|
root++;
|
||
|
|
}
|
||
|
|
return root as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a / b`, rounded toward negative infinity rather than toward zero, so -7 / 2
|
||
|
|
#! gives -4 where `/` gives -3. Of unsigned integers, it is `/`.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a The dividend.
|
||
|
|
#! @param b The divisor, which stops the program where it is zero, as `/`
|
||
|
|
#! does.
|
||
|
|
#! @returns The quotient.
|
||
|
|
pub def div_floor<T>(a: T, b: T): T where T: integer {
|
||
|
|
val quotient := a / b;
|
||
|
|
if is_signed::<T>() && quotient * b != a && (a < 0) != (b < 0) {
|
||
|
|
return quotient - 1;
|
||
|
|
}
|
||
|
|
return quotient;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What is left dividing toward negative infinity, which has the divisor's sign,
|
||
|
|
#! so -7 and 2 give 1 where `%` gives -1. `div_floor(a, b) * b + rem_floor(a, b)`
|
||
|
|
#! is `a`. Of unsigned integers, it is `%`.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param a The dividend.
|
||
|
|
#! @param b The divisor, which stops the program where it is zero, as `%`
|
||
|
|
#! does.
|
||
|
|
#! @returns The remainder.
|
||
|
|
pub def rem_floor<T>(a: T, b: T): T where T: integer {
|
||
|
|
val rest := a % b;
|
||
|
|
if is_signed::<T>() && rest != 0 && (rest < 0) != (b < 0) {
|
||
|
|
return rest + b;
|
||
|
|
}
|
||
|
|
return rest;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# What the functions above know of a type of integer.
|
||
|
|
|
||
|
|
# The bits of a type, low ones set, as a word.
|
||
|
|
def width_mask<T>(): u64 where T: integer {
|
||
|
|
if const size_of::<T>() == 8 {
|
||
|
|
return 0xFFFFFFFFFFFFFFFFu;
|
||
|
|
}
|
||
|
|
return (1u << (size_of::<T>() * 8)) - 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Whether a type of integer is signed, as its every bit set is below zero.
|
||
|
|
def is_signed<T>(): bool where T: integer {
|
||
|
|
return ~(0 as T) < 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The greatest value of a type of integer.
|
||
|
|
def greatest_of<T>(): T where T: integer {
|
||
|
|
return is_signed::<T>() ? (width_mask::<T>() >> 1) as T : ~(0 as T);
|
||
|
|
}
|
||
|
|
|
||
|
|
# The least value of a type of integer.
|
||
|
|
def least_of<T>(): T where T: integer {
|
||
|
|
return is_signed::<T>() ? (1u << (size_of::<T>() * 8 - 1)) as T : 0 as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
# An integer's distance from zero, as a word, which holds it whatever its type.
|
||
|
|
def magnitude_of<T>(x: T): u64 where T: integer {
|
||
|
|
if is_signed::<T>() && x < 0 {
|
||
|
|
return 0 - (x as i64) as u64;
|
||
|
|
}
|
||
|
|
return x as u64 & width_mask::<T>();
|
||
|
|
}
|
||
|
|
|
||
|
|
# Euclid's greatest common divisor, of words.
|
||
|
|
def word_gcd(a: u64, b: u64): u64 {
|
||
|
|
var x := a;
|
||
|
|
var y := b;
|
||
|
|
while y != 0 {
|
||
|
|
val rest := x % y;
|
||
|
|
x = y;
|
||
|
|
y = rest;
|
||
|
|
}
|
||
|
|
return x;
|
||
|
|
}
|