Skip to content

Commit 933d480

Browse files
committedNov 26, 2017
FEAT: Add mixed operations. Fast<f64> + f64 and so on
1 parent 83187b4 commit 933d480

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed
 

‎src/lib.rs

+45-15
Original file line numberDiff line numberDiff line change
@@ -75,43 +75,73 @@ pub fn regular_sum(xs: &[f64]) -> f64 {
7575
macro_rules! impl_op {
7676
($($name:ident, $method:ident, $intrins:ident;)*) => {
7777
$(
78-
impl $name for Fast<f64> {
78+
// Fast<F> + F
79+
impl $name<f64> for Fast<f64> {
7980
type Output = Self;
8081
#[inline(always)]
81-
fn $method(self, rhs: Self) -> Self::Output {
82+
fn $method(self, rhs: f64) -> Self::Output {
8283
unsafe {
83-
Fast($intrins(self.0, rhs.0))
84+
Fast($intrins(self.0, rhs))
8485
}
8586
}
8687
}
8788

88-
impl $name for Fast<f32> {
89+
impl $name<f32> for Fast<f32> {
8990
type Output = Self;
9091
#[inline(always)]
91-
fn $method(self, rhs: Self) -> Self::Output {
92+
fn $method(self, rhs: f32) -> Self::Output {
9293
unsafe {
93-
Fast($intrins(self.0, rhs.0))
94+
Fast($intrins(self.0, rhs))
9495
}
9596
}
9697
}
97-
)*
9898

99-
}
100-
}
99+
// F + Fast<F>
100+
impl $name<Fast<f64>> for f64 {
101+
type Output = Fast<f64>;
102+
#[inline(always)]
103+
fn $method(self, rhs: Fast<f64>) -> Self::Output {
104+
Fast(self).$method(rhs.0)
105+
}
106+
}
101107

102-
macro_rules! impl_assignop {
103-
($($name:ident, $method:ident, $intrins:ident;)*) => {
104-
$(
108+
impl $name<Fast<f32>> for f32 {
109+
type Output = Fast<f32>;
110+
#[inline(always)]
111+
fn $method(self, rhs: Fast<f32>) -> Self::Output {
112+
Fast(self).$method(rhs.0)
113+
}
114+
}
115+
116+
// Fast<F> + Fast<F>
105117
impl $name for Fast<f64> {
118+
type Output = Self;
106119
#[inline(always)]
107-
fn $method(&mut self, rhs: Self) {
108-
*self = *self + rhs
120+
fn $method(self, rhs: Self) -> Self::Output {
121+
self.$method(rhs.0)
109122
}
110123
}
111124

112125
impl $name for Fast<f32> {
126+
type Output = Self;
127+
#[inline(always)]
128+
fn $method(self, rhs: Self) -> Self::Output {
129+
self.$method(rhs.0)
130+
}
131+
}
132+
)*
133+
134+
}
135+
}
136+
137+
macro_rules! impl_assignop {
138+
($($name:ident, $method:ident, $intrins:ident;)*) => {
139+
$(
140+
impl<F, Rhs> $name<Rhs> for Fast<F>
141+
where Self: Add<Rhs, Output=Self> + Copy,
142+
{
113143
#[inline(always)]
114-
fn $method(&mut self, rhs: Self) {
144+
fn $method(&mut self, rhs: Rhs) {
115145
*self = *self + rhs
116146
}
117147
}

0 commit comments

Comments
 (0)
Please sign in to comment.