From 1640d82975ac27bb53e0a76281554ea1cf1cd3e1 Mon Sep 17 00:00:00 2001 From: tyshkor Date: Wed, 5 Oct 2022 23:04:25 +0200 Subject: [PATCH 01/12] add signed integers and tests --- sway_libs/src/lib.sw | 7 + sway_libs/src/signed_integers/i128.sw | 155 +++++++++++++++++ sway_libs/src/signed_integers/i16.sw | 151 +++++++++++++++++ sway_libs/src/signed_integers/i256.sw | 157 ++++++++++++++++++ sway_libs/src/signed_integers/i32.sw | 151 +++++++++++++++++ sway_libs/src/signed_integers/i64.sw | 151 +++++++++++++++++ sway_libs/src/signed_integers/i8.sw | 147 ++++++++++++++++ .../signed_integers/i128_test/.gitignore | 2 + .../signed_integers/i128_test/Forc.toml | 8 + .../signed_integers/i128_test/src/main.sw | 64 +++++++ .../signed_integers/i16_test/.gitignore | 2 + .../signed_integers/i16_test/Forc.toml | 8 + .../signed_integers/i16_test/src/main.sw | 27 +++ .../signed_integers/i256_test/.gitignore | 2 + .../signed_integers/i256_test/Forc.toml | 8 + .../signed_integers/i256_test/src/main.sw | 81 +++++++++ .../signed_integers/i32_test/.gitignore | 2 + .../signed_integers/i32_test/Forc.toml | 8 + .../signed_integers/i32_test/src/main.sw | 27 +++ .../signed_integers/i64_test/.gitignore | 2 + .../signed_integers/i64_test/Forc.toml | 8 + .../signed_integers/i64_test/src/main.sw | 27 +++ .../signed_integers/i8_test/.gitignore | 2 + .../signed_integers/i8_test/Forc.toml | 8 + .../signed_integers/i8_test/src/main.sw | 27 +++ 25 files changed, 1232 insertions(+) create mode 100644 sway_libs/src/signed_integers/i128.sw create mode 100644 sway_libs/src/signed_integers/i16.sw create mode 100644 sway_libs/src/signed_integers/i256.sw create mode 100644 sway_libs/src/signed_integers/i32.sw create mode 100644 sway_libs/src/signed_integers/i64.sw create mode 100644 sway_libs/src/signed_integers/i8.sw create mode 100644 tests/src/test_projects/signed_integers/i128_test/.gitignore create mode 100644 tests/src/test_projects/signed_integers/i128_test/Forc.toml create mode 100644 tests/src/test_projects/signed_integers/i128_test/src/main.sw create mode 100644 tests/src/test_projects/signed_integers/i16_test/.gitignore create mode 100644 tests/src/test_projects/signed_integers/i16_test/Forc.toml create mode 100644 tests/src/test_projects/signed_integers/i16_test/src/main.sw create mode 100644 tests/src/test_projects/signed_integers/i256_test/.gitignore create mode 100644 tests/src/test_projects/signed_integers/i256_test/Forc.toml create mode 100644 tests/src/test_projects/signed_integers/i256_test/src/main.sw create mode 100644 tests/src/test_projects/signed_integers/i32_test/.gitignore create mode 100644 tests/src/test_projects/signed_integers/i32_test/Forc.toml create mode 100644 tests/src/test_projects/signed_integers/i32_test/src/main.sw create mode 100644 tests/src/test_projects/signed_integers/i64_test/.gitignore create mode 100644 tests/src/test_projects/signed_integers/i64_test/Forc.toml create mode 100644 tests/src/test_projects/signed_integers/i64_test/src/main.sw create mode 100644 tests/src/test_projects/signed_integers/i8_test/.gitignore create mode 100644 tests/src/test_projects/signed_integers/i8_test/Forc.toml create mode 100644 tests/src/test_projects/signed_integers/i8_test/src/main.sw diff --git a/sway_libs/src/lib.sw b/sway_libs/src/lib.sw index 21838a91..8a5cac8e 100644 --- a/sway_libs/src/lib.sw +++ b/sway_libs/src/lib.sw @@ -1,3 +1,10 @@ library sway_libs; dep merkle_proof/binary_merkle_proof; + +dep signed_integers/i8; +dep signed_integers/i16; +dep signed_integers/i32; +dep signed_integers/i64; +dep signed_integers/i128; +dep signed_integers/i256; diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw new file mode 100644 index 00000000..ce425abb --- /dev/null +++ b/sway_libs/src/signed_integers/i128.sw @@ -0,0 +1,155 @@ +library i128; + +use core::num::*; +use std::assert::assert; +use std::u128::U128; + +/// The 128-bit signed integer type. +/// Represented as an underlying U128 value. +/// Actual value is underlying value minus 2 ^ 127 +/// Max value is 2 ^ 127 - 1, min value is - 2 ^ 127 +pub struct I128 { + underlying: U128, +} + +pub trait From { + /// Function for creating I128 from U128 + fn from(underlying: U128) -> Self; +} + +impl From for I128 { + /// Helper function to get a signed number from with an underlying + fn from(underlying: U128) -> Self { + Self { + underlying + } + } +} + +impl core::ops::Eq for I128 { + fn eq(self, other: Self) -> bool { + self.underlying == other.underlying + } +} + +impl core::ops::Ord for I128 { + fn gt(self, other: Self) -> bool { + self.underlying > other.underlying + } + + fn lt(self, other: Self) -> bool { + self.underlying < other.underlying + } +} + +impl I128 { + /// The underlying value that corresponds to zero signed value + pub fn indent() -> U128 { + U128 { + upper: 1, + lower: 0, + } + } +} + +impl I128 { + /// Initializes a new, zeroed I128. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } + } + + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~U128::min(), + } + } + + /// The largest value that can be represented by this type, + pub fn max() -> Self { + Self { + underlying: ~U128::max(), + } + } + + /// The size of this type in bits. + pub fn bits() -> u32 { + 128 + } + + /// Helper function to get a negative value of unsigned number + pub fn neg_from(value: U128) -> Self { + Self { + underlying: ~Self::indent() - value, + } + } + + /// Helper function to get a positive value from unsigned number + fn from_uint(value: U128) -> Self { + // as the minimal value of I128 is -~I128::indent() (1 << 63) we should add ~I128::indent() (1 << 63) + let underlying: U128 = value + ~Self::indent(); + Self { + underlying + } + } +} + +impl core::ops::Add for I128 { + /// Add a I128 to a I128. Panics on overflow. + fn add(self, other: Self) -> Self { + // subtract 1 << 63 to avoid double move + ~Self::from(self.underlying - ~Self::indent() + other.underlying) + } +} + +impl core::ops::Subtract for I128 { + /// Subtract a I128 from a I128. Panics of overflow. + fn subtract(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self > other { + // add 1 << 63 to avoid loosing the move + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); + } else { + // subtract from 1 << 63 as we are getting a negative value + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); + } + res + } +} + +impl core::ops::Multiply for I128 { + /// Multiply a I128 with a I128. Panics of overflow. + fn multiply(self, other: Self) -> Self { + let mut res = ~Self::new(); + if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && (other.underlying > ~Self::indent() || other.underlying == ~Self::indent()) { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); + } else if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && other.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); + } else if self.underlying < ~Self::indent() && (other.underlying > ~Self::indent() || other.underlying == ~Self::indent()) { + res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); + } + res + } +} + +impl core::ops::Divide for I128 { + /// Divide a I128 by a I128. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); + let mut res = ~Self::new(); + if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && divisor.underlying > ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && divisor.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + } + res + } +} diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw new file mode 100644 index 00000000..0c749874 --- /dev/null +++ b/sway_libs/src/signed_integers/i16.sw @@ -0,0 +1,151 @@ +library i16; + +use core::num::*; +use std::assert::assert; + +/// The 16-bit signed integer type. +/// Represented as an underlying u16 value. +/// Actual value is underlying value minus 2 ^ 15 +/// Max value is 2 ^ 15 - 1, min value is - 2 ^ 15 +pub struct I16 { + underlying: u16, +} + +pub trait From { + /// Function for creating I16 from u16 + fn from(underlying: u16) -> Self; +} + +impl From for I16 { + /// Helper function to get a signed number from with an underlying + fn from(underlying: u16) -> Self { + Self { + underlying + } + } +} + +impl core::ops::Eq for I16 { + fn eq(self, other: Self) -> bool { + self.underlying == other.underlying + } +} + +impl core::ops::Ord for I16 { + fn gt(self, other: Self) -> bool { + self.underlying > other.underlying + } + + fn lt(self, other: Self) -> bool { + self.underlying < other.underlying + } +} + +impl I16 { + /// The underlying value that corresponds to zero signed value + pub fn indent() -> u16 { + 32768u16 + } +} + +impl I16 { + /// Initializes a new, zeroed I16. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } + } + + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~u16::min(), + } + } + + /// The largest value that can be represented by this type, + pub fn max() -> Self { + Self { + underlying: ~u16::max(), + } + } + + /// The size of this type in bits. + pub fn bits() -> u32 { + 16 + } + + /// Helper function to get a negative value of unsigned number + pub fn neg_from(value: u16) -> Self { + Self { + underlying: ~Self::indent() - value, + } + } + + /// Helper function to get a positive value from unsigned number + fn from_uint(value: u16) -> Self { + // as the minimal value of I16 is -~I16::indent() (1 << 15) we should add ~I16::indent() (1 << 15) + let underlying: u16 = value + ~Self::indent(); + Self { + underlying + } + } +} + +impl core::ops::Add for I16 { + /// Add a I16 to a I16. Panics on overflow. + fn add(self, other: Self) -> Self { + // subtract 1 << 15 to avoid double move + ~Self::from(self.underlying - ~Self::indent() + other.underlying) + } +} + +impl core::ops::Subtract for I16 { + /// Subtract a I16 from a I16. Panics of overflow. + fn subtract(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self > other { + // add 1 << 15 to avoid loosing the move + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); + } else { + // subtract from 1 << 15 as we are getting a negative value + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); + } + res + } +} + +impl core::ops::Multiply for I16 { + /// Multiply a I16 with a I16. Panics of overflow. + fn multiply(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self.underlying >= ~Self::indent() && other.underlying >= ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); + } else if self.underlying < ~Self::indent() && other.underlying >= ~Self::indent() { + res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); + } + res + } +} + +impl core::ops::Divide for I16 { + /// Divide a I16 by a I16. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); + let mut res = ~Self::new(); + if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + } + res + } +} diff --git a/sway_libs/src/signed_integers/i256.sw b/sway_libs/src/signed_integers/i256.sw new file mode 100644 index 00000000..9d154aa1 --- /dev/null +++ b/sway_libs/src/signed_integers/i256.sw @@ -0,0 +1,157 @@ +library i256; + +use core::num::*; +use std::assert::assert; +use std::u256::U256; + +/// The 128-bit signed integer type. +/// Represented as an underlying U256 value. +/// Actual value is underlying value minus 2 ^ 255 +/// Max value is 2 ^ 255 - 1, min value is - 2 ^ 255 +pub struct I256 { + underlying: U256, +} + +pub trait From { + /// Function for creating I256 from U256 + fn from(underlying: U256) -> Self; +} + +impl From for I256 { + /// Helper function to get a signed number from with an underlying + fn from(underlying: U256) -> Self { + Self { + underlying, + } + } +} + +impl core::ops::Eq for I256 { + fn eq(self, other: Self) -> bool { + self.underlying == other.underlying + } +} + +impl core::ops::Ord for I256 { + fn gt(self, other: Self) -> bool { + self.underlying > other.underlying + } + + fn lt(self, other: Self) -> bool { + self.underlying < other.underlying + } +} + +impl I256 { + /// The underlying value that corresponds to zero signed value + pub fn indent() -> U256 { + U256 { + a: 0, + b: 1, + c: 0, + d: 0, + } + } +} + +impl I256 { + /// Initializes a new, zeroed I256. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } + } + + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~U256::min(), + } + } + + /// The largest value that can be represented by this type, + pub fn max() -> Self { + Self { + underlying: ~U256::max(), + } + } + + /// The size of this type in bits. + pub fn bits() -> u32 { + 128 + } + + /// Helper function to get a negative value of unsigned number + pub fn neg_from(value: U256) -> Self { + Self { + underlying: ~Self::indent() - value, + } + } + + /// Helper function to get a positive value from unsigned number + fn from_uint(value: U256) -> Self { + // as the minimal value of I256 is -~I256::indent() (1 << 63) we should add ~I256::indent() (1 << 63) + let underlying: U256 = value + ~Self::indent(); + Self { + underlying + } + } +} + +impl core::ops::Add for I256 { + /// Add a I256 to a I256. Panics on overflow. + fn add(self, other: Self) -> Self { + // subtract 1 << 63 to avoid double move + ~Self::from(self.underlying - ~Self::indent() + other.underlying) + } +} + +impl core::ops::Subtract for I256 { + /// Subtract a I256 from a I256. Panics of overflow. + fn subtract(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self > other { + // add 1 << 63 to avoid loosing the move + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); + } else { + // subtract from 1 << 63 as we are getting a negative value + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); + } + res + } +} + +impl core::ops::Multiply for I256 { + /// Multiply a I256 with a I256. Panics of overflow. + fn multiply(self, other: Self) -> Self { + let mut res = ~Self::new(); + if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && (other.underlying > ~Self::indent() || other.underlying == ~Self::indent()) { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); + } else if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && other.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); + } else if self.underlying < ~Self::indent() && (other.underlying > ~Self::indent() || other.underlying == ~Self::indent()) { + res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); + } + res + } +} + +impl core::ops::Divide for I256 { + /// Divide a I256 by a I256. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); + let mut res = ~Self::new(); + if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && divisor.underlying > ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && divisor.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + } + res + } +} diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw new file mode 100644 index 00000000..c88c0568 --- /dev/null +++ b/sway_libs/src/signed_integers/i32.sw @@ -0,0 +1,151 @@ +library i32; + +use core::num::*; +use std::assert::assert; + +/// The 32-bit signed integer type. +/// Represented as an underlying u32 value. +/// Actual value is underlying value minus 2 ^ 31 +/// Max value is 2 ^ 31 - 1, min value is - 2 ^ 31 +pub struct I32 { + underlying: u32, +} + +pub trait From { + /// Function for creating I32 from u32 + fn from(underlying: u32) -> Self; +} + +impl From for I32 { + /// Helper function to get a signed number from with an underlying + fn from(underlying: u32) -> Self { + Self { + underlying, + } + } +} + +impl core::ops::Eq for I32 { + fn eq(self, other: Self) -> bool { + self.underlying == other.underlying + } +} + +impl core::ops::Ord for I32 { + fn gt(self, other: Self) -> bool { + self.underlying > other.underlying + } + + fn lt(self, other: Self) -> bool { + self.underlying < other.underlying + } +} + +impl I32 { + /// The underlying value that corresponds to zero signed value + pub fn indent() -> u32 { + 2147483648u32 + } +} + +impl I32 { + /// Initializes a new, zeroed I32. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } + } + + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~u32::min(), + } + } + + /// The largest value that can be represented by this type, + pub fn max() -> Self { + Self { + underlying: ~u32::max(), + } + } + + /// The size of this type in bits. + pub fn bits() -> u32 { + 32 + } + + /// Helper function to get a negative value of unsigned numbers + pub fn neg_from(value: u32) -> Self { + Self { + underlying: ~Self::indent() - value, + } + } + + /// Helper function to get a positive value from unsigned number + fn from_uint(value: u32) -> Self { + // as the minimal value of I32 is 2147483648 (1 << 31) we should add ~I32::indent() (1 << 31) + let underlying: u32 = value + ~Self::indent(); + Self { + underlying + } + } +} + +impl core::ops::Add for I32 { + /// Add a I32 to a I32. Panics on overflow. + fn add(self, other: Self) -> Self { + // subtract 1 << 31 to avoid double move + ~Self::from(self.underlying - ~Self::indent() + other.underlying) + } +} + +impl core::ops::Subtract for I32 { + /// Subtract a I32 from a I32. Panics of overflow. + fn subtract(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self > other { + // add 1 << 31 to avoid loosing the move + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); + } else { + // subtract from 1 << 31 as we are getting a negative value + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); + } + res + } +} + +impl core::ops::Multiply for I32 { + /// Multiply a I32 with a I32. Panics of overflow. + fn multiply(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self.underlying >= ~Self::indent() && other.underlying >= ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); + } else if self.underlying < ~Self::indent() && other.underlying >= ~Self::indent() { + res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); + } + res + } +} + +impl core::ops::Divide for I32 { + /// Divide a I32 by a I32. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); + let mut res = ~Self::new(); + if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + } + res + } +} diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw new file mode 100644 index 00000000..5fa5826c --- /dev/null +++ b/sway_libs/src/signed_integers/i64.sw @@ -0,0 +1,151 @@ +library i64; + +use core::num::*; +use std::assert::assert; + +/// The 64-bit signed integer type. +/// Represented as an underlying u64 value. +/// Actual value is underlying value minus 2 ^ 63 +/// Max value is 2 ^ 63 - 1, min value is - 2 ^ 63 +pub struct I64 { + underlying: u64, +} + +pub trait From { + /// Function for creating I64 from u64 + fn from(underlying: u64) -> Self; +} + +impl From for I64 { + /// Helper function to get a signed number from with an underlying + fn from(underlying: u64) -> Self { + Self { + underlying, + } + } +} + +impl core::ops::Eq for I64 { + fn eq(self, other: Self) -> bool { + self.underlying == other.underlying + } +} + +impl core::ops::Ord for I64 { + fn gt(self, other: Self) -> bool { + self.underlying > other.underlying + } + + fn lt(self, other: Self) -> bool { + self.underlying < other.underlying + } +} + +impl I64 { + /// The underlying value that corresponds to zero signed value + pub fn indent() -> u64 { + 9223372036854775808u64 + } +} + +impl I64 { + /// Initializes a new, zeroed I64. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } + } + + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~u64::min(), + } + } + + /// The largest value that can be represented by this type, + pub fn max() -> Self { + Self { + underlying: ~u64::max(), + } + } + + /// The size of this type in bits. + pub fn bits() -> u32 { + 64 + } + + /// Helper function to get a negative value of unsigned number + pub fn neg_from(value: u64) -> Self { + Self { + underlying: ~Self::indent() - value, + } + } + + /// Helper function to get a positive value from unsigned number + fn from_uint(value: u64) -> Self { + // as the minimal value of I64 is -~I64::indent() (1 << 63) we should add ~I64::indent() (1 << 63) + let underlying: u64 = value + ~Self::indent(); + Self { + underlying + } + } +} + +impl core::ops::Add for I64 { + /// Add a I64 to a I64. Panics on overflow. + fn add(self, other: Self) -> Self { + // subtract 1 << 63 to avoid double move + ~Self::from(self.underlying - ~Self::indent() + other.underlying) + } +} + +impl core::ops::Subtract for I64 { + /// Subtract a I64 from a I64. Panics of overflow. + fn subtract(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self > other { + // add 1 << 63 to avoid loosing the move + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); + } else { + // subtract from 1 << 63 as we are getting a negative value + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); + } + res + } +} + +impl core::ops::Multiply for I64 { + /// Multiply a I64 with a I64. Panics of overflow. + fn multiply(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self.underlying >= ~Self::indent() && other.underlying >= ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); + } else if self.underlying < ~Self::indent() && other.underlying >= ~Self::indent() { + res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); + } + res + } +} + +impl core::ops::Divide for I64 { + /// Divide a I64 by a I64. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); + let mut res = ~Self::new(); + if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + } + res + } +} diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw new file mode 100644 index 00000000..d67c56a8 --- /dev/null +++ b/sway_libs/src/signed_integers/i8.sw @@ -0,0 +1,147 @@ +library i8; + +use core::num::*; +use std::assert::assert; + +/// The 8-bit signed integer type. +/// Represented as an underlying u8 value. +/// Actual value is underlying value minus 2 ^ 7 +/// Max value is 2 ^ 7 - 1, min value is - 2 ^ 7 +pub struct I8 { + underlying: u8, +} + +pub trait From { + /// Function for creating I8 from u8 + fn from(underlying: u8) -> Self; +} + +impl From for I8 { + /// Helper function to get a signed number from with an underlying + fn from(underlying: u8) -> Self { + Self { + underlying + } + } +} + +impl core::ops::Eq for I8 { + fn eq(self, other: Self) -> bool { + self.underlying == other.underlying + } +} + +impl core::ops::Ord for I8 { + fn gt(self, other: Self) -> bool { + self.underlying > other.underlying + } + + fn lt(self, other: Self) -> bool { + self.underlying < other.underlying + } +} + +impl I8 { + /// The underlying value that corresponds to zero signed value + pub fn indent() -> u8 { + 128u8 + } +} + +impl I8 { + /// Initializes a new, zeroed I8. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } + } + + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~u8::min(), + } + } + + /// The largest value that can be represented by this type, + pub fn max() -> Self { + Self { + underlying: ~u8::max(), + } + } + + /// The size of this type in bits. + pub fn bits() -> u32 { + 8 + } + + /// Helper function to get a negative value of unsigned number + pub fn neg_from(value: u8) -> Self { + Self { + underlying: ~Self::indent() - value, + } + } + + /// Helper function to get a positive value from unsigned number + fn from_uint(value: u8) -> Self { + let underlying: u8 = value + ~Self::indent(); // as the minimal value of I8 is -~I8::indent() (1 << 7) we should add ~I8::indent() (1 << 7) + Self { + underlying + } + } +} + +impl core::ops::Add for I8 { + /// Add a I8 to a I8. Panics on overflow. + fn add(self, other: Self) -> Self { + ~Self::from(self.underlying - ~Self::indent() + other.underlying) // subtract 1 << 7 to avoid double move + } +} + +impl core::ops::Subtract for I8 { + /// Subtract a I8 from a I8. Panics of overflow. + fn subtract(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self > other { + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); // add 1 << 7 to avoid loosing the move + } else { + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); // subtract from 1 << 7 as we are getting a negative value + } + res + } +} + +impl core::ops::Multiply for I8 { + /// Multiply a I8 with a I8. Panics of overflow. + fn multiply(self, other: Self) -> Self { + let mut res = ~Self::new(); + if self.underlying >= ~Self::indent() && other.underlying >= ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() && other.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); + } else if self.underlying < ~Self::indent() && other.underlying >= ~Self::indent() { + res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); + } + res + } +} + +impl core::ops::Divide for I8 { + /// Divide a I8 by a I8. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); + let mut res = ~Self::new(); + if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() && divisor.underlying < ~Self::indent() { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + } + res + } +} diff --git a/tests/src/test_projects/signed_integers/i128_test/.gitignore b/tests/src/test_projects/signed_integers/i128_test/.gitignore new file mode 100644 index 00000000..77d3844f --- /dev/null +++ b/tests/src/test_projects/signed_integers/i128_test/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/test_projects/signed_integers/i128_test/Forc.toml b/tests/src/test_projects/signed_integers/i128_test/Forc.toml new file mode 100644 index 00000000..5c815bc2 --- /dev/null +++ b/tests/src/test_projects/signed_integers/i128_test/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "i128_test" + +[dependencies] +sway_libs = { path = "../../../../../sway_libs" } diff --git a/tests/src/test_projects/signed_integers/i128_test/src/main.sw b/tests/src/test_projects/signed_integers/i128_test/src/main.sw new file mode 100644 index 00000000..59c7efbd --- /dev/null +++ b/tests/src/test_projects/signed_integers/i128_test/src/main.sw @@ -0,0 +1,64 @@ +script; + +use std::assert::assert; +use sway_libs::i128::I128; +use std::u128::U128; +use core::num::*; + +fn main() -> bool { + let u128_one = U128 { + upper: 0, + lower: 1, + }; + let u128_two = U128 { + upper: 0, + lower: 2, + }; + let one = ~I128::from_uint(u128_one); + let mut res = one + ~I128::from_uint(u128_one); + assert(res == ~I128::from_uint(u128_two)); + + let u128_10 = U128 { + upper: 0, + lower: 10, + }; + let u128_11 = U128 { + upper: 0, + lower: 11, + }; + res = ~I128::from_uint(u128_10) - ~I128::from_uint(u128_11); + assert(res.underlying.lower == ~u64::max()); + + res = ~I128::from_uint(u128_10) * ~I128::neg_from(u128_one); + assert(res == ~I128::neg_from(u128_10)); + + res = ~I128::from_uint(u128_10) * ~I128::from_uint(u128_10); + let u128_100 = U128 { + upper: 0, + lower: 100, + }; + assert(res == ~I128::from_uint(u128_100)); + + let u128_lower_max_u64 = U128 { + upper: 0, + lower: ~u64::max(), + }; + + res = ~I128::from_uint(u128_10) / ~I128::from(u128_lower_max_u64); + assert(res == ~I128::neg_from(u128_10)); + + let u128_5 = U128 { + upper: 0, + lower: 5, + }; + + let u128_2 = U128 { + upper: 0, + lower: 2, + }; + + res = ~I128::from_uint(u128_10) / ~I128::from_uint(u128_5); + assert(res == ~I128::from_uint(u128_2)); + + true +} diff --git a/tests/src/test_projects/signed_integers/i16_test/.gitignore b/tests/src/test_projects/signed_integers/i16_test/.gitignore new file mode 100644 index 00000000..77d3844f --- /dev/null +++ b/tests/src/test_projects/signed_integers/i16_test/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/test_projects/signed_integers/i16_test/Forc.toml b/tests/src/test_projects/signed_integers/i16_test/Forc.toml new file mode 100644 index 00000000..06eae41f --- /dev/null +++ b/tests/src/test_projects/signed_integers/i16_test/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "i16_test" + +[dependencies] +sway_libs = { path = "../../../../../sway_libs" } diff --git a/tests/src/test_projects/signed_integers/i16_test/src/main.sw b/tests/src/test_projects/signed_integers/i16_test/src/main.sw new file mode 100644 index 00000000..280b601d --- /dev/null +++ b/tests/src/test_projects/signed_integers/i16_test/src/main.sw @@ -0,0 +1,27 @@ +script; + +use std::assert::assert; +use std::i16::I16; + +fn main() -> bool { + let one = ~I16::from_uint(1u16); + let mut res = one + ~I16::from_uint(1u16); + assert(res == ~I16::from_uint(2u16)); + + res = ~I16::from_uint(10u16) - ~I16::from_uint(11u16); + assert(res == ~I16::from(32767u16)); + + res = ~I16::from_uint(10u16) * ~I16::neg_from(1u16); + assert(res == ~I16::neg_from(10u16)); + + res = ~I16::from_uint(10u16) * ~I16::from_uint(10u16); + assert(res == ~I16::from_uint(100u16)); + + res = ~I16::from_uint(10u16) / ~I16::neg_from(1u16); + assert(res == ~I16::neg_from(10u16)); + + res = ~I16::from_uint(10u16) / ~I16::from_uint(5u16); + assert(res == ~I16::from_uint(2u16)); + + true +} diff --git a/tests/src/test_projects/signed_integers/i256_test/.gitignore b/tests/src/test_projects/signed_integers/i256_test/.gitignore new file mode 100644 index 00000000..77d3844f --- /dev/null +++ b/tests/src/test_projects/signed_integers/i256_test/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/test_projects/signed_integers/i256_test/Forc.toml b/tests/src/test_projects/signed_integers/i256_test/Forc.toml new file mode 100644 index 00000000..57489f19 --- /dev/null +++ b/tests/src/test_projects/signed_integers/i256_test/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "i256_test" + +[dependencies] +sway_libs = { path = "../../../../../sway_libs" } diff --git a/tests/src/test_projects/signed_integers/i256_test/src/main.sw b/tests/src/test_projects/signed_integers/i256_test/src/main.sw new file mode 100644 index 00000000..0ece77d2 --- /dev/null +++ b/tests/src/test_projects/signed_integers/i256_test/src/main.sw @@ -0,0 +1,81 @@ +script; + +use std::assert::assert; +use sway_libs::i256::I256; +use std::u256::U256; +use core::num::*; + +fn main() -> bool { + let u128_one = U256 { + a: 0, + b: 0, + c: 0, + d: 1, + }; + let u128_two = U256 { + a: 0, + b: 0, + c: 0, + d: 2, + }; + let one = ~I256::from_uint(u128_one); + let mut res = one + ~I256::from_uint(u128_one); + assert(res == ~I256::from_uint(u128_two)); + + let u128_10 = U256 { + a: 0, + b: 0, + c: 0, + d: 10, + }; + let u128_11 = U256 { + a: 0, + b: 0, + c: 0, + d: 11, + }; + res = ~I256::from_uint(u128_10) - ~I256::from_uint(u128_11); + assert(res.underlying.c == ~u64::max()); + assert(res.underlying.d == ~u64::max()); + + res = ~I256::from_uint(u128_10) * ~I256::neg_from(u128_one); + assert(res == ~I256::neg_from(u128_10)); + + res = ~I256::from_uint(u128_10) * ~I256::from_uint(u128_10); + let u128_100 = U256 { + a: 0, + b: 0, + c: 0, + d: 100, + }; + assert(res == ~I256::from_uint(u128_100)); + + let u128_lower_max_u64 = U256 { + a: 0, + b: 0, + c: 0, + d: ~u64::max(), + }; + + res = ~I256::from_uint(u128_10) / ~I256::from(u128_lower_max_u64); + assert(res == ~I256::neg_from(u128_10)); + + let u128_5 = U256 { + a: 0, + b: 0, + c: 0, + d: 5, + }; + + let u128_2 = U256 { + a: 0, + b: 0, + c: 0, + d: 2, + }; + + res = ~I256::from_uint(u128_10) / ~I256::from_uint(u128_5); + assert(res == ~I256::from_uint(u128_2)); + + true +} diff --git a/tests/src/test_projects/signed_integers/i32_test/.gitignore b/tests/src/test_projects/signed_integers/i32_test/.gitignore new file mode 100644 index 00000000..77d3844f --- /dev/null +++ b/tests/src/test_projects/signed_integers/i32_test/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/test_projects/signed_integers/i32_test/Forc.toml b/tests/src/test_projects/signed_integers/i32_test/Forc.toml new file mode 100644 index 00000000..78b60362 --- /dev/null +++ b/tests/src/test_projects/signed_integers/i32_test/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "i32_test" + +[dependencies] +sway_libs = { path = "../../../../../sway_libs" } diff --git a/tests/src/test_projects/signed_integers/i32_test/src/main.sw b/tests/src/test_projects/signed_integers/i32_test/src/main.sw new file mode 100644 index 00000000..4d3d4efa --- /dev/null +++ b/tests/src/test_projects/signed_integers/i32_test/src/main.sw @@ -0,0 +1,27 @@ +script; + +use std::assert::assert; +use sway_libs::i32::I32; + +fn main() -> bool { + let one = ~I32::from_uint(1u32); + let mut res = one + ~I32::from_uint(1u32); + assert(res == ~I32::from_uint(2u32)); + + res = ~I32::from_uint(10u32) - ~I32::from_uint(11u32); + assert(res == ~I32::from(2147483647u32)); + + res = ~I32::from_uint(10u32) * ~I32::neg_from(1u32); + assert(res == ~I32::neg_from(10u32)); + + res = ~I32::from_uint(10u32) * ~I32::from_uint(10u32); + assert(res == ~I32::from_uint(100u32)); + + res = ~I32::from_uint(10u32) / ~I32::neg_from(1u32); + assert(res == ~I32::neg_from(10u32)); + + res = ~I32::from_uint(10u32) / ~I32::from_uint(5u32); + assert(res == ~I32::from_uint(2u32)); + + true +} diff --git a/tests/src/test_projects/signed_integers/i64_test/.gitignore b/tests/src/test_projects/signed_integers/i64_test/.gitignore new file mode 100644 index 00000000..77d3844f --- /dev/null +++ b/tests/src/test_projects/signed_integers/i64_test/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/test_projects/signed_integers/i64_test/Forc.toml b/tests/src/test_projects/signed_integers/i64_test/Forc.toml new file mode 100644 index 00000000..fc62ab1b --- /dev/null +++ b/tests/src/test_projects/signed_integers/i64_test/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "i64_test" + +[dependencies] +sway_libs = { path = "../../../../../sway_libs" } diff --git a/tests/src/test_projects/signed_integers/i64_test/src/main.sw b/tests/src/test_projects/signed_integers/i64_test/src/main.sw new file mode 100644 index 00000000..a4df2988 --- /dev/null +++ b/tests/src/test_projects/signed_integers/i64_test/src/main.sw @@ -0,0 +1,27 @@ +script; + +use std::assert::assert; +use sway_libs::i64::I64; + +fn main() -> bool { + let one = ~I64::from_uint(1u64); + let mut res = one + ~I64::from_uint(1u64); + assert(res == ~I64::from_uint(2u64)); + + res = ~I64::from_uint(10u64) - ~I64::from_uint(11u64); + assert(res == ~I64::from(9223372036854775807u64)); + + res = ~I64::from_uint(10u64) * ~I64::neg_from(1); + assert(res == ~I64::neg_from(10)); + + res = ~I64::from_uint(10u64) * ~I64::from_uint(10u64); + assert(res == ~I64::from_uint(100u64)); + + res = ~I64::from_uint(10u64) / ~I64::from(9223372036854775807u64); + assert(res == ~I64::neg_from(10u64)); + + res = ~I64::from_uint(10u64) / ~I64::from_uint(5u64); + assert(res == ~I64::from_uint(2u64)); + + true +} diff --git a/tests/src/test_projects/signed_integers/i8_test/.gitignore b/tests/src/test_projects/signed_integers/i8_test/.gitignore new file mode 100644 index 00000000..77d3844f --- /dev/null +++ b/tests/src/test_projects/signed_integers/i8_test/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/test_projects/signed_integers/i8_test/Forc.toml b/tests/src/test_projects/signed_integers/i8_test/Forc.toml new file mode 100644 index 00000000..f4b83f1e --- /dev/null +++ b/tests/src/test_projects/signed_integers/i8_test/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "i8_test" + +[dependencies] +sway_libs = { path = "../../../../../sway_libs" } diff --git a/tests/src/test_projects/signed_integers/i8_test/src/main.sw b/tests/src/test_projects/signed_integers/i8_test/src/main.sw new file mode 100644 index 00000000..b1f42009 --- /dev/null +++ b/tests/src/test_projects/signed_integers/i8_test/src/main.sw @@ -0,0 +1,27 @@ +script; + +use std::assert::assert; +use sway_libs::i8::I8; + +fn main() -> bool { + let one = ~I8::from_uint(1u8); + let mut res = one + ~I8::from_uint(1u8); + assert(res == ~I8::from_uint(2u8)); + + res = ~I8::from_uint(10u8) - ~I8::from_uint(11u8); + assert(res == ~I8::from(127u8)); + + res = ~I8::from_uint(10u8) * ~I8::from(127u8); + assert(res == ~I8::from(118u8)); + + res = ~I8::from_uint(10u8) * ~I8::from_uint(10u8); + assert(res == ~I8::from_uint(100u8)); + + res = ~I8::from_uint(10u8) / ~I8::from(127u8); + assert(res == ~I8::from(118u8)); + + res = ~I8::from_uint(10u8) / ~I8::from_uint(5u8); + assert(res == ~I8::from_uint(2u8)); + + true +} From d73505329df7c503b7d54f143b892c919fc4d4f7 Mon Sep 17 00:00:00 2001 From: tyshkor Date: Thu, 6 Oct 2022 14:43:12 +0200 Subject: [PATCH 02/12] fmt fixes --- sway_libs/src/signed_integers/i128.sw | 48 +++++++++++++++++-------- sway_libs/src/signed_integers/i16.sw | 46 +++++++++++++++--------- sway_libs/src/signed_integers/i256.sw | 50 ++++++++++++++++++--------- sway_libs/src/signed_integers/i32.sw | 44 ++++++++++++++--------- sway_libs/src/signed_integers/i64.sw | 44 ++++++++++++++--------- sway_libs/src/signed_integers/i8.sw | 44 ++++++++++++++--------- 6 files changed, 180 insertions(+), 96 deletions(-) diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index ce425abb..5fc848c2 100644 --- a/sway_libs/src/signed_integers/i128.sw +++ b/sway_libs/src/signed_integers/i128.sw @@ -20,9 +20,7 @@ pub trait From { impl From for I128 { /// Helper function to get a signed number from with an underlying fn from(underlying: U128) -> Self { - Self { - underlying - } + Self { underlying } } } @@ -90,9 +88,7 @@ impl I128 { fn from_uint(value: U128) -> Self { // as the minimal value of I128 is -~I128::indent() (1 << 63) we should add ~I128::indent() (1 << 63) let underlying: U128 = value + ~Self::indent(); - Self { - underlying - } + Self { underlying } } } @@ -123,13 +119,25 @@ impl core::ops::Multiply for I128 { /// Multiply a I128 with a I128. Panics of overflow. fn multiply(self, other: Self) -> Self { let mut res = ~Self::new(); - if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && (other.underlying > ~Self::indent() || other.underlying == ~Self::indent()) { - res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && (other.underlying > ~Self::indent() + || other.underlying == ~Self::indent()) + { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); - } else if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && other.underlying < ~Self::indent() { + } else if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && other.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); - } else if self.underlying < ~Self::indent() && (other.underlying > ~Self::indent() || other.underlying == ~Self::indent()) { + } else if self.underlying < ~Self::indent() + && (other.underlying > ~Self::indent() + || other.underlying == ~Self::indent()) + { res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); } res @@ -141,13 +149,23 @@ impl core::ops::Divide for I128 { fn divide(self, divisor: Self) -> Self { assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && divisor.underlying > ~Self::indent() { + if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && divisor.underlying > ~Self::indent() + { res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && divisor.underlying < ~Self::indent() { + } else if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && divisor.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw index 0c749874..b6256c6c 100644 --- a/sway_libs/src/signed_integers/i16.sw +++ b/sway_libs/src/signed_integers/i16.sw @@ -19,9 +19,7 @@ pub trait From { impl From for I16 { /// Helper function to get a signed number from with an underlying fn from(underlying: u16) -> Self { - Self { - underlying - } + Self { underlying } } } @@ -85,10 +83,8 @@ impl I16 { /// Helper function to get a positive value from unsigned number fn from_uint(value: u16) -> Self { // as the minimal value of I16 is -~I16::indent() (1 << 15) we should add ~I16::indent() (1 << 15) - let underlying: u16 = value + ~Self::indent(); - Self { - underlying - } + let underlying: u16 = value + ~Self::indent(); + Self { underlying } } } @@ -119,13 +115,21 @@ impl core::ops::Multiply for I16 { /// Multiply a I16 with a I16. Panics of overflow. fn multiply(self, other: Self) -> Self { let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() && other.underlying >= ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + if self.underlying >= ~Self::indent() + && other.underlying >= ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() && other.underlying < ~Self::indent() { + } else if self.underlying >= ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); - } else if self.underlying < ~Self::indent() && other.underlying >= ~Self::indent() { + } else if self.underlying < ~Self::indent() + && other.underlying >= ~Self::indent() + { res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); } res @@ -137,13 +141,21 @@ impl core::ops::Divide for I16 { fn divide(self, divisor: Self) -> Self { assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + if self.underlying >= ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() && divisor.underlying < ~Self::indent() { + } else if self.underlying >= ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res diff --git a/sway_libs/src/signed_integers/i256.sw b/sway_libs/src/signed_integers/i256.sw index 9d154aa1..e9e4f11b 100644 --- a/sway_libs/src/signed_integers/i256.sw +++ b/sway_libs/src/signed_integers/i256.sw @@ -20,9 +20,7 @@ pub trait From { impl From for I256 { /// Helper function to get a signed number from with an underlying fn from(underlying: U256) -> Self { - Self { - underlying, - } + Self { underlying } } } @@ -92,9 +90,7 @@ impl I256 { fn from_uint(value: U256) -> Self { // as the minimal value of I256 is -~I256::indent() (1 << 63) we should add ~I256::indent() (1 << 63) let underlying: U256 = value + ~Self::indent(); - Self { - underlying - } + Self { underlying } } } @@ -125,13 +121,25 @@ impl core::ops::Multiply for I256 { /// Multiply a I256 with a I256. Panics of overflow. fn multiply(self, other: Self) -> Self { let mut res = ~Self::new(); - if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && (other.underlying > ~Self::indent() || other.underlying == ~Self::indent()) { - res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && (other.underlying > ~Self::indent() + || other.underlying == ~Self::indent()) + { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); - } else if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && other.underlying < ~Self::indent() { + } else if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && other.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); - } else if self.underlying < ~Self::indent() && (other.underlying > ~Self::indent() || other.underlying == ~Self::indent()) { + } else if self.underlying < ~Self::indent() + && (other.underlying > ~Self::indent() + || other.underlying == ~Self::indent()) + { res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); } res @@ -143,13 +151,23 @@ impl core::ops::Divide for I256 { fn divide(self, divisor: Self) -> Self { assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && divisor.underlying > ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) && divisor.underlying < ~Self::indent() { + } else if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && divisor.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index c88c0568..793ec709 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -19,9 +19,7 @@ pub trait From { impl From for I32 { /// Helper function to get a signed number from with an underlying fn from(underlying: u32) -> Self { - Self { - underlying, - } + Self { underlying } } } @@ -86,9 +84,7 @@ impl I32 { fn from_uint(value: u32) -> Self { // as the minimal value of I32 is 2147483648 (1 << 31) we should add ~I32::indent() (1 << 31) let underlying: u32 = value + ~Self::indent(); - Self { - underlying - } + Self { underlying } } } @@ -119,13 +115,21 @@ impl core::ops::Multiply for I32 { /// Multiply a I32 with a I32. Panics of overflow. fn multiply(self, other: Self) -> Self { let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() && other.underlying >= ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + if self.underlying >= ~Self::indent() + && other.underlying >= ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() && other.underlying < ~Self::indent() { + } else if self.underlying >= ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); - } else if self.underlying < ~Self::indent() && other.underlying >= ~Self::indent() { + } else if self.underlying < ~Self::indent() + && other.underlying >= ~Self::indent() + { res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); } res @@ -137,13 +141,21 @@ impl core::ops::Divide for I32 { fn divide(self, divisor: Self) -> Self { assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + if self.underlying >= ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() && divisor.underlying < ~Self::indent() { + } else if self.underlying >= ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw index 5fa5826c..a0690b99 100644 --- a/sway_libs/src/signed_integers/i64.sw +++ b/sway_libs/src/signed_integers/i64.sw @@ -19,9 +19,7 @@ pub trait From { impl From for I64 { /// Helper function to get a signed number from with an underlying fn from(underlying: u64) -> Self { - Self { - underlying, - } + Self { underlying } } } @@ -86,9 +84,7 @@ impl I64 { fn from_uint(value: u64) -> Self { // as the minimal value of I64 is -~I64::indent() (1 << 63) we should add ~I64::indent() (1 << 63) let underlying: u64 = value + ~Self::indent(); - Self { - underlying - } + Self { underlying } } } @@ -119,13 +115,21 @@ impl core::ops::Multiply for I64 { /// Multiply a I64 with a I64. Panics of overflow. fn multiply(self, other: Self) -> Self { let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() && other.underlying >= ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + if self.underlying >= ~Self::indent() + && other.underlying >= ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() && other.underlying < ~Self::indent() { + } else if self.underlying >= ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); - } else if self.underlying < ~Self::indent() && other.underlying >= ~Self::indent() { + } else if self.underlying < ~Self::indent() + && other.underlying >= ~Self::indent() + { res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); } res @@ -137,13 +141,21 @@ impl core::ops::Divide for I64 { fn divide(self, divisor: Self) -> Self { assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + if self.underlying >= ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() && divisor.underlying < ~Self::indent() { + } else if self.underlying >= ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw index d67c56a8..78246209 100644 --- a/sway_libs/src/signed_integers/i8.sw +++ b/sway_libs/src/signed_integers/i8.sw @@ -19,9 +19,7 @@ pub trait From { impl From for I8 { /// Helper function to get a signed number from with an underlying fn from(underlying: u8) -> Self { - Self { - underlying - } + Self { underlying } } } @@ -85,9 +83,7 @@ impl I8 { /// Helper function to get a positive value from unsigned number fn from_uint(value: u8) -> Self { let underlying: u8 = value + ~Self::indent(); // as the minimal value of I8 is -~I8::indent() (1 << 7) we should add ~I8::indent() (1 << 7) - Self { - underlying - } + Self { underlying } } } @@ -115,13 +111,21 @@ impl core::ops::Multiply for I8 { /// Multiply a I8 with a I8. Panics of overflow. fn multiply(self, other: Self) -> Self { let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() && other.underlying >= ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && other.underlying < ~Self::indent() { + if self.underlying >= ~Self::indent() + && other.underlying >= ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) * (other.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) * (~Self::indent() - other.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() && other.underlying < ~Self::indent() { + } else if self.underlying >= ~Self::indent() + && other.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) * (~Self::indent() - other.underlying)); - } else if self.underlying < ~Self::indent() && other.underlying >= ~Self::indent() { + } else if self.underlying < ~Self::indent() + && other.underlying >= ~Self::indent() + { res = ~Self::from(~Self::indent() - (other.underlying - ~Self::indent()) * (~Self::indent() - self.underlying)); } res @@ -133,13 +137,21 @@ impl core::ops::Divide for I8 { fn divide(self, divisor: Self) -> Self { assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying -~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() && divisor.underlying < ~Self::indent() { + if self.underlying >= ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() && divisor.underlying < ~Self::indent() { + } else if self.underlying >= ~Self::indent() + && divisor.underlying < ~Self::indent() + { res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() && divisor.underlying > ~Self::indent() { + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res From 1c531a4fa4e0253f38baf3ba8aeee2dba62b86b0 Mon Sep 17 00:00:00 2001 From: tyshkor Date: Thu, 6 Oct 2022 14:53:28 +0200 Subject: [PATCH 03/12] fmt fixes --- .../src/merkle_proof/binary_merkle_proof.sw | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/sway_libs/src/merkle_proof/binary_merkle_proof.sw b/sway_libs/src/merkle_proof/binary_merkle_proof.sw index 6c7b04ba..11687130 100644 --- a/sway_libs/src/merkle_proof/binary_merkle_proof.sw +++ b/sway_libs/src/merkle_proof/binary_merkle_proof.sw @@ -17,32 +17,28 @@ pub const LEAF = 0u8; pub const NODE = 1u8; /// Returns the computed leaf hash of "MTH({d(0)}) = SHA-256(0x00 || d(0))". -/// +/// /// # Arguments -/// +/// /// * 'data' - The hash of the leaf data. pub fn leaf_digest(data: b256) -> b256 { sha256((LEAF, data)) } /// Returns the computed node hash of "MTH(D[n]) = SHA-256(0x01 || MTH(D[0:k]) || MTH(D[k:n]))". -/// +/// /// # Arguments -/// +/// /// * 'left' - The hash of the left node. /// * 'right' - The hash of the right node. pub fn node_digest(left: b256, right: b256) -> b256 { - sha256(( - NODE, - left, - right, - )) + sha256((NODE, left, right, )) } /// Calculates the length of the path to a leaf -/// +/// /// # Arguments -/// +/// /// * `key` - The key or index of the leaf. /// * `num_leaves` - The total number of leaves in the Merkle Tree. fn path_length_from_key(key: u64, num_leaves: u64) -> u64 { @@ -80,16 +76,16 @@ fn path_length_from_key(key: u64, num_leaves: u64) -> u64 { } /// This function will compute and return a Merkle root given a leaf and corresponding proof. -/// +/// /// # Arguments -/// +/// /// * 'key' - The key or index of the leaf to prove. /// * `merkle_leaf` - The hash of a leaf on the Merkle Tree. /// * 'num_leaves' - The number of leaves in the Merkle Tree. /// * `proof` - The Merkle proof that will be used to traverse the Merkle Tree and compute a root. -/// +/// /// # Reverts -/// +/// /// * When an incorrect proof length is provided. /// * When there is one or no leaves and a proof is provided. /// * When the key is greater than or equal to the number of leaves. @@ -156,9 +152,9 @@ pub fn process_proof( } /// Calculates the starting bit of the path to a leaf -/// +/// /// # Arguments -/// +/// /// * `num_leaves` - The number of leaves in the Merkle Tree. fn starting_bit(num_leaves: u64) -> u64 { let mut starting_bit = 0; @@ -172,9 +168,9 @@ fn starting_bit(num_leaves: u64) -> u64 { /// This function will take a Merkle leaf and proof and return whether the corresponding root /// matches the root given. -/// +/// /// # Arguments -/// +/// /// * 'key' - The key or index of the leaf to verify. /// * `merkle_leaf` - The hash of a leaf on the Merkle Tree. /// * `merkle_root` - The pre-computed Merkle root that will be used to verify the leaf and proof. From 9045e549a1bffef4c89a12739090a9efbd28bce3 Mon Sep 17 00:00:00 2001 From: tyshkor Date: Fri, 14 Oct 2022 18:15:57 +0200 Subject: [PATCH 04/12] prelude --- sway_libs/src/signed_integers/i128.sw | 2 -- 1 file changed, 2 deletions(-) diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index 5fc848c2..7041daf8 100644 --- a/sway_libs/src/signed_integers/i128.sw +++ b/sway_libs/src/signed_integers/i128.sw @@ -1,7 +1,5 @@ library i128; -use core::num::*; -use std::assert::assert; use std::u128::U128; /// The 128-bit signed integer type. From 5df9dddf7042961bf5b80eae1b535e20a4582c2c Mon Sep 17 00:00:00 2001 From: tyshkor Date: Fri, 14 Oct 2022 18:46:33 +0200 Subject: [PATCH 05/12] remove prelude imports everywhere --- sway_libs/src/signed_integers/i16.sw | 3 --- sway_libs/src/signed_integers/i256.sw | 2 -- sway_libs/src/signed_integers/i32.sw | 3 --- sway_libs/src/signed_integers/i64.sw | 3 --- sway_libs/src/signed_integers/i8.sw | 3 --- 5 files changed, 14 deletions(-) diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw index b6256c6c..f5aa55cc 100644 --- a/sway_libs/src/signed_integers/i16.sw +++ b/sway_libs/src/signed_integers/i16.sw @@ -1,8 +1,5 @@ library i16; -use core::num::*; -use std::assert::assert; - /// The 16-bit signed integer type. /// Represented as an underlying u16 value. /// Actual value is underlying value minus 2 ^ 15 diff --git a/sway_libs/src/signed_integers/i256.sw b/sway_libs/src/signed_integers/i256.sw index e9e4f11b..ebf99e83 100644 --- a/sway_libs/src/signed_integers/i256.sw +++ b/sway_libs/src/signed_integers/i256.sw @@ -1,7 +1,5 @@ library i256; -use core::num::*; -use std::assert::assert; use std::u256::U256; /// The 128-bit signed integer type. diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index 793ec709..324c5ef7 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -1,8 +1,5 @@ library i32; -use core::num::*; -use std::assert::assert; - /// The 32-bit signed integer type. /// Represented as an underlying u32 value. /// Actual value is underlying value minus 2 ^ 31 diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw index a0690b99..52222ad2 100644 --- a/sway_libs/src/signed_integers/i64.sw +++ b/sway_libs/src/signed_integers/i64.sw @@ -1,8 +1,5 @@ library i64; -use core::num::*; -use std::assert::assert; - /// The 64-bit signed integer type. /// Represented as an underlying u64 value. /// Actual value is underlying value minus 2 ^ 63 diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw index 78246209..9ea95857 100644 --- a/sway_libs/src/signed_integers/i8.sw +++ b/sway_libs/src/signed_integers/i8.sw @@ -1,8 +1,5 @@ library i8; -use core::num::*; -use std::assert::assert; - /// The 8-bit signed integer type. /// Represented as an underlying u8 value. /// Actual value is underlying value minus 2 ^ 7 From ab7ee05c118f336e8306107d451e8275bd09d039 Mon Sep 17 00:00:00 2001 From: tyshkor Date: Fri, 14 Oct 2022 18:50:02 +0200 Subject: [PATCH 06/12] return imports --- sway_libs/src/signed_integers/i16.sw | 2 ++ sway_libs/src/signed_integers/i32.sw | 2 ++ sway_libs/src/signed_integers/i64.sw | 2 ++ sway_libs/src/signed_integers/i8.sw | 2 ++ 4 files changed, 8 insertions(+) diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw index f5aa55cc..68ad50fe 100644 --- a/sway_libs/src/signed_integers/i16.sw +++ b/sway_libs/src/signed_integers/i16.sw @@ -1,5 +1,7 @@ library i16; +use core::num::*; + /// The 16-bit signed integer type. /// Represented as an underlying u16 value. /// Actual value is underlying value minus 2 ^ 15 diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index 324c5ef7..0358b624 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -1,5 +1,7 @@ library i32; +use core::num::*; + /// The 32-bit signed integer type. /// Represented as an underlying u32 value. /// Actual value is underlying value minus 2 ^ 31 diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw index 52222ad2..53f12761 100644 --- a/sway_libs/src/signed_integers/i64.sw +++ b/sway_libs/src/signed_integers/i64.sw @@ -1,5 +1,7 @@ library i64; +use core::num::*; + /// The 64-bit signed integer type. /// Represented as an underlying u64 value. /// Actual value is underlying value minus 2 ^ 63 diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw index 9ea95857..3e911c7e 100644 --- a/sway_libs/src/signed_integers/i8.sw +++ b/sway_libs/src/signed_integers/i8.sw @@ -1,5 +1,7 @@ library i8; +use core::num::*; + /// The 8-bit signed integer type. /// Represented as an underlying u8 value. /// Actual value is underlying value minus 2 ^ 7 From f9ba4f5e061fb5323ee428420c840138d6337c5c Mon Sep 17 00:00:00 2001 From: tyshkor Date: Fri, 14 Oct 2022 19:26:08 +0200 Subject: [PATCH 07/12] alphabetical order --- sway_libs/src/lib.sw | 3 +- sway_libs/src/signed_integers/i128.sw | 98 +++++++++++++-------------- sway_libs/src/signed_integers/i16.sw | 94 ++++++++++++------------- sway_libs/src/signed_integers/i256.sw | 98 +++++++++++++-------------- sway_libs/src/signed_integers/i32.sw | 38 +++++------ sway_libs/src/signed_integers/i64.sw | 36 +++++----- sway_libs/src/signed_integers/i8.sw | 88 ++++++++++++------------ 7 files changed, 227 insertions(+), 228 deletions(-) diff --git a/sway_libs/src/lib.sw b/sway_libs/src/lib.sw index 000a463d..6844c66c 100644 --- a/sway_libs/src/lib.sw +++ b/sway_libs/src/lib.sw @@ -1,11 +1,10 @@ library sway_libs; dep merkle_proof/binary_merkle_proof; -dep string/string; - dep signed_integers/i8; dep signed_integers/i16; dep signed_integers/i32; dep signed_integers/i64; dep signed_integers/i128; dep signed_integers/i256; +dep string/string; diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index 7041daf8..45273b2c 100644 --- a/sway_libs/src/signed_integers/i128.sw +++ b/sway_libs/src/signed_integers/i128.sw @@ -49,18 +49,16 @@ impl I128 { } impl I128 { - /// Initializes a new, zeroed I128. - pub fn new() -> Self { - Self { - underlying: ~Self::indent(), - } + /// The size of this type in bits. + pub fn bits() -> u32 { + 128 } - /// The smallest value that can be represented by this integer type. - pub fn min() -> Self { - Self { - underlying: ~U128::min(), - } + /// Helper function to get a positive value from unsigned number + fn from_uint(value: U128) -> Self { + // as the minimal value of I128 is -~I128::indent() (1 << 63) we should add ~I128::indent() (1 << 63) + let underlying: U128 = value + ~Self::indent(); + Self { underlying } } /// The largest value that can be represented by this type, @@ -70,9 +68,11 @@ impl I128 { } } - /// The size of this type in bits. - pub fn bits() -> u32 { - 128 + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~U128::min(), + } } /// Helper function to get a negative value of unsigned number @@ -82,11 +82,11 @@ impl I128 { } } - /// Helper function to get a positive value from unsigned number - fn from_uint(value: U128) -> Self { - // as the minimal value of I128 is -~I128::indent() (1 << 63) we should add ~I128::indent() (1 << 63) - let underlying: U128 = value + ~Self::indent(); - Self { underlying } + /// Initializes a new, zeroed I128. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } } } @@ -98,16 +98,29 @@ impl core::ops::Add for I128 { } } -impl core::ops::Subtract for I128 { - /// Subtract a I128 from a I128. Panics of overflow. - fn subtract(self, other: Self) -> Self { +impl core::ops::Divide for I128 { + /// Divide a I128 by a I128. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if self > other { - // add 1 << 63 to avoid loosing the move - res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); - } else { - // subtract from 1 << 63 as we are getting a negative value - res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); + if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && divisor.underlying < ~Self::indent() + { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res } @@ -142,29 +155,16 @@ impl core::ops::Multiply for I128 { } } -impl core::ops::Divide for I128 { - /// Divide a I128 by a I128. Panics if divisor is zero. - fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); +impl core::ops::Subtract for I128 { + /// Subtract a I128 from a I128. Panics of overflow. + fn subtract(self, other: Self) -> Self { let mut res = ~Self::new(); - if (self.underlying > ~Self::indent() - || self.underlying == ~Self::indent()) - && divisor.underlying > ~Self::indent() - { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() - && divisor.underlying < ~Self::indent() - { - res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if (self.underlying > ~Self::indent() - || self.underlying == ~Self::indent()) - && divisor.underlying < ~Self::indent() - { - res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() - && divisor.underlying > ~Self::indent() - { - res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + if self > other { + // add 1 << 63 to avoid loosing the move + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); + } else { + // subtract from 1 << 63 as we are getting a negative value + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); } res } diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw index 68ad50fe..b07183a1 100644 --- a/sway_libs/src/signed_integers/i16.sw +++ b/sway_libs/src/signed_integers/i16.sw @@ -46,18 +46,16 @@ impl I16 { } impl I16 { - /// Initializes a new, zeroed I16. - pub fn new() -> Self { - Self { - underlying: ~Self::indent(), - } + /// The size of this type in bits. + pub fn bits() -> u32 { + 16 } - /// The smallest value that can be represented by this integer type. - pub fn min() -> Self { - Self { - underlying: ~u16::min(), - } + /// Helper function to get a positive value from unsigned number + fn from_uint(value: u16) -> Self { + // as the minimal value of I16 is -~I16::indent() (1 << 15) we should add ~I16::indent() (1 << 15) + let underlying: u16 = value + ~Self::indent(); + Self { underlying } } /// The largest value that can be represented by this type, @@ -67,9 +65,11 @@ impl I16 { } } - /// The size of this type in bits. - pub fn bits() -> u32 { - 16 + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~u16::min(), + } } /// Helper function to get a negative value of unsigned number @@ -79,11 +79,11 @@ impl I16 { } } - /// Helper function to get a positive value from unsigned number - fn from_uint(value: u16) -> Self { - // as the minimal value of I16 is -~I16::indent() (1 << 15) we should add ~I16::indent() (1 << 15) - let underlying: u16 = value + ~Self::indent(); - Self { underlying } + /// Initializes a new, zeroed I16. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } } } @@ -95,16 +95,27 @@ impl core::ops::Add for I16 { } } -impl core::ops::Subtract for I16 { - /// Subtract a I16 from a I16. Panics of overflow. - fn subtract(self, other: Self) -> Self { +impl core::ops::Divide for I16 { + /// Divide a I16 by a I16. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if self > other { - // add 1 << 15 to avoid loosing the move - res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); - } else { - // subtract from 1 << 15 as we are getting a negative value - res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); + if self.underlying >= ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() + && divisor.underlying < ~Self::indent() + { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res } @@ -135,27 +146,16 @@ impl core::ops::Multiply for I16 { } } -impl core::ops::Divide for I16 { - /// Divide a I16 by a I16. Panics if divisor is zero. - fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); +impl core::ops::Subtract for I16 { + /// Subtract a I16 from a I16. Panics of overflow. + fn subtract(self, other: Self) -> Self { let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() - && divisor.underlying > ~Self::indent() - { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() - && divisor.underlying < ~Self::indent() - { - res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() - && divisor.underlying < ~Self::indent() - { - res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() - && divisor.underlying > ~Self::indent() - { - res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + if self > other { + // add 1 << 15 to avoid loosing the move + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); + } else { + // subtract from 1 << 15 as we are getting a negative value + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); } res } diff --git a/sway_libs/src/signed_integers/i256.sw b/sway_libs/src/signed_integers/i256.sw index ebf99e83..2445170b 100644 --- a/sway_libs/src/signed_integers/i256.sw +++ b/sway_libs/src/signed_integers/i256.sw @@ -51,18 +51,16 @@ impl I256 { } impl I256 { - /// Initializes a new, zeroed I256. - pub fn new() -> Self { - Self { - underlying: ~Self::indent(), - } + /// The size of this type in bits. + pub fn bits() -> u32 { + 128 } - /// The smallest value that can be represented by this integer type. - pub fn min() -> Self { - Self { - underlying: ~U256::min(), - } + /// Helper function to get a positive value from unsigned number + fn from_uint(value: U256) -> Self { + // as the minimal value of I256 is -~I256::indent() (1 << 63) we should add ~I256::indent() (1 << 63) + let underlying: U256 = value + ~Self::indent(); + Self { underlying } } /// The largest value that can be represented by this type, @@ -72,9 +70,11 @@ impl I256 { } } - /// The size of this type in bits. - pub fn bits() -> u32 { - 128 + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~U256::min(), + } } /// Helper function to get a negative value of unsigned number @@ -84,11 +84,11 @@ impl I256 { } } - /// Helper function to get a positive value from unsigned number - fn from_uint(value: U256) -> Self { - // as the minimal value of I256 is -~I256::indent() (1 << 63) we should add ~I256::indent() (1 << 63) - let underlying: U256 = value + ~Self::indent(); - Self { underlying } + /// Initializes a new, zeroed I256. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } } } @@ -100,16 +100,29 @@ impl core::ops::Add for I256 { } } -impl core::ops::Subtract for I256 { - /// Subtract a I256 from a I256. Panics of overflow. - fn subtract(self, other: Self) -> Self { +impl core::ops::Divide for I256 { + /// Divide a I256 by a I256. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if self > other { - // add 1 << 63 to avoid loosing the move - res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); - } else { - // subtract from 1 << 63 as we are getting a negative value - res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); + if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if (self.underlying > ~Self::indent() + || self.underlying == ~Self::indent()) + && divisor.underlying < ~Self::indent() + { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res } @@ -144,29 +157,16 @@ impl core::ops::Multiply for I256 { } } -impl core::ops::Divide for I256 { - /// Divide a I256 by a I256. Panics if divisor is zero. - fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); +impl core::ops::Subtract for I256 { + /// Subtract a I256 from a I256. Panics of overflow. + fn subtract(self, other: Self) -> Self { let mut res = ~Self::new(); - if (self.underlying > ~Self::indent() - || self.underlying == ~Self::indent()) - && divisor.underlying > ~Self::indent() - { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() - && divisor.underlying < ~Self::indent() - { - res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if (self.underlying > ~Self::indent() - || self.underlying == ~Self::indent()) - && divisor.underlying < ~Self::indent() - { - res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() - && divisor.underlying > ~Self::indent() - { - res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + if self > other { + // add 1 << 63 to avoid loosing the move + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); + } else { + // subtract from 1 << 63 as we are getting a negative value + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); } res } diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index 0358b624..7d1842ca 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -46,18 +46,16 @@ impl I32 { } impl I32 { - /// Initializes a new, zeroed I32. - pub fn new() -> Self { - Self { - underlying: ~Self::indent(), - } + /// The size of this type in bits. + pub fn bits() -> u32 { + 32 } - /// The smallest value that can be represented by this integer type. - pub fn min() -> Self { - Self { - underlying: ~u32::min(), - } + /// Helper function to get a positive value from unsigned number + fn from_uint(value: u32) -> Self { + // as the minimal value of I32 is 2147483648 (1 << 31) we should add ~I32::indent() (1 << 31) + let underlying: u32 = value + ~Self::indent(); + Self { underlying } } /// The largest value that can be represented by this type, @@ -67,9 +65,11 @@ impl I32 { } } - /// The size of this type in bits. - pub fn bits() -> u32 { - 32 + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~u32::min(), + } } /// Helper function to get a negative value of unsigned numbers @@ -78,12 +78,12 @@ impl I32 { underlying: ~Self::indent() - value, } } - - /// Helper function to get a positive value from unsigned number - fn from_uint(value: u32) -> Self { - // as the minimal value of I32 is 2147483648 (1 << 31) we should add ~I32::indent() (1 << 31) - let underlying: u32 = value + ~Self::indent(); - Self { underlying } + + /// Initializes a new, zeroed I32. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } } } diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw index 53f12761..7d515d59 100644 --- a/sway_libs/src/signed_integers/i64.sw +++ b/sway_libs/src/signed_integers/i64.sw @@ -46,18 +46,16 @@ impl I64 { } impl I64 { - /// Initializes a new, zeroed I64. - pub fn new() -> Self { - Self { - underlying: ~Self::indent(), - } + /// The size of this type in bits. + pub fn bits() -> u32 { + 64 } - /// The smallest value that can be represented by this integer type. - pub fn min() -> Self { - Self { - underlying: ~u64::min(), - } + /// Helper function to get a positive value from unsigned number + fn from_uint(value: u64) -> Self { + // as the minimal value of I64 is -~I64::indent() (1 << 63) we should add ~I64::indent() (1 << 63) + let underlying: u64 = value + ~Self::indent(); + Self { underlying } } /// The largest value that can be represented by this type, @@ -67,9 +65,11 @@ impl I64 { } } - /// The size of this type in bits. - pub fn bits() -> u32 { - 64 + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~u64::min(), + } } /// Helper function to get a negative value of unsigned number @@ -79,11 +79,11 @@ impl I64 { } } - /// Helper function to get a positive value from unsigned number - fn from_uint(value: u64) -> Self { - // as the minimal value of I64 is -~I64::indent() (1 << 63) we should add ~I64::indent() (1 << 63) - let underlying: u64 = value + ~Self::indent(); - Self { underlying } + /// Initializes a new, zeroed I64. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } } } diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw index 3e911c7e..bc25af97 100644 --- a/sway_libs/src/signed_integers/i8.sw +++ b/sway_libs/src/signed_integers/i8.sw @@ -46,18 +46,15 @@ impl I8 { } impl I8 { - /// Initializes a new, zeroed I8. - pub fn new() -> Self { - Self { - underlying: ~Self::indent(), - } + /// The size of this type in bits. + pub fn bits() -> u32 { + 8 } - /// The smallest value that can be represented by this integer type. - pub fn min() -> Self { - Self { - underlying: ~u8::min(), - } + /// Helper function to get a positive value from unsigned number + fn from_uint(value: u8) -> Self { + let underlying: u8 = value + ~Self::indent(); // as the minimal value of I8 is -~I8::indent() (1 << 7) we should add ~I8::indent() (1 << 7) + Self { underlying } } /// The largest value that can be represented by this type, @@ -67,9 +64,11 @@ impl I8 { } } - /// The size of this type in bits. - pub fn bits() -> u32 { - 8 + /// The smallest value that can be represented by this integer type. + pub fn min() -> Self { + Self { + underlying: ~u8::min(), + } } /// Helper function to get a negative value of unsigned number @@ -79,10 +78,11 @@ impl I8 { } } - /// Helper function to get a positive value from unsigned number - fn from_uint(value: u8) -> Self { - let underlying: u8 = value + ~Self::indent(); // as the minimal value of I8 is -~I8::indent() (1 << 7) we should add ~I8::indent() (1 << 7) - Self { underlying } + /// Initializes a new, zeroed I8. + pub fn new() -> Self { + Self { + underlying: ~Self::indent(), + } } } @@ -93,14 +93,27 @@ impl core::ops::Add for I8 { } } -impl core::ops::Subtract for I8 { - /// Subtract a I8 from a I8. Panics of overflow. - fn subtract(self, other: Self) -> Self { +impl core::ops::Divide for I8 { + /// Divide a I8 by a I8. Panics if divisor is zero. + fn divide(self, divisor: Self) -> Self { + assert(divisor != ~Self::new()); let mut res = ~Self::new(); - if self > other { - res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); // add 1 << 7 to avoid loosing the move - } else { - res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); // subtract from 1 << 7 as we are getting a negative value + if self.underlying >= ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); + } else if self.underlying < ~Self::indent() + && divisor.underlying < ~Self::indent() + { + res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); + } else if self.underlying >= ~Self::indent() + && divisor.underlying < ~Self::indent() + { + res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); + } else if self.underlying < ~Self::indent() + && divisor.underlying > ~Self::indent() + { + res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); } res } @@ -131,27 +144,14 @@ impl core::ops::Multiply for I8 { } } -impl core::ops::Divide for I8 { - /// Divide a I8 by a I8. Panics if divisor is zero. - fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); +impl core::ops::Subtract for I8 { + /// Subtract a I8 from a I8. Panics of overflow. + fn subtract(self, other: Self) -> Self { let mut res = ~Self::new(); - if self.underlying >= ~Self::indent() - && divisor.underlying > ~Self::indent() - { - res = ~Self::from((self.underlying - ~Self::indent()) / (divisor.underlying - ~Self::indent()) + ~Self::indent()); - } else if self.underlying < ~Self::indent() - && divisor.underlying < ~Self::indent() - { - res = ~Self::from((~Self::indent() - self.underlying) / (~Self::indent() - divisor.underlying) + ~Self::indent()); - } else if self.underlying >= ~Self::indent() - && divisor.underlying < ~Self::indent() - { - res = ~Self::from(~Self::indent() - (self.underlying - ~Self::indent()) / (~Self::indent() - divisor.underlying)); - } else if self.underlying < ~Self::indent() - && divisor.underlying > ~Self::indent() - { - res = ~Self::from(~Self::indent() - (~Self::indent() - self.underlying) / (divisor.underlying - ~Self::indent())); + if self > other { + res = ~Self::from(self.underlying - other.underlying + ~Self::indent()); // add 1 << 7 to avoid loosing the move + } else { + res = ~Self::from(~Self::indent() - (other.underlying - self.underlying)); // subtract from 1 << 7 as we are getting a negative value } res } From 9196c79f8e7e138db517e72a563a721a41770aad Mon Sep 17 00:00:00 2001 From: tyshkor Date: Fri, 14 Oct 2022 19:31:53 +0200 Subject: [PATCH 08/12] `forc fmt` --- sway_libs/src/signed_integers/i32.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index 7d1842ca..f66beff1 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -78,7 +78,7 @@ impl I32 { underlying: ~Self::indent() - value, } } - + /// Initializes a new, zeroed I32. pub fn new() -> Self { Self { From ac6893f12c30e238f0df4a7a16da28e17daf40ad Mon Sep 17 00:00:00 2001 From: tyshkor Date: Fri, 14 Oct 2022 20:05:52 +0200 Subject: [PATCH 09/12] error handling --- sway_libs/src/signed_integers/i128.sw | 6 +++++- sway_libs/src/signed_integers/i16.sw | 6 +++++- sway_libs/src/signed_integers/i256.sw | 6 +++++- sway_libs/src/signed_integers/i32.sw | 6 +++++- sway_libs/src/signed_integers/i64.sw | 6 +++++- sway_libs/src/signed_integers/i8.sw | 6 +++++- 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index 45273b2c..88752b24 100644 --- a/sway_libs/src/signed_integers/i128.sw +++ b/sway_libs/src/signed_integers/i128.sw @@ -10,6 +10,10 @@ pub struct I128 { underlying: U128, } +pub enum Error { + ZeroDivisor: (), +} + pub trait From { /// Function for creating I128 from U128 fn from(underlying: U128) -> Self; @@ -101,7 +105,7 @@ impl core::ops::Add for I128 { impl core::ops::Divide for I128 { /// Divide a I128 by a I128. Panics if divisor is zero. fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); + require(divisor != ~Self::new(), Error::ZeroDivisor); let mut res = ~Self::new(); if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw index b07183a1..1c831ec8 100644 --- a/sway_libs/src/signed_integers/i16.sw +++ b/sway_libs/src/signed_integers/i16.sw @@ -10,6 +10,10 @@ pub struct I16 { underlying: u16, } +pub enum Error { + ZeroDivisor: (), +} + pub trait From { /// Function for creating I16 from u16 fn from(underlying: u16) -> Self; @@ -98,7 +102,7 @@ impl core::ops::Add for I16 { impl core::ops::Divide for I16 { /// Divide a I16 by a I16. Panics if divisor is zero. fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); + require(divisor != ~Self::new(), Error::ZeroDivisor); let mut res = ~Self::new(); if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() diff --git a/sway_libs/src/signed_integers/i256.sw b/sway_libs/src/signed_integers/i256.sw index 2445170b..389d6da0 100644 --- a/sway_libs/src/signed_integers/i256.sw +++ b/sway_libs/src/signed_integers/i256.sw @@ -10,6 +10,10 @@ pub struct I256 { underlying: U256, } +pub enum Error { + ZeroDivisor: (), +} + pub trait From { /// Function for creating I256 from U256 fn from(underlying: U256) -> Self; @@ -103,7 +107,7 @@ impl core::ops::Add for I256 { impl core::ops::Divide for I256 { /// Divide a I256 by a I256. Panics if divisor is zero. fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); + require(divisor != ~Self::new(), Error::ZeroDivisor); let mut res = ~Self::new(); if (self.underlying > ~Self::indent() || self.underlying == ~Self::indent()) diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index f66beff1..308ce2f6 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -10,6 +10,10 @@ pub struct I32 { underlying: u32, } +pub enum Error { + ZeroDivisor: (), +} + pub trait From { /// Function for creating I32 from u32 fn from(underlying: u32) -> Self; @@ -138,7 +142,7 @@ impl core::ops::Multiply for I32 { impl core::ops::Divide for I32 { /// Divide a I32 by a I32. Panics if divisor is zero. fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); + require(divisor != ~Self::new(), Error::ZeroDivisor); let mut res = ~Self::new(); if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw index 7d515d59..3c2e3b4d 100644 --- a/sway_libs/src/signed_integers/i64.sw +++ b/sway_libs/src/signed_integers/i64.sw @@ -10,6 +10,10 @@ pub struct I64 { underlying: u64, } +pub enum Error { + ZeroDivisor: (), +} + pub trait From { /// Function for creating I64 from u64 fn from(underlying: u64) -> Self; @@ -138,7 +142,7 @@ impl core::ops::Multiply for I64 { impl core::ops::Divide for I64 { /// Divide a I64 by a I64. Panics if divisor is zero. fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); + require(divisor != ~Self::new(), Error::ZeroDivisor); let mut res = ~Self::new(); if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw index bc25af97..20d46166 100644 --- a/sway_libs/src/signed_integers/i8.sw +++ b/sway_libs/src/signed_integers/i8.sw @@ -10,6 +10,10 @@ pub struct I8 { underlying: u8, } +pub enum Error { + ZeroDivisor: (), +} + pub trait From { /// Function for creating I8 from u8 fn from(underlying: u8) -> Self; @@ -96,7 +100,7 @@ impl core::ops::Add for I8 { impl core::ops::Divide for I8 { /// Divide a I8 by a I8. Panics if divisor is zero. fn divide(self, divisor: Self) -> Self { - assert(divisor != ~Self::new()); + require(divisor != ~Self::new(), Error::ZeroDivisor); let mut res = ~Self::new(); if self.underlying >= ~Self::indent() && divisor.underlying > ~Self::indent() From a07188a6f65bafd1e03c9e8374316ba3676dfe72 Mon Sep 17 00:00:00 2001 From: tyshkor Date: Mon, 17 Oct 2022 09:38:02 +0200 Subject: [PATCH 10/12] documentaion --- sway_libs/src/signed_integers/README.md | 47 +++++++++++++++++++ .../src/signed_integers/SPECIFICATION.md | 35 ++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 sway_libs/src/signed_integers/README.md create mode 100644 sway_libs/src/signed_integers/SPECIFICATION.md diff --git a/sway_libs/src/signed_integers/README.md b/sway_libs/src/signed_integers/README.md new file mode 100644 index 00000000..39c0697f --- /dev/null +++ b/sway_libs/src/signed_integers/README.md @@ -0,0 +1,47 @@ +# Overview + +The Signed Integers library provides an interface to use signed numbers in Sway. It has 6 distinct types: `I8`, `I16`, `I32`, `I64`, `I128`, `I256`. These types are stack allocated. + +These types are stored as unsigned integers, therefore either `u64` or a number of them. Therefore the size can be known at compile time and the length is static. + +For more information please see the [specification](./SPECIFICATION.md). + +# Using the Library + +## Getting Started + +In order to use the `Signed Integers` type it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [README.md](../../../README.md). + +```rust +use sway_libs::i8::I8; +``` + +Once imported, a `Signed Integer` type can be instantiated defining a new variable and calling the `new` function. + +```rust +let mut i8_value = ~I8::new(); +``` + +## Basic Functionality + +Basic arithmetic operations are working as usual + +```rust +// Add 2 signed values +let i8_value_3 = i8_value_1 + i8_value_2; + +// Subtract one signed value from another +let i8_value_3 = i8_value_1 - i8_value_2; +``` + +Helper functions + +```rust +// To get a negative value from an unsigned value +let neg_value = ~I8::neg_from(); + +// Maximum value +let max_i8_value = ~I8::max(); +``` + +For more information please see the [specification](./SPECIFICATION.md). diff --git a/sway_libs/src/signed_integers/SPECIFICATION.md b/sway_libs/src/signed_integers/SPECIFICATION.md new file mode 100644 index 00000000..c933fa94 --- /dev/null +++ b/sway_libs/src/signed_integers/SPECIFICATION.md @@ -0,0 +1,35 @@ +# Overview + +This document provides an overview of the Signed Integers library. + +It outlines the use cases, i.e. specification, and describes how to implement the library. + +## Use Cases + +The Signed Integers library can be used anytime a one needs negative numbers. + +## Public Functions + +### `bits()` + +The size of this type in bits. + +### `from_uint()` + +Helper function to get a positive value from unsigned number + +### `max()` + +The largest value that can be represented by this type. + +### `min()` + +The smallest value that can be represented by this integer type. + +### `neg_from` + +Helper function to get a negative value of unsigned number + +### Basic arithmetic operations + +`+`, `-`, `*`, `/` From 79708b5a0af78831dd946545007605c62e841911 Mon Sep 17 00:00:00 2001 From: tyshkor Date: Tue, 18 Oct 2022 22:13:23 +0200 Subject: [PATCH 11/12] `common` + test --- sway_libs/src/lib.sw | 1 + sway_libs/src/signed_integers/common.sw | 10 ++++ sway_libs/src/signed_integers/i128.sw | 18 ++++-- sway_libs/src/signed_integers/i16.sw | 14 +++-- sway_libs/src/signed_integers/i256.sw | 20 +++++-- sway_libs/src/signed_integers/i32.sw | 14 +++-- sway_libs/src/signed_integers/i64.sw | 14 +++-- sway_libs/src/signed_integers/i8.sw | 14 +++-- .../src/signed_integers/signed_integers.sw | 10 ++++ .../twos_complement_test/.gitignore | 2 + .../twos_complement_test/Forc.toml | 8 +++ .../twos_complement_test/src/main.sw | 60 +++++++++++++++++++ 12 files changed, 161 insertions(+), 24 deletions(-) create mode 100644 sway_libs/src/signed_integers/common.sw create mode 100644 sway_libs/src/signed_integers/signed_integers.sw create mode 100644 tests/src/test_projects/signed_integers/twos_complement_test/.gitignore create mode 100644 tests/src/test_projects/signed_integers/twos_complement_test/Forc.toml create mode 100644 tests/src/test_projects/signed_integers/twos_complement_test/src/main.sw diff --git a/sway_libs/src/lib.sw b/sway_libs/src/lib.sw index 6844c66c..9b8f6943 100644 --- a/sway_libs/src/lib.sw +++ b/sway_libs/src/lib.sw @@ -1,6 +1,7 @@ library sway_libs; dep merkle_proof/binary_merkle_proof; +dep signed_integers/signed_integers; dep signed_integers/i8; dep signed_integers/i16; dep signed_integers/i32; diff --git a/sway_libs/src/signed_integers/common.sw b/sway_libs/src/signed_integers/common.sw new file mode 100644 index 00000000..4676812e --- /dev/null +++ b/sway_libs/src/signed_integers/common.sw @@ -0,0 +1,10 @@ +library common; + +pub trait TwosComplement { + fn twos_complement(self) -> Self; +} + + +pub enum Error { + ZeroDivisor: (), +} diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index 88752b24..5951b2b3 100644 --- a/sway_libs/src/signed_integers/i128.sw +++ b/sway_libs/src/signed_integers/i128.sw @@ -1,6 +1,8 @@ library i128; use std::u128::U128; +use ::signed_integers::common::Error; +use ::signed_integers::common::TwosComplement; /// The 128-bit signed integer type. /// Represented as an underlying U128 value. @@ -10,10 +12,6 @@ pub struct I128 { underlying: U128, } -pub enum Error { - ZeroDivisor: (), -} - pub trait From { /// Function for creating I128 from U128 fn from(underlying: U128) -> Self; @@ -173,3 +171,15 @@ impl core::ops::Subtract for I128 { res } } + +impl TwosComplement for I128 { + fn twos_complement(self) -> Self { + let u128_one = U128 { + upper: 0, + lower: 1, + }; + let one = ~I128::from_uint(u128_one); + let res = self.not() - one; + res + } +} diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw index 1c831ec8..2fa506d2 100644 --- a/sway_libs/src/signed_integers/i16.sw +++ b/sway_libs/src/signed_integers/i16.sw @@ -1,6 +1,8 @@ library i16; use core::num::*; +use ::signed_integers::common::Error; +use ::signed_integers::common::TwosComplement; /// The 16-bit signed integer type. /// Represented as an underlying u16 value. @@ -10,10 +12,6 @@ pub struct I16 { underlying: u16, } -pub enum Error { - ZeroDivisor: (), -} - pub trait From { /// Function for creating I16 from u16 fn from(underlying: u16) -> Self; @@ -164,3 +162,11 @@ impl core::ops::Subtract for I16 { res } } + +impl TwosComplement for I16 { + fn twos_complement(self) -> Self { + let one = ~I16::from_uint(1u16); + let res = self.not() - one; + res + } +} diff --git a/sway_libs/src/signed_integers/i256.sw b/sway_libs/src/signed_integers/i256.sw index 389d6da0..371e8d34 100644 --- a/sway_libs/src/signed_integers/i256.sw +++ b/sway_libs/src/signed_integers/i256.sw @@ -1,6 +1,8 @@ library i256; use std::u256::U256; +use ::signed_integers::common::Error; +use ::signed_integers::common::TwosComplement; /// The 128-bit signed integer type. /// Represented as an underlying U256 value. @@ -10,10 +12,6 @@ pub struct I256 { underlying: U256, } -pub enum Error { - ZeroDivisor: (), -} - pub trait From { /// Function for creating I256 from U256 fn from(underlying: U256) -> Self; @@ -175,3 +173,17 @@ impl core::ops::Subtract for I256 { res } } + +impl TwosComplement for I256 { + fn twos_complement(self) -> Self { + let u128_one = U256 { + a: 0, + b: 0, + c: 0, + d: 1, + }; + let one = ~I256::from_uint(u128_one); + let res = self.not() - one; + res + } +} diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index 308ce2f6..60ea8140 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -1,6 +1,8 @@ library i32; use core::num::*; +use ::signed_integers::common::Error; +use ::signed_integers::common::TwosComplement; /// The 32-bit signed integer type. /// Represented as an underlying u32 value. @@ -10,10 +12,6 @@ pub struct I32 { underlying: u32, } -pub enum Error { - ZeroDivisor: (), -} - pub trait From { /// Function for creating I32 from u32 fn from(underlying: u32) -> Self; @@ -164,3 +162,11 @@ impl core::ops::Divide for I32 { res } } + +impl TwosComplement for I32 { + fn twos_complement(self) -> Self { + let one = ~I32::from_uint(1u32); + let res = self.not() - one; + res + } +} diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw index 3c2e3b4d..d40cda51 100644 --- a/sway_libs/src/signed_integers/i64.sw +++ b/sway_libs/src/signed_integers/i64.sw @@ -1,6 +1,8 @@ library i64; use core::num::*; +use ::signed_integers::common::Error; +use ::signed_integers::common::TwosComplement; /// The 64-bit signed integer type. /// Represented as an underlying u64 value. @@ -10,10 +12,6 @@ pub struct I64 { underlying: u64, } -pub enum Error { - ZeroDivisor: (), -} - pub trait From { /// Function for creating I64 from u64 fn from(underlying: u64) -> Self; @@ -164,3 +162,11 @@ impl core::ops::Divide for I64 { res } } + +impl TwosComplement for I64 { + fn twos_complement(self) -> Self { + let one = ~I64::from_uint(1u64); + let res = self.not() - one; + res + } +} diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw index 20d46166..186b0458 100644 --- a/sway_libs/src/signed_integers/i8.sw +++ b/sway_libs/src/signed_integers/i8.sw @@ -1,6 +1,8 @@ library i8; use core::num::*; +use ::signed_integers::common::Error; +use ::signed_integers::common::TwosComplement; /// The 8-bit signed integer type. /// Represented as an underlying u8 value. @@ -10,10 +12,6 @@ pub struct I8 { underlying: u8, } -pub enum Error { - ZeroDivisor: (), -} - pub trait From { /// Function for creating I8 from u8 fn from(underlying: u8) -> Self; @@ -160,3 +158,11 @@ impl core::ops::Subtract for I8 { res } } + +impl TwosComplement for I8 { + fn twos_complement(self) -> Self { + let one = ~I8::from_uint(1u8); + let res = self.not() - one; + res + } +} diff --git a/sway_libs/src/signed_integers/signed_integers.sw b/sway_libs/src/signed_integers/signed_integers.sw new file mode 100644 index 00000000..df751464 --- /dev/null +++ b/sway_libs/src/signed_integers/signed_integers.sw @@ -0,0 +1,10 @@ +library signed_integers; + +dep common; + +dep i8; +dep i16; +dep i32; +dep i64; +dep i128; +dep i256; diff --git a/tests/src/test_projects/signed_integers/twos_complement_test/.gitignore b/tests/src/test_projects/signed_integers/twos_complement_test/.gitignore new file mode 100644 index 00000000..77d3844f --- /dev/null +++ b/tests/src/test_projects/signed_integers/twos_complement_test/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/test_projects/signed_integers/twos_complement_test/Forc.toml b/tests/src/test_projects/signed_integers/twos_complement_test/Forc.toml new file mode 100644 index 00000000..8dc1ea82 --- /dev/null +++ b/tests/src/test_projects/signed_integers/twos_complement_test/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "twos_complement_test" + +[dependencies] +sway_libs = { path = "../../../../../sway_libs" } diff --git a/tests/src/test_projects/signed_integers/twos_complement_test/src/main.sw b/tests/src/test_projects/signed_integers/twos_complement_test/src/main.sw new file mode 100644 index 00000000..d0133b20 --- /dev/null +++ b/tests/src/test_projects/signed_integers/twos_complement_test/src/main.sw @@ -0,0 +1,60 @@ +script; + +use std::assert::assert; +use sway_libs::i8::I8; +use std::i16::I16; +use sway_libs::i32::I32; +use sway_libs::i64::I64; +use sway_libs::i128::I128; +use std::u128::U128; +use sway_libs::i256::I256; +use std::u256::U256; +use core::num::*; + +fn main() -> bool { + let one_i8 = ~I8::from_uint(1u8); + let mut res_i8 = one.twos_complement(); + assert(res_i8 == ~I8::from_uint(2u8)); + + let one = ~I16::from_uint(1u16); + let mut res_i16 = one.twos_complement(); + assert(res_i16 == ~I16::from_uint(2u16)); + + let one = ~I32::from_uint(1u32); + let mut res_i32 = one.twos_complement(); + assert(res_i32 == ~I32::from_uint(2u32)); + + let one = ~I64::from_uint(1u64); + let mut res_i64 = one.twos_complement(); + assert(res_i64 == ~I64::from_uint(2u64)); + + let u128_one = U128 { + upper: 0, + lower: 1, + }; + let u128_two = U128 { + upper: 0, + lower: 2, + }; + let one = ~I128::from_uint(u128_one); + let mut res_i128 = one.twos_complement(); + assert(res_i128 == ~I128::from_uint(u128_two)); + + let u256_one = U256 { + a: 0, + b: 0, + c: 0, + d: 1, + }; + let u256_two = U256 { + a: 0, + b: 0, + c: 0, + d: 2, + }; + let one = ~I256::from_uint(u128_one); + let mut res_i256 = one.twos_complement(); + assert(res_i256 == ~I256::from_uint(u128_two)); + + true +} From 472122afd30d77d49d95e54be10373f0da3e6b79 Mon Sep 17 00:00:00 2001 From: tyshkor Date: Fri, 21 Oct 2022 19:27:09 +0200 Subject: [PATCH 12/12] update since `!` operation became avaliable --- sway_libs/src/signed_integers/i128.sw | 2 +- sway_libs/src/signed_integers/i16.sw | 2 +- sway_libs/src/signed_integers/i256.sw | 2 +- sway_libs/src/signed_integers/i32.sw | 2 +- sway_libs/src/signed_integers/i64.sw | 2 +- sway_libs/src/signed_integers/i8.sw | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index 5951b2b3..c5358802 100644 --- a/sway_libs/src/signed_integers/i128.sw +++ b/sway_libs/src/signed_integers/i128.sw @@ -179,7 +179,7 @@ impl TwosComplement for I128 { lower: 1, }; let one = ~I128::from_uint(u128_one); - let res = self.not() - one; + let res = !self - one; res } } diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw index 2fa506d2..e4c43a59 100644 --- a/sway_libs/src/signed_integers/i16.sw +++ b/sway_libs/src/signed_integers/i16.sw @@ -166,7 +166,7 @@ impl core::ops::Subtract for I16 { impl TwosComplement for I16 { fn twos_complement(self) -> Self { let one = ~I16::from_uint(1u16); - let res = self.not() - one; + let res = !self - one; res } } diff --git a/sway_libs/src/signed_integers/i256.sw b/sway_libs/src/signed_integers/i256.sw index 371e8d34..abff77e2 100644 --- a/sway_libs/src/signed_integers/i256.sw +++ b/sway_libs/src/signed_integers/i256.sw @@ -183,7 +183,7 @@ impl TwosComplement for I256 { d: 1, }; let one = ~I256::from_uint(u128_one); - let res = self.not() - one; + let res = !self - one; res } } diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index 60ea8140..76de98d4 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -166,7 +166,7 @@ impl core::ops::Divide for I32 { impl TwosComplement for I32 { fn twos_complement(self) -> Self { let one = ~I32::from_uint(1u32); - let res = self.not() - one; + let res = !self - one; res } } diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw index d40cda51..355a736c 100644 --- a/sway_libs/src/signed_integers/i64.sw +++ b/sway_libs/src/signed_integers/i64.sw @@ -166,7 +166,7 @@ impl core::ops::Divide for I64 { impl TwosComplement for I64 { fn twos_complement(self) -> Self { let one = ~I64::from_uint(1u64); - let res = self.not() - one; + let res = !self - one; res } } diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw index 186b0458..22addf84 100644 --- a/sway_libs/src/signed_integers/i8.sw +++ b/sway_libs/src/signed_integers/i8.sw @@ -162,7 +162,7 @@ impl core::ops::Subtract for I8 { impl TwosComplement for I8 { fn twos_complement(self) -> Self { let one = ~I8::from_uint(1u8); - let res = self.not() - one; + let res = !self - one; res } }