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
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(feature = "unstable", feature(stdsimd))]
#![cfg_attr(feature = "unstable", feature(arm_target_feature))]
#![cfg_attr(docsrs, feature(doc_cfg))]
//
#![deny(
    missing_debug_implementations,
    clippy::all,
    clippy::cargo,
    clippy::missing_inline_in_public_items
)]
#![warn(clippy::todo)]

#[cfg(feature = "alloc")]
extern crate alloc;

#[macro_export]
macro_rules! item_group {
    ($($item:item)*) => {
        $($item)*
    }
}

#[macro_use]
pub mod tools;

#[macro_use]
pub mod traits;

pub mod arch {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    pub mod x86;

    #[cfg(all(feature = "unstable", target_arch = "arm"))]
    pub mod arm;

    #[cfg(all(feature = "unstable", target_arch = "aarch64"))]
    pub mod aarch64;

    #[cfg(target_arch = "wasm32")]
    pub mod wasm;
}

pub mod ascii;
pub mod hex;

#[macro_export]
macro_rules! simd_dispatch {
    (
        name        = $name:ident,
        signature   = $(for<$($lifetime:lifetime),+>)? fn($($arg_name: ident: $arg_type: ty),*) -> $ret:ty,
        fallback    = $fallback_fn:ident,
        simd        = $simd_fn:ident,
        safety      = {$($unsafe:ident)?},
    ) => {
        pub mod $name {
            #![allow(
                unsafe_op_in_unsafe_fn,
                clippy::missing_safety_doc,
            )]

            use super::*;

            use $crate::traits::InstructionSet;

            const _: $(for<$($lifetime),+>)? $($unsafe)? fn($($arg_type),*) -> $ret = $fallback_fn;

            #[inline]
            pub $($unsafe)? fn fallback$(<$($lifetime),+>)?($($arg_name:$arg_type),*) -> $ret {
                $fallback_fn($($arg_name),*)
            }

            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
            $crate::item_group!{
                use $crate::arch::x86::{AVX2, SSE41};

                const _: $(for<$($lifetime),+>)? $($unsafe)? fn(AVX2 $(,$arg_type)*) -> $ret = $simd_fn::<AVX2>;
                const _: $(for<$($lifetime),+>)? $($unsafe)? fn(SSE41$(,$arg_type)*) -> $ret = $simd_fn::<SSE41>;

                #[inline]
                #[target_feature(enable = "avx2")]
                pub unsafe fn avx2$(<$($lifetime),+>)?($($arg_name:$arg_type),*) -> $ret {
                    $simd_fn(AVX2::new() $(,$arg_name)*)
                }

                #[inline]
                #[target_feature(enable = "sse4.1")]
                pub unsafe fn sse41$(<$($lifetime),+>)?($($arg_name:$arg_type),*) -> $ret {
                    $simd_fn(SSE41::new() $(,$arg_name)*)
                }
            }

            #[cfg(all(feature = "unstable", any(target_arch = "arm", target_arch = "aarch64")))]
            $crate::item_group!{
                #[cfg(target_arch = "aarch64")]
                use $crate::arch::aarch64::NEON;
                #[cfg(target_arch = "arm")]
                use $crate::arch::arm::NEON;

                const _: $(for<$($lifetime),+>)? $($unsafe)? fn(NEON$(,$arg_type)*) -> $ret = $simd_fn::<NEON>;

                #[inline]
                #[target_feature(enable = "neon")]
                pub unsafe fn neon$(<$($lifetime),+>)?($($arg_name:$arg_type),*) -> $ret {
                    $simd_fn(NEON::new() $(,$arg_name)*)
                }
            }

            #[cfg(target_arch = "wasm32")]
            $crate::item_group!{
                use $crate::arch::wasm;

                const _: $(for<$($lifetime),+>)? $($unsafe)? fn(wasm::SIMD128$(,$arg_type)*) -> $ret = $simd_fn::<wasm::SIMD128>;

                #[inline]
                #[target_feature(enable = "simd128")]
                pub unsafe fn simd128$(<$($lifetime),+>)?($($arg_name:$arg_type),*) -> $ret {
                    $simd_fn(wasm::SIMD128::new() $(,$arg_name)*)
                }
            }

            #[inline(always)]
            fn resolve() -> $(for<$($lifetime),+>)? unsafe fn($($arg_type),*) -> $ret {
                #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
                if $crate::arch::x86::AVX2::is_enabled() {
                    return avx2;
                }
                #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
                if $crate::arch::x86::SSE41::is_enabled() {
                    return sse41;
                }
                #[cfg(all(feature = "unstable", target_arch = "aarch64"))]
                if $crate::arch::aarch64::NEON::is_enabled() {
                    return neon;
                }
                #[cfg(all(feature = "unstable", target_arch = "arm"))]
                if $crate::arch::arm::NEON::is_enabled() {
                    return neon;
                }
                #[cfg(target_arch = "wasm32")]
                if $crate::arch::wasm::SIMD128::is_enabled() {
                    return simd128;
                }
                $fallback_fn
            }

            use core::sync::atomic::{AtomicPtr, Ordering::Relaxed};

            static IFUNC: AtomicPtr<()> = AtomicPtr::new(init_ifunc as *mut ());

            $($unsafe)? fn init_ifunc$(<$($lifetime),+>)?($($arg_name:$arg_type),*) -> $ret {
                let f = resolve();
                IFUNC.store(f as *mut (), Relaxed);
                unsafe { f($($arg_name),*) }
            }

            #[inline(always)]
            pub $($unsafe)? fn auto_indirect$(<$($lifetime),+>)?($($arg_name:$arg_type),*) -> $ret {
                unsafe {
                    let f: unsafe fn($($arg_type),*) -> $ret = core::mem::transmute(IFUNC.load(Relaxed));
                    f($($arg_name),*)
                }
            }
        }
    }
}