sha1_smol/
simd.rs

1// Copyright (c) 2006-2009 Graydon Hoare
2// Copyright (c) 2009-2013 Mozilla Foundation
3
4// Permission is hereby granted, free of charge, to any
5// person obtaining a copy of this software and associated
6// documentation files (the "Software"), to deal in the
7// Software without restriction, including without
8// limitation the rights to use, copy, modify, merge,
9// publish, distribute, sublicense, and/or sell copies of
10// the Software, and to permit persons to whom the Software
11// is furnished to do so, subject to the following
12// conditions:
13
14// The above copyright notice and this permission notice
15// shall be included in all copies or substantial portions
16// of the Software.
17
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
19// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
20// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
21// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
22// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
25// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26// DEALINGS IN THE SOFTWARE.
27
28pub use self::fake::*;
29
30pub trait SimdExt {
31    fn simd_eq(self, rhs: Self) -> Self;
32}
33
34impl SimdExt for fake::u32x4 {
35    fn simd_eq(self, rhs: Self) -> Self {
36        if self == rhs {
37            fake::u32x4(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff)
38        } else {
39            fake::u32x4(0, 0, 0, 0)
40        }
41    }
42}
43
44mod fake {
45    use core::ops::{Add, BitAnd, BitOr, BitXor, Shl, Shr, Sub};
46
47    #[derive(Clone, Copy, PartialEq, Eq)]
48    #[allow(non_camel_case_types)]
49    pub struct u32x4(pub u32, pub u32, pub u32, pub u32);
50
51    impl Add for u32x4 {
52        type Output = u32x4;
53
54        fn add(self, rhs: u32x4) -> u32x4 {
55            u32x4(
56                self.0.wrapping_add(rhs.0),
57                self.1.wrapping_add(rhs.1),
58                self.2.wrapping_add(rhs.2),
59                self.3.wrapping_add(rhs.3),
60            )
61        }
62    }
63
64    impl Sub for u32x4 {
65        type Output = u32x4;
66
67        fn sub(self, rhs: u32x4) -> u32x4 {
68            u32x4(
69                self.0.wrapping_sub(rhs.0),
70                self.1.wrapping_sub(rhs.1),
71                self.2.wrapping_sub(rhs.2),
72                self.3.wrapping_sub(rhs.3),
73            )
74        }
75    }
76
77    impl BitAnd for u32x4 {
78        type Output = u32x4;
79
80        fn bitand(self, rhs: u32x4) -> u32x4 {
81            u32x4(
82                self.0 & rhs.0,
83                self.1 & rhs.1,
84                self.2 & rhs.2,
85                self.3 & rhs.3,
86            )
87        }
88    }
89
90    impl BitOr for u32x4 {
91        type Output = u32x4;
92
93        fn bitor(self, rhs: u32x4) -> u32x4 {
94            u32x4(
95                self.0 | rhs.0,
96                self.1 | rhs.1,
97                self.2 | rhs.2,
98                self.3 | rhs.3,
99            )
100        }
101    }
102
103    impl BitXor for u32x4 {
104        type Output = u32x4;
105
106        fn bitxor(self, rhs: u32x4) -> u32x4 {
107            u32x4(
108                self.0 ^ rhs.0,
109                self.1 ^ rhs.1,
110                self.2 ^ rhs.2,
111                self.3 ^ rhs.3,
112            )
113        }
114    }
115
116    impl Shl<usize> for u32x4 {
117        type Output = u32x4;
118
119        fn shl(self, amt: usize) -> u32x4 {
120            u32x4(self.0 << amt, self.1 << amt, self.2 << amt, self.3 << amt)
121        }
122    }
123
124    impl Shl<u32x4> for u32x4 {
125        type Output = u32x4;
126
127        fn shl(self, rhs: u32x4) -> u32x4 {
128            u32x4(
129                self.0 << rhs.0,
130                self.1 << rhs.1,
131                self.2 << rhs.2,
132                self.3 << rhs.3,
133            )
134        }
135    }
136
137    impl Shr<usize> for u32x4 {
138        type Output = u32x4;
139
140        fn shr(self, amt: usize) -> u32x4 {
141            u32x4(self.0 >> amt, self.1 >> amt, self.2 >> amt, self.3 >> amt)
142        }
143    }
144
145    impl Shr<u32x4> for u32x4 {
146        type Output = u32x4;
147
148        fn shr(self, rhs: u32x4) -> u32x4 {
149            u32x4(
150                self.0 >> rhs.0,
151                self.1 >> rhs.1,
152                self.2 >> rhs.2,
153                self.3 >> rhs.3,
154            )
155        }
156    }
157
158    #[derive(Clone, Copy)]
159    #[allow(non_camel_case_types)]
160    pub struct u64x2(pub u64, pub u64);
161
162    impl Add for u64x2 {
163        type Output = u64x2;
164
165        fn add(self, rhs: u64x2) -> u64x2 {
166            u64x2(self.0.wrapping_add(rhs.0), self.1.wrapping_add(rhs.1))
167        }
168    }
169}