Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TotalOrd for signed integers #312

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ env:
CARGO_TERM_COLOR: always
REGISTRY: ghcr.io
RUST_VERSION: 1.80.1
FORC_VERSION: 0.66.2
FORC_VERSION: 0.66.5
CORE_VERSION: 0.40.0

jobs:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Description of the upcoming release here.
### Added

- [#309](https://github.com/FuelLabs/sway-libs/pull/309) Adds fallback function test cases to the Reentrancy Guard Library.
- [#312](https://github.com/FuelLabs/sway-libs/pull/312) Implements `TotalOrd` trait for `I8`, `I16`, `I32`, `I64`, `I128`, and `I256`.

### Changed

Expand Down
4 changes: 2 additions & 2 deletions libs/Forc.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[package]]
name = "core"
source = "path+from-root-7053AAA90CC5E690"
source = "path+from-root-6A8836696D55BC6E"

[[package]]
name = "standards"
Expand All @@ -9,7 +9,7 @@ dependencies = ["std"]

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.66.2#31486c0b47669612acb7c64d66ecb50aea281282"
source = "git+https://github.com/fuellabs/sway?tag=v0.66.5#94a066652468b4afa3bd396dacef482ed590976b"
dependencies = ["core"]

[[package]]
Expand Down
98 changes: 55 additions & 43 deletions libs/src/signed_integers/i128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,42 @@ impl core::ops::Ord for I128 {

impl core::ops::OrdEq for I128 {}

impl core::ops::TotalOrd for I128 {
fn min(self, other: Self) -> Self {
if self.underlying < other.underlying {
self
} else {
other
}
}

fn max(self, other: Self) -> Self {
if self.underlying > other.underlying {
self
} else {
other
}
}
}

impl I128 {
/// The size of this type in bits.
///
/// # Returns
///
/// [u64] - The defined size of the `I128` type.
/// The smallest value that can be represented by this integer type.
///
/// # Examples
///
/// ``sway
/// ```sway
/// use sway_libs::signed_integers::i128::I128;
///
/// fn foo() {
/// let bits = I128::bits();
/// assert(bits == 128);
/// let i128 = I128::MIN;
/// assert(i128.underlying() == U128::min());
/// }
/// ```
pub fn bits() -> u64 {
128
}
const MIN: Self = Self {
underlying: U128::min(),
};

/// Helper function to get a positive value from an unsigned number.
///
/// # Arguments
///
/// * `underlying`: [U128] - The unsigned number to become the underlying value for the `I128`.
/// The largest value that can be represented by this integer type.
///
/// # Returns
///
Expand All @@ -92,46 +102,45 @@ impl I128 {
///
/// ```sway
/// use sway_libs::signed_integers::i128::I128;
/// use std::U128::*;
///
/// fn foo() {
/// let underlying = U128::from((0, 1));
/// let i128 = I128::from_uint(underlying);
/// assert(i128.underlying() == underlying);
/// let i128 = I128::MAX;
/// assert(i128.underlying() == U128::max());
/// }
/// ```
pub fn from_uint(underlying: U128) -> Self {
Self { underlying }
}
const MAX: Self = Self {
underlying: U128::max(),
};

/// The largest value that can be represented by this integer type.
/// The size of this type in bits.
///
/// # Returns
///
/// * [I128] - The newly created `I128` struct.
/// [u64] - The defined size of the `I128` type.
///
/// # Examples
///
/// ```sway
/// ``sway
/// use sway_libs::signed_integers::i128::I128;
/// use std::U128::*;
///
/// fn foo() {
/// let i128 = I128::max();
/// assert(i128.underlying() == U128::max());
/// let bits = I128::bits();
/// assert(bits == 128);
/// }
/// ```
pub fn max() -> Self {
Self {
underlying: U128::max(),
}
pub fn bits() -> u64 {
128
}

/// The smallest value that can be represented by this integer type.
/// Helper function to get a positive value from an unsigned number.
///
/// # Arguments
///
/// * `underlying`: [U128] - The unsigned number to become the underlying value for the `I128`.
///
/// # Returns
///
/// * [I128] - The newly created `I128` type.
/// * [I128] - The newly created `I128` struct.
///
/// # Examples
///
Expand All @@ -140,14 +149,13 @@ impl I128 {
/// use std::U128::*;
///
/// fn foo() {
/// let i128 = I128::min();
/// assert(i128.underlying() == U128::min());
/// let underlying = U128::from((0, 1));
/// let i128 = I128::from_uint(underlying);
/// assert(i128.underlying() == underlying);
/// }
/// ```
pub fn min() -> Self {
Self {
underlying: U128::min(),
}
pub fn from_uint(underlying: U128) -> Self {
Self { underlying }
}

/// Helper function to get a negative value of an unsigned number.
Expand Down Expand Up @@ -395,8 +403,12 @@ impl core::ops::Subtract for I128 {

impl WrappingNeg for I128 {
fn wrapping_neg(self) -> Self {
if self == self::min() {
return self::min()
// TODO: Replace the hardcoded min with Self::MIN once https://github.com/FuelLabs/sway/issues/6772 is closed
let min = Self {
underlying: U128::min(),
};
if self == min {
return min
}
self * Self::neg_try_from(U128::from((0, 1))).unwrap()
}
Expand Down
96 changes: 55 additions & 41 deletions libs/src/signed_integers/i16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,42 @@ impl core::ops::Ord for I16 {

impl core::ops::OrdEq for I16 {}

impl core::ops::TotalOrd for I16 {
fn min(self, other: Self) -> Self {
if self.underlying < other.underlying {
self
} else {
other
}
}

fn max(self, other: Self) -> Self {
if self.underlying > other.underlying {
self
} else {
other
}
}
}

impl I16 {
/// The size of this type in bits.
///
/// # Returns
///
/// [u64] - The defined size of the `I16` type.
/// The smallest value that can be represented by this integer type.
///
/// # Examples
///
/// ``sway
/// ```sway
/// use sway_libs::signed_integers::i16::I16;
///
/// fn foo() {
/// let bits = I16::bits();
/// assert(bits == 16);
/// let i16 = I16::MIN;
/// assert(i16.underlying() == u16::min());
/// }
/// ```
pub fn bits() -> u64 {
16
}
const MIN: Self = Self {
underlying: u16::min(),
};

/// Helper function to get a positive value from an unsigned number
///
/// # Arguments
///
/// * `underlying`: [u16] - The unsigned number to become the underlying value for the `I16`.
/// The largest value that can be represented by this integer type.
///
/// # Returns
///
Expand All @@ -93,57 +103,57 @@ impl I16 {
/// use sway_libs::signed_integers::i16::I16;
///
/// fn foo() {
/// let underlying = 1u16;
/// let i16 = I16::from_uint(underlying);
/// assert(i16.underlying() == underlying);
/// let i16 = I16::MAX;
/// assert(i16.underlying() == u16::max());
/// }
/// ```
pub fn from_uint(underlying: u16) -> Self {
Self { underlying }
}
const MAX: Self = Self {
underlying: u16::max(),
};

/// The largest value that can be represented by this integer type.
/// The size of this type in bits.
///
/// # Returns
///
/// * [I16] - The newly created `I16` struct.
/// [u64] - The defined size of the `I16` type.
///
/// # Examples
///
/// ```sway
/// ``sway
/// use sway_libs::signed_integers::i16::I16;
///
/// fn foo() {
/// let i16 = I16::max();
/// assert(i16.underlying() == u16::max());
/// let bits = I16::bits();
/// assert(bits == 16);
/// }
/// ```
pub fn max() -> Self {
Self {
underlying: u16::max(),
}
pub fn bits() -> u64 {
16
}

/// The smallest value that can be represented by this integer type.
/// Helper function to get a positive value from an unsigned number
///
/// # Arguments
///
/// * `underlying`: [u16] - The unsigned number to become the underlying value for the `I16`.
///
/// # Returns
///
/// * [I16] - The newly created `I16` type.
/// * [I16] - The newly created `I16` struct.
///
/// # Examples
///
/// ```sway
/// use sway_libs::signed_integers::i16::I16;
///
/// fn foo() {
/// let i16 = I16::min();
/// assert(i16.underlying() == u16::min());
/// let underlying = 1u16;
/// let i16 = I16::from_uint(underlying);
/// assert(i16.underlying() == underlying);
/// }
/// ```
pub fn min() -> Self {
Self {
underlying: u16::min(),
}
pub fn from_uint(underlying: u16) -> Self {
Self { underlying }
}

/// Helper function to get a negative value of an unsigned number.
Expand Down Expand Up @@ -383,8 +393,12 @@ impl core::ops::Subtract for I16 {

impl WrappingNeg for I16 {
fn wrapping_neg(self) -> Self {
if self == self::min() {
return self::min()
// TODO: Replace the hardcoded min with Self::MIN once https://github.com/FuelLabs/sway/issues/6772 is closed
let min = Self {
underlying: u16::min(),
};
if self == min {
return min
}
self * Self::neg_try_from(1u16).unwrap()
}
Expand Down
Loading
Loading