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
use crate::traits::{SIMD128, SIMD256};
use core::fmt;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::ptr::NonNull;
pub struct OutBuf<'a> {
data: NonNull<MaybeUninit<u8>>,
len: usize,
_marker: PhantomData<&'a mut [MaybeUninit<u8>]>,
}
unsafe impl<'a> Send for OutBuf<'a> {}
unsafe impl<'a> Sync for OutBuf<'a> {}
impl<'a> OutBuf<'a> {
#[inline]
pub unsafe fn from_raw(data: *mut MaybeUninit<u8>, len: usize) -> Self {
Self {
data: NonNull::new_unchecked(data),
len,
_marker: PhantomData,
}
}
#[inline]
pub fn new(slice: &'a mut [u8]) -> Self {
let (data, len): _ = (slice.as_mut_ptr(), slice.len());
unsafe { Self::from_raw(data.cast(), len) }
}
#[inline]
pub fn uninit(slice: &'a mut [MaybeUninit<u8>]) -> Self {
let (data, len): _ = (slice.as_mut_ptr(), slice.len());
unsafe { Self::from_raw(data, len) }
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len == 0
}
#[inline(always)]
pub fn len(&self) -> usize {
self.len
}
#[inline(always)]
pub fn as_mut_ptr(&self) -> *mut u8 {
self.data.as_ptr().cast()
}
}
impl<'a> From<&'a mut [u8]> for OutBuf<'a> {
#[inline(always)]
fn from(slice: &'a mut [u8]) -> Self {
Self::new(slice)
}
}
impl<'a> From<&'a mut [MaybeUninit<u8>]> for OutBuf<'a> {
#[inline(always)]
fn from(slice: &'a mut [MaybeUninit<u8>]) -> Self {
Self::uninit(slice)
}
}
impl fmt::Debug for OutBuf<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OutBuf")
.field("data", &self.data)
.field("len", &self.len)
.finish()
}
}
#[derive(Debug)]
#[repr(C, align(16))]
pub struct Bytes16(pub [u8; 16]);
#[derive(Debug)]
#[repr(C, align(32))]
pub struct Bytes32(pub [u8; 32]);
pub trait Load<T> {
type Output;
fn load(self, src: T) -> Self::Output;
}
impl<S: SIMD128> Load<&'_ Bytes16> for S {
type Output = S::V128;
#[inline(always)]
fn load(self, src: &'_ Bytes16) -> Self::Output {
unsafe { self.v128_load(src.0.as_ptr()) }
}
}
impl<S: SIMD256> Load<&'_ Bytes32> for S {
type Output = S::V256;
#[inline(always)]
fn load(self, src: &'_ Bytes32) -> Self::Output {
unsafe { self.v256_load(src.0.as_ptr()) }
}
}
#[allow(unused_macros)]
macro_rules! debug_assert_ptr_align {
($ptr:expr, $align:literal) => {{
let align: usize = $align;
let ptr = $ptr as *const _ as *const ();
let addr = ptr as usize;
debug_assert!(addr % align == 0)
}};
}
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
#[inline]
pub unsafe fn alloc_uninit_bytes(len: usize) -> Box<[MaybeUninit<u8>]> {
#[cfg(any(debug_assertions, miri))]
{
assert!(len > 0 && len <= (isize::MAX as usize))
}
use alloc::alloc::{alloc, handle_alloc_error, Layout};
use core::slice;
let layout = Layout::from_size_align_unchecked(len, 1);
let p = alloc(layout);
if p.is_null() {
handle_alloc_error(layout)
}
let ptr = p.cast();
Box::from_raw(slice::from_raw_parts_mut(ptr, len))
}