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
178
179
180
181
182
183
184
185
186
187
use std::cmp::Ordering;

mod arch;
mod libc;
mod mem;

#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub struct RangeError;

pub fn memchr(buf: &[u8], c: u8) -> Option<usize> {
    #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
    let r = arch::x86::_memchr_impl(buf, c);
    //
    #[cfg(not(any(target_arch = "x86_64", target_arch = "x86",)))]
    let r = mem::_memchr_impl(buf, c);
    //
    r
}

pub fn memchr_basic(buf: &[u8], c: u8) -> Option<usize> {
    mem::_memchr_impl(buf, c)
}

pub fn memchr_libc(buf: &[u8], c: u8) -> Option<usize> {
    libc::_memchr_impl(buf, c)
}

pub fn memcmp(a: &[u8], b: &[u8]) -> Ordering {
    #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
    let r = arch::x86::_memcmp_impl(a, b);
    //
    #[cfg(target_arch = "aarch64")]
    let r = libc::_memcmp_impl(a, b);
    //
    #[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64",)))]
    let r = mem::_memcmp_impl(a, b);
    //
    r
}

pub fn memcmp_basic(a: &[u8], b: &[u8]) -> Ordering {
    mem::_memcmp_impl(a, b)
}

pub fn memcmp_libc(a: &[u8], b: &[u8]) -> Ordering {
    libc::_memcmp_impl(a, b)
}

pub fn memcpy(dst: &mut [u8], src: &[u8]) -> Result<(), RangeError> {
    #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
    let r = arch::x86::_memcpy_impl(dst, src);
    //
    #[cfg(target_arch = "arm")]
    let r = mem::_memcpy_impl(dst, src);
    //
    #[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "arm")))]
    let r = libc::_memcpy_impl(dst, src);
    //
    r
}

pub fn memcpy_basic(dst: &mut [u8], src: &[u8]) -> Result<(), RangeError> {
    mem::_memcpy_impl(dst, src)
}

pub fn memcpy_libc(dst: &mut [u8], src: &[u8]) -> Result<(), RangeError> {
    libc::_memcpy_impl(dst, src)
}

pub fn memeq(a: &[u8], b: &[u8]) -> bool {
    #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
    let r = arch::x86::_memeq_impl(a, b);
    //
    #[cfg(target_arch = "aarch64")]
    let r = libc::_memeq_impl(a, b);
    //
    #[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64",)))]
    let r = mem::_memeq_impl(a, b);
    //
    r
}

pub fn memeq_basic(a: &[u8], b: &[u8]) -> bool {
    mem::_memeq_impl(a, b)
}

pub fn memeq_libc(a: &[u8], b: &[u8]) -> bool {
    libc::_memeq_impl(a, b)
}

pub fn memmem(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
    let r = arch::x86::_memmem_impl(haystack, needle);
    //
    #[cfg(target_arch = "aarch64")]
    let r = libc::_memmem_impl(haystack, needle);
    //
    #[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64",)))]
    let r = mem::_memmem_impl(haystack, needle);
    //
    r
}

pub fn memmem_basic(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    mem::_memmem_impl(haystack, needle)
}

pub fn memmem_libc(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    libc::_memmem_impl(haystack, needle)
}

pub fn memset(buf: &mut [u8], c: u8, n: usize) -> Result<(), RangeError> {
    #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
    let r = arch::x86::_memset_impl(buf, c, n);
    //
    #[cfg(target_arch = "arm")]
    let r = mem::_memset_impl(buf, c, n);
    //
    #[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "arm")))]
    let r = libc::_memset_impl(buf, c, n);
    //
    r
}

pub fn memset_basic(buf: &mut [u8], c: u8, n: usize) -> Result<(), RangeError> {
    mem::_memset_impl(buf, c, n)
}

pub fn memset_libc(buf: &mut [u8], c: u8, n: usize) -> Result<(), RangeError> {
    libc::_memset_impl(buf, c, n)
}

/*
pub fn memmem(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    if is_x86_feature_detected!("avx") {
        unsafe { memmem_avx(haystack, needle) }
    } else if is_x86_feature_detected!("sse2") {
        unsafe {  memmem_sse2(haystack, needle) }
    } else {
        memmem_basic(haystack, needle)
    }
}

pub fn memmem_basic(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    basic::memmem_impl(haystack, needle)
}

#[target_feature(enable = "sse2")]
pub unsafe fn memmem_sse2(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    basic::memmem_impl(haystack, needle)
}

#[target_feature(enable = "avx")]
pub unsafe fn memmem_avx(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    basic::memmem_impl(haystack, needle)
}

pub fn memcpy(dst: &mut [u8], src: &[u8], n: usize) -> Result<(), RangeError> {
    if is_x86_feature_detected!("avx") {
        unsafe { memcpy_avx(dst, src, n) }
    } else if is_x86_feature_detected!("sse2") {
        unsafe {  memcpy_sse2(dst, src, n) }
    } else {
        memcpy_basic(dst, src, n)
    }
}

pub fn memcpy_basic(dst: &mut [u8], src: &[u8], n: usize) -> Result<(), RangeError> {
    basic::memcpy_impl(dst, src, n)
}

#[target_feature(enable = "sse2")]
pub unsafe fn memcpy_sse2(dst: &mut [u8], src: &[u8], n: usize) -> Result<(), RangeError> {
    basic::memcpy_impl(dst, src, n)
}

#[target_feature(enable = "avx")]
pub unsafe fn memcpy_avx(dst: &mut [u8], src: &[u8], n: usize) -> Result<(), RangeError> {
    basic::memcpy_impl(dst, src, n)
}
*/

/*
 * Refer.
 *   https://mmi.hatenablog.com/entry/2017/07/27/230005
 *   you should have memcpy(), memcmp(), memset() on nostd environments
*/