1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Runtime detection of x86 and x86_64 capabilities.

use avx::AvxF32;
use sse42::{Sse42F32, Sse42F32x4};
use combinators::{SimdFnF32, ThunkF32, ThunkF32x4};
use traits::{SimdF32, F32x4};

pub trait GeneratorF32: Sized {
    type IterF32: Iterator<Item=f32>;
    type IterSse42: Iterator<Item=Sse42F32>;
    type IterAvx: Iterator<Item=AvxF32>;
    fn gen_f32(self, cap: f32) -> Self::IterF32;
    fn gen_sse42(self, cap: Sse42F32) -> Self::IterSse42;
    fn gen_avx(self, cap: AvxF32) -> Self::IterAvx;

    #[inline]
    fn map<F>(self, f: F) -> F32Map<Self, F>
        where Self: Sized, F: SimdFnF32
    {
        F32Map { inner: self, f }
    }

    #[inline]
    fn collect(self, obuf: &mut [f32]) {
        if is_x86_feature_detected!("avx") {
            unsafe { collect_avx(self, obuf); }
        } else if is_x86_feature_detected!("sse4.2") {
            unsafe { collect_sse42(self, obuf); }
        } else {
            let mut iter = self.gen_f32(0.0);
            for i in (0..obuf.len()).step_by(1) {
                let x = iter.next().unwrap();
                x.write_to_slice(&mut obuf[i..]);
            }
        }
    }
}

#[target_feature(enable = "avx")]
unsafe fn collect_avx<G: GeneratorF32>(gen: G, obuf: &mut [f32]) {
    let mut iter = gen.gen_avx(AvxF32::create());
    for i in (0..obuf.len()).step_by(8) {
        let x = iter.next().unwrap();
        x.write_to_slice(&mut obuf[i..]);
    }
}

#[target_feature(enable = "sse4.2")]
unsafe fn collect_sse42<G: GeneratorF32>(gen: G, obuf: &mut [f32]) {
    let mut iter = gen.gen_sse42(Sse42F32::create());
    for i in (0..obuf.len()).step_by(4) {
        let x = iter.next().unwrap();
        x.write_to_slice(&mut obuf[i..]);
    }
}

pub struct F32Map<G: GeneratorF32, F: SimdFnF32> {
    inner: G,
    f: F,
}

pub struct F32MapIter<S: SimdF32, I: Iterator<Item = S>, F: SimdFnF32> {
    inner: I,
    f: F,
}

impl<S, I, F> Iterator for F32MapIter<S, I, F>
    where S: SimdF32, I: Iterator<Item = S>, F: SimdFnF32
{
    type Item = S;
    fn next(&mut self) -> Option<S> {
        self.inner.next().map(|x| self.f.call(x))
    }
}

impl<G: GeneratorF32, F: SimdFnF32> GeneratorF32 for F32Map<G, F> {
    type IterF32 = F32MapIter<f32, G::IterF32, F>;
    type IterSse42 = F32MapIter<Sse42F32, G::IterSse42, F>;
    type IterAvx = F32MapIter<AvxF32, G::IterAvx, F>;
    fn gen_f32(self, cap: f32) -> Self::IterF32 {
        F32MapIter { inner: self.inner.gen_f32(cap), f: self.f }
    }
    fn gen_sse42(self, cap: Sse42F32) -> Self::IterSse42 {
        F32MapIter { inner: self.inner.gen_sse42(cap), f: self.f }
    }
    fn gen_avx(self, cap: AvxF32) -> Self::IterAvx {
        F32MapIter { inner: self.inner.gen_avx(cap), f: self.f }
    }
}


pub struct CountGen {
    init: f32,
    step: f32,
}

pub struct CountStream<S: SimdF32> {
    val: S,
    step: f32,
}

#[inline]
pub fn count(init: f32, step: f32) -> CountGen {
    CountGen { init, step }
}

impl CountGen {
    #[inline]
    fn gen<S: SimdF32>(self, cap: S) -> CountStream<S> {
        CountStream {
            val: cap.steps() * self.step + self.init,
            step: self.step * (cap.width() as f32),
        }        
    }
}

// Note that the following is 100% boilerplate and could be easily
// generated by macro.
//
// Also parametrized `gen` could be a trait item when rust #44265
// lands.
impl GeneratorF32 for CountGen {
    type IterF32 = CountStream<f32>;
    type IterSse42 = CountStream<Sse42F32>;
    type IterAvx = CountStream<AvxF32>;
    #[inline]
    fn gen_f32(self, cap: f32) -> CountStream<f32> {
        self.gen(cap)
    }
    #[inline]
    fn gen_sse42(self, cap: Sse42F32) -> CountStream<Sse42F32> {
        self.gen(cap)
    }
    #[inline]
    fn gen_avx(self, cap: AvxF32) -> CountStream<AvxF32> {
        self.gen(cap)
    }
}

impl<S: SimdF32> Iterator for CountStream<S> {
    type Item = S;
    #[inline]
    fn next(&mut self) -> Option<S> {
        let val = self.val;
        self.val = self.val + self.step;
        Some(val)
    }
}

// x86 thunk runner for SimdF32

#[target_feature(enable = "avx")]
unsafe fn run_f32_avx<S: ThunkF32>(thunk: S) {
    thunk.call(AvxF32::create());
}

#[target_feature(enable = "sse4.2")]
unsafe fn run_f32_sse42<S: ThunkF32>(thunk: S) {
    thunk.call(Sse42F32::create());
}

pub fn run_f32<S: ThunkF32>(thunk: S) {
    if is_x86_feature_detected!("avx") {
        unsafe { run_f32_avx(thunk); }
    } else if is_x86_feature_detected!("sse4.2") {
        unsafe { run_f32_sse42(thunk); }
    } else {
        thunk.call(0.0f32);
    }
}

// x86 thunk runner for F32x4

#[target_feature(enable = "sse4.2")]
unsafe fn run_f32x4_sse42<S: ThunkF32x4>(thunk: S) {
    thunk.call(Sse42F32x4::create());
}

pub fn run_f32x4<S: ThunkF32x4>(thunk: S) {
    if is_x86_feature_detected!("sse4.2") {
        unsafe { run_f32x4_sse42(thunk); }
    }
}