Skip to content

Commit af483c4

Browse files
committed
FEATURE: Compound assignment operators
Also, modifies binary operators to use Array objects instead of borrowed references.
1 parent c1ebe05 commit af483c4

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

examples/helloworld.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828
af_print!("sin(a) + cos(a) => ", b2);
2929
af_print!("!a => ", b3);
3030

31-
let test = &a + &b;
31+
let test = a.clone() + b.clone();
3232
af_print!("a + b", test);
3333

3434
// Index array using sequences
@@ -82,4 +82,4 @@ fn main() {
8282
let u8_cnst = &constant(1 as u8, dims);
8383
af_print!("u8 constant array", u8_cnst);
8484
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single());
85-
}
85+
}

examples/unified.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ fn test_backend(){
1111
let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);
1212

1313
println!("Create a 10-by-10 matrix of random floats on the compute device");
14-
let a = randu::<f32>(dims);
14+
let mut a = randu::<f32>(dims);
15+
let b = randu::<f32>(dims);
16+
print(&a);
17+
print(&b);
18+
a += b;
1519
print(&a);
1620
}
1721

src/arith/mod.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ use error::HANDLE_ERROR;
88
use self::libc::{c_int};
99
use data::{constant, tile};
1010
use self::num::Complex;
11+
use index::{Indexer, assign_gen};
12+
use seq::Seq;
13+
use std::mem;
1114

1215
type MutAfArray = *mut self::libc::c_longlong;
1316
type MutDouble = *mut self::libc::c_double;
1417
type MutUint = *mut self::libc::c_uint;
1518
type AfArray = self::libc::c_longlong;
1619

1720
use std::ops::{Add, Sub, Div, Mul, BitAnd, BitOr, BitXor, Not, Rem, Shl, Shr};
21+
use std::ops::{AddAssign, SubAssign, DivAssign, MulAssign, BitAndAssign, BitOrAssign, BitXorAssign,
22+
RemAssign, ShlAssign, ShrAssign};
1823

1924
#[allow(dead_code)]
2025
extern {
@@ -311,10 +316,10 @@ arith_scalar_spec!(u8);
311316

312317
macro_rules! arith_func {
313318
($op_name:ident, $fn_name:ident, $ffi_fn: ident) => (
314-
impl<'f> $op_name<&'f Array> for &'f Array {
319+
impl $op_name<Array> for Array {
315320
type Output = Array;
316321

317-
fn $fn_name(self, rhs:&'f Array) -> Array {
322+
fn $fn_name(self, rhs: Array) -> Array {
318323
unsafe {
319324
let mut temp: i64 = 0;
320325
let err_val = $ffi_fn(&mut temp as MutAfArray,
@@ -336,4 +341,49 @@ arith_func!(BitAnd, bitand, af_bitand);
336341
arith_func!(BitOr, bitor, af_bitor);
337342
arith_func!(BitXor, bitxor, af_bitxor);
338343
arith_func!(Shl, shl, af_bitshiftl);
339-
arith_func!(Shr, shr, af_bitshiftr);
344+
arith_func!(Shr, shr, af_bitshiftr);
345+
346+
macro_rules! arith_assign_func {
347+
($op_name:ident, $fn_name:ident, $func: ident) => (
348+
impl $op_name<Array> for Array {
349+
350+
#[allow(unused_variables)]
351+
fn $fn_name(&mut self, rhs: Array) {
352+
let mut idxrs = Indexer::new();
353+
idxrs.set_index(&Seq::<f32>::default(), 0, Some(false));
354+
idxrs.set_index(&Seq::<f32>::default(), 1, Some(false));
355+
let tmp = assign_gen(self as &Array, &idxrs,
356+
& $func(self as &Array, &rhs, false));
357+
mem::replace(self, tmp);
358+
}
359+
}
360+
)
361+
}
362+
363+
arith_assign_func!(AddAssign, add_assign, add);
364+
arith_assign_func!(SubAssign, sub_assign, sub);
365+
arith_assign_func!(MulAssign, mul_assign, mul);
366+
arith_assign_func!(DivAssign, div_assign, div);
367+
arith_assign_func!(RemAssign, rem_assign, rem);
368+
arith_assign_func!(ShlAssign, shl_assign, shiftl);
369+
arith_assign_func!(ShrAssign, shr_assign, shiftr);
370+
371+
macro_rules! bit_assign_func {
372+
($op_name:ident, $fn_name:ident, $func: ident) => (
373+
impl $op_name<Array> for Array {
374+
375+
#[allow(unused_variables)]
376+
fn $fn_name(&mut self, rhs: Array) {
377+
let mut idxrs = Indexer::new();
378+
idxrs.set_index(&Seq::<f32>::default(), 0, Some(false));
379+
idxrs.set_index(&Seq::<f32>::default(), 1, Some(false));
380+
let tmp = assign_gen(self as &Array, &idxrs, & $func(self as &Array, &rhs));
381+
mem::replace(self, tmp);
382+
}
383+
}
384+
)
385+
}
386+
387+
bit_assign_func!(BitAndAssign, bitand_assign, bitand);
388+
bit_assign_func!(BitOrAssign, bitor_assign, bitor);
389+
bit_assign_func!(BitXorAssign, bitxor_assign, bitxor);

0 commit comments

Comments
 (0)