8#include <native/targets.h>
22export import native.simd;
23#if defined(__x86_64__) || defined(_M_X64)
24import native.x86.memory;
27export namespace native {
29struct simd_traits<jam::ptr<T>> {
using storage_type = std::uint32_t; };
32namespace jam::detail {
35template<
class T,
class Raw,
class Self>
36struct alignas(Raw) ptr_storage {
37 using value_type = ptr<T>;
38 static constexpr std::size_t lanes = Raw::lanes;
40 std::array<value_type, lanes> values_{};
42 [[nodiscard]] hint_inline
constexpr value_type
const & operator[](std::size_t i)
const noexcept {
return values_[i]; }
43 [[nodiscard]] hint_inline
constexpr value_type & operator[](std::size_t i)
noexcept {
return values_[i]; }
45 constexpr ptr_storage() noexcept = default;
46 hint_inline constexpr ptr_storage(ptr_storage const & other) noexcept {
assign(other); }
47 hint_inline
constexpr ptr_storage & operator=(ptr_storage
const & other)
noexcept {
assign(other);
return *
this; }
48 hint_inline
constexpr void unsafe_assign(ptr_storage
const & other)
noexcept {
49 for (std::size_t i = 0; i != lanes; ++i) values_[i].
unsafe_assign(other.values_[i]);
51 hint_inline
constexpr void remember() noexcept {
52 if !
consteval {
if (
auto * h = heap::current()) h->remember(std::span<value_type>{values_}); }
54 hint_inline
constexpr void assign(ptr_storage
const & other)
noexcept {
unsafe_assign(other); remember(); }
56 hint_inline
constexpr ptr_storage(value_type
const & value)
noexcept {
57 for (
auto & lane : values_) lane.unsafe_assign(value);
61 hint_inline
constexpr explicit ptr_storage(std::array<value_type, lanes>
const & values)
noexcept
63 for (std::size_t i = 0; i != lanes; ++i) values_[i].
unsafe_assign(values[i]);
67 hint_inline
constexpr explicit ptr_storage(Raw
const & raw)
noexcept {
68 std::array<std::uint32_t, lanes> offsets;
69 std::memcpy(offsets.data(), &raw,
sizeof(offsets));
70 for (std::size_t i = 0; i != lanes; ++i) values_[i].
unsafe_assign(offsets[i]);
74 template<std::
size_t Alignment = 1>
75 [[nodiscard]]
static hint_inline
constexpr Self load_memory(value_type
const * p)
noexcept {
76 std::array<value_type, lanes> values;
77 for (std::size_t i = 0; i != lanes; ++i) values[i].
unsafe_assign(p[i]);
81 template<std::
size_t Alignment = 1>
82 hint_inline
constexpr void store_memory(value_type * p)
const noexcept {
83 for (std::size_t i = 0; i != lanes; ++i) p[i].
unsafe_assign(values_[i]);
84 if !
consteval {
if (
auto * h = heap::current()) h->remember(std::span<value_type>{p, lanes}); }
87 [[nodiscard]]
static hint_inline
constexpr Self load(value_type
const * p)
noexcept {
return load_memory(p); }
89 hint_inline
constexpr void store(value_type * p)
const noexcept { store_memory(p); }
92#if defined(__x86_64__) || defined(_M_X64)
93#define JAM_PTR_TARGETS avx512, avx2, scalar
95#define JAM_PTR_TARGETS neon, scalar
98#define JAM_PTR_SIMD(name, ISA, ...) \
99export namespace native { \
104template<class T, class Raw, class Self> \
105 requires (native::target<Raw::architecture, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
106struct simd_customization<jam::ptr<T>, Raw, Self> : jam::detail::ptr_storage<T, Raw, Self> { \
107 using base = jam::detail::ptr_storage<T, Raw, Self>; \
110 using raw_type = Raw; \
111 using mask_type = typename Raw::mask_type; \
113 using base::values_; \
116 [[nodiscard]] hint_inline constexpr Raw to_native() const noexcept { \
117 std::array<std::uint32_t, lanes> offsets; \
118 for (std::size_t i = 0; i != lanes; ++i) offsets[i] = values_[i].get(); \
119 return Raw::load(offsets.data()); \
122 [[nodiscard]] friend hint_inline constexpr mask_type operator==(Self const & a, Self const & b) noexcept { \
123 if constexpr (requires(Raw x) { x == x; }) return a.to_native() == b.to_native(); \
125 std::uint64_t bits = 0; \
126 for (std::size_t i = 0; i != lanes; ++i) bits |= std::uint64_t(a[i] == b[i]) << i; \
127 return mask_type::from_bitset(bits); \
131 [[nodiscard]] friend hint_inline constexpr mask_type operator!=(Self const & a, Self const & b) noexcept { \
132 if constexpr (requires(Raw x) { x != x; }) return a.to_native() != b.to_native(); \
134 std::uint64_t bits = 0; \
135 for (std::size_t i = 0; i != lanes; ++i) bits |= std::uint64_t(a[i] != b[i]) << i; \
136 return mask_type::from_bitset(bits); \
140 [[nodiscard]] friend hint_inline constexpr mask_type operator!=(Self const & a, std::nullptr_t) noexcept { \
141 if constexpr (requires(Raw x) { x != x; }) return a.to_native() != Raw{std::uint32_t{0}}; \
143 std::uint64_t bits = 0; \
144 for (std::size_t i = 0; i != lanes; ++i) bits |= std::uint64_t(bool(a[i])) << i; \
145 return mask_type::from_bitset(bits); \
149 [[nodiscard]] friend hint_inline constexpr mask_type operator==(Self const & a, std::nullptr_t) noexcept { \
150 if constexpr (requires(Raw x) { x == x; }) return a.to_native() == Raw{std::uint32_t{0}}; \
152 std::uint64_t bits = 0; \
153 for (std::size_t i = 0; i != lanes; ++i) bits |= std::uint64_t(!a[i]) << i; \
154 return mask_type::from_bitset(bits); \
159NATIVE_TARGET_VARIANTS(pointer, JAM_PTR_SIMD, JAM_PTR_TARGETS)
162export namespace jam {
164template<
class T, std::
size_t N, native::isa<> A>
166 native::simd<
ptr<T>, N, A>
const & source)
noexcept { target.unsafe_assign(source); }
168template<
class T, std::
size_t N, native::isa<> A>
169hint_inline
void assign(native::simd<ptr<T>, N, A> & target,
170 native::simd<
ptr<T>, N, A>
const & source)
noexcept { target.assign(source); }
172template<
class T, std::
size_t N, native::isa<> A, std::
size_t K>
173hint_inline
void unsafe_assign(native::wide<native::simd<ptr<T>, N, A>, K> & target,
174 native::wide<native::simd<ptr<T>, N, A>, K>
const & source)
noexcept {
175 for (std::size_t i = 0; i != K; ++i) target.registers[i].unsafe_assign(source.registers[i]);
178template<
class T, std::
size_t N, native::isa<> A, std::
size_t K>
179hint_inline
void assign(native::wide<native::simd<ptr<T>, N, A>, K> & target,
180 native::wide<native::simd<ptr<T>, N, A>, K>
const & source)
noexcept {
181 for (std::size_t i = 0; i != K; ++i) target.registers[i].assign(source.registers[i]);
185template<
class T, std::
size_t N, native::isa<> A>
186struct tracer<native::simd<ptr<T>, N, A>> : detail::structural_tracer {
187 template<
class Visitor>
188 static hint_inline
constexpr void trace(Visitor & visit, native::simd<
ptr<T>, N, A>
const & value)
189 noexcept requires detail::visits<ptr<T>, Visitor> {
190 for (std::size_t i = 0; i != N; ++i) detail::trace_parts(visit, value[i]);
194template<
class V, std::
size_t K>
195struct tracer<native::wide<V, K>> : detail::structural_tracer {
196 template<
class Visitor>
197 static hint_inline
constexpr void trace(Visitor & visit, native::wide<V, K>
const & value)
198 noexcept requires detail::visits<V, Visitor> {
199 for (
auto const & part : value.registers) detail::trace_parts(visit, part);
204namespace jam::detail {
206using gather_bits = std::conditional_t<
sizeof(U) == 4, std::uint32_t, std::uint64_t>;
207template<
class T,
class U, std::
size_t N, native::isa<> A>
208concept gather_shape = !std::is_const_v<U> && !std::is_volatile_v<U> &&
209 (std::same_as<U, std::uint32_t> || std::same_as<U, std::int32_t> ||
210 std::same_as<U, std::uint64_t> || std::same_as<U, std::int64_t> ||
211 std::same_as<U, float> || std::same_as<U, double> || is_ptr<U>) &&
212 requires {
sizeof(native::simd<U, N, A>);
sizeof(native::simd<gather_bits<U>, N, A>); };
219template<
class T,
class U>
220hint_inline hint_const std::ptrdiff_t member_displacement(U T::* member)
noexcept {
221 assert(member !=
nullptr);
223 static_assert(
sizeof(member) ==
sizeof(std::int32_t),
"gather requires the flat MS data-member-pointer ABI");
224 return __builtin_bit_cast(std::int32_t, member);
226 static_assert(
sizeof(member) ==
sizeof(std::ptrdiff_t),
"gather requires the Itanium data-member-pointer ABI");
227 return __builtin_bit_cast(std::ptrdiff_t, member);
232#if defined(__x86_64__) || defined(_M_X64)
235#define JAM_GATHER_X86 \
236 if constexpr (A.has(native::x86_feature::avx2) && (N == 4 || N == 8 || N == 16)) { \
237 using V = native::simd<std::uint32_t, N, A>; \
238 using I = native::simd<std::int32_t, N, A>; \
239 auto const displacement = detail::member_displacement(member); \
240 auto const encoded = nodes.to_native(); \
241 auto const indices = __builtin_bit_cast(I, encoded & V{std::uint32_t{0x7fffffffu}}); \
242 for (unsigned generation = 0; generation != 2; ++generation) { \
243 auto const selected = active & ((encoded >> native::imm<31>) == V{generation}); \
244 auto const & g = generation ? arena->young() : arena->old(); \
245 auto const address = reinterpret_cast<std::uintptr_t>(g.data()) + displacement; \
246 auto const * base = reinterpret_cast<B const *>(address); \
247 if constexpr (sizeof(B) == 4) { \
248 if constexpr (A.has(native::x86_feature::avx512f)) \
249 result = native::mask_vpgatherdd<8>(result, selected, base, indices); \
250 else result = native::mask_vpgatherdd<8>(result, \
251 I::from_native(selected.to_native()), base, indices); \
253 if constexpr (A.has(native::x86_feature::avx512f)) \
254 result = native::mask_vpgatherdq<8>(result, selected, base, indices); \
256 using M64 = native::simd<std::int64_t, N, A>; \
257 using D = std::int32_t __attribute__((ext_vector_type(N))); \
258 using Q = std::int64_t __attribute__((ext_vector_type(N))); \
259 auto const mask = __builtin_bit_cast(M64, __builtin_convertvector(__builtin_bit_cast(D, selected), Q)); \
260 result = native::mask_vpgatherdq<8>(result, mask, base, indices); \
266#define JAM_GATHER_X86
269#define JAM_GATHERS(name, ISA, ...) \
270export namespace jam { \
271template<class T, class U, class Owner, std::size_t N, native::isa<> A> \
272 requires detail::gather_shape<T, U, N, A> && std::convertible_to<U Owner::*, U T::*> && \
273 (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
277[[nodiscard]] hint_inline auto gather(native::simd<ptr<T>, N, A> const & nodes, \
278 U Owner::* selected, typename native::simd<ptr<T>, N, A>::mask_type active) noexcept { \
279 U T::* member = selected; \
280 using B = detail::gather_bits<U>; \
281 using R = native::simd<B, N, A>; \
282 using Out = native::simd<U, N, A>; \
283 auto * arena = heap::current(); \
284 assert(member != nullptr); \
288 auto const bits = active.to_bitset(); \
289 std::array<B, N> values{}; \
290 for (std::size_t i = 0; i != N; ++i) if ((bits >> i) & 1) { \
291 auto const * p = arena->address(nodes[i]); \
292 std::memcpy(&values[i], &(p->*member), sizeof(B)); \
294 result = R::load(values.data()); \
296 if constexpr (detail::is_ptr<U>) return Out{result}; \
297 else return __builtin_bit_cast(Out, result); \
300template<class T, class U, class Owner, std::size_t N, native::isa<> A> \
301 requires detail::gather_shape<T, U, N, A> && std::convertible_to<U Owner::*, U T::*> && \
302 (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
304[[nodiscard]] hint_inline auto gather(native::simd<ptr<T>, N, A> const & nodes, \
305 U Owner::* member) noexcept { \
306 return gather(nodes, member, nodes != nullptr); \
309template<class T, class U, class Owner, std::size_t N, native::isa<> A, std::size_t K> \
310 requires detail::gather_shape<T, U, N, A> && std::convertible_to<U Owner::*, U T::*> && \
311 (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
313[[nodiscard]] hint_inline auto gather(native::wide<native::simd<ptr<T>, N, A>, K> const & nodes, \
314 U Owner::* member, native::wide<typename native::simd<ptr<T>, N, A>::mask_type, K> const & active) noexcept { \
315 auto const & [...p] = nodes.registers; \
316 auto const & [...m] = active.registers; \
317 return native::wide<native::simd<U, N, A>, K>{std::array<native::simd<U, N, A>, K>{gather(p, member, m)...}}; \
320template<class T, class U, class Owner, std::size_t N, native::isa<> A, std::size_t K> \
321 requires detail::gather_shape<T, U, N, A> && std::convertible_to<U Owner::*, U T::*> && \
322 (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
324[[nodiscard]] hint_inline auto gather(native::wide<native::simd<ptr<T>, N, A>, K> const & nodes, \
325 U Owner::* member) noexcept { \
326 using M = typename native::simd<ptr<T>, N, A>::mask_type; \
327 auto const & [...p] = nodes.registers; \
328 native::wide<M, K> active{std::array<M, K>{(p != nullptr)...}}; \
329 return gather(nodes, member, active); \
334export namespace jam {
342template<
class T,
class U,
class Owner, std::
size_t N, native::isa<> A>
343native::simd<U, N, A>
gather(native::simd<
ptr<T>, N, A>
const & nodes,
344 U Owner::* member,
typename native::simd<
ptr<T>, N, A>::mask_type active)
noexcept;
347template<
class T,
class U,
class Owner, std::
size_t N, native::isa<> A>
348native::simd<U, N, A>
gather(native::simd<
ptr<T>, N, A>
const & nodes, U Owner::* member)
noexcept;
351template<
class T,
class U,
class Owner, std::
size_t N, native::isa<> A, std::
size_t K>
352native::wide<native::simd<U, N, A>, K>
gather(native::wide<native::simd<
ptr<T>, N, A>, K>
const & nodes,
353 U Owner::* member, native::wide<
typename native::simd<
ptr<T>, N, A>::mask_type, K>
const & active)
noexcept;
356template<
class T,
class U,
class Owner, std::
size_t N, native::isa<> A, std::
size_t K>
357native::wide<native::simd<U, N, A>, K>
gather(native::wide<native::simd<
ptr<T>, N, A>, K>
const & nodes,
358 U Owner::* member)
noexcept;
361NATIVE_TARGET_VARIANTS(pointer_gather, JAM_GATHERS, JAM_PTR_TARGETS)
365#undef JAM_PTR_TARGETS
A typed heap offset, with no root registration or ownership.
constexpr void unsafe_assign(std::array< ptr< T >, N > &target, std::array< ptr< T >, N > const &source) noexcept
Bulk-copy pointer bits without registration; the caller owns the barrier.
constexpr void assign(std::array< ptr< T >, N > &target, std::array< ptr< T >, N > const &source) noexcept
Copy an array and register young lanes with one old-range check.
native::simd< U, N, A > gather(native::simd< ptr< T >, N, A > const &nodes, U Owner::*member, typename native::simd< ptr< T >, N, A >::mask_type active) noexcept
Gather a member from selected lanes; inactive lanes return zero or null.
Per-value tracing customization; values without a hook or manifest are leaves.An allocation-only clai...