summaryrefslogtreecommitdiffstats
path: root/vendor/ppv-lite86/src/soft.rs
blob: d12dac528f16695349812081eff181a93f263670 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Implement 256- and 512- bit in terms of 128-bit, for machines without native wide SIMD.

use core::marker::PhantomData;
use core::ops::*;
use crate::types::*;
use crate::{vec128_storage, vec256_storage, vec512_storage};

#[derive(Copy, Clone, Default)]
#[allow(non_camel_case_types)]
pub struct x2<W, G>(pub [W; 2], PhantomData<G>);
impl<W, G> x2<W, G> {
    #[inline(always)]
    pub fn new(xs: [W; 2]) -> Self {
        x2(xs, PhantomData)
    }
}
macro_rules! fwd_binop_x2 {
    ($trait:ident, $fn:ident) => {
        impl<W: $trait + Copy, G> $trait for x2<W, G> {
            type Output = x2<W::Output, G>;
            #[inline(always)]
            fn $fn(self, rhs: Self) -> Self::Output {
                x2::new([self.0[0].$fn(rhs.0[0]), self.0[1].$fn(rhs.0[1])])
            }
        }
    };
}
macro_rules! fwd_binop_assign_x2 {
    ($trait:ident, $fn_assign:ident) => {
        impl<W: $trait + Copy, G> $trait for x2<W, G> {
            #[inline(always)]
            fn $fn_assign(&mut self, rhs: Self) {
                (self.0[0]).$fn_assign(rhs.0[0]);
                (self.0[1]).$fn_assign(rhs.0[1]);
            }
        }
    };
}
macro_rules! fwd_unop_x2 {
    ($fn:ident) => {
        #[inline(always)]
        fn $fn(self) -> Self {
            x2::new([self.0[0].$fn(), self.0[1].$fn()])
        }
    };
}
impl<W, G> RotateEachWord32 for x2<W, G>
where
    W: Copy + RotateEachWord32,
{
    fwd_unop_x2!(rotate_each_word_right7);
    fwd_unop_x2!(rotate_each_word_right8);
    fwd_unop_x2!(rotate_each_word_right11);
    fwd_unop_x2!(rotate_each_word_right12);
    fwd_unop_x2!(rotate_each_word_right16);
    fwd_unop_x2!(rotate_each_word_right20);
    fwd_unop_x2!(rotate_each_word_right24);
    fwd_unop_x2!(rotate_each_word_right25);
}
impl<W, G> RotateEachWord64 for x2<W, G>
where
    W: Copy + RotateEachWord64,
{
    fwd_unop_x2!(rotate_each_word_right32);
}
impl<W, G> RotateEachWord128 for x2<W, G> where W: RotateEachWord128 {}
impl<W, G> BitOps0 for x2<W, G>
where
    W: BitOps0,
    G: Copy,
{
}
impl<W, G> BitOps32 for x2<W, G>
where
    W: BitOps32 + BitOps0,
    G: Copy,
{
}
impl<W, G> BitOps64 for x2<W, G>
where
    W: BitOps64 + BitOps0,
    G: Copy,
{
}
impl<W, G> BitOps128 for x2<W, G>
where
    W: BitOps128 + BitOps0,
    G: Copy,
{
}
fwd_binop_x2!(BitAnd, bitand);
fwd_binop_x2!(BitOr, bitor);
fwd_binop_x2!(BitXor, bitxor);
fwd_binop_x2!(AndNot, andnot);
fwd_binop_assign_x2!(BitAndAssign, bitand_assign);
fwd_binop_assign_x2!(BitOrAssign, bitor_assign);
fwd_binop_assign_x2!(BitXorAssign, bitxor_assign);
impl<W, G> ArithOps for x2<W, G>
where
    W: ArithOps,
    G: Copy,
{
}
fwd_binop_x2!(Add, add);
fwd_binop_assign_x2!(AddAssign, add_assign);
impl<W: Not + Copy, G> Not for x2<W, G> {
    type Output = x2<W::Output, G>;
    #[inline(always)]
    fn not(self) -> Self::Output {
        x2::new([self.0[0].not(), self.0[1].not()])
    }
}
impl<W, G> UnsafeFrom<[W; 2]> for x2<W, G> {
    #[inline(always)]
    unsafe fn unsafe_from(xs: [W; 2]) -> Self {
        x2::new(xs)
    }
}
impl<W: Copy, G> Vec2<W> for x2<W, G> {
    #[inline(always)]
    fn extract(self, i: u32) -> W {
        self.0[i as usize]
    }
    #[inline(always)]
    fn insert(mut self, w: W, i: u32) -> Self {
        self.0[i as usize] = w;
        self
    }
}
impl<W: Copy + Store<vec128_storage>, G> Store<vec256_storage> for x2<W, G> {
    #[inline(always)]
    unsafe fn unpack(p: vec256_storage) -> Self {
        let p = p.split128();
        x2::new([W::unpack(p[0]), W::unpack(p[1])])
    }
}
impl<W, G> From<x2<W, G>> for vec256_storage
where
    W: Copy,
    vec128_storage: From<W>,
{
    #[inline(always)]
    fn from(x: x2<W, G>) -> Self {
        vec256_storage::new128([x.0[0].into(), x.0[1].into()])
    }
}
impl<W, G> Swap64 for x2<W, G>
where
    W: Swap64 + Copy,
{
    fwd_unop_x2!(swap1);
    fwd_unop_x2!(swap2);
    fwd_unop_x2!(swap4);
    fwd_unop_x2!(swap8);
    fwd_unop_x2!(swap16);
    fwd_unop_x2!(swap32);
    fwd_unop_x2!(swap64);
}
impl<W: Copy, G> MultiLane<[W; 2]> for x2<W, G> {
    #[inline(always)]
    fn to_lanes(self) -> [W; 2] {
        self.0
    }
    #[inline(always)]
    fn from_lanes(lanes: [W; 2]) -> Self {
        x2::new(lanes)
    }
}
impl<W: BSwap + Copy, G> BSwap for x2<W, G> {
    #[inline(always)]
    fn bswap(self) -> Self {
        x2::new([self.0[0].bswap(), self.0[1].bswap()])
    }
}
impl<W: StoreBytes + BSwap + Copy, G> StoreBytes for x2<W, G> {
    #[inline(always)]
    unsafe fn unsafe_read_le(input: &[u8]) -> Self {
        let input = input.split_at(16);
        x2::new([W::unsafe_read_le(input.0), W::unsafe_read_le(input.1)])
    }
    #[inline(always)]
    unsafe fn unsafe_read_be(input: &[u8]) -> Self {
        x2::unsafe_read_le(input).bswap()
    }
    #[inline(always)]
    fn write_le(self, out: &mut [u8]) {
        let out = out.split_at_mut(16);
        self.0[0].write_le(out.0);
        self.0[1].write_le(out.1);
    }
    #[inline(always)]
    fn write_be(self, out: &mut [u8]) {
        let out = out.split_at_mut(16);
        self.0[0].write_be(out.0);
        self.0[1].write_be(out.1);
    }
}

#[derive(Copy, Clone, Default)]
#[allow(non_camel_case_types)]
pub struct x4<W>(pub [W; 4]);
impl<W> x4<W> {
    #[inline(always)]
    pub fn new(xs: [W; 4]) -> Self {
        x4(xs)
    }
}
macro_rules! fwd_binop_x4 {
    ($trait:ident, $fn:ident) => {
        impl<W: $trait + Copy> $trait for x4<W> {
            type Output = x4<W::Output>;
            #[inline(always)]
            fn $fn(self, rhs: Self) -> Self::Output {
                x4([
                    self.0[0].$fn(rhs.0[0]),
                    self.0[1].$fn(rhs.0[1]),
                    self.0[2].$fn(rhs.0[2]),
                    self.0[3].$fn(rhs.0[3]),
                ])
            }
        }
    };
}
macro_rules! fwd_binop_assign_x4 {
    ($trait:ident, $fn_assign:ident) => {
        impl<W: $trait + Copy> $trait for x4<W> {
            #[inline(always)]
            fn $fn_assign(&mut self, rhs: Self) {
                self.0[0].$fn_assign(rhs.0[0]);
                self.0[1].$fn_assign(rhs.0[1]);
                self.0[2].$fn_assign(rhs.0[2]);
                self.0[3].$fn_assign(rhs.0[3]);
            }
        }
    };
}
macro_rules! fwd_unop_x4 {
    ($fn:ident) => {
        #[inline(always)]
        fn $fn(self) -> Self {
            x4([self.0[0].$fn(), self.0[1].$fn(), self.0[2].$fn(), self.0[3].$fn()])
        }
    };
}
impl<W> RotateEachWord32 for x4<W>
where
    W: Copy + RotateEachWord32,
{
    fwd_unop_x4!(rotate_each_word_right7);
    fwd_unop_x4!(rotate_each_word_right8);
    fwd_unop_x4!(rotate_each_word_right11);
    fwd_unop_x4!(rotate_each_word_right12);
    fwd_unop_x4!(rotate_each_word_right16);
    fwd_unop_x4!(rotate_each_word_right20);
    fwd_unop_x4!(rotate_each_word_right24);
    fwd_unop_x4!(rotate_each_word_right25);
}
impl<W> RotateEachWord64 for x4<W>
where
    W: Copy + RotateEachWord64,
{
    fwd_unop_x4!(rotate_each_word_right32);
}
impl<W> RotateEachWord128 for x4<W> where W: RotateEachWord128 {}
impl<W> BitOps0 for x4<W> where W: BitOps0 {}
impl<W> BitOps32 for x4<W> where W: BitOps32 + BitOps0 {}
impl<W> BitOps64 for x4<W> where W: BitOps64 + BitOps0 {}
impl<W> BitOps128 for x4<W> where W: BitOps128 + BitOps0 {}
fwd_binop_x4!(BitAnd, bitand);
fwd_binop_x4!(BitOr, bitor);
fwd_binop_x4!(BitXor, bitxor);
fwd_binop_x4!(AndNot, andnot);
fwd_binop_assign_x4!(BitAndAssign, bitand_assign);
fwd_binop_assign_x4!(BitOrAssign, bitor_assign);
fwd_binop_assign_x4!(BitXorAssign, bitxor_assign);
impl<W> ArithOps for x4<W> where W: ArithOps {}
fwd_binop_x4!(Add, add);
fwd_binop_assign_x4!(AddAssign, add_assign);
impl<W: Not + Copy> Not for x4<W> {
    type Output = x4<W::Output>;
    #[inline(always)]
    fn not(self) -> Self::Output {
        x4([
            self.0[0].not(),
            self.0[1].not(),
            self.0[2].not(),
            self.0[3].not(),
        ])
    }
}
impl<W> UnsafeFrom<[W; 4]> for x4<W> {
    #[inline(always)]
    unsafe fn unsafe_from(xs: [W; 4]) -> Self {
        x4(xs)
    }
}
impl<W: Copy> Vec4<W> for x4<W> {
    #[inline(always)]
    fn extract(self, i: u32) -> W {
        self.0[i as usize]
    }
    #[inline(always)]
    fn insert(mut self, w: W, i: u32) -> Self {
        self.0[i as usize] = w;
        self
    }
}
impl<W: Copy + Store<vec128_storage>> Store<vec512_storage> for x4<W> {
    #[inline(always)]
    unsafe fn unpack(p: vec512_storage) -> Self {
        let p = p.split128();
        x4([
            W::unpack(p[0]),
            W::unpack(p[1]),
            W::unpack(p[2]),
            W::unpack(p[3]),
        ])
    }
}
impl<W> From<x4<W>> for vec512_storage
where
    W: Copy,
    vec128_storage: From<W>,
{
    #[inline(always)]
    fn from(x: x4<W>) -> Self {
        vec512_storage::new128([x.0[0].into(), x.0[1].into(), x.0[2].into(), x.0[3].into()])
    }
}
impl<W> Swap64 for x4<W>
where
    W: Swap64 + Copy,
{
    fwd_unop_x4!(swap1);
    fwd_unop_x4!(swap2);
    fwd_unop_x4!(swap4);
    fwd_unop_x4!(swap8);
    fwd_unop_x4!(swap16);
    fwd_unop_x4!(swap32);
    fwd_unop_x4!(swap64);
}
impl<W: Copy> MultiLane<[W; 4]> for x4<W> {
    #[inline(always)]
    fn to_lanes(self) -> [W; 4] {
        self.0
    }
    #[inline(always)]
    fn from_lanes(lanes: [W; 4]) -> Self {
        x4(lanes)
    }
}
impl<W: BSwap + Copy> BSwap for x4<W> {
    #[inline(always)]
    fn bswap(self) -> Self {
        x4([
            self.0[0].bswap(),
            self.0[1].bswap(),
            self.0[2].bswap(),
            self.0[3].bswap(),
        ])
    }
}
impl<W: StoreBytes + BSwap + Copy> StoreBytes for x4<W> {
    #[inline(always)]
    unsafe fn unsafe_read_le(input: &[u8]) -> Self {
        x4([
            W::unsafe_read_le(&input[0..16]),
            W::unsafe_read_le(&input[16..32]),
            W::unsafe_read_le(&input[32..48]),
            W::unsafe_read_le(&input[48..64]),
        ])
    }
    #[inline(always)]
    unsafe fn unsafe_read_be(input: &[u8]) -> Self {
        x4::unsafe_read_le(input).bswap()
    }
    #[inline(always)]
    fn write_le(self, out: &mut [u8]) {
        self.0[0].write_le(&mut out[0..16]);
        self.0[1].write_le(&mut out[16..32]);
        self.0[2].write_le(&mut out[32..48]);
        self.0[3].write_le(&mut out[48..64]);
    }
    #[inline(always)]
    fn write_be(self, out: &mut [u8]) {
        self.0[0].write_be(&mut out[0..16]);
        self.0[1].write_be(&mut out[16..32]);
        self.0[2].write_be(&mut out[32..48]);
        self.0[3].write_be(&mut out[48..64]);
    }
}
impl<W: Copy + LaneWords4> LaneWords4 for x4<W> {
    #[inline(always)]
    fn shuffle_lane_words2301(self) -> Self {
        x4([
            self.0[0].shuffle_lane_words2301(),
            self.0[1].shuffle_lane_words2301(),
            self.0[2].shuffle_lane_words2301(),
            self.0[3].shuffle_lane_words2301(),
        ])
    }
    #[inline(always)]
    fn shuffle_lane_words1230(self) -> Self {
        x4([
            self.0[0].shuffle_lane_words1230(),
            self.0[1].shuffle_lane_words1230(),
            self.0[2].shuffle_lane_words1230(),
            self.0[3].shuffle_lane_words1230(),
        ])
    }
    #[inline(always)]
    fn shuffle_lane_words3012(self) -> Self {
        x4([
            self.0[0].shuffle_lane_words3012(),
            self.0[1].shuffle_lane_words3012(),
            self.0[2].shuffle_lane_words3012(),
            self.0[3].shuffle_lane_words3012(),
        ])
    }
}