use num::cast::AsPrimitive;
use num::{BigInt, FromPrimitive, Num, ToPrimitive};
use std::cmp::Ordering;
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
pub struct i256 {
low: u128,
high: i128,
}
impl std::fmt::Debug for i256 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self)
}
}
impl std::fmt::Display for i256 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", BigInt::from_signed_bytes_le(&self.to_le_bytes()))
}
}
impl PartialOrd for i256 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for i256 {
fn cmp(&self, other: &Self) -> Ordering {
self.high.cmp(&other.high).then(self.low.cmp(&other.low))
}
}
impl i256 {
pub const ZERO: Self = i256 { low: 0, high: 0 };
pub const ONE: Self = i256 { low: 1, high: 0 };
pub const MINUS_ONE: Self = i256 {
low: u128::MAX,
high: -1,
};
pub const MAX: Self = i256 {
low: u128::MAX,
high: i128::MAX,
};
pub const MIN: Self = i256 {
low: u128::MIN,
high: i128::MIN,
};
#[inline]
pub const fn from_le_bytes(b: [u8; 32]) -> Self {
let (low, high) = split_array(b);
Self {
high: i128::from_le_bytes(high),
low: u128::from_le_bytes(low),
}
}
#[inline]
pub const fn from_be_bytes(b: [u8; 32]) -> Self {
let (high, low) = split_array(b);
Self {
high: i128::from_be_bytes(high),
low: u128::from_be_bytes(low),
}
}
pub const fn from_i128(v: i128) -> Self {
Self::from_parts(v as u128, v >> 127)
}
#[inline]
pub fn from_string(value_str: &str) -> Option<Self> {
let numbers = BigInt::from_str_radix(value_str, 10).ok()?;
let (integer, overflow) = Self::from_bigint_with_overflow(numbers);
if overflow {
None
} else {
Some(integer)
}
}
pub fn from_f64(v: f64) -> Option<Self> {
BigInt::from_f64(v).and_then(|i| {
let (integer, overflow) = i256::from_bigint_with_overflow(i);
if overflow {
None
} else {
Some(integer)
}
})
}
#[inline]
pub const fn from_parts(low: u128, high: i128) -> Self {
Self { low, high }
}
pub const fn to_parts(self) -> (u128, i128) {
(self.low, self.high)
}
pub fn to_i128(self) -> Option<i128> {
let as_i128 = self.low as i128;
let high_negative = self.high < 0;
let low_negative = as_i128 < 0;
let high_valid = self.high == -1 || self.high == 0;
(high_negative == low_negative && high_valid).then_some(self.low as i128)
}
#[inline]
pub const fn to_le_bytes(self) -> [u8; 32] {
let low = self.low.to_le_bytes();
let high = self.high.to_le_bytes();
let mut t = [0; 32];
let mut i = 0;
while i != 16 {
t[i] = low[i];
t[i + 16] = high[i];
i += 1;
}
t
}
#[inline]
pub const fn to_be_bytes(self) -> [u8; 32] {
let low = self.low.to_be_bytes();
let high = self.high.to_be_bytes();
let mut t = [0; 32];
let mut i = 0;
while i != 16 {
t[i] = high[i];
t[i + 16] = low[i];
i += 1;
}
t
}
fn from_bigint_with_overflow(v: BigInt) -> (Self, bool) {
let v_bytes = v.to_signed_bytes_le();
match v_bytes.len().cmp(&32) {
Ordering::Less => {
let mut bytes = if num::Signed::is_negative(&v) {
[255_u8; 32]
} else {
[0; 32]
};
bytes[0..v_bytes.len()].copy_from_slice(&v_bytes[..v_bytes.len()]);
(Self::from_le_bytes(bytes), false)
}
Ordering::Equal => (Self::from_le_bytes(v_bytes.try_into().unwrap()), false),
Ordering::Greater => {
(Self::from_le_bytes(v_bytes[..32].try_into().unwrap()), true)
}
}
}
#[inline]
pub fn wrapping_abs(self) -> Self {
let sa = self.high >> 127;
let sa = Self::from_parts(sa as u128, sa);
Self::from_parts(self.low ^ sa.low, self.high ^ sa.high).wrapping_sub(sa)
}
#[inline]
pub fn checked_abs(self) -> Option<Self> {
(self != Self::MIN).then(|| self.wrapping_abs())
}
#[inline]
pub fn wrapping_neg(self) -> Self {
Self::from_parts(!self.low, !self.high).wrapping_add(i256::ONE)
}
#[inline]
pub fn checked_neg(self) -> Option<Self> {
(self != Self::MIN).then(|| self.wrapping_neg())
}
#[inline]
pub fn wrapping_add(self, other: Self) -> Self {
let (low, carry) = self.low.overflowing_add(other.low);
let high = self.high.wrapping_add(other.high).wrapping_add(carry as _);
Self { low, high }
}
#[inline]
pub fn checked_add(self, other: Self) -> Option<Self> {
let (low, carry) = self.low.overflowing_add(other.low);
let high = self.high.checked_add(other.high)?.checked_add(carry as _)?;
Some(Self { low, high })
}
#[inline]
pub fn wrapping_sub(self, other: Self) -> Self {
let (low, carry) = self.low.overflowing_sub(other.low);
let high = self.high.wrapping_sub(other.high).wrapping_sub(carry as _);
Self { low, high }
}
#[inline]
pub fn checked_sub(self, other: Self) -> Option<Self> {
let (low, carry) = self.low.overflowing_sub(other.low);
let high = self.high.checked_sub(other.high)?.checked_sub(carry as _)?;
Some(Self { low, high })
}
#[inline]
pub fn wrapping_mul(self, other: Self) -> Self {
let (low, high) = mulx(self.low, other.low);
let hl = self.high.wrapping_mul(other.low as i128);
let lh = (self.low as i128).wrapping_mul(other.high);
Self {
low,
high: (high as i128).wrapping_add(hl).wrapping_add(lh),
}
}
#[inline]
pub fn checked_mul(self, other: Self) -> Option<Self> {
let l_sa = self.high >> 127;
let r_sa = other.high >> 127;
let out_sa = l_sa ^ r_sa;
let l_abs = self.wrapping_abs();
let r_abs = other.wrapping_abs();
if l_abs.high != 0 && r_abs.high != 0 {
return None;
}
let (low, high) = mulx(l_abs.low, r_abs.low);
let hl = (l_abs.high as u128).checked_mul(r_abs.low)?;
let lh = l_abs.low.checked_mul(r_abs.high as u128)?;
let high: i128 = high.checked_add(hl)?.checked_add(lh)?.try_into().ok()?;
let (low, c) = (low ^ out_sa as u128).overflowing_sub(out_sa as u128);
let high = (high ^ out_sa).wrapping_sub(out_sa).wrapping_sub(c as i128);
Some(Self { low, high })
}
#[inline]
pub fn wrapping_div(self, other: Self) -> Self {
let l = BigInt::from_signed_bytes_le(&self.to_le_bytes());
let r = BigInt::from_signed_bytes_le(&other.to_le_bytes());
Self::from_bigint_with_overflow(l / r).0
}
#[inline]
pub fn checked_div(self, other: Self) -> Option<Self> {
let l = BigInt::from_signed_bytes_le(&self.to_le_bytes());
let r = BigInt::from_signed_bytes_le(&other.to_le_bytes());
let (val, overflow) = Self::from_bigint_with_overflow(l / r);
(!overflow).then_some(val)
}
#[inline]
pub fn wrapping_rem(self, other: Self) -> Self {
let l = BigInt::from_signed_bytes_le(&self.to_le_bytes());
let r = BigInt::from_signed_bytes_le(&other.to_le_bytes());
Self::from_bigint_with_overflow(l % r).0
}
#[inline]
pub fn checked_rem(self, other: Self) -> Option<Self> {
if other == Self::ZERO {
return None;
}
let l = BigInt::from_signed_bytes_le(&self.to_le_bytes());
let r = BigInt::from_signed_bytes_le(&other.to_le_bytes());
let (val, overflow) = Self::from_bigint_with_overflow(l % r);
(!overflow).then_some(val)
}
#[inline]
pub fn checked_pow(self, mut exp: u32) -> Option<Self> {
if exp == 0 {
return Some(i256::from_i128(1));
}
let mut base = self;
let mut acc: Self = i256::from_i128(1);
while exp > 1 {
if (exp & 1) == 1 {
acc = acc.checked_mul(base)?;
}
exp /= 2;
base = base.checked_mul(base)?;
}
acc.checked_mul(base)
}
#[inline]
pub fn wrapping_pow(self, mut exp: u32) -> Self {
if exp == 0 {
return i256::from_i128(1);
}
let mut base = self;
let mut acc: Self = i256::from_i128(1);
while exp > 1 {
if (exp & 1) == 1 {
acc = acc.wrapping_mul(base);
}
exp /= 2;
base = base.wrapping_mul(base);
}
acc.wrapping_mul(base)
}
}
const fn split_array<const N: usize, const M: usize>(
vals: [u8; N],
) -> ([u8; M], [u8; M]) {
let mut a = [0; M];
let mut b = [0; M];
let mut i = 0;
while i != M {
a[i] = vals[i];
b[i] = vals[i + M];
i += 1;
}
(a, b)
}
#[inline]
fn mulx(a: u128, b: u128) -> (u128, u128) {
let split = |a: u128| (a & (u64::MAX as u128), a >> 64);
const MASK: u128 = u64::MAX as _;
let (a_low, a_high) = split(a);
let (b_low, b_high) = split(b);
let (mut low, mut carry) = split(a_low * b_low);
carry += a_high * b_low;
low += carry << 64;
let mut high = carry >> 64;
carry = low >> 64;
low &= MASK;
carry += b_high * a_low;
low += carry << 64;
high += carry >> 64;
high += a_high * b_high;
(low, high)
}
macro_rules! derive_op {
($t:ident, $op:ident, $wrapping:ident, $checked:ident) => {
impl std::ops::$t for i256 {
type Output = i256;
#[cfg(debug_assertions)]
fn $op(self, rhs: Self) -> Self::Output {
self.$checked(rhs).expect("i256 overflow")
}
#[cfg(not(debug_assertions))]
fn $op(self, rhs: Self) -> Self::Output {
self.$wrapping(rhs)
}
}
};
}
derive_op!(Add, add, wrapping_add, checked_add);
derive_op!(Sub, sub, wrapping_sub, checked_sub);
derive_op!(Mul, mul, wrapping_mul, checked_mul);
derive_op!(Div, div, wrapping_div, checked_div);
derive_op!(Rem, rem, wrapping_rem, checked_rem);
impl std::ops::Neg for i256 {
type Output = i256;
#[cfg(debug_assertions)]
fn neg(self) -> Self::Output {
self.checked_neg().expect("i256 overflow")
}
#[cfg(not(debug_assertions))]
fn neg(self) -> Self::Output {
self.wrapping_neg()
}
}
macro_rules! define_as_primitive {
($native_ty:ty) => {
impl AsPrimitive<i256> for $native_ty {
fn as_(self) -> i256 {
i256::from_i128(self as i128)
}
}
};
}
define_as_primitive!(i8);
define_as_primitive!(i16);
define_as_primitive!(i32);
define_as_primitive!(i64);
define_as_primitive!(u8);
define_as_primitive!(u16);
define_as_primitive!(u32);
define_as_primitive!(u64);
impl ToPrimitive for i256 {
fn to_i64(&self) -> Option<i64> {
let as_i128 = self.low as i128;
let high_negative = self.high < 0;
let low_negative = as_i128 < 0;
let high_valid = self.high == -1 || self.high == 0;
if high_negative == low_negative && high_valid {
let (low_bytes, high_bytes) = split_array(u128::to_le_bytes(self.low));
let high = i64::from_le_bytes(high_bytes);
let low = i64::from_le_bytes(low_bytes);
let high_negative = high < 0;
let low_negative = low < 0;
let high_valid = self.high == -1 || self.high == 0;
(high_negative == low_negative && high_valid).then_some(low)
} else {
None
}
}
fn to_u64(&self) -> Option<u64> {
let as_i128 = self.low as i128;
let high_negative = self.high < 0;
let low_negative = as_i128 < 0;
let high_valid = self.high == -1 || self.high == 0;
if high_negative == low_negative && high_valid {
self.low.to_u64()
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use num::{BigInt, FromPrimitive, Signed, ToPrimitive};
use rand::{thread_rng, Rng};
use std::ops::Neg;
#[test]
fn test_signed_cmp() {
let a = i256::from_parts(i128::MAX as u128, 12);
let b = i256::from_parts(i128::MIN as u128, 12);
assert!(a < b);
let a = i256::from_parts(i128::MAX as u128, 12);
let b = i256::from_parts(i128::MIN as u128, -12);
assert!(a > b);
}
#[test]
fn test_to_i128() {
let vals = [
BigInt::from_i128(-1).unwrap(),
BigInt::from_i128(i128::MAX).unwrap(),
BigInt::from_i128(i128::MIN).unwrap(),
BigInt::from_u128(u128::MIN).unwrap(),
BigInt::from_u128(u128::MAX).unwrap(),
];
for v in vals {
let (t, overflow) = i256::from_bigint_with_overflow(v.clone());
assert!(!overflow);
assert_eq!(t.to_i128(), v.to_i128(), "{} vs {}", v, t);
}
}
fn test_ops(il: i256, ir: i256) {
let bl = BigInt::from_signed_bytes_le(&il.to_le_bytes());
let br = BigInt::from_signed_bytes_le(&ir.to_le_bytes());
assert_eq!(il.cmp(&ir), bl.cmp(&br), "{} cmp {}", bl, br);
assert_eq!(i256::from_le_bytes(il.to_le_bytes()), il);
assert_eq!(i256::from_be_bytes(il.to_be_bytes()), il);
assert_eq!(i256::from_le_bytes(ir.to_le_bytes()), ir);
assert_eq!(i256::from_be_bytes(ir.to_be_bytes()), ir);
assert_eq!(il.to_i128(), bl.to_i128(), "{}", bl);
assert_eq!(ir.to_i128(), br.to_i128(), "{}", br);
let (abs, overflow) = i256::from_bigint_with_overflow(bl.abs());
assert_eq!(il.wrapping_abs(), abs);
assert_eq!(il.checked_abs().is_none(), overflow);
let (abs, overflow) = i256::from_bigint_with_overflow(br.abs());
assert_eq!(ir.wrapping_abs(), abs);
assert_eq!(ir.checked_abs().is_none(), overflow);
let (neg, overflow) = i256::from_bigint_with_overflow(bl.clone().neg());
assert_eq!(il.wrapping_neg(), neg);
assert_eq!(il.checked_neg().is_none(), overflow);
let (neg, overflow) = i256::from_bigint_with_overflow(br.clone().neg());
assert_eq!(ir.wrapping_neg(), neg);
assert_eq!(ir.checked_neg().is_none(), overflow);
let actual = il.wrapping_add(ir);
let (expected, overflow) =
i256::from_bigint_with_overflow(bl.clone() + br.clone());
assert_eq!(actual, expected);
let checked = il.checked_add(ir);
match overflow {
true => assert!(checked.is_none()),
false => assert_eq!(checked.unwrap(), actual),
}
let actual = il.wrapping_sub(ir);
let (expected, overflow) =
i256::from_bigint_with_overflow(bl.clone() - br.clone());
assert_eq!(actual.to_string(), expected.to_string());
let checked = il.checked_sub(ir);
match overflow {
true => assert!(checked.is_none()),
false => assert_eq!(checked.unwrap(), actual),
}
let actual = il.wrapping_mul(ir);
let (expected, overflow) =
i256::from_bigint_with_overflow(bl.clone() * br.clone());
assert_eq!(actual.to_string(), expected.to_string());
let checked = il.checked_mul(ir);
match overflow {
true => assert!(
checked.is_none(),
"{} * {} = {} vs {} * {} = {}",
il,
ir,
actual,
bl,
br,
expected
),
false => assert_eq!(
checked.unwrap(),
actual,
"{} * {} = {} vs {} * {} = {}",
il,
ir,
actual,
bl,
br,
expected
),
}
for exp in vec![0, 1, 3, 8, 100].into_iter() {
let actual = il.wrapping_pow(exp);
let (expected, overflow) =
i256::from_bigint_with_overflow(bl.clone().pow(exp));
assert_eq!(actual.to_string(), expected.to_string());
let checked = il.checked_pow(exp);
match overflow {
true => assert!(
checked.is_none(),
"{} ^ {} = {} vs {} * {} = {}",
il,
exp,
actual,
bl,
exp,
expected
),
false => assert_eq!(
checked.unwrap(),
actual,
"{} ^ {} = {} vs {} * {} = {}",
il,
exp,
actual,
bl,
exp,
expected
),
}
}
}
#[test]
fn test_i256() {
let candidates = [
i256::from_parts(0, 0),
i256::from_parts(0, 1),
i256::from_parts(0, -1),
i256::from_parts(u128::MAX, 1),
i256::from_parts(u128::MAX, -1),
i256::from_parts(0, 1),
i256::from_parts(0, -1),
i256::from_parts(100, 32),
];
for il in candidates {
for ir in candidates {
test_ops(il, ir)
}
}
}
#[test]
#[cfg_attr(miri, ignore)]
fn test_i256_fuzz() {
let mut rng = thread_rng();
for _ in 0..1000 {
let mut l = [0_u8; 32];
let len = rng.gen_range(0..32);
l.iter_mut().take(len).for_each(|x| *x = rng.gen());
let mut r = [0_u8; 32];
let len = rng.gen_range(0..32);
r.iter_mut().take(len).for_each(|x| *x = rng.gen());
test_ops(i256::from_le_bytes(l), i256::from_le_bytes(r))
}
}
#[test]
fn test_i256_to_primitive() {
let a = i256::MAX;
assert!(a.to_i64().is_none());
assert!(a.to_u64().is_none());
let a = i256::from_i128(i128::MAX);
assert!(a.to_i64().is_none());
assert!(a.to_u64().is_none());
let a = i256::from_i128(i64::MAX as i128);
assert_eq!(a.to_i64().unwrap(), i64::MAX);
assert_eq!(a.to_u64().unwrap(), i64::MAX as u64);
let a = i256::from_i128(i64::MAX as i128 + 1);
assert!(a.to_i64().is_none());
assert_eq!(a.to_u64().unwrap(), i64::MAX as u64 + 1);
let a = i256::MIN;
assert!(a.to_i64().is_none());
assert!(a.to_u64().is_none());
let a = i256::from_i128(i128::MIN);
assert!(a.to_i64().is_none());
assert!(a.to_u64().is_none());
let a = i256::from_i128(i64::MIN as i128);
assert_eq!(a.to_i64().unwrap(), i64::MIN);
assert!(a.to_u64().is_none());
let a = i256::from_i128(i64::MIN as i128 - 1);
assert!(a.to_i64().is_none());
assert!(a.to_u64().is_none());
}
}