Skip to content

Commit

Permalink
Merge pull request #1 from script3/256-interface-fix
Browse files Browse the repository at this point in the history
feat: update SorobanFixedPoint interface to prevent excess clone calls
  • Loading branch information
mootz12 authored Apr 17, 2024
2 parents bd96277 + 58b455c commit 462c7c1
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 114 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -12,4 +12,4 @@ keywords = ["no_std", "wasm"]
rust-version = "1.74"

[dependencies]
soroban-sdk = { version = "20.0.0" }
soroban-sdk = { version = "20.5.0" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 34 additions & 34 deletions src/i128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,34 @@ fn div_ceil(r: i128, z: i128) -> Option<i128> {
}

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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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));
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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));
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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));
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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));
}
Expand Down
Loading

0 comments on commit 462c7c1

Please sign in to comment.