-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Div and Rem traits for I32 and I64 in house
- Loading branch information
Showing
8 changed files
with
133 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
scarb nightly-2024-06-10 | ||
scarb 2.6.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pub mod math; | |
pub mod trig; | ||
pub mod erf; | ||
pub mod helpers; | ||
pub mod lut; | ||
pub mod lut; | ||
pub mod core_trait; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
pub impl I32Div of Div<i32> { | ||
fn div(lhs: i32, rhs: i32) -> i32 { | ||
assert(rhs != 0, 'divisor cannot be 0'); | ||
|
||
let mut lhs_positive = lhs; | ||
let mut rhs_positive = rhs; | ||
|
||
if lhs < 0 { | ||
lhs_positive = lhs * -1; | ||
} | ||
if rhs < 0 { | ||
rhs_positive = rhs * -1; | ||
} | ||
|
||
let lhs_u32: u32 = lhs_positive.try_into().unwrap(); | ||
let rhs_u32: u32 = rhs_positive.try_into().unwrap(); | ||
|
||
let mut result = lhs_u32 / rhs_u32; | ||
let felt_result: felt252 = result.into(); | ||
let signed_int_result: i32 = felt_result.try_into().unwrap(); | ||
|
||
// avoids mul overflow for f16x16 | ||
if sign_i32(lhs) * rhs < 0 { | ||
signed_int_result * -1 | ||
} else { | ||
signed_int_result | ||
} | ||
} | ||
} | ||
|
||
pub impl I32Rem of Rem<i32> { | ||
fn rem(lhs: i32, rhs: i32) -> i32 { | ||
let div = Div::div(lhs, rhs); | ||
lhs - rhs * div | ||
} | ||
} | ||
|
||
|
||
pub impl I64Div of Div<i64> { | ||
fn div(lhs: i64, rhs: i64) -> i64 { | ||
assert(rhs != 0, 'divisor cannot be 0'); | ||
|
||
let mut lhs_positive = lhs; | ||
let mut rhs_positive = rhs; | ||
|
||
if lhs < 0 { | ||
lhs_positive = lhs * -1; | ||
} | ||
if rhs < 0 { | ||
rhs_positive = rhs * -1; | ||
} | ||
|
||
let lhs_u64: u64 = lhs_positive.try_into().unwrap(); | ||
let rhs_u64: u64 = rhs_positive.try_into().unwrap(); | ||
|
||
let mut result = lhs_u64 / rhs_u64; | ||
let felt_result: felt252 = result.into(); | ||
let signed_int_result: i64 = felt_result.try_into().unwrap(); | ||
|
||
// avoids mul overflow for f16x16 | ||
if sign_i64(lhs) * rhs < 0 { | ||
signed_int_result * -1 | ||
} else { | ||
signed_int_result | ||
} | ||
} | ||
} | ||
|
||
pub impl I64Rem of Rem<i64> { | ||
fn rem(lhs: i64, rhs: i64) -> i64 { | ||
let div = Div::div(lhs, rhs); | ||
lhs - rhs * div | ||
} | ||
} | ||
|
||
pub fn sign_i32(a: i32) -> i32 { | ||
if a == 0 { | ||
0 | ||
} else if a > 0 { | ||
1 | ||
} else { | ||
-1 | ||
} | ||
} | ||
|
||
pub fn sign_i64(a: i64) -> i64 { | ||
if a == 0 { | ||
0 | ||
} else if a > 0 { | ||
1 | ||
} else { | ||
-1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters