Skip to content

Commit

Permalink
addition of f32x32 type4
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Jul 8, 2024
1 parent 454c646 commit 1d7bea6
Show file tree
Hide file tree
Showing 13 changed files with 557 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// 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');
Expand Down Expand Up @@ -73,6 +75,54 @@ pub impl I64Rem of Rem<i64> {
}
}

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
} else if a > 0 {
1
} else {
-1
}
}

pub fn sign_i32(a: i32) -> i32 {
if a == 0 {
0
Expand Down
1 change: 0 additions & 1 deletion packages/orion-numbers/src/f16x16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ pub mod trig;
pub mod erf;
pub mod helpers;
pub mod lut;
pub mod core_trait;
8 changes: 3 additions & 5 deletions packages/orion-numbers/src/f16x16/core.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use orion_numbers::f16x16::{math, trig, erf};
use core::traits::TryInto;
use core::option::OptionTrait;
use core::traits::Mul;
use orion_numbers::FixedTrait;

pub type f16x16 = i32;

Expand All @@ -12,8 +10,8 @@ pub const HALF: f16x16 = 32768; // 2 ** 15
pub const MAX: f16x16 = 2147483647; // 2 ** 31 -1
pub const MIN: f16x16 = -2147483648; // 2 ** 31

#[generate_trait]
pub impl f16x16Impl of FixedTrait {

pub impl F16x16Impl of FixedTrait<f16x16> {
fn ZERO() -> f16x16 {
0
}
Expand Down
3 changes: 2 additions & 1 deletion packages/orion-numbers/src/f16x16/erf.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use orion_numbers::f16x16::{core::{FixedTrait, f16x16, ONE}, lut};
use orion_numbers::f16x16::{core::{f16x16, ONE}, lut};
use orion_numbers::FixedTrait;

const ERF_COMPUTATIONAL_ACCURACY: i32 = 100;
const ROUND_CHECK_NUMBER: i32 = 10;
Expand Down
9 changes: 4 additions & 5 deletions packages/orion-numbers/src/f16x16/helpers.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use orion_numbers::f16x16::core::{FixedTrait, f16x16, ONE, HALF};

use orion_numbers::f16x16::core_trait::I32Div;
use orion_numbers::f16x16::core::{F16x16Impl, f16x16, ONE, HALF};
use orion_numbers::core_trait::I32Div;

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

Expand All @@ -14,7 +13,7 @@ pub fn assert_precise(
Option::None => DEFAULT_PRECISION,
};

let diff = (result - FixedTrait::from_felt(expected));
let diff = (result - F16x16Impl::from_felt(expected));

if (diff > precision) {
//println!("{}", result);
Expand All @@ -30,7 +29,7 @@ pub fn assert_relative(
Option::None => DEFAULT_PRECISION,
};

let diff = result - FixedTrait::from_felt(expected);
let diff = result - F16x16Impl::from_felt(expected);
let rel_diff = diff / result;

if (rel_diff > precision) {
Expand Down
2 changes: 1 addition & 1 deletion packages/orion-numbers/src/f16x16/lut.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use orion_numbers::f16x16::core::ONE;

use orion_numbers::f16x16::core_trait::I32Div;
use orion_numbers::core_trait::I32Div;

// Calculates the most significant bit
pub fn msb(whole: i32) -> (i32, i32) {
Expand Down
Loading

0 comments on commit 1d7bea6

Please sign in to comment.