rk_engine/cpp/utils/cmp_memcpy.hpp

103 lines
3.3 KiB
C++
Raw Permalink Normal View History

2023-01-03 20:50:18 +01:00
// Copyright (C) 2023 RozK
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef RK_ENGINE_CMP_MEMCPY_H
#define RK_ENGINE_CMP_MEMCPY_H
2023-01-05 03:39:32 +01:00
#include "../types.hpp"
2023-01-03 20:50:18 +01:00
2023-01-04 09:33:49 +01:00
template<typename _small>
2023-01-06 17:03:21 +01:00
[[RK_FAST]]
inline bool _rk_cmp_memcpy_small(
2023-01-04 09:33:49 +01:00
_small * __restrict dst,
_small const * __restrict src,
unsigned count) {
_small cmp = 0;
2023-01-03 20:50:18 +01:00
do {
cmp |= *dst ^ *src;
*dst++ = *src++;
} while(--count > 0);
return (cmp != 0);
}
template<typename _big, typename _small>
2023-01-06 17:03:21 +01:00
[[RK_FAST, RK_FLATTEN]]
inline bool _rk_cmp_memcpy_big(
2023-01-04 09:33:49 +01:00
_small * const __restrict _dst,
_small const * const __restrict _src,
unsigned const _count) {
unsigned const ratio = sizeof(_big) / sizeof(_small);
unsigned big_count = _count / ratio;
unsigned const small_count = _count % ratio;
2023-01-03 20:50:18 +01:00
_big * dst = reinterpret_cast<_big *>(_dst);
_big const * src = reinterpret_cast<_big const *>(_src);
_big cmp = 0;
do {
cmp |= *dst ^ *src;
*dst++ = *src++;
2023-01-04 09:33:49 +01:00
} while(--big_count > 0);
2023-01-03 20:50:18 +01:00
bool modified = (cmp != 0);
2023-01-04 09:33:49 +01:00
if (small_count) {
modified |= _rk_cmp_memcpy_small<_small>(
reinterpret_cast<_small *>(dst), reinterpret_cast<_small const *>(src), small_count);
2023-01-03 20:50:18 +01:00
}
return modified;
}
#ifdef RK_CMP_MEMCPY_UNALIGNED
2023-01-04 09:33:49 +01:00
#define _rk_count_and_alignment(_t) (count >= (sizeof(_t) / sizeof(_small)))
2023-01-03 20:50:18 +01:00
#else
2023-01-04 09:33:49 +01:00
#define _rk_count_and_alignment(_t) ((count >= (sizeof(_t) / sizeof(_small))) && !(alignment % sizeof(_t)))
2023-01-03 20:50:18 +01:00
#endif
2023-01-04 09:33:49 +01:00
template<typename _small>
2023-01-06 17:03:21 +01:00
[[RK_HOT, RK_FAST, RK_FLATTEN]]
2023-01-03 20:50:18 +01:00
bool rk_cmp_memcpy(
2023-01-04 09:33:49 +01:00
_small * const __restrict _dst,
_small const * const __restrict _src,
unsigned const count) {
2023-01-03 20:50:18 +01:00
#ifndef RK_CMP_MEMCPY_UNALIGNED
unsigned const alignment = reinterpret_cast<uintptr_t>(_dst) | reinterpret_cast<uintptr_t const>(_src);
#endif
2023-01-04 09:33:49 +01:00
if (sizeof(_small) < sizeof(rk_ullong)) {
if (_rk_count_and_alignment(rk_ullong)) {
return _rk_cmp_memcpy_big<rk_ullong, _small>(_dst, _src, count);
}
2023-01-03 20:50:18 +01:00
}
2023-01-04 09:33:49 +01:00
if (sizeof(_small) < sizeof(rk_ulong)) {
if (_rk_count_and_alignment(rk_ulong)) {
return _rk_cmp_memcpy_big<rk_ulong, _small>(_dst, _src, count);
}
2023-01-03 20:50:18 +01:00
}
2023-01-04 09:33:49 +01:00
if (sizeof(_small) < sizeof(rk_uint)) {
if (_rk_count_and_alignment(rk_uint)) {
return _rk_cmp_memcpy_big<rk_uint, _small>(_dst, _src, count);
}
2023-01-03 20:50:18 +01:00
}
2023-01-04 09:33:49 +01:00
if (sizeof(_small) < sizeof(rk_ushort)) {
if (_rk_count_and_alignment(rk_ushort)) {
return _rk_cmp_memcpy_big<rk_ushort, _small>(_dst, _src, count);
}
2023-01-03 20:50:18 +01:00
}
2023-01-04 09:33:49 +01:00
if (count) {
return _rk_cmp_memcpy_small<_small>(_dst, _src, count);
2023-01-03 20:50:18 +01:00
}
return false;
}
2023-01-04 09:33:49 +01:00
#undef _rk_count_and_alignment
2023-01-03 20:50:18 +01:00
#endif // RK_ENGINE_CMP_MEMCPY_H