From 276602e5f392145080efbee46bb5eca8843893e0 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 23 Aug 2024 13:42:44 -0700 Subject: [PATCH] WideMul trait --- Cargo.lock | 1 + fixed_wide/Cargo.lock | 1 + fixed_wide/Cargo.toml | 1 + fixed_wide/src/wide.rs | 30 ++++++++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b0d4fcb..3ce3b79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,6 +58,7 @@ version = "0.1.0" dependencies = [ "bnum", "fixed", + "typenum", ] [[package]] diff --git a/fixed_wide/Cargo.lock b/fixed_wide/Cargo.lock index 6e5c37e..54c950e 100644 --- a/fixed_wide/Cargo.lock +++ b/fixed_wide/Cargo.lock @@ -50,6 +50,7 @@ version = "0.1.0" dependencies = [ "bnum", "fixed", + "typenum", ] [[package]] diff --git a/fixed_wide/Cargo.toml b/fixed_wide/Cargo.toml index 89be919..54644ec 100644 --- a/fixed_wide/Cargo.toml +++ b/fixed_wide/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" [dependencies] bnum = "0.11.0" fixed = "1.28.0" +typenum = "1.17.0" diff --git a/fixed_wide/src/wide.rs b/fixed_wide/src/wide.rs index 98ab750..a57d255 100644 --- a/fixed_wide/src/wide.rs +++ b/fixed_wide/src/wide.rs @@ -1,4 +1,30 @@ -pub struct Fixed{ - bits:I, +use bnum::types::{I256,I512}; +use fixed::{FixedI64,FixedI128}; +use typenum::{Sum, Unsigned}; +use typenum::consts::{U32,U64,U96,U128,U160,U192,U224,U256}; + +pub trait WideMul{ + type Output; + fn wide_mul(self,rhs:Rhs)->Self::Output; +} + +pub struct Fixed{ + bits:Int, phantom:std::marker::PhantomData, } +pub type FixedI256=Fixed; +pub type FixedI512=Fixed; + +impl WideMul> for FixedI128 + where + A:std::ops::Add, + B:Unsigned, +{ + type Output=FixedI256>; + fn wide_mul(self,rhs:FixedI128)->Self::Output{ + FixedI256{ + bits:I256::from(self.to_bits())*I256::from(rhs.to_bits()), + phantom:std::marker::PhantomData, + } + } +}