From 58b455cd1ace597c89dccc36bf5a844bc4864e27 Mon Sep 17 00:00:00 2001 From: mootz12 Date: Wed, 17 Apr 2024 13:13:59 -0400 Subject: [PATCH] feat: update SorobanFixedPoint interface to prevent excess clone calls --- Cargo.toml | 4 +-- README.md | 2 +- src/i128.rs | 68 +++++++++++++++++++------------------- src/i256.rs | 56 +++++++++++++++---------------- src/soroban_fixed_point.rs | 8 ++--- src/u128.rs | 54 +++++++++++++++--------------- src/u256.rs | 36 ++++++++++---------- 7 files changed, 114 insertions(+), 114 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c1d74a5..a7f2931 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "soroban-fixed-point-math" -version = "1.0.0" +version = "1.1.0" description = "Fixed point math support for Soroban smart contracts" homepage = "https://github.com/script3/soroban-fixed-point-math" repository = "https://github.com/script3/soroban-fixed-point-math" @@ -12,4 +12,4 @@ keywords = ["no_std", "wasm"] rust-version = "1.74" [dependencies] -soroban-sdk = { version = "20.0.0" } \ No newline at end of file +soroban-sdk = { version = "20.5.0" } \ No newline at end of file diff --git a/README.md b/README.md index 7df3fcc..f196cd7 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ use soroban_sdk::{Env, I256}; let env: Env //... from your contract, or Env::default() in a test let x: I256 = I256::from_i32(&env, 1_5000000); let y: I256 = I256::from_i32(&env, 2_0000000); -assert_eq!(x.fixed_mul_ceil(x, y, I256::from_i32(&env, 1_0000000)), 3_0000000); +assert_eq!(x.fixed_mul_ceil(&x, &y, I256::from_i32(&env, 1_0000000)), 3_0000000); ``` ## Acknowledgements diff --git a/src/i128.rs b/src/i128.rs index da0a310..35c1d05 100644 --- a/src/i128.rs +++ b/src/i128.rs @@ -57,34 +57,34 @@ fn div_ceil(r: i128, z: i128) -> Option { } impl SorobanFixedPoint for i128 { - fn fixed_mul_floor(self, env: &Env, y: i128, denominator: i128) -> i128 { - scaled_mul_div_floor(self, env, y, denominator) + fn fixed_mul_floor(&self, env: &Env, y: &i128, denominator: &i128) -> i128 { + scaled_mul_div_floor(&self, env, y, denominator) } - fn fixed_mul_ceil(self, env: &Env, y: i128, denominator: i128) -> i128 { - scaled_mul_div_ceil(self, env, y, denominator) + fn fixed_mul_ceil(&self, env: &Env, y: &i128, denominator: &i128) -> i128 { + scaled_mul_div_ceil(&self, env, y, denominator) } - fn fixed_div_floor(self, env: &Env, y: i128, denominator: i128) -> i128 { - scaled_mul_div_floor(self, env, denominator, y) + fn fixed_div_floor(&self, env: &Env, y: &i128, denominator: &i128) -> i128 { + scaled_mul_div_floor(&self, env, denominator, y) } - fn fixed_div_ceil(self, env: &Env, y: i128, denominator: i128) -> i128 { - scaled_mul_div_ceil(self, env, denominator, y) + fn fixed_div_ceil(&self, env: &Env, y: &i128, denominator: &i128) -> i128 { + scaled_mul_div_ceil(&self, env, denominator, y) } } /// Performs floor(x * y / z) -fn scaled_mul_div_floor(x: i128, env: &Env, y: i128, z: i128) -> i128 { - return match x.checked_mul(y) { - Some(r) => div_floor(r, z).unwrap_optimized(), +fn scaled_mul_div_floor(x: &i128, env: &Env, y: &i128, z: &i128) -> i128 { + return match x.checked_mul(*y) { + Some(r) => div_floor(r, *z).unwrap_optimized(), None => { // scale to i256 and retry let res = crate::i256::mul_div_floor( &env, - I256::from_i128(&env, x), - I256::from_i128(&env, y), - I256::from_i128(&env, z), + &I256::from_i128(&env, *x), + &I256::from_i128(&env, *y), + &I256::from_i128(&env, *z), ); // will panic if result is not representable in i128 res.to_i128().unwrap_optimized() @@ -93,16 +93,16 @@ fn scaled_mul_div_floor(x: i128, env: &Env, y: i128, z: i128) -> i128 { } /// Performs floor(x * y / z) -fn scaled_mul_div_ceil(x: i128, env: &Env, y: i128, z: i128) -> i128 { - return match x.checked_mul(y) { - Some(r) => div_ceil(r, z).unwrap_optimized(), +fn scaled_mul_div_ceil(x: &i128, env: &Env, y: &i128, z: &i128) -> i128 { + return match x.checked_mul(*y) { + Some(r) => div_ceil(r, *z).unwrap_optimized(), None => { // scale to i256 and retry let res = crate::i256::mul_div_ceil( &env, - I256::from_i128(&env, x), - I256::from_i128(&env, y), - I256::from_i128(&env, z), + &I256::from_i128(&env, *x), + &I256::from_i128(&env, *y), + &I256::from_i128(&env, *z), ); // will panic if result is not representable in i128 res.to_i128().unwrap_optimized() @@ -314,7 +314,7 @@ mod test_soroban_fixed_point { let y: i128 = 314_1592653; let denominator: i128 = 1_0000001; - let result = x.fixed_mul_floor(&env, y, denominator); + let result = x.fixed_mul_floor(&env, &y, &denominator); assert_eq!(result, 483_5313675) } @@ -326,7 +326,7 @@ mod test_soroban_fixed_point { let y: i128 = 314_1592653; let denominator: i128 = 1_0000001; - let result = x.fixed_mul_floor(&env, y, denominator); + let result = x.fixed_mul_floor(&env, &y, &denominator); assert_eq!(result, -483_5313676) } @@ -338,7 +338,7 @@ mod test_soroban_fixed_point { let y: i128 = 10i128.pow(27); let denominator: i128 = 10i128.pow(18); - let result = x.fixed_mul_floor(&env, y, denominator); + let result = x.fixed_mul_floor(&env, &y, &denominator); assert_eq!(result, 170_141_183_460_469_231_731 * 10i128.pow(9)); } @@ -352,7 +352,7 @@ mod test_soroban_fixed_point { let y: i128 = 314_1592653; let denominator: i128 = 1_0000001; - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, 483_5313676) } @@ -364,7 +364,7 @@ mod test_soroban_fixed_point { let y: i128 = 314_1592653; let denominator: i128 = 1_0000001; - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, -483_5313675) } @@ -376,7 +376,7 @@ mod test_soroban_fixed_point { let y: i128 = 1_000_000_000_000_000_000; let denominator: i128 = 1_000_000_000_000_000_000; - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, 170_141_183_460_469_231_731) } @@ -388,7 +388,7 @@ mod test_soroban_fixed_point { let y: i128 = 10i128.pow(27); let denominator: i128 = 10i128.pow(18); - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, 170_141_183_460_469_231_731 * 10i128.pow(9)); } @@ -402,7 +402,7 @@ mod test_soroban_fixed_point { let y: i128 = 1_5391280; let denominator: i128 = 1_0000000; - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, 204_1150997) } @@ -414,7 +414,7 @@ mod test_soroban_fixed_point { let y: i128 = -1_5391280; let denominator: i128 = 1_0000000; - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, -204_1150998) } @@ -426,7 +426,7 @@ mod test_soroban_fixed_point { let y: i128 = 10i128.pow(18); let denominator: i128 = 10i128.pow(27); - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, 170_141_183_460_469_231_731 * 10i128.pow(9)); } @@ -440,7 +440,7 @@ mod test_soroban_fixed_point { let y: i128 = 1_5391280; let denominator: i128 = 1_0000000; - let result = x.fixed_div_ceil(&env, y, denominator); + let result = x.fixed_div_ceil(&env, &y, &denominator); assert_eq!(result, 204_1150998) } @@ -452,7 +452,7 @@ mod test_soroban_fixed_point { let y: i128 = -1_5391280; let denominator: i128 = 1_0000000; - let result = x.fixed_div_ceil(&env, y, denominator); + let result = x.fixed_div_ceil(&env, &y, &denominator); assert_eq!(result, -204_1150997) } @@ -464,7 +464,7 @@ mod test_soroban_fixed_point { let y: i128 = 1_000_000_000_000_000_000; let denominator: i128 = 1_000_000_000_000_000_000; - let result = x.fixed_div_ceil(&env, y, denominator); + let result = x.fixed_div_ceil(&env, &y, &denominator); assert_eq!(result, 170_141_183_460_469_231_731) } @@ -476,7 +476,7 @@ mod test_soroban_fixed_point { let y: i128 = 10i128.pow(18); let denominator: i128 = 10i128.pow(27); - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, 170_141_183_460_469_231_731 * 10i128.pow(9)); } diff --git a/src/i256.rs b/src/i256.rs index 6988bf2..50675ea 100644 --- a/src/i256.rs +++ b/src/i256.rs @@ -3,28 +3,28 @@ use soroban_sdk::{Env, I256}; use crate::soroban_fixed_point::SorobanFixedPoint; impl SorobanFixedPoint for I256 { - fn fixed_mul_floor(self, env: &Env, y: I256, denominator: I256) -> I256 { - mul_div_floor(env, self, y, denominator) + fn fixed_mul_floor(&self, env: &Env, y: &I256, denominator: &I256) -> I256 { + mul_div_floor(env, &self, y, denominator) } - fn fixed_mul_ceil(self, env: &Env, y: I256, denominator: I256) -> I256 { - mul_div_ceil(env, self, y, denominator) + fn fixed_mul_ceil(&self, env: &Env, y: &I256, denominator: &I256) -> I256 { + mul_div_ceil(env, &self, y, denominator) } - fn fixed_div_floor(self, env: &Env, y: I256, denominator: I256) -> I256 { - mul_div_floor(env, self, denominator, y) + fn fixed_div_floor(&self, env: &Env, y: &I256, denominator: &I256) -> I256 { + mul_div_floor(env, &self, denominator, y) } - fn fixed_div_ceil(self, env: &Env, y: I256, denominator: I256) -> I256 { - mul_div_ceil(env, self, denominator, y) + fn fixed_div_ceil(&self, env: &Env, y: &I256, denominator: &I256) -> I256 { + mul_div_ceil(env, &self, denominator, y) } } /// Performs floor(x * y / z) -pub(crate) fn mul_div_floor(env: &Env, x: I256, y: I256, z: I256) -> I256 { +pub(crate) fn mul_div_floor(env: &Env, x: &I256, y: &I256, z: &I256) -> I256 { let zero = I256::from_i32(env, 0); let r = x.mul(&y); - if r < zero || (r > zero && z < zero) { + if r < zero || (r > zero && z.clone() < zero) { // ceiling is taken by default for a negative result let remainder = r.rem_euclid(&z); let one = I256::from_i32(env, 1); @@ -36,10 +36,10 @@ pub(crate) fn mul_div_floor(env: &Env, x: I256, y: I256, z: I256) -> I256 { } /// Performs ceil(x * y / z) -pub(crate) fn mul_div_ceil(env: &Env, x: I256, y: I256, z: I256) -> I256 { +pub(crate) fn mul_div_ceil(env: &Env, x: &I256, y: &I256, z: &I256) -> I256 { let zero = I256::from_i32(env, 0); let r = x.mul(&y); - if r <= zero || (r > zero && z < zero) { + if r <= zero || (r > zero && z.clone() < zero) { // ceiling is taken by default for a negative or zero result r.div(&z) } else { @@ -63,7 +63,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 314_1592653); let denominator: I256 = I256::from_i128(&env, 1_0000001); - let result = x.fixed_mul_floor(&env, y, denominator); + let result = x.fixed_mul_floor(&env, &y, &denominator); assert_eq!(result, I256::from_i128(&env, 483_5313675)); } @@ -75,7 +75,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 314_1592653); let denominator: I256 = I256::from_i128(&env, 1_0000001); - let result = x.fixed_mul_floor(&env, y, denominator); + let result = x.fixed_mul_floor(&env, &y, &denominator); assert_eq!(result, I256::from_i128(&env, -483_5313676)); } @@ -87,7 +87,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 10i128.pow(38)); let denominator: I256 = I256::from_i128(&env, 10i128.pow(18)); - let result = x.clone().fixed_mul_floor(&env, y, denominator); + let result = x.clone().fixed_mul_floor(&env, &y, &denominator); let expected_result = x.mul(&I256::from_i128(&env, 10i128.pow(20))); assert_eq!(result, expected_result); @@ -102,7 +102,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 10i128.pow(39)); let denominator: I256 = I256::from_i128(&env, 10i128.pow(18)); - x.fixed_mul_floor(&env, y, denominator); + x.fixed_mul_floor(&env, &y, &denominator); } /********** fixed_mul_ceil **********/ @@ -114,7 +114,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 314_1592653); let denominator: I256 = I256::from_i128(&env, 1_0000001); - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, I256::from_i128(&env, 483_5313676)); } @@ -126,7 +126,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 314_1592653); let denominator: I256 = I256::from_i128(&env, 1_0000001); - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, I256::from_i128(&env, -483_5313675)); } @@ -138,7 +138,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 10i128.pow(38)); let denominator: I256 = I256::from_i128(&env, 10i128.pow(18)); - let result = x.clone().fixed_mul_ceil(&env, y, denominator); + let result = x.clone().fixed_mul_ceil(&env, &y, &denominator); let expected_result = x.mul(&I256::from_i128(&env, 10i128.pow(20))); assert_eq!(result, expected_result); @@ -153,7 +153,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 10i128.pow(39)); let denominator: I256 = I256::from_i128(&env, 10i128.pow(18)); - x.fixed_mul_ceil(&env, y, denominator); + x.fixed_mul_ceil(&env, &y, &denominator); } /********** fixed_div_floor **********/ @@ -165,7 +165,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 1_5391280); let denominator: I256 = I256::from_i128(&env, 1_0000000); - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, I256::from_i128(&env, 204_1150997)); } @@ -177,7 +177,7 @@ mod tests { let y: I256 = I256::from_i128(&env, -1_5391280); let denominator: I256 = I256::from_i128(&env, 1_0000000); - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, I256::from_i128(&env, -204_1150998)); } @@ -189,7 +189,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 10i128.pow(27)); let denominator: I256 = I256::from_i128(&env, 10i128.pow(38)); - let result = x.clone().fixed_div_floor(&env, y, denominator); + let result = x.clone().fixed_div_floor(&env, &y, &denominator); let expected_result = x.mul(&I256::from_i128(&env, 10i128.pow(11))); assert_eq!(result, expected_result); @@ -204,7 +204,7 @@ mod tests { // 256 bit max ~= 5.8e76, 128 bit max ~= 1.7e38, need to multiply by at least 10^39 let denominator: I256 = I256::from_i128(&env, 10i128.pow(39)); - x.fixed_div_floor(&env, y, denominator); + x.fixed_div_floor(&env, &y, &denominator); } /********** fixed_div_ceil **********/ @@ -216,7 +216,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 1_5391280); let denominator: I256 = I256::from_i128(&env, 1_0000000); - let result = x.fixed_div_ceil(&env, y, denominator); + let result = x.fixed_div_ceil(&env, &y, &denominator); assert_eq!(result, I256::from_i128(&env, 204_1150998)); } @@ -228,7 +228,7 @@ mod tests { let y: I256 = I256::from_i128(&env, -1_5391280); let denominator: I256 = I256::from_i128(&env, 1_0000000); - let result = x.fixed_div_ceil(&env, y, denominator); + let result = x.fixed_div_ceil(&env, &y, &denominator); assert_eq!(result, I256::from_i128(&env, -204_1150997)); } @@ -240,7 +240,7 @@ mod tests { let y: I256 = I256::from_i128(&env, 10i128.pow(27)); let denominator: I256 = I256::from_i128(&env, 10i128.pow(38)); - let result = x.clone().fixed_div_ceil(&env, y, denominator); + let result = x.clone().fixed_div_ceil(&env, &y, &denominator); let expected_result = x.mul(&I256::from_i128(&env, 10i128.pow(11))); assert_eq!(result, expected_result); @@ -255,6 +255,6 @@ mod tests { // 256 bit max ~= 5.8e76, 128 bit max ~= 1.7e38, need to multiply by at least 10^39 let denominator: I256 = I256::from_i128(&env, 10i128.pow(39)); - x.fixed_div_ceil(&env, y, denominator); + x.fixed_div_ceil(&env, &y, &denominator); } } diff --git a/src/soroban_fixed_point.rs b/src/soroban_fixed_point.rs index e5368a9..6d283ed 100644 --- a/src/soroban_fixed_point.rs +++ b/src/soroban_fixed_point.rs @@ -12,26 +12,26 @@ pub trait SorobanFixedPoint: Sized { /// ### Panics /// This method will panic if the denominator is 0, a phantom overflow occurs, or /// the result does not fit in Self. - fn fixed_mul_floor(self, env: &Env, y: Self, denominator: Self) -> Self; + fn fixed_mul_floor(&self, env: &Env, y: &Self, denominator: &Self) -> Self; /// Safely calculates ceil(x * y / denominator). /// /// ### Panics /// This method will panic if the denominator is 0, a phantom overflow occurs, or /// the result does not fit in Self. - fn fixed_mul_ceil(self, env: &Env, y: Self, denominator: Self) -> Self; + fn fixed_mul_ceil(&self, env: &Env, y: &Self, denominator: &Self) -> Self; /// Safely calculates floor(x * denominator / y). /// /// ### Panics /// This method will panic if the denominator is 0, a phantom overflow occurs, or /// the result does not fit in Self. - fn fixed_div_floor(self, env: &Env, y: Self, denominator: Self) -> Self; + fn fixed_div_floor(&self, env: &Env, y: &Self, denominator: &Self) -> Self; /// Safely calculates ceil(x * denominator / y). /// /// ### Panics /// This method will panic if the denominator is 0, a phantom overflow occurs, or /// the result does not fit in Self. - fn fixed_div_ceil(self, env: &Env, y: Self, denominator: Self) -> Self; + fn fixed_div_ceil(&self, env: &Env, y: &Self, denominator: &Self) -> Self; } diff --git a/src/u128.rs b/src/u128.rs index db9296f..7afc716 100644 --- a/src/u128.rs +++ b/src/u128.rs @@ -40,33 +40,33 @@ fn div_ceil(r: u128, z: u128) -> Option { } impl SorobanFixedPoint for u128 { - fn fixed_mul_floor(self, env: &Env, y: u128, denominator: u128) -> u128 { + fn fixed_mul_floor(&self, env: &Env, y: &u128, denominator: &u128) -> u128 { scaled_mul_div_floor(self, env, y, denominator) } - fn fixed_mul_ceil(self, env: &Env, y: u128, denominator: u128) -> u128 { - scaled_mul_div_ceil(self, env, y, denominator) + fn fixed_mul_ceil(&self, env: &Env, y: &u128, denominator: &u128) -> u128 { + scaled_mul_div_ceil(&self, env, y, denominator) } - fn fixed_div_floor(self, env: &Env, y: u128, denominator: u128) -> u128 { + fn fixed_div_floor(&self, env: &Env, y: &u128, denominator: &u128) -> u128 { scaled_mul_div_floor(self, env, denominator, y) } - fn fixed_div_ceil(self, env: &Env, y: u128, denominator: u128) -> u128 { + fn fixed_div_ceil(&self, env: &Env, y: &u128, denominator: &u128) -> u128 { scaled_mul_div_ceil(self, env, denominator, y) } } /// Performs floor(x * y / z) -fn scaled_mul_div_floor(x: u128, env: &Env, y: u128, z: u128) -> u128 { - return match x.checked_mul(y) { - Some(r) => r.checked_div(z).unwrap_optimized(), +fn scaled_mul_div_floor(x: &u128, env: &Env, y: &u128, z: &u128) -> u128 { + return match x.checked_mul(*y) { + Some(r) => r.checked_div(*z).unwrap_optimized(), None => { // scale to U256 and retry let res = crate::u256::mul_div_floor( - U256::from_u128(&env, x), - U256::from_u128(&env, y), - U256::from_u128(&env, z), + &U256::from_u128(&env, *x), + &U256::from_u128(&env, *y), + &U256::from_u128(&env, *z), ); // will panic if result is not representable in u128 res.to_u128().unwrap_optimized() @@ -75,16 +75,16 @@ fn scaled_mul_div_floor(x: u128, env: &Env, y: u128, z: u128) -> u128 { } /// Performs floor(x * y / z) -fn scaled_mul_div_ceil(x: u128, env: &Env, y: u128, z: u128) -> u128 { - return match x.checked_mul(y) { - Some(r) => div_ceil(r, z).unwrap_optimized(), +fn scaled_mul_div_ceil(x: &u128, env: &Env, y: &u128, z: &u128) -> u128 { + return match x.checked_mul(*y) { + Some(r) => div_ceil(r, *z).unwrap_optimized(), None => { // scale to U256 and retry let res = crate::u256::mul_div_ceil( &env, - U256::from_u128(&env, x), - U256::from_u128(&env, y), - U256::from_u128(&env, z), + &U256::from_u128(&env, *x), + &U256::from_u128(&env, *y), + &U256::from_u128(&env, *z), ); // will panic if result is not representable in u128 res.to_u128().unwrap_optimized() @@ -252,7 +252,7 @@ mod test_soroban_fixed_point { let y: u128 = 314_1592653; let denominator: u128 = 1_0000001; - let result = x.fixed_mul_floor(&env, y, denominator); + let result = x.fixed_mul_floor(&env, &y, &denominator); assert_eq!(result, 483_5313675) } @@ -264,7 +264,7 @@ mod test_soroban_fixed_point { let y: u128 = 10u128.pow(27); let denominator: u128 = 10u128.pow(18); - let result = x.fixed_mul_floor(&env, y, denominator); + let result = x.fixed_mul_floor(&env, &y, &denominator); assert_eq!(result, 340_282_366_920_938_463_463 * 10u128.pow(9)); } @@ -278,7 +278,7 @@ mod test_soroban_fixed_point { let y: u128 = 314_1592653; let denominator: u128 = 1_0000001; - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, 483_5313676) } @@ -290,7 +290,7 @@ mod test_soroban_fixed_point { let y: u128 = 1_000_000_000_000_000_000; let denominator: u128 = 1_000_000_000_000_000_000; - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, 340_282_366_920_938_463_463) } @@ -302,7 +302,7 @@ mod test_soroban_fixed_point { let y: u128 = 10u128.pow(27); let denominator: u128 = 10u128.pow(18); - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, 340_282_366_920_938_463_463 * 10u128.pow(9)); } @@ -316,7 +316,7 @@ mod test_soroban_fixed_point { let y: u128 = 1_5391280; let denominator: u128 = 1_0000000; - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, 204_1150997) } @@ -328,7 +328,7 @@ mod test_soroban_fixed_point { let y: u128 = 10u128.pow(18); let denominator: u128 = 10u128.pow(27); - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, 340_282_366_920_938_463_463 * 10u128.pow(9)); } @@ -342,7 +342,7 @@ mod test_soroban_fixed_point { let y: u128 = 1_5391280; let denominator: u128 = 1_0000000; - let result = x.fixed_div_ceil(&env, y, denominator); + let result = x.fixed_div_ceil(&env, &y, &denominator); assert_eq!(result, 204_1150998) } @@ -354,7 +354,7 @@ mod test_soroban_fixed_point { let y: u128 = 1_000_000_000_000_000_000; let denominator: u128 = 1_000_000_000_000_000_000; - let result = x.fixed_div_ceil(&env, y, denominator); + let result = x.fixed_div_ceil(&env, &y, &denominator); assert_eq!(result, 340_282_366_920_938_463_463) } @@ -366,7 +366,7 @@ mod test_soroban_fixed_point { let y: u128 = 10u128.pow(18); let denominator: u128 = 10u128.pow(27); - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, 340_282_366_920_938_463_463 * 10u128.pow(9)); } diff --git a/src/u256.rs b/src/u256.rs index c283210..40baa5b 100644 --- a/src/u256.rs +++ b/src/u256.rs @@ -3,31 +3,31 @@ use soroban_sdk::{Env, U256}; use crate::soroban_fixed_point::SorobanFixedPoint; impl SorobanFixedPoint for U256 { - fn fixed_mul_floor(self, _env: &Env, y: U256, denominator: U256) -> U256 { + fn fixed_mul_floor(&self, _env: &Env, y: &U256, denominator: &U256) -> U256 { mul_div_floor(self, y, denominator) } - fn fixed_mul_ceil(self, env: &Env, y: U256, denominator: U256) -> U256 { + fn fixed_mul_ceil(&self, env: &Env, y: &U256, denominator: &U256) -> U256 { mul_div_ceil(env, self, y, denominator) } - fn fixed_div_floor(self, _env: &Env, y: U256, denominator: U256) -> U256 { + fn fixed_div_floor(&self, _env: &Env, y: &U256, denominator: &U256) -> U256 { mul_div_floor(self, denominator, y) } - fn fixed_div_ceil(self, env: &Env, y: U256, denominator: U256) -> U256 { + fn fixed_div_ceil(&self, env: &Env, y: &U256, denominator: &U256) -> U256 { mul_div_ceil(env, self, denominator, y) } } /// Performs floor(x * y / z) -pub(crate) fn mul_div_floor(x: U256, y: U256, z: U256) -> U256 { +pub(crate) fn mul_div_floor(x: &U256, y: &U256, z: &U256) -> U256 { // floor taken by default x.mul(&y).div(&z) } /// Performs ceil(x * y / z) -pub(crate) fn mul_div_ceil(env: &Env, x: U256, y: U256, z: U256) -> U256 { +pub(crate) fn mul_div_ceil(env: &Env, x: &U256, y: &U256, z: &U256) -> U256 { let r = x.mul(&y); let remainder = r.rem_euclid(&z); let zero = U256::from_u32(env, 0); @@ -48,7 +48,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 314_1592653); let denominator: U256 = U256::from_u128(&env, 1_0000001); - let result = x.fixed_mul_floor(&env, y, denominator); + let result = x.fixed_mul_floor(&env, &y, &denominator); assert_eq!(result, U256::from_u128(&env, 483_5313675)); } @@ -60,7 +60,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 10u128.pow(38)); let denominator: U256 = U256::from_u128(&env, 10u128.pow(18)); - let result = x.clone().fixed_mul_floor(&env, y, denominator); + let result = x.clone().fixed_mul_floor(&env, &y, &denominator); let expected_result = x.mul(&U256::from_u128(&env, 10u128.pow(20))); assert_eq!(result, expected_result); @@ -75,7 +75,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 10u128.pow(39)); let denominator: U256 = U256::from_u128(&env, 10u128.pow(18)); - x.fixed_mul_floor(&env, y, denominator); + x.fixed_mul_floor(&env, &y, &denominator); } /********** fixed_mul_ceil **********/ @@ -87,7 +87,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 314_1592653); let denominator: U256 = U256::from_u128(&env, 1_0000001); - let result = x.fixed_mul_ceil(&env, y, denominator); + let result = x.fixed_mul_ceil(&env, &y, &denominator); assert_eq!(result, U256::from_u128(&env, 483_5313676)); } @@ -99,7 +99,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 10u128.pow(38)); let denominator: U256 = U256::from_u128(&env, 10u128.pow(18)); - let result = x.clone().fixed_mul_ceil(&env, y, denominator); + let result = x.clone().fixed_mul_ceil(&env, &y, &denominator); let expected_result = x.mul(&U256::from_u128(&env, 10u128.pow(20))); assert_eq!(result, expected_result); @@ -114,7 +114,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 10u128.pow(39)); let denominator: U256 = U256::from_u128(&env, 10u128.pow(18)); - x.fixed_mul_ceil(&env, y, denominator); + x.fixed_mul_ceil(&env, &y, &denominator); } /********** fixed_div_floor **********/ @@ -126,7 +126,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 1_5391280); let denominator: U256 = U256::from_u128(&env, 1_0000000); - let result = x.fixed_div_floor(&env, y, denominator); + let result = x.fixed_div_floor(&env, &y, &denominator); assert_eq!(result, U256::from_u128(&env, 204_1150997)); } @@ -138,7 +138,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 10u128.pow(27)); let denominator: U256 = U256::from_u128(&env, 10u128.pow(38)); - let result = x.clone().fixed_div_floor(&env, y, denominator); + let result = x.clone().fixed_div_floor(&env, &y, &denominator); let expected_result = x.mul(&U256::from_u128(&env, 10u128.pow(11))); assert_eq!(result, expected_result); @@ -153,7 +153,7 @@ mod tests { // 256 bit max ~= 1.2e77, 128 bit max ~= 3.4e38, need to multiply by at least 10^39 let denominator: U256 = U256::from_u128(&env, 10u128.pow(39)); - x.fixed_div_floor(&env, y, denominator); + x.fixed_div_floor(&env, &y, &denominator); } /********** fixed_div_ceil **********/ @@ -165,7 +165,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 1_5391280); let denominator: U256 = U256::from_u128(&env, 1_0000000); - let result = x.fixed_div_ceil(&env, y, denominator); + let result = x.fixed_div_ceil(&env, &y, &denominator); assert_eq!(result, U256::from_u128(&env, 204_1150998)); } @@ -177,7 +177,7 @@ mod tests { let y: U256 = U256::from_u128(&env, 10u128.pow(27)); let denominator: U256 = U256::from_u128(&env, 10u128.pow(38)); - let result = x.clone().fixed_div_ceil(&env, y, denominator); + let result = x.clone().fixed_div_ceil(&env, &y, &denominator); let expected_result = x.mul(&U256::from_u128(&env, 10u128.pow(11))); assert_eq!(result, expected_result); @@ -192,6 +192,6 @@ mod tests { // 256 bit max ~= 1.2e77, 128 bit max ~= 3.4e38, need to multiply by at least 10^39 let denominator: U256 = U256::from_u128(&env, 10u128.pow(39)); - x.fixed_div_ceil(&env, y, denominator); + x.fixed_div_ceil(&env, &y, &denominator); } }