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
// Thanks to zuur#9735 (thomcc on GH) for code.
//! "Sloppy" pattern memsets.
//!
//! - A "pattern" memset is a memset where rather than taking a single byte
//! to fill the memory with, it takes a `u16`, `u32`, `u64`, ... instead.
//! This is useful, for example, if you need the bytes to alternate value
//! (or, well, match some other pattern, ...)
//!
//! The closest comparison in the Rust stdlib is probably [`slice::fill`],
//! but these are unsafe, operating on raw pointers, and more importantly,
//! these are also "sloppy".
//!
//! - "Sloppy" in this case refers to the fact that these may write up to
//! 16 bytes past the end of the `ptr`/`len` pair they are provided.
//! (Obviously this is unsafe, and must be done carefully). This is
//! sometimes desirable for performance (upwards of 10% in the decoder)
//!
//! Anyway, watch out for accidentally narrowed provenance, and be sure
//! you allocate padding/slop for this. For example:
//!
//! ```ignore
//! const SYMBOLS: usize = 32;
//!
//! struct HistFreqs {
//! // `+ 8` for use with sloppy memset.
//! counts: [u16; SYMBOLS + 8],
//! }
//! // Then
//! unsafe { sloppy_memset_pat16(some_hist_freqs.counts.as_mut_ptr()) }
//! ```
use crateis_aligned_usize;
use is_aligned;
use ;
/// Similar to `core::ptr::write_bytes(ptr, v, len)` but may
/// write up to 16 bytes past the end (in exchange for perf).
///
/// Safety:
/// - `ptr` must be valid for a write of `len + 16` (which
/// must not overflow) bytes.
///
/// Important note: this is "sloppy", and the values outside of the
/// `0..len` range (that is, the final 16 items in the "trailing
/// slop") may or may not be written to. You
/// should not use this function if that could be a problem.
pub unsafe
/// Fill `ptr..ptr.add(count)` with `val`. Note that `count`
/// is in units of `u16`, not `u8`, possibly writing no more
/// than 16 bytes past the end (to go fast).
///
/// Safety:
/// - `ptr` must be aligned to `align_of::<u16>()`.
/// - `ptr` must be valid for a write of `count * size_of::<u16>() + 16`
/// bytes (where that expression must not overflow, of course).
///
/// Important note: this is "sloppy", and the values outside of the
/// `0..count` range (that is, the final `(16 / size_of::<u16>()) == 2`
/// items in the "trailing slop") may or may not be written to. You
/// should not use this function if that could be a problem.
pub unsafe
/// Fill `ptr..ptr.add(count)` with `val`. Note that `count`
/// is in units of `u32`, not `u8`, possibly writing no more
/// than 16 bytes past the end (to go fast).
///
/// Safety:
/// - `ptr` must be aligned to `align_of::<u32>()`.
/// - `ptr` must be valid for a write of `count * size_of::<u32>() + 16`
/// bytes (where that expression must not overflow, of course).
///
/// Important note: this is "sloppy", and the values outside of the
/// `0..count` range (that is, the final `(16 / size_of::<u32>()) == 4`
/// items in the "trailing slop") may or may not be written to. You
/// should not use this function if that could be a problem.
pub unsafe
/// Fill `ptr..ptr.add(count)` with `val`. Note that `count`
/// is in units of `u64`, not `u8`, possibly writing no more
/// than 16 bytes past the end (to go fast).
///
/// Safety:
/// - `ptr` must be aligned to `align_of::<u64>()`.
/// - `ptr` must be valid for a write of `count * size_of::<u64>() + 16`
/// bytes (where that expression must not overflow, of course).
///
/// Important note: this is "sloppy", and the values outside of the
/// `0..count` range (that is, the final `(16 / size_of::<u64>()) == 2`
/// items in the "trailing slop") may or may not be written to. You
/// should not use this function if that could be a problem.
pub unsafe
unsafe
// This is where our sloppiness threshold comes from.
const _: = assert!;
const _: = assert!;