matrixmultiply/
loopmacros.rs

1// Copyright 2016 - 2018 Ulrik Sverdrup "bluss"
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9// Unroll only in non-debug builds
10
11#[cfg(not(debug_assertions))]
12macro_rules! repeat {
13    (1 $e:expr) => { $e; };
14    (2 $e:expr) => { $e;$e; };
15    (3 $e:expr) => { $e;$e; $e; };
16    (4 $e:expr) => { $e;$e; $e;$e; };
17    (5 $e:expr) => { $e;$e; $e;$e; $e; };
18    (6 $e:expr) => { $e;$e; $e;$e; $e;$e; };
19    (7 $e:expr) => { $e;$e; $e;$e; $e;$e; $e; };
20    (8 $e:expr) => { $e;$e; $e;$e; $e;$e; $e;$e; };
21}
22
23#[cfg(debug_assertions)]
24macro_rules! loop4 {
25    ($i:ident, $e:expr) => {
26        for $i in 0..4 { $e }
27    }
28}
29
30#[cfg(feature = "cgemm")]
31macro_rules! loop2 {
32    ($i:ident, $e:expr) => {{
33        let $i = 0; $e;
34        let $i = 1; $e;
35    }}
36}
37
38#[cfg(not(debug_assertions))]
39macro_rules! loop4 {
40    ($i:ident, $e:expr) => {{
41        let $i = 0; $e;
42        let $i = 1; $e;
43        let $i = 2; $e;
44        let $i = 3; $e;
45    }}
46}
47
48#[cfg(debug_assertions)]
49macro_rules! loop8 {
50    ($i:ident, $e:expr) => {
51        for $i in 0..8 { $e }
52    }
53}
54
55#[cfg(not(debug_assertions))]
56macro_rules! loop8 {
57    ($i:ident, $e:expr) => {{
58        let $i = 0; $e;
59        let $i = 1; $e;
60        let $i = 2; $e;
61        let $i = 3; $e;
62        let $i = 4; $e;
63        let $i = 5; $e;
64        let $i = 6; $e;
65        let $i = 7; $e;
66    }}
67}
68
69#[cfg(debug_assertions)]
70macro_rules! unroll_by {
71    ($by:tt => $ntimes:expr, $e:expr) => {
72        for _ in 0..$ntimes { $e }
73    }
74}
75
76#[cfg(not(debug_assertions))]
77macro_rules! unroll_by {
78    ($by:tt => $ntimes:expr, $e:expr) => {{
79        // using while loop to avoid problems
80        // with requiring inlining of foor loop parts
81        let k = $ntimes;
82        let mut _index = 0;
83        let _target = k / $by;
84        while _index < _target {
85            repeat!($by $e);
86            _index += 1;
87        }
88
89        let mut _index = 0;
90        let _target = k % $by;
91        while _index < _target {
92            $e;
93            _index += 1;
94        }
95    }}
96}
97
98#[allow(unused)]
99#[cfg(debug_assertions)]
100macro_rules! unroll_by_with_last {
101    ($by:tt => $ntimes:expr, $is_last:ident, $e:expr) => {{
102        let k = $ntimes - 1;
103        let $is_last = false;
104        for _ in 0..k {
105            $e;
106        }
107        let $is_last = true;
108        #[allow(unused_assignments)]
109        $e;
110    }}
111}
112
113#[allow(unused)]
114#[cfg(not(debug_assertions))]
115macro_rules! unroll_by_with_last {
116    ($by:tt => $ntimes:expr, $is_last:ident, $e:expr) => {{
117        let k = $ntimes - 1;
118        let $is_last = false;
119        for _ in 0..k / $by {
120            repeat!($by $e);
121        }
122        for _ in 0..k % $by {
123            $e;
124        }
125        let $is_last = true;
126        #[allow(unused_assignments)]
127        $e;
128    }}
129}