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
use core::fmt;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use crate::traits::{SIMD128, SIMD256};
pub struct OutBuf<'a, T> {
base: *mut T,
len: usize,
_marker: PhantomData<&'a mut [MaybeUninit<T>]>,
}
unsafe impl<'a, T: Send> Send for OutBuf<'a, T> {}
unsafe impl<'a, T: Sync> Sync for OutBuf<'a, T> {}
impl<'a, T> OutBuf<'a, T> {
#[inline]
pub unsafe fn new(base: *mut T, len: usize) -> Self {
Self {
base,
len,
_marker: PhantomData,
}
}
#[inline]
pub fn from_slice_mut(slice: &'a mut [T]) -> Self {
let (base, len) = (slice.as_mut_ptr(), slice.len());
unsafe { Self::new(base, len) }
}
#[inline]
pub fn from_uninit_mut(slice: &'a mut [MaybeUninit<T>]) -> Self {
let (base, len) = (slice.as_mut_ptr(), slice.len());
unsafe { Self::new(base.cast(), 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 T {
self.base
}
}
impl<T> fmt::Debug for OutBuf<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OutBuf")
.field("base", &self.base)
.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")]
pub unsafe fn alloc_uninit_bytes(len: usize) -> Box<[MaybeUninit<u8>]> {
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))
}