Skip to main content

rumtk_arena/mem/
copy.rs

1/*
2 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
4 *     Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 *     Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 *     This program is free software: you can redistribute it and/or modify
8 *     it under the terms of the GNU General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 *
12 *     This program is distributed in the hope that it will be useful,
13 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *     GNU General Public License for more details.
16 *
17 *     You should have received a copy of the GNU General Public License
18 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20
21use crate::cpu::CPU_SIMD_64_SIZE;
22
23#[inline]
24pub fn copy_simd_slice<'a, const LANE_SIZE: usize>(src: &[u8], mut dst: &'a mut [u8]) -> &'a mut [u8] {
25    let (prefix, middle, postfix) = src.as_simd::<LANE_SIZE>();
26    let prefix_len = prefix.len();
27    let postfix_len = postfix.len();
28
29    dst[..prefix_len].copy_from_slice(prefix);
30    dst = &mut dst[prefix_len..];
31
32    for chunk in middle.into_iter() {
33        chunk.copy_to_slice(&mut dst[..LANE_SIZE]);
34        dst = &mut dst[LANE_SIZE..];
35    }
36
37    dst[..postfix_len].copy_from_slice(postfix);
38    dst
39}
40
41#[inline]
42pub fn copy_from_slice<'a>(src: &[u8], dst: &'a mut [u8]) -> &'a mut [u8] {
43    debug_assert!(src.len() <= dst.len(), "Destination memory slice is smaller than source! This is a bug near the call site of copy_from_slice!");
44    copy_simd_slice::<CPU_SIMD_64_SIZE>(
45        src,
46        dst,
47    )
48}
49
50#[macro_export]
51macro_rules! rumtk_mem_quick_array_init {
52    ( $typ:ty, $size:expr ) => {{
53        const DATA_SLICE_LEN: usize = $size * size_of::<$typ>();
54        let arr: [$typ; $size] = unsafe { mem::transmute([0u8; DATA_SLICE_LEN]) };
55        arr
56    }};
57    ( $typ:ty, $size:expr, $default:expr ) => {{
58        let arr: [$typ; $size] = [const {$default}; $size];
59        arr
60    }};
61}