pub struct I256 { /* private fields */ }Expand description
A 256-bit signed integer.
Limbs are the two’s complement representation, least-significant-first, so
the layout and the wire encoding match U256 exactly. See the module
documentation for the semantics.
Implementations§
Source§impl I256
impl I256
Sourcepub const MIN: I256
pub const MIN: I256
The smallest representable value, -2^255.
Its magnitude is one larger than MAX’s, which is why
negating, absolute value and division by -1 can all overflow.
Sourcepub const fn from_limbs(limbs: [u32; 8]) -> I256
pub const fn from_limbs(limbs: [u32; 8]) -> I256
Constructs a value from its two’s complement limbs, least-significant-first.
Sourcepub const fn to_limbs(self) -> [u32; 8]
pub const fn to_limbs(self) -> [u32; 8]
Returns the two’s complement limbs, least-significant-first.
Sourcepub const fn from(n: i128) -> I256
pub const fn from(n: i128) -> I256
Constructs a value from an i128.
Inherent for the reason U256::from is: From is implemented for
every signed width, which would leave I256::from(-1000) ambiguous,
and an inherent function shadows the trait in path form and pins the
literal to i128. It is also usable in const position.
The trait impls are untouched, so .into() and generic T: Into<I256>
still convert from any width – including the unsigned ones, which all
fit.
Sourcepub const fn from_i128(n: i128) -> I256
pub const fn from_i128(n: i128) -> I256
Constructs a value from an i128, under a name no trait shadows.
Sourcepub const fn from_i32(n: i32) -> I256
pub const fn from_i32(n: i32) -> I256
Constructs a value from an i32, without widening at the call site.
Sourcepub const fn from_i64(n: i64) -> I256
pub const fn from_i64(n: i64) -> I256
Constructs a value from an i64, without widening at the call site.
Sourcepub const fn cast_unsigned(self) -> U256
pub const fn cast_unsigned(self) -> U256
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if this is strictly less than zero.
Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if this is strictly greater than zero.
Sourcepub const fn unsigned_abs(self) -> U256
pub const fn unsigned_abs(self) -> U256
Sourcepub const fn const_cmp(self, other: I256) -> Ordering
pub const fn const_cmp(self, other: I256) -> Ordering
Comparison usable from a const fn.
Flipping the sign bit maps the signed order onto the unsigned one, so
this is U256::const_cmp on the flipped values.
Sourcepub const fn to_be_bytes(self) -> [u8; 32]
pub const fn to_be_bytes(self) -> [u8; 32]
Returns the big-endian two’s complement byte representation.
#[inline], not the inline(never) the unsigned pair carries: the
body is a reinterpretation and a call, so the byte-swap loop this
reaches is U256::to_be_bytes, which is already outlined on rv32
for exactly that reason. Outlining the wrapper too would add a second
symbol and a call/return per conversion, not remove one.
Sourcepub const fn from_be_bytes(bytes: [u8; 32]) -> I256
pub const fn from_be_bytes(bytes: [u8; 32]) -> I256
Constructs a value from its big-endian two’s complement byte representation.
#[inline] for the reason to_be_bytes gives.
Source§impl I256
impl I256
Sourcepub const fn overflowing_neg(self) -> (I256, bool)
pub const fn overflowing_neg(self) -> (I256, bool)
Negation, with a flag reporting whether it overflowed.
Only MIN overflows: its magnitude has no positive
counterpart, and it negates to itself.
Sourcepub const fn checked_neg(self) -> Option<I256>
pub const fn checked_neg(self) -> Option<I256>
Negation, returning None on overflow.
Sourcepub const fn wrapping_neg(self) -> I256
pub const fn wrapping_neg(self) -> I256
Negation, wrapping on overflow (MIN negates to itself).
Sourcepub const fn saturating_neg(self) -> I256
pub const fn saturating_neg(self) -> I256
Negation, saturating at MAX.
Sourcepub const fn overflowing_abs(self) -> (I256, bool)
pub const fn overflowing_abs(self) -> (I256, bool)
Absolute value, with a flag reporting whether it overflowed.
Sourcepub const fn checked_abs(self) -> Option<I256>
pub const fn checked_abs(self) -> Option<I256>
Absolute value, returning None on overflow.
Sourcepub const fn wrapping_abs(self) -> I256
pub const fn wrapping_abs(self) -> I256
Absolute value, wrapping on overflow (MIN maps to itself).
Sourcepub const fn saturating_abs(self) -> I256
pub const fn saturating_abs(self) -> I256
Absolute value, saturating at MAX.
Sourcepub const fn abs(self) -> I256
pub const fn abs(self) -> I256
Absolute value.
See unsigned_abs for the total version: MIN
has no representable absolute value here.
§Panics
On MIN, if overflow-checks is on – otherwise it wraps to MIN,
like the primitives.
Sourcepub const fn overflowing_add(self, rhs: I256) -> (I256, bool)
pub const fn overflowing_add(self, rhs: I256) -> (I256, bool)
Wrapping addition, with a flag reporting whether it overflowed.
Sourcepub const fn overflowing_sub(self, rhs: I256) -> (I256, bool)
pub const fn overflowing_sub(self, rhs: I256) -> (I256, bool)
Wrapping subtraction, with a flag reporting whether it overflowed.
Sourcepub const fn checked_add(self, rhs: I256) -> Option<I256>
pub const fn checked_add(self, rhs: I256) -> Option<I256>
Addition, returning None on overflow.
Sourcepub const fn checked_sub(self, rhs: I256) -> Option<I256>
pub const fn checked_sub(self, rhs: I256) -> Option<I256>
Subtraction, returning None on overflow.
Sourcepub const fn wrapping_add(self, rhs: I256) -> I256
pub const fn wrapping_add(self, rhs: I256) -> I256
Addition, wrapping on overflow.
Sourcepub const fn wrapping_sub(self, rhs: I256) -> I256
pub const fn wrapping_sub(self, rhs: I256) -> I256
Subtraction, wrapping on overflow.
Sourcepub const fn overflowing_mul(self, rhs: I256) -> (I256, bool)
pub const fn overflowing_mul(self, rhs: I256) -> (I256, bool)
Wrapping multiplication, with a flag reporting whether it overflowed.
One 256x256 multiply, not two. The overflow test needs the magnitude
product, and the wrapped signed bits follow from it without a second
pass: a * b = (-1)^(sign a ^ sign b) * |a| * |b|, and that identity
survives reduction mod 2^256, so negating the magnitude product gives
exactly the bits cast_unsigned().wrapping_mul() would have.
Which callers this helps is worth being precise about. It is every form
that consumes the overflow flag – checked_mul,
saturating_mul, this function, and each step
of pow. The bare * operator is not among them: in a
release contract profile OVERFLOW_CHECKS folds to false, the flag goes
unread, and LLVM was already dropping the second multiply as dead.
Measured on rv32 by scripts/bigint-bench.sh -o "mul signed_mul": a
saturating signed multiply costs 1095.02 gas against 1019.02 for a bare
wrapping unsigned one – +76 gas for the sign handling and the bound
check, where a second full schoolbook pass would have been on the
order of +1,000.
Sourcepub const fn checked_mul(self, rhs: I256) -> Option<I256>
pub const fn checked_mul(self, rhs: I256) -> Option<I256>
Multiplication, returning None on overflow.
Sourcepub const fn wrapping_mul(self, rhs: I256) -> I256
pub const fn wrapping_mul(self, rhs: I256) -> I256
Multiplication, wrapping on overflow.
Sourcepub const fn checked_div_rem(self, rhs: I256) -> Option<(I256, I256)>
pub const fn checked_div_rem(self, rhs: I256) -> Option<(I256, I256)>
Divides, returning the quotient and remainder, or None if rhs is
zero or the division overflows.
The quotient truncates toward zero and the remainder takes the sign of
the dividend, matching Rust’s primitives and Solidity’s int256.
MIN / -1 is the one overflow: its quotient is 2^255.
Sourcepub const fn div_rem(self, rhs: I256) -> (I256, I256)
pub const fn div_rem(self, rhs: I256) -> (I256, I256)
Divides, returning the quotient and remainder.
§Panics
If rhs is zero, or on MIN / -1. Both panics are unconditional,
and deliberately so: division overflow is the one arithmetic overflow
the primitives do not key off overflow-checks. i128::MIN / -1
panics under -C overflow-checks=off exactly as it does with checks
on, because the alternative on real hardware is a trap rather than a
wrapped value. Matching the primitives here means not gating this.
The consequence is worth stating plainly, since it is the opposite of
what the rest of this type does: a contract profile silently wraps
MIN + 1 and MIN * 2, but aborts on MIN / -1. Reach for
wrapping_div or
checked_div if an abort is not what the call
site wants.
Sourcepub const fn overflowing_div(self, rhs: I256) -> (I256, bool)
pub const fn overflowing_div(self, rhs: I256) -> (I256, bool)
Division, with a flag reporting whether it overflowed.
The flag is set only by MIN / -1, whose wrapped quotient is MIN.
§Panics
If rhs is zero.
Sourcepub const fn overflowing_rem(self, rhs: I256) -> (I256, bool)
pub const fn overflowing_rem(self, rhs: I256) -> (I256, bool)
Remainder, with a flag reporting whether it overflowed.
The flag is set only by MIN % -1, whose wrapped remainder is zero.
§Panics
If rhs is zero.
Sourcepub const fn checked_div(self, rhs: I256) -> Option<I256>
pub const fn checked_div(self, rhs: I256) -> Option<I256>
Division, returning None if rhs is zero or the division overflows.
Sourcepub const fn checked_rem(self, rhs: I256) -> Option<I256>
pub const fn checked_rem(self, rhs: I256) -> Option<I256>
Remainder, returning None if rhs is zero or the division overflows.
Sourcepub const fn wrapping_div(self, rhs: I256) -> I256
pub const fn wrapping_div(self, rhs: I256) -> I256
Sourcepub const fn wrapping_rem(self, rhs: I256) -> I256
pub const fn wrapping_rem(self, rhs: I256) -> I256
Sourcepub const fn div_euclid(self, rhs: I256) -> I256
pub const fn div_euclid(self, rhs: I256) -> I256
Sourcepub const fn rem_euclid(self, rhs: I256) -> I256
pub const fn rem_euclid(self, rhs: I256) -> I256
Sourcepub const fn wrapping_shl(self, n: u32) -> I256
pub const fn wrapping_shl(self, n: u32) -> I256
Left shift, masking the shift amount to 0..256.
Bit-identical to the unsigned shift: bits that fall off the top, including the sign bit, are simply gone.
Sourcepub const fn wrapping_shr(self, n: u32) -> I256
pub const fn wrapping_shr(self, n: u32) -> I256
Arithmetic right shift, masking the shift amount to 0..256.
The sign bit is replicated into the vacated positions, so this rounds
toward negative infinity: (-1) >> 1 is -1, not MAX.
Sourcepub const fn overflowing_shl(self, n: u32) -> (I256, bool)
pub const fn overflowing_shl(self, n: u32) -> (I256, bool)
Left shift, masking the shift amount and reporting whether the mask changed it.
Sourcepub const fn overflowing_shr(self, n: u32) -> (I256, bool)
pub const fn overflowing_shr(self, n: u32) -> (I256, bool)
Arithmetic right shift, masking the shift amount and reporting whether the mask changed it.
Sourcepub const fn checked_shl(self, n: u32) -> Option<I256>
pub const fn checked_shl(self, n: u32) -> Option<I256>
Left shift, returning None if the shift amount is at least 256.
Sourcepub const fn checked_shr(self, n: u32) -> Option<I256>
pub const fn checked_shr(self, n: u32) -> Option<I256>
Arithmetic right shift, returning None if the shift amount is at
least 256.
Sourcepub const fn const_bitand(self, rhs: I256) -> I256
pub const fn const_bitand(self, rhs: I256) -> I256
Bitwise AND, usable from a const fn.
Sourcepub const fn const_bitor(self, rhs: I256) -> I256
pub const fn const_bitor(self, rhs: I256) -> I256
Bitwise OR, usable from a const fn.
Sourcepub const fn const_bitxor(self, rhs: I256) -> I256
pub const fn const_bitxor(self, rhs: I256) -> I256
Bitwise XOR, usable from a const fn.
Sourcepub const fn overflowing_pow(self, exp: u32) -> (I256, bool)
pub const fn overflowing_pow(self, exp: u32) -> (I256, bool)
Exponentiation, with a flag reporting whether it overflowed.
x.pow(0) is one for every x, matching the primitives.
Sourcepub const fn checked_pow(self, exp: u32) -> Option<I256>
pub const fn checked_pow(self, exp: u32) -> Option<I256>
Exponentiation, returning None on overflow.
Sourcepub const fn wrapping_pow(self, exp: u32) -> I256
pub const fn wrapping_pow(self, exp: u32) -> I256
Exponentiation, wrapping on overflow.
Source§impl I256
impl I256
Sourcepub const fn min(self, other: I256) -> I256
pub const fn min(self, other: I256) -> I256
The smaller of two values. SignedMath.min.
Shadows Ord::min in method position, which resolves to the same
answer; this one is a const fn.
Sourcepub const fn max(self, other: I256) -> I256
pub const fn max(self, other: I256) -> I256
The larger of two values. SignedMath.max.
Shadows Ord::max in method position, which resolves to the same
answer; this one is a const fn.
Sourcepub const fn average(self, other: I256) -> I256
pub const fn average(self, other: I256) -> I256
The average of two values, rounded toward zero, without an
intermediate that overflows. SignedMath.average.
(a & b) + ((a ^ b) >> 1) is the floor average – the shift is
arithmetic, so it rounds toward negative infinity – and the
correction below pulls a negative, inexact result back up by one.
Note that this differs from U256::average only in that
correction, because for a value that cannot be negative the two
directions coincide.
Sourcepub const fn checked_mul_div(x: I256, y: I256, d: I256) -> Option<I256>
pub const fn checked_mul_div(x: I256, y: I256, d: I256) -> Option<I256>
x * y / d computed on the exact 512-bit product, truncating toward
zero. Returns None if d is zero or the quotient is out of range.
The signed counterpart of U256::mul_div; see the module docs for
why it is a composition rather than a port.
Sourcepub const fn mul_div(x: I256, y: I256, d: I256) -> I256
pub const fn mul_div(x: I256, y: I256, d: I256) -> I256
x * y / d computed on the exact 512-bit product, truncating toward
zero.
§Panics
If d is zero, or if the quotient is out of range. Both panics are
unconditional, as on the unsigned side.
Trait Implementations§
Source§impl AddAssign for I256
impl AddAssign for I256
Source§fn add_assign(&mut self, rhs: I256)
fn add_assign(&mut self, rhs: I256)
+= operation. Read moreSource§impl BitAndAssign for I256
impl BitAndAssign for I256
Source§fn bitand_assign(&mut self, rhs: I256)
fn bitand_assign(&mut self, rhs: I256)
&= operation. Read moreSource§impl BitOrAssign for I256
impl BitOrAssign for I256
Source§fn bitor_assign(&mut self, rhs: I256)
fn bitor_assign(&mut self, rhs: I256)
|= operation. Read moreSource§impl BitXorAssign for I256
impl BitXorAssign for I256
Source§fn bitxor_assign(&mut self, rhs: I256)
fn bitxor_assign(&mut self, rhs: I256)
^= operation. Read moreSource§impl CollectAuthHandles for I256
impl CollectAuthHandles for I256
fn collect_auth_handles( &self, _out: &mut AuthHandleBuf, ) -> Result<(), FaultString>
Source§impl ContractTypeRefSpec for I256
impl ContractTypeRefSpec for I256
Source§const INFO: ContractTypeInfo<'static>
const INFO: ContractTypeInfo<'static>
__THRUPUT_SPEC_ANCHOR_ROOT) and resolved by spec-tool.Source§const SIGNED_PREIMAGE: Option<&'static ContractTypeView<'static>> = _
const SIGNED_PREIMAGE: Option<&'static ContractTypeView<'static>> = _
ContractTypeView::SignedToken’s payload): the type itself by
default, or None for a type-erased payload (the SDK’s Opaque
overrides this).impl Copy for I256
Source§impl DivAssign for I256
impl DivAssign for I256
Source§fn div_assign(&mut self, rhs: I256)
fn div_assign(&mut self, rhs: I256)
/= operation. Read moreimpl Eq for I256
Source§impl Mintable for I256
impl Mintable for I256
fn mint_into_parent(preimage: I256, _children: &mut ChildNodeBuf) -> Self
Source§impl MulAssign for I256
impl MulAssign for I256
Source§fn mul_assign(&mut self, rhs: I256)
fn mul_assign(&mut self, rhs: I256)
*= operation. Read moreSource§impl Ord for I256
impl Ord for I256
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for I256
impl PartialOrd for I256
Source§impl<'r> ReadXDR<'r> for I256
impl<'r> ReadXDR<'r> for I256
Source§impl ReceiveAuthHandles for I256
impl ReceiveAuthHandles for I256
fn receive_auth_handles( &mut self, _handles: &mut impl Iterator<Item = u32>, ) -> Result<(), AuthTokenError>
Source§impl RemAssign for I256
impl RemAssign for I256
Source§fn rem_assign(&mut self, rhs: I256)
fn rem_assign(&mut self, rhs: I256)
%= operation. Read moreSource§impl ShlAssign<i8> for I256
impl ShlAssign<i8> for I256
Source§fn shl_assign(&mut self, rhs: i8)
fn shl_assign(&mut self, rhs: i8)
<<= operation. Read moreSource§impl ShlAssign<i16> for I256
impl ShlAssign<i16> for I256
Source§fn shl_assign(&mut self, rhs: i16)
fn shl_assign(&mut self, rhs: i16)
<<= operation. Read moreSource§impl ShlAssign<i32> for I256
impl ShlAssign<i32> for I256
Source§fn shl_assign(&mut self, rhs: i32)
fn shl_assign(&mut self, rhs: i32)
<<= operation. Read moreSource§impl ShlAssign<i64> for I256
impl ShlAssign<i64> for I256
Source§fn shl_assign(&mut self, rhs: i64)
fn shl_assign(&mut self, rhs: i64)
<<= operation. Read moreSource§impl ShlAssign<i128> for I256
impl ShlAssign<i128> for I256
Source§fn shl_assign(&mut self, rhs: i128)
fn shl_assign(&mut self, rhs: i128)
<<= operation. Read moreSource§impl ShlAssign<isize> for I256
impl ShlAssign<isize> for I256
Source§fn shl_assign(&mut self, rhs: isize)
fn shl_assign(&mut self, rhs: isize)
<<= operation. Read moreSource§impl ShlAssign<u8> for I256
impl ShlAssign<u8> for I256
Source§fn shl_assign(&mut self, rhs: u8)
fn shl_assign(&mut self, rhs: u8)
<<= operation. Read moreSource§impl ShlAssign<u16> for I256
impl ShlAssign<u16> for I256
Source§fn shl_assign(&mut self, rhs: u16)
fn shl_assign(&mut self, rhs: u16)
<<= operation. Read moreSource§impl ShlAssign<u32> for I256
impl ShlAssign<u32> for I256
Source§fn shl_assign(&mut self, rhs: u32)
fn shl_assign(&mut self, rhs: u32)
<<= operation. Read moreSource§impl ShlAssign<u64> for I256
impl ShlAssign<u64> for I256
Source§fn shl_assign(&mut self, rhs: u64)
fn shl_assign(&mut self, rhs: u64)
<<= operation. Read moreSource§impl ShlAssign<u128> for I256
impl ShlAssign<u128> for I256
Source§fn shl_assign(&mut self, rhs: u128)
fn shl_assign(&mut self, rhs: u128)
<<= operation. Read moreSource§impl ShlAssign<usize> for I256
impl ShlAssign<usize> for I256
Source§fn shl_assign(&mut self, rhs: usize)
fn shl_assign(&mut self, rhs: usize)
<<= operation. Read moreSource§impl ShrAssign<i8> for I256
impl ShrAssign<i8> for I256
Source§fn shr_assign(&mut self, rhs: i8)
fn shr_assign(&mut self, rhs: i8)
>>= operation. Read moreSource§impl ShrAssign<i16> for I256
impl ShrAssign<i16> for I256
Source§fn shr_assign(&mut self, rhs: i16)
fn shr_assign(&mut self, rhs: i16)
>>= operation. Read moreSource§impl ShrAssign<i32> for I256
impl ShrAssign<i32> for I256
Source§fn shr_assign(&mut self, rhs: i32)
fn shr_assign(&mut self, rhs: i32)
>>= operation. Read moreSource§impl ShrAssign<i64> for I256
impl ShrAssign<i64> for I256
Source§fn shr_assign(&mut self, rhs: i64)
fn shr_assign(&mut self, rhs: i64)
>>= operation. Read moreSource§impl ShrAssign<i128> for I256
impl ShrAssign<i128> for I256
Source§fn shr_assign(&mut self, rhs: i128)
fn shr_assign(&mut self, rhs: i128)
>>= operation. Read moreSource§impl ShrAssign<isize> for I256
impl ShrAssign<isize> for I256
Source§fn shr_assign(&mut self, rhs: isize)
fn shr_assign(&mut self, rhs: isize)
>>= operation. Read moreSource§impl ShrAssign<u8> for I256
impl ShrAssign<u8> for I256
Source§fn shr_assign(&mut self, rhs: u8)
fn shr_assign(&mut self, rhs: u8)
>>= operation. Read moreSource§impl ShrAssign<u16> for I256
impl ShrAssign<u16> for I256
Source§fn shr_assign(&mut self, rhs: u16)
fn shr_assign(&mut self, rhs: u16)
>>= operation. Read moreSource§impl ShrAssign<u32> for I256
impl ShrAssign<u32> for I256
Source§fn shr_assign(&mut self, rhs: u32)
fn shr_assign(&mut self, rhs: u32)
>>= operation. Read moreSource§impl ShrAssign<u64> for I256
impl ShrAssign<u64> for I256
Source§fn shr_assign(&mut self, rhs: u64)
fn shr_assign(&mut self, rhs: u64)
>>= operation. Read moreSource§impl ShrAssign<u128> for I256
impl ShrAssign<u128> for I256
Source§fn shr_assign(&mut self, rhs: u128)
fn shr_assign(&mut self, rhs: u128)
>>= operation. Read moreSource§impl ShrAssign<usize> for I256
impl ShrAssign<usize> for I256
Source§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
>>= operation. Read moreimpl StructuralPartialEq for I256
Source§impl SubAssign for I256
impl SubAssign for I256
Source§fn sub_assign(&mut self, rhs: I256)
fn sub_assign(&mut self, rhs: I256)
-= operation. Read more