Grok 10.0.3
copy-inl.h
Go to the documentation of this file.
1// Copyright 2022 Google LLC
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16// Per-target include guard
17#if defined(HIGHWAY_HWY_CONTRIB_ALGO_COPY_INL_H_) == \
18 defined(HWY_TARGET_TOGGLE)
19#ifdef HIGHWAY_HWY_CONTRIB_ALGO_COPY_INL_H_
20#undef HIGHWAY_HWY_CONTRIB_ALGO_COPY_INL_H_
21#else
22#define HIGHWAY_HWY_CONTRIB_ALGO_COPY_INL_H_
23#endif
24
25#include <string.h> // memcpy
26
27#include "hwy/highway.h"
28
30namespace hwy {
31namespace HWY_NAMESPACE {
32
33// These functions avoid having to write a loop plus remainder handling in the
34// (unfortunately still common) case where arrays are not aligned/padded. If the
35// inputs are known to be aligned/padded, it is more efficient to write a single
36// loop using Load(). We do not provide a CopyAlignedPadded because it
37// would be more verbose than such a loop.
38
39// Fills `to`[0, `count`) with `value`.
40template <class D, typename T = TFromD<D>>
41void Fill(D d, T value, size_t count, T* HWY_RESTRICT to) {
42 const size_t N = Lanes(d);
43 const Vec<D> v = Set(d, value);
44
45 size_t idx = 0;
46 for (; idx + N <= count; idx += N) {
47 StoreU(v, d, to + idx);
48 }
49
50 // `count` was a multiple of the vector length `N`: already done.
51 if (HWY_UNLIKELY(idx == count)) return;
52
53 const size_t remaining = count - idx;
54 HWY_DASSERT(0 != remaining && remaining < N);
55 SafeFillN(remaining, value, d, to + idx);
56}
57
58// Copies `from`[0, `count`) to `to`, which must not overlap `from`.
59template <class D, typename T = TFromD<D>>
60void Copy(D d, const T* HWY_RESTRICT from, size_t count, T* HWY_RESTRICT to) {
61 const size_t N = Lanes(d);
62
63 size_t idx = 0;
64 for (; idx + N <= count; idx += N) {
65 const Vec<D> v = LoadU(d, from + idx);
66 StoreU(v, d, to + idx);
67 }
68
69 // `count` was a multiple of the vector length `N`: already done.
70 if (HWY_UNLIKELY(idx == count)) return;
71
72 const size_t remaining = count - idx;
73 HWY_DASSERT(0 != remaining && remaining < N);
74 SafeCopyN(remaining, d, from + idx, to + idx);
75}
76
77// For idx in [0, count) in ascending order, appends `from[idx]` to `to` if the
78// corresponding mask element of `func(d, v)` is true. Returns the STL-style end
79// of the newly written elements in `to`.
80//
81// `func` is either a functor with a templated operator()(d, v) returning a
82// mask, or a generic lambda if using C++14. Due to apparent limitations of
83// Clang on Windows, it is currently necessary to add HWY_ATTR before the
84// opening { of the lambda to avoid errors about "function .. requires target".
85//
86// NOTE: this is only supported for 16-, 32- or 64-bit types.
87// NOTE: Func may be called a second time for elements it has already seen, but
88// these elements will not be written to `to` again.
89template <class D, class Func, typename T = TFromD<D>>
90T* CopyIf(D d, const T* HWY_RESTRICT from, size_t count, T* HWY_RESTRICT to,
91 const Func& func) {
92 const size_t N = Lanes(d);
93
94 size_t idx = 0;
95 for (; idx + N <= count; idx += N) {
96 const Vec<D> v = LoadU(d, from + idx);
97 to += CompressBlendedStore(v, func(d, v), d, to);
98 }
99
100 // `count` was a multiple of the vector length `N`: already done.
101 if (HWY_UNLIKELY(idx == count)) return to;
102
103#if HWY_MEM_OPS_MIGHT_FAULT
104 // Proceed one by one.
105 const CappedTag<T, 1> d1;
106 for (; idx < count; ++idx) {
107 using V1 = Vec<decltype(d1)>;
108 // Workaround for -Waggressive-loop-optimizations on GCC 8
109 // (iteration 2305843009213693951 invokes undefined behavior for T=i64)
110 const uintptr_t addr = reinterpret_cast<uintptr_t>(from);
111 const T* HWY_RESTRICT from_idx =
112 reinterpret_cast<const T * HWY_RESTRICT>(addr + (idx * sizeof(T)));
113 const V1 v = LoadU(d1, from_idx);
114 // Avoid storing to `to` unless we know it should be kept - otherwise, we
115 // might overrun the end if it was allocated for the exact count.
116 if (CountTrue(d1, func(d1, v)) == 0) continue;
117 StoreU(v, d1, to);
118 to += 1;
119 }
120#else
121 // Start index of the last unaligned whole vector, ending at the array end.
122 const size_t last = count - N;
123 // Number of elements before `from` or already written.
124 const size_t invalid = idx - last;
125 HWY_DASSERT(0 != invalid && invalid < N);
126 const Mask<D> mask = Not(FirstN(d, invalid));
127 const Vec<D> v = MaskedLoad(mask, d, from + last);
128 to += CompressBlendedStore(v, And(mask, func(d, v)), d, to);
129#endif
130 return to;
131}
132
133// NOLINTNEXTLINE(google-readability-namespace-comments)
134} // namespace HWY_NAMESPACE
135} // namespace hwy
137
138#endif // HIGHWAY_HWY_CONTRIB_ALGO_COPY_INL_H_
#define HWY_RESTRICT
Definition: base.h:61
#define HWY_DASSERT(condition)
Definition: base.h:191
#define HWY_UNLIKELY(expr)
Definition: base.h:67
HWY_AFTER_NAMESPACE()
HWY_BEFORE_NAMESPACE()
d
Definition: rvv-inl.h:1742
T * CopyIf(D d, const T *HWY_RESTRICT from, size_t count, T *HWY_RESTRICT to, const Func &func)
Definition: copy-inl.h:90
HWY_API Mask128< T, N > FirstN(const Simd< T, N, 0 > d, size_t num)
Definition: arm_neon-inl.h:2409
HWY_API Vec128< T, N > And(const Vec128< T, N > a, const Vec128< T, N > b)
Definition: arm_neon-inl.h:1934
HWY_API Vec128< T > Not(const Vec128< T > v)
Definition: arm_neon-inl.h:1916
typename detail::CappedTagChecker< T, kLimit >::type CappedTag
Definition: ops/shared-inl.h:172
HWY_API Vec128< T, N > MaskedLoad(Mask128< T, N > m, Simd< T, N, 0 > d, const T *HWY_RESTRICT aligned)
Definition: arm_neon-inl.h:2711
HWY_API size_t CountTrue(Full128< T >, const Mask128< T > mask)
Definition: arm_neon-inl.h:5269
HWY_API constexpr size_t Lanes(Simd< T, N, kPow2 >)
Definition: arm_sve-inl.h:236
HWY_API void StoreU(const Vec128< uint8_t > v, Full128< uint8_t >, uint8_t *HWY_RESTRICT unaligned)
Definition: arm_neon-inl.h:2725
svuint16_t Set(Simd< bfloat16_t, N, kPow2 > d, bfloat16_t arg)
Definition: arm_sve-inl.h:312
HWY_API Vec128< uint8_t > LoadU(Full128< uint8_t >, const uint8_t *HWY_RESTRICT unaligned)
Definition: arm_neon-inl.h:2544
void Fill(D d, T value, size_t count, T *HWY_RESTRICT to)
Definition: copy-inl.h:41
HWY_API size_t CompressBlendedStore(Vec128< T, N > v, Mask128< T, N > m, Simd< T, N, 0 > d, T *HWY_RESTRICT unaligned)
Definition: arm_neon-inl.h:5846
void Copy(D d, const T *HWY_RESTRICT from, size_t count, T *HWY_RESTRICT to)
Definition: copy-inl.h:60
HWY_API void SafeFillN(const size_t num, const T value, D d, T *HWY_RESTRICT to)
Definition: generic_ops-inl.h:88
HWY_API void SafeCopyN(const size_t num, D d, const T *HWY_RESTRICT from, T *HWY_RESTRICT to)
Definition: generic_ops-inl.h:103
decltype(MaskFromVec(Zero(D()))) Mask
Definition: generic_ops-inl.h:38
N
Definition: rvv-inl.h:1742
const vfloat64m1_t v
Definition: rvv-inl.h:1742
decltype(Zero(D())) Vec
Definition: generic_ops-inl.h:32
Definition: aligned_allocator.h:27
FuncOutput(*)(const void *, FuncInput) Func
Definition: nanobenchmark.h:105
#define HWY_NAMESPACE
Definition: set_macros-inl.h:82