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
// Copyright 2023 Divy Srivastava <dj.srivastava23@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO(@littledivy): Compiler does a good job at auto-vectorizing `unmask_fallback` with
// -C target-cpu=native. Below is a manual implementation.
//
// #[cfg(all(target_arch = "x86_64", feature = "simd"))]
// #[inline]
// fn unmask_x86_64(payload: &mut [u8], mask: [u8; 4]) {
// #[inline]
// fn sse2(payload: &mut [u8], mask: [u8; 4]) {
// const ALIGNMENT: usize = 16;
// unsafe {
// use std::arch::x86_64::*;
//
// let len = payload.len();
// if len < ALIGNMENT {
// return unmask_fallback(payload, mask);
// }
//
// let start = len - len % ALIGNMENT;
//
// let mut aligned_mask = [0; ALIGNMENT];
//
// for j in (0..ALIGNMENT).step_by(4) {
// aligned_mask[j] = mask[j % 4];
// aligned_mask[j + 1] = mask[(j % 4) + 1];
// aligned_mask[j + 2] = mask[(j % 4) + 2];
// aligned_mask[j + 3] = mask[(j % 4) + 3];
// }
//
// let mask_m = _mm_loadu_si128(aligned_mask.as_ptr() as *const _);
//
// for index in (0..start).step_by(ALIGNMENT) {
// let ptr = payload.as_mut_ptr().add(index);
// let mut v = _mm_loadu_si128(ptr as *const _);
// v = _mm_xor_si128(v, mask_m);
// _mm_storeu_si128(ptr as *mut _, v);
// }
//
// if len != start {
// unmask_fallback(&mut payload[start..], mask);
// }
// }
// }
// #[cfg(target_feature = "sse2")]
// {
// return sse2(payload, mask);
// }
//
// #[cfg(not(target_feature = "sse2"))]
// {
// use core::mem;
// use std::sync::atomic::AtomicPtr;
// use std::sync::atomic::Ordering;
//
// type FnRaw = *mut ();
// type FnImpl = unsafe fn(&mut [u8], [u8; 4]);
//
// unsafe fn get_impl(input: &mut [u8], mask: [u8; 4]) {
// let fun = if std::is_x86_feature_detected!("sse2") {
// sse2
// } else {
// unmask_fallback
// };
// FN.store(fun as FnRaw, Ordering::Relaxed);
// (fun)(input, mask);
// }
//
// static FN: AtomicPtr<()> = AtomicPtr::new(get_impl as FnRaw);
//
// if payload.len() < 16 {
// return unmask_fallback(payload, mask);
// }
//
// let fun = FN.load(Ordering::Relaxed);
// unsafe { mem::transmute::<FnRaw, FnImpl>(fun)(payload, mask) }
// }
// }
// Faster version of `unmask_easy()` which operates on 4-byte blocks.
// https://github.com/snapview/tungstenite-rs/blob/e5efe537b87a6705467043fe44bb220ddf7c1ce8/src/protocol/frame/mask.rs#L23
//
// https://godbolt.org/z/EPTYo5jK8
/// Unmask a payload using the given 4-byte mask.