Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Cairo toolchain
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.4.0"
scarb-version: "2.8.4"

- name: Run tests
run: scarb test
2 changes: 1 addition & 1 deletion Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ version = 1

[[package]]
name = "cubit"
version = "1.3.0"
version = "1.4.0"
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cubit"
version = "1.3.0"
cairo-version = ">=2.4.0"
version = "1.4.0"
cairo-version = ">=2.8.4"
edition = "2023_10"
description = "Math library in Cairo using a 64.64 fixed point representation"
homepage = "https://github.com/influenceth/cubit"
Expand Down
257 changes: 79 additions & 178 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/f128/math/ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::result::{ResultTrait, ResultTraitImpl};
use core::traits::{Into, TryInto};
use core::integer;
use core::integer::{u256_safe_div_rem, u256_as_non_zero, upcast};
use core::num::traits::{Sqrt, WideMul};

use cubit::f128::math::lut;
use cubit::f128::types::fixed::{
Expand Down Expand Up @@ -204,7 +205,7 @@ struct f64 {
}

fn mul_64(a: f64, b: f64) -> f64 {
let prod_u128 = integer::u64_wide_mul(a.mag, b.mag);
let prod_u128 = a.mag.wide_mul(b.mag);
return f64 { mag: (prod_u128 / 4294967296).try_into().unwrap(), sign: a.sign ^ b.sign };
}

Expand Down Expand Up @@ -289,8 +290,8 @@ fn round(a: Fixed) -> Fixed {
// x must be positive
fn sqrt(a: Fixed) -> Fixed {
assert(a.sign == false, 'must be positive');
let root = integer::u128_sqrt(a.mag);
let scale_root = integer::u128_sqrt(ONE_u128);
let root = a.mag.sqrt();
let scale_root = ONE_u128.sqrt();
let res_u128 = upcast(root) * ONE_u128 / upcast(scale_root);
return FixedTrait::new(res_u128, false);
}
Expand Down
9 changes: 5 additions & 4 deletions src/f128/types/fixed.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::integer::{U256DivRem, u256_safe_divmod, u256_as_non_zero, u256_from_fe
use core::option::OptionTrait;
use core::result::{ResultTrait, ResultTraitImpl};
use core::traits::{TryInto, Into};
use core::num::traits::WideMul;

use starknet::storage_access::StorePacking;

Expand Down Expand Up @@ -92,16 +93,16 @@ impl FixedImpl of FixedTrait {
}

fn new_unscaled(mag: u128, sign: bool) -> Fixed {
return FixedTrait::new(mag * ONE_u128, sign);
return Self::new(mag * ONE_u128, sign);
}

fn from_felt(val: felt252) -> Fixed {
let mag = core::integer::u128_try_from_felt252(utils::felt_abs(val)).unwrap();
return FixedTrait::new(mag, utils::felt_sign(val));
return Self::new(mag, utils::felt_sign(val));
}

fn from_unscaled_felt(val: felt252) -> Fixed {
return FixedTrait::from_felt(val * ONE);
return Self::from_felt(val * ONE);
}

fn abs(self: Fixed) -> Fixed {
Expand Down Expand Up @@ -551,7 +552,7 @@ impl FixedOne of core::num::traits::One<Fixed> {
}
#[inline(always)]
fn is_one(self: @Fixed) -> bool {
*self == FixedOne::one()
*self == Self::one()
}
#[inline(always)]
fn is_non_one(self: @Fixed) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/f128/types/vec2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::debug::PrintTrait;

use cubit::f128::types::fixed::{Fixed, FixedTrait, FixedPrint};

#[derive(Copy, Drop, Serde, Store)]
#[derive(Copy, Drop, Serde)]
struct Vec2 {
x: Fixed,
y: Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/f128/types/vec3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::debug::PrintTrait;

use cubit::f128::types::fixed::{Fixed, FixedTrait, FixedPrint};

#[derive(Copy, Drop, Serde, Store)]
#[derive(Copy, Drop, Serde)]
struct Vec3 {
x: Fixed,
y: Fixed,
Expand Down
2 changes: 1 addition & 1 deletion src/f128/types/vec4.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::debug::PrintTrait;

use cubit::f128::types::fixed::{Fixed, FixedTrait, FixedPrint};

#[derive(Copy, Drop, Serde, Store)]
#[derive(Copy, Drop, Serde)]
struct Vec4 {
x: Fixed,
y: Fixed,
Expand Down
9 changes: 5 additions & 4 deletions src/f64/math/ops.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::option::OptionTrait;
use core::result::{ResultTrait, ResultTraitImpl};
use core::traits::{Into, TryInto};
use core::integer::{u64_safe_divmod, u64_as_non_zero, u64_wide_mul};
use core::integer::{u64_safe_divmod, u64_as_non_zero};
use core::num::traits::{Sqrt, WideMul};

use cubit::f64::math::lut;
use cubit::f64::types::fixed::{HALF, ONE, Fixed, FixedIntoFelt252, FixedTrait};
Expand Down Expand Up @@ -43,7 +44,7 @@ fn ceil(a: Fixed) -> Fixed {
}

fn div(a: Fixed, b: Fixed) -> Fixed {
let a_u128 = core::integer::u64_wide_mul(a.mag, ONE);
let a_u128 = a.mag.wide_mul(ONE);
let res_u128 = a_u128 / b.mag.into();

// Re-apply sign
Expand Down Expand Up @@ -182,7 +183,7 @@ fn lt(a: Fixed, b: Fixed) -> bool {
}

fn mul(a: Fixed, b: Fixed) -> Fixed {
let prod_u128 = core::integer::u64_wide_mul(a.mag, b.mag);
let prod_u128 = a.mag.wide_mul(b.mag);

// Re-apply sign
return FixedTrait::new((prod_u128 / ONE.into()).try_into().unwrap(), a.sign ^ b.sign);
Expand Down Expand Up @@ -269,7 +270,7 @@ fn round(a: Fixed) -> Fixed {
// x must be positive
fn sqrt(a: Fixed) -> Fixed {
assert(a.sign == false, 'must be positive');
let root = core::integer::u128_sqrt(a.mag.into() * ONE.into());
let root = (a.mag * ONE).sqrt();
return FixedTrait::new(root.into(), false);
}

Expand Down
6 changes: 3 additions & 3 deletions src/f64/types/fixed.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ impl FixedImpl of FixedTrait {

fn from_felt(val: felt252) -> Fixed {
let mag = core::integer::u64_try_from_felt252(utils::felt_abs(val)).unwrap();
return FixedTrait::new(mag, utils::felt_sign(val));
return Self::new(mag, utils::felt_sign(val));
}

fn from_unscaled_felt(val: felt252) -> Fixed {
return FixedTrait::from_felt(val * ONE.into());
return Self::from_felt(val * ONE.into());
}

fn abs(self: Fixed) -> Fixed {
Expand Down Expand Up @@ -548,7 +548,7 @@ impl FixedOne of core::num::traits::One<Fixed> {
}
#[inline(always)]
fn is_one(self: @Fixed) -> bool {
*self == FixedOne::one()
*self == Self::one()
}
#[inline(always)]
fn is_non_one(self: @Fixed) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/f64/types/vec2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::debug::PrintTrait;

use cubit::f64::types::fixed::{Fixed, FixedTrait, FixedPrint};

#[derive(Copy, Drop, Serde, Store)]
#[derive(Copy, Drop, Serde)]
struct Vec2 {
x: Fixed,
y: Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/f64/types/vec3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::debug::PrintTrait;

use cubit::f64::types::fixed::{Fixed, FixedTrait, FixedPrint};

#[derive(Copy, Drop, Serde, Store)]
#[derive(Copy, Drop, Serde)]
struct Vec3 {
x: Fixed,
y: Fixed,
Expand Down
2 changes: 1 addition & 1 deletion src/f64/types/vec4.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::debug::PrintTrait;

use cubit::f64::types::fixed::{Fixed, FixedTrait, FixedPrint};

#[derive(Copy, Drop, Serde, Store)]
#[derive(Copy, Drop, Serde)]
struct Vec4 {
x: Fixed,
y: Fixed,
Expand Down