Skip to content

Commit

Permalink
update orion-numbers with cairo 2.7.0-rc3
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelDkhn committed Jul 24, 2024
1 parent f793758 commit 2ee39e6
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 166 deletions.
2 changes: 1 addition & 1 deletion packages/orion-numbers/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.6.5
scarb 2.7.0-rc.3
4 changes: 2 additions & 2 deletions packages/orion-numbers/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2023_11"

[dependencies]

[scripts]
test = "scarb cairo-test -f test"
[dev-dependencies]
cairo_test = "2.7.0-rc.3"

[cairo]
enable-gas=false
115 changes: 0 additions & 115 deletions packages/orion-numbers/src/core_trait.cairo
Original file line number Diff line number Diff line change
@@ -1,118 +1,3 @@
// Basic Arithmetic Trait on integer 32, 64 and 128, should be included in Cairo core soon.

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 impl I128Div of Div<i128> {
fn div(lhs: i128, rhs: i128) -> i128 {
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_u128: u128 = lhs_positive.try_into().unwrap();
let rhs_u128: u128 = rhs_positive.try_into().unwrap();

let mut result = lhs_u128 / rhs_u128;
let felt_result: felt252 = result.into();
let signed_int_result: i128 = felt_result.try_into().unwrap();

// avoids mul overflow for f16x16
if sign_i128(lhs) * rhs < 0 {
signed_int_result * -1
} else {
signed_int_result
}
}
}

pub impl I128Rem of Rem<i128> {
fn rem(lhs: i128, rhs: i128) -> i128 {
let div = Div::div(lhs, rhs);
lhs - rhs * div
}
}


pub fn sign_i128(a: i128) -> i128 {
if a == 0 {
0
Expand Down
1 change: 0 additions & 1 deletion packages/orion-numbers/src/f16x16/erf.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ mod tests {
use super::{erf, f16x16};

#[test]
#[available_gas(1000000000)]
fn test_erf() {
// 1.0
let f1 = 65536;
Expand Down
1 change: 0 additions & 1 deletion packages/orion-numbers/src/f16x16/helpers.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use orion_numbers::f16x16::core::{F16x16Impl, f16x16, ONE, HALF};
use orion_numbers::core_trait::I32Div;

const DEFAULT_PRECISION: i32 = 7; // 1e-4

Expand Down
2 changes: 0 additions & 2 deletions packages/orion-numbers/src/f16x16/lut.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use orion_numbers::f16x16::core::ONE;

use orion_numbers::core_trait::I32Div;

// Calculates the most significant bit
pub fn msb(whole: i32) -> (i32, i32) {
if whole < 256 {
Expand Down
31 changes: 10 additions & 21 deletions packages/orion-numbers/src/f16x16/math.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::option::OptionTrait;
use core::traits::TryInto;
use core::integer;
use core::num::traits::{WideMul, Sqrt};
use orion_numbers::f16x16::{core::{F16x16Impl, f16x16, ONE, HALF}, lut};

use orion_numbers::core_trait::{I32Rem, I32Div, I64Div}; //I32TryIntoNonZero, I32DivRem


pub fn abs(a: f16x16) -> f16x16 {
if a >= 0 {
a
Expand Down Expand Up @@ -33,7 +33,7 @@ pub fn ceil(a: f16x16) -> f16x16 {
}

pub fn div(a: f16x16, b: f16x16) -> f16x16 {
let a_i64 = integer::i32_wide_mul(a, ONE);
let a_i64 = WideMul::wide_mul(a, ONE);
let res_i64 = a_i64 / b.into();

// Re-apply sign
Expand Down Expand Up @@ -141,7 +141,7 @@ pub fn log10(a: f16x16) -> f16x16 {
}

pub fn mul(a: f16x16, b: f16x16) -> f16x16 {
let prod_i64 = integer::i32_wide_mul(a, b);
let prod_i64 = WideMul::wide_mul(a, b);

// Re-apply sign
F16x16Impl::new((prod_i64 / ONE.into()).try_into().unwrap())
Expand Down Expand Up @@ -220,17 +220,19 @@ pub fn sign(a: f16x16) -> f16x16 {
// x must be positive
pub fn sqrt(a: f16x16) -> f16x16 {
assert(a >= 0, 'must be positive');
//let a: usize = a.try_into().unwrap();

let root = integer::u64_sqrt(a.try_into().unwrap() * ONE.try_into().unwrap());
let a: u64 = a.try_into().unwrap();
let one: u64 = ONE.try_into().unwrap();

let root: u32 = Sqrt::sqrt(a * one);

F16x16Impl::new(root.try_into().unwrap())
}


// Tests
//
//
//
// --------------------------------------------------------------------------------------------------------------

#[cfg(test)]
Expand All @@ -242,8 +244,6 @@ mod tests {
ln, log2, log10, pow, round, sign
};

use orion_numbers::core_trait::{I32Rem, I32Div};

#[test]
fn test_into() {
let a = F16x16Impl::new_unscaled(5);
Expand All @@ -257,21 +257,18 @@ mod tests {
}

#[test]
#[available_gas(10000000)]
fn test_exp() {
let a = F16x16Impl::new_unscaled(2);
assert_relative(exp(a), 484249, 'invalid exp of 2', Option::None(())); // 7.389056098793725
}

#[test]
#[available_gas(400000)]
fn test_exp2() {
let a = F16x16Impl::new_unscaled(5);
assert(exp2(a) == 2097152, 'invalid exp2 of 2');
}

#[test]
#[available_gas(20000)]
fn test_exp2_int() {
assert(exp2_int(5).into() == 2097152, 'invalid exp2 of 2');
}
Expand All @@ -283,7 +280,6 @@ mod tests {
}

#[test]
#[available_gas(1000000)]
fn test_ln() {
let mut a = F16x16Impl::new_unscaled(1);
assert(ln(a) == 0, 'invalid ln of 1');
Expand All @@ -293,7 +289,6 @@ mod tests {
}

#[test]
#[available_gas(1000000)]
fn test_log2() {
let mut a = F16x16Impl::new_unscaled(32);
assert(log2(a) == F16x16Impl::new_unscaled(5), 'invalid log2 32');
Expand All @@ -303,23 +298,20 @@ mod tests {
}

#[test]
#[available_gas(1000000)]
fn test_log10() {
let a = F16x16Impl::new_unscaled(100);
assert_relative(log10(a), 2 * ONE.into(), 'invalid log10', Option::None(()));
}


#[test]
#[available_gas(600000)]
fn test_pow() {
let a = F16x16Impl::new_unscaled(3);
let b = F16x16Impl::new_unscaled(4);
assert(pow(a, b) == 81 * ONE, 'invalid pos base power');
}

#[test]
#[available_gas(900000)]
fn test_pow_frac() {
let a = F16x16Impl::new_unscaled(3);
let b = F16x16Impl::new(32768); // 0.5
Expand Down Expand Up @@ -352,7 +344,6 @@ mod tests {
}

#[test]
#[available_gas(2000000)]
fn test_sign() {
let a = F16x16Impl::new(0);
assert(a.sign() == 0, 'invalid sign (0)');
Expand All @@ -371,7 +362,6 @@ mod tests {
}

#[test]
#[available_gas(100000)]
fn test_msb() {
let a = F16x16Impl::new_unscaled(100);
let (msb, div) = lut::msb(a / ONE);
Expand Down Expand Up @@ -427,7 +417,6 @@ mod tests {
}

#[test]
#[available_gas(100000)]
fn test_mul_pos() {
let a = 190054;
let b = 190054;
Expand Down
Loading

0 comments on commit 2ee39e6

Please sign in to comment.