Skip to content

Commit

Permalink
implement FP16x16Wide
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelDkhn committed Oct 21, 2023
1 parent 0714ac6 commit cb7e483
Show file tree
Hide file tree
Showing 17 changed files with 3,619 additions and 32 deletions.
170 changes: 169 additions & 1 deletion src/numbers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl FP8x23Number of NumberTrait<FP8x23, u32> {
}

use orion::numbers::fixed_point::implementations::fp16x16::core::{
FP16x16Impl, FP16x16, FP16x16IntoFP64x64
FP16x16Impl, FP16x16, FP16x16IntoFP16x16Wide
};
use orion::numbers::fixed_point::implementations::fp16x16::math::core as core_fp16x16;
use orion::numbers::fixed_point::implementations::fp16x16::math::comp as comp_fp16x16;
Expand Down Expand Up @@ -382,6 +382,174 @@ impl FP16x16Number of NumberTrait<FP16x16, u32> {
}
}

use orion::numbers::fixed_point::implementations::fp16x16wide::core::{
FP16x16Impl as FP16x16WideImpl, FP16x16Wide, FP16x16IntoFP64x64 as FP16x16WideIntoFP64x64
};
use orion::numbers::fixed_point::implementations::fp16x16wide::math::core as core_fp16x16wide;
use orion::numbers::fixed_point::implementations::fp16x16wide::math::comp as comp_fp16x16wide;

impl FP16x16WideNumber of NumberTrait<FP16x16Wide, u64> {
fn new(mag: u64, sign: bool) -> FP16x16Wide {
FP16x16WideImpl::new(mag, sign)
}

fn new_unscaled(mag: u64, sign: bool) -> FP16x16Wide {
FP16x16WideImpl::new_unscaled(mag, sign)
}

fn from_felt(val: felt252) -> FP16x16Wide {
FP16x16WideImpl::from_felt(val)
}

fn ceil(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::ceil(self)
}

fn exp(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::exp(self)
}

fn exp2(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::exp2(self)
}

fn floor(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::floor(self)
}

fn ln(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::ln(self)
}

fn log2(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::log2(self)
}

fn log10(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::log10(self)
}

fn pow(self: FP16x16Wide, b: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::pow(self, b)
}

fn round(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::round(self)
}

fn sqrt(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::sqrt(self)
}

fn acos(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::acos(self)
}

fn asin(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::asin(self)
}

fn atan(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::atan(self)
}

fn cos(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::cos(self)
}

fn sin(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::sin(self)
}

fn tan(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::tan(self)
}

fn acosh(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::acosh(self)
}

fn asinh(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::asinh(self)
}

fn atanh(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::atanh(self)
}

fn cosh(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::cosh(self)
}

fn sinh(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::sinh(self)
}

fn tanh(self: FP16x16Wide) -> FP16x16Wide {
FP16x16WideImpl::tanh(self)
}

fn zero() -> FP16x16Wide {
FP16x16WideImpl::ZERO()
}
fn is_zero(self: FP16x16Wide) -> bool {
core_fp16x16wide::eq(@self, @FP16x16WideImpl::ZERO())
}

fn one() -> FP16x16Wide {
FP16x16WideImpl::ONE()
}

fn neg_one() -> FP16x16Wide {
FP16x16Wide { mag: core_fp16x16wide::ONE, sign: true }
}

fn is_one(self: FP16x16Wide) -> bool {
core_fp16x16wide::eq(@self, @FP16x16WideImpl::ONE())
}

fn abs(self: FP16x16Wide) -> FP16x16Wide {
core_fp16x16wide::abs(self)
}

fn min_value() -> FP16x16Wide {
FP16x16Wide { mag: core_fp16x16wide::MAX, sign: true }
}

fn max_value() -> FP16x16Wide {
FP16x16Wide { mag: core_fp16x16wide::MAX, sign: false }
}

fn min(self: FP16x16Wide, other: FP16x16Wide) -> FP16x16Wide {
comp_fp16x16wide::min(self, other)
}

fn max(self: FP16x16Wide, other: FP16x16Wide) -> FP16x16Wide {
comp_fp16x16wide::max(self, other)
}

fn mag(self: FP16x16Wide) -> u64 {
self.mag
}

fn is_neg(self: FP16x16Wide) -> bool {
self.sign
}

fn xor(lhs: FP16x16Wide, rhs: FP16x16Wide) -> bool {
comp_fp16x16wide::xor(lhs, rhs)
}

fn or(lhs: FP16x16Wide, rhs: FP16x16Wide) -> bool {
comp_fp16x16wide::or(lhs, rhs)
}

fn sign(self: FP16x16Wide) -> FP16x16Wide {
core_fp16x16wide::sign(self)
}
}


use orion::numbers::fixed_point::implementations::fp64x64::core::{FP64x64Impl, FP64x64};
use orion::numbers::fixed_point::implementations::fp64x64::core as core_fp64x64;
use orion::numbers::fixed_point::implementations::fp64x64::comp as comp_fp64x64;
Expand Down
1 change: 1 addition & 0 deletions src/numbers/fixed_point/implementations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ mod fp8x23;
mod fp16x16;
mod fp64x64;
mod fp32x32;
mod fp16x16wide;
26 changes: 7 additions & 19 deletions src/numbers/fixed_point/implementations/fp16x16/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use result::{ResultTrait, ResultTraitImpl};
use traits::{TryInto, Into};

use orion::numbers::signed_integer::{i32::i32, i8::i8};
use orion::numbers::{FP64x64, FP64x64Impl};
use orion::numbers::{FP16x16Wide, FP16x16WideImpl};
use orion::numbers::fixed_point::core::FixedTrait;
use orion::numbers::fixed_point::implementations::fp16x16::math::{core, trig, hyp};
use orion::numbers::fixed_point::utils;
Expand Down Expand Up @@ -193,31 +193,19 @@ impl FP16x16Print of PrintTrait<FP16x16> {
}
}

impl FP16x16IntoFP64x64 of Into<FP16x16, FP64x64> {
fn into(self: FP16x16) -> FP64x64 {
return FP64x64 { mag: self.mag.into() * 281474976710656_u128, sign: self.sign };
impl FP16x16IntoFP16x16Wide of Into<FP16x16, FP16x16Wide> {
fn into(self: FP16x16) -> FP16x16Wide {
return FP16x16Wide { mag: self.mag.into(), sign: self.sign };
}
}

#[test]
fn test_fp16x16_into_fp32x32() {
let a = FP16x16Impl::new_unscaled(42, true);
let b: FP64x64 = a.into();
assert(b == FP64x64Impl::new_unscaled(42, true), 'invalid conversion');
}

impl FP64x64TryIntoFP16x16 of TryInto<FP64x64, FP16x16> {
fn try_into(self: FP64x64) -> Option<FP16x16> {
Option::Some(FP16x16 { mag: (self.mag / 281474976710656_u128).try_into().unwrap(), sign: self.sign })
impl FP16x16WideTryIntoFP16x16 of TryInto<FP16x16Wide, FP16x16> {
fn try_into(self: FP16x16Wide) -> Option<FP16x16> {
Option::Some(FP16x16 { mag: (self.mag).try_into().unwrap(), sign: self.sign })
}
}

#[test]
fn test_fp32x32_try_into_fp16x16() {
let a = FP64x64Impl::new_unscaled(42, true);
let b: FP16x16 = a.try_into().unwrap();
assert(b == FP16x16Impl::new_unscaled(42, true), 'invalid conversion');
}

// Into a raw felt without unscaling
impl FP16x16IntoFelt252 of Into<FP16x16, felt252> {
Expand Down
3 changes: 3 additions & 0 deletions src/numbers/fixed_point/implementations/fp16x16wide.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod core;
mod math;
mod helpers;
Loading

0 comments on commit cb7e483

Please sign in to comment.