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
28use core::ops::{Add, BitAnd, BitOr, BitXor, Shl, Shr, Sub};
29
30#[derive(Clone, Copy, PartialEq, Eq)]
31#[allow(non_camel_case_types)]
32pub struct u32x4(pub u32, pub u32, pub u32, pub u32);
33
34impl Add for u32x4 {
35    type Output = u32x4;
36
37    fn add(self, rhs: u32x4) -> u32x4 {
38        u32x4(
39            self.0.wrapping_add(rhs.0),
40            self.1.wrapping_add(rhs.1),
41            self.2.wrapping_add(rhs.2),
42            self.3.wrapping_add(rhs.3),
43        )
44    }
45}
46
47impl Sub for u32x4 {
48    type Output = u32x4;
49
50    fn sub(self, rhs: u32x4) -> u32x4 {
51        u32x4(
52            self.0.wrapping_sub(rhs.0),
53            self.1.wrapping_sub(rhs.1),
54            self.2.wrapping_sub(rhs.2),
55            self.3.wrapping_sub(rhs.3),
56        )
57    }
58}
59
60impl BitAnd for u32x4 {
61    type Output = u32x4;
62
63    fn bitand(self, rhs: u32x4) -> u32x4 {
64        u32x4(
65            self.0 & rhs.0,
66            self.1 & rhs.1,
67            self.2 & rhs.2,
68            self.3 & rhs.3,
69        )
70    }
71}
72
73impl BitOr for u32x4 {
74    type Output = u32x4;
75
76    fn bitor(self, rhs: u32x4) -> u32x4 {
77        u32x4(
78            self.0 | rhs.0,
79            self.1 | rhs.1,
80            self.2 | rhs.2,
81            self.3 | rhs.3,
82        )
83    }
84}
85
86impl BitXor for u32x4 {
87    type Output = u32x4;
88
89    fn bitxor(self, rhs: u32x4) -> u32x4 {
90        u32x4(
91            self.0 ^ rhs.0,
92            self.1 ^ rhs.1,
93            self.2 ^ rhs.2,
94            self.3 ^ rhs.3,
95        )
96    }
97}
98
99impl Shl<usize> for u32x4 {
100    type Output = u32x4;
101
102    fn shl(self, amt: usize) -> u32x4 {
103        u32x4(self.0 << amt, self.1 << amt, self.2 << amt, self.3 << amt)
104    }
105}
106
107impl Shl<u32x4> for u32x4 {
108    type Output = u32x4;
109
110    fn shl(self, rhs: u32x4) -> u32x4 {
111        u32x4(
112            self.0 << rhs.0,
113            self.1 << rhs.1,
114            self.2 << rhs.2,
115            self.3 << rhs.3,
116        )
117    }
118}
119
120impl Shr<usize> for u32x4 {
121    type Output = u32x4;
122
123    fn shr(self, amt: usize) -> u32x4 {
124        u32x4(self.0 >> amt, self.1 >> amt, self.2 >> amt, self.3 >> amt)
125    }
126}
127
128impl Shr<u32x4> for u32x4 {
129    type Output = u32x4;
130
131    fn shr(self, rhs: u32x4) -> u32x4 {
132        u32x4(
133            self.0 >> rhs.0,
134            self.1 >> rhs.1,
135            self.2 >> rhs.2,
136            self.3 >> rhs.3,
137        )
138    }
139}
140
141#[derive(Clone, Copy)]
142#[allow(non_camel_case_types)]
143pub struct u64x2(pub u64, pub u64);
144
145impl Add for u64x2 {
146    type Output = u64x2;
147
148    fn add(self, rhs: u64x2) -> u64x2 {
149        u64x2(self.0.wrapping_add(rhs.0), self.1.wrapping_add(rhs.1))
150    }
151}