Debian Codegraph @

include/core/hist/detail/data/CompactCoordinatesXYZW.h

raw
1 // SPDX-License-Identifier: LGPL-3.0-or-later
2 // Author: Kristian Lytje
3
4 /**
5 * @brief This file contains custom vector instructions for efficient scattering calculations.
6 *
7 * The implementation is specialized for generic systems defined by a collection of [x, y, z, w] float vectors.
8 * This is useful for the Simple excluded volume model, where all atoms have the same form factor type but different weights.
9 * Similarly, it is useful for SANS calculations, where the lack of q-dependence means the form factors may be encoded as simple weights.
10 *
11 * For more complex X-ray calculations with different atomic species, the CompactCoordinatesXYZWFF implementation may be more useful,
12 * since it is specialized for systems defined by a collection of [x: float, y: float, z: float, ff_type: int32] vectors.
13 */
14
15 #pragma once
16
17 #include <hist/detail/data/IntrinsicMacros.h>
18 #include <hist/detail/data/WidthControllers.h>
19 #include <hist/detail/data/IntrinsicHelpers.h>
20 #include <math/Vector3.h>
21 #include <constants/Constants.h>
22 #include <settings/Flags.h>
23
24 #include <array>
25 #include <cstdint>
26 #include <span>
27
28 namespace ausaxs::hist::detail::xyzw {
29 struct EvaluatedResult {
30 float distance; // The raw distance
31 int32_t distance_bin; // The distance bin index
32 float weight; // The combined weight
33 };
34
35 struct EvaluatedResultRounded {
36 int32_t distance; // The distance bin
37 float weight; // The combined weight
38 };
39
40 struct QuadEvaluatedResult {
41 std::array<float, 4> distances; // The raw distances (for weighted bin center calculation)
42 std::array<int32_t, 4> distance_bins; // The distance bin indices (for array indexing)
43 std::array<float, 4> weights; // The combined weight
44 };
45
46 struct QuadEvaluatedResultRounded {
47 std::array<int32_t, 4> distances; // The distance bin
48 std::array<float, 4> weights; // The combined weight
49 };
50
51 struct alignas(32) OctoEvaluatedResult {
52 std::array<float, 8> distances; // The raw distances (for weighted bin center calculation)
53 std::array<int32_t, 8> distance_bins; // The distance bin indices (for array indexing)
54 std::array<float, 8> weights; // The combined weight
55 };
56
57 struct alignas(32) OctoEvaluatedResultRounded {
58 std::array<int32_t, 8> distances; // The distance bin
59 std::array<float, 8> weights; // The combined weight
60 };
61
62 struct alignas(32) HexaEvaluatedResult {
63 std::array<float, 16> distances;
64 std::array<int32_t, 16> distance_bins;
65 std::array<float, 16> weights;
66 };
67
68 struct alignas(32) HexaEvaluatedResultRounded {
69 std::array<int32_t, 16> distances;
70 std::array<float, 16> weights;
71 };
72
73 // assert that it is safe to perform memcpy and reinterpret_cast on these structures
74 static_assert(sizeof(EvaluatedResult) == 12, "hist::detail::EvaluatedResult is not 12 bytes long");
75 static_assert(sizeof(EvaluatedResultRounded) == 8, "hist::detail::EvaluatedResultRounded is not 8 bytes long");
76 static_assert(sizeof(QuadEvaluatedResult) == 48, "hist::detail::QuadEvaluatedResult is not 48 bytes long");
77 static_assert(sizeof(QuadEvaluatedResultRounded) == 32, "hist::detail::QuadEvaluatedResultRounded is not 32 bytes long");
78 static_assert(sizeof(OctoEvaluatedResult) == 96, "hist::detail::OctoEvaluatedResult is not 96 bytes long");
79 static_assert(sizeof(OctoEvaluatedResultRounded) == 64, "hist::detail::OctoEvaluatedResultRounded is not 64 bytes long");
80 static_assert(sizeof(HexaEvaluatedResult) == 192, "hist::detail::HexaEvaluatedResult is not 192 bytes long");
81 static_assert(sizeof(HexaEvaluatedResultRounded) == 128, "hist::detail::HexaEvaluatedResultRounded is not 128 bytes long");
82
83 // ensure our structures are trivially copyable
84 static_assert(std::is_trivial_v<EvaluatedResult>, "hist::detail::EvaluatedResult is not trivial");
85 static_assert(std::is_trivial_v<EvaluatedResultRounded>, "hist::detail::EvaluatedResultRounded is not trivial");
86 static_assert(std::is_trivial_v<QuadEvaluatedResult>, "hist::detail::QuadEvaluatedResult is not trivial");
87 static_assert(std::is_trivial_v<QuadEvaluatedResultRounded>, "hist::detail::QuadEvaluatedResultRounded is not trivial");
88 static_assert(std::is_trivial_v<OctoEvaluatedResult>, "hist::detail::OctoEvaluatedResult is not trivial");
89 static_assert(std::is_trivial_v<OctoEvaluatedResultRounded>, "hist::detail::OctoEvaluatedResultRounded is not trivial");
90 static_assert(std::is_trivial_v<HexaEvaluatedResult>, "hist::detail::HexaEvaluatedResult is not trivial");
91 static_assert(std::is_trivial_v<HexaEvaluatedResultRounded>, "hist::detail::HexaEvaluatedResultRounded is not trivial");
92
93 // check that the structures have a standard memory layout. this is required for the reinterpret_casts.
94 static_assert(std::is_standard_layout_v<EvaluatedResult>, "hist::detail::EvaluatedResult is not trivial");
95 static_assert(std::is_standard_layout_v<EvaluatedResultRounded>, "hist::detail::EvaluatedResultRounded is not trivial");
96 static_assert(std::is_standard_layout_v<QuadEvaluatedResult>, "hist::detail::QuadEvaluatedResult is not trivial");
97 static_assert(std::is_standard_layout_v<QuadEvaluatedResultRounded>, "hist::detail::QuadEvaluatedResultRounded is not trivial");
98 static_assert(std::is_standard_layout_v<OctoEvaluatedResult>, "hist::detail::OctoEvaluatedResult is not trivial");
99 static_assert(std::is_standard_layout_v<OctoEvaluatedResultRounded>, "hist::detail::OctoEvaluatedResultRounded is not trivial");
100 static_assert(std::is_standard_layout_v<HexaEvaluatedResult>, "hist::detail::HexaEvaluatedResult is not trivial");
101 static_assert(std::is_standard_layout_v<HexaEvaluatedResultRounded>, "hist::detail::HexaEvaluatedResultRounded is not trivial");
102 }
103
104 namespace ausaxs::hist::detail {
105 template<bool variable_bin_width>
106 class CompactCoordinatesXYZW : public WidthController<variable_bin_width> {
107 public:
108 using WidthController<variable_bin_width>::get_inv_width;
109 CompactCoordinatesXYZW() noexcept = default;
110 CompactCoordinatesXYZW(const CompactCoordinatesXYZW& other) noexcept = default;
111 CompactCoordinatesXYZW(CompactCoordinatesXYZW&& other) noexcept = default;
112 CompactCoordinatesXYZW& operator=(const CompactCoordinatesXYZW& other) noexcept = default;
113 CompactCoordinatesXYZW& operator=(CompactCoordinatesXYZW&& other) noexcept = default;
114
115 template<numeric T, numeric V>
116 CompactCoordinatesXYZW(const Vector3<T>& v, V w) noexcept : value{.pos={static_cast<float>(v.x()), static_cast<float>(v.y()), static_cast<float>(v.z())}, .w=static_cast<float>(w)} {}
117 CompactCoordinatesXYZW(const Vector3<float>& v, float w) noexcept : value{.pos=v, .w=w} {}
118
119 xyzw::EvaluatedResultRounded evaluate_rounded(const CompactCoordinatesXYZW& other) const noexcept;
120 xyzw::EvaluatedResult evaluate(const CompactCoordinatesXYZW& other) const noexcept;
121
122 xyzw::QuadEvaluatedResultRounded evaluate_rounded_4(std::span<const CompactCoordinatesXYZW, 4> others) const noexcept;
123 xyzw::QuadEvaluatedResult evaluate_4(std::span<const CompactCoordinatesXYZW, 4> others) const noexcept;
124
125 xyzw::OctoEvaluatedResultRounded evaluate_rounded_8(std::span<const CompactCoordinatesXYZW, 8> others) const noexcept;
126 xyzw::OctoEvaluatedResult evaluate_8(std::span<const CompactCoordinatesXYZW, 8> others) const noexcept;
127
128 xyzw::HexaEvaluatedResultRounded evaluate_rounded_16(std::span<const CompactCoordinatesXYZW, 16> others) const noexcept;
129 xyzw::HexaEvaluatedResult evaluate_16(std::span<const CompactCoordinatesXYZW, 16> others) const noexcept;
130
131 union {
132 struct {Vector3<float> pos; float w;} value;
133 std::array<float, 4> data;
134 };
135
136 protected:
137 xyzw::EvaluatedResultRounded evaluate_rounded_scalar(const CompactCoordinatesXYZW& other) const noexcept;
138 xyzw::EvaluatedResult evaluate_scalar(const CompactCoordinatesXYZW& other) const noexcept;
139
140 xyzw::QuadEvaluatedResultRounded evaluate_rounded_4_scalar(std::span<const CompactCoordinatesXYZW, 4> others) const noexcept;
141 xyzw::QuadEvaluatedResult evaluate_4_scalar(std::span<const CompactCoordinatesXYZW, 4> others) const noexcept;
142 void evaluate_rounded_4_scalar_into(std::span<const CompactCoordinatesXYZW, 4> others, int32_t* dist_out, float* wt_out) const noexcept;
143 void evaluate_4_scalar_into(std::span<const CompactCoordinatesXYZW, 4> others, float* dist_out, int32_t* bin_out, float* wt_out) const noexcept;
144 void evaluate_rounded_8_scalar_into(std::span<const CompactCoordinatesXYZW, 8> others, int32_t* dist_out, float* wt_out) const noexcept;
145 void evaluate_8_scalar_into(std::span<const CompactCoordinatesXYZW, 8> others, float* dist_out, int32_t* bin_out, float* wt_out) const noexcept;
146
147 #if defined AUSAXS_USE_SSE2
148 xyzw::QuadEvaluatedResultRounded evaluate_rounded_4_sse(std::span<const CompactCoordinatesXYZW, 4> others) const noexcept;
149 xyzw::QuadEvaluatedResult evaluate_4_sse(std::span<const CompactCoordinatesXYZW, 4> others) const noexcept;
150 void evaluate_rounded_4_sse_into(std::span<const CompactCoordinatesXYZW, 4> others, int32_t* dist_out, float* wt_out) const noexcept;
151 void evaluate_4_sse_into(std::span<const CompactCoordinatesXYZW, 4> others, float* dist_out, int32_t* bin_out, float* wt_out) const noexcept;
152 #endif
153
154 #if defined AUSAXS_USE_AVX2
155 xyzw::OctoEvaluatedResultRounded evaluate_rounded_8_avx(std::span<const CompactCoordinatesXYZW, 8> others) const noexcept;
156 xyzw::OctoEvaluatedResult evaluate_8_avx(std::span<const CompactCoordinatesXYZW, 8> others) const noexcept;
157 void evaluate_rounded_8_avx_into(std::span<const CompactCoordinatesXYZW, 8> others, int32_t* dist_out, float* wt_out) const noexcept;
158 void evaluate_8_avx_into(std::span<const CompactCoordinatesXYZW, 8> others, float* dist_out, int32_t* bin_out, float* wt_out) const noexcept;
159 #endif
160
161 #if defined AUSAXS_USE_AVX512
162 xyzw::HexaEvaluatedResultRounded evaluate_rounded_16_avx512(std::span<const CompactCoordinatesXYZW, 16> others) const noexcept;
163 xyzw::HexaEvaluatedResult evaluate_16_avx512(std::span<const CompactCoordinatesXYZW, 16> others) const noexcept;
164 #endif
165 };
166 static_assert(sizeof(CompactCoordinatesXYZW<true>) == 16, "CompactCoordinatesXYZW is not 16 bytes. This is required for aligning SIMD instructions.");
167 static_assert(std::is_trivial_v<CompactCoordinatesXYZW<true>>, "CompactCoordinatesXYZW is not trivial");
168 static_assert(std::is_standard_layout_v<CompactCoordinatesXYZW<true>>, "CompactCoordinatesXYZW is not standard layout");
169 static_assert(supports_nothrow_move_v<CompactCoordinatesXYZW<true>>, "CompactCoordinatesXYZW should support nothrow move semantics.");
170 static_assert(sizeof(CompactCoordinatesXYZW<false>) == 16, "CompactCoordinatesXYZW is not 16 bytes. This is required for aligning SIMD instructions.");
171 static_assert(std::is_trivial_v<CompactCoordinatesXYZW<false>>, "CompactCoordinatesXYZW is not trivial");
172 static_assert(std::is_standard_layout_v<CompactCoordinatesXYZW<false>>,"CompactCoordinatesXYZW is not standard layout");
173 static_assert(supports_nothrow_move_v<CompactCoordinatesXYZW<false>>, "CompactCoordinatesXYZW should support nothrow move semantics.");
174 }
175
176 //#########################################//
177 //############ IMPLEMENTATION #############//
178 //#########################################//
179
180 // implementation defined in header to support efficient inlining
181
182 template<bool vbw>
183 inline ausaxs::hist::detail::xyzw::EvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate(const CompactCoordinatesXYZW& other) const noexcept {
184 return evaluate_scalar(other);
185 }
186
187 template<bool vbw>
188 inline ausaxs::hist::detail::xyzw::EvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded(const CompactCoordinatesXYZW& other) const noexcept {
189 return evaluate_rounded_scalar(other);
190 }
191
192 template<bool vbw>
193 inline ausaxs::hist::detail::xyzw::QuadEvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_4(
194 std::span<const CompactCoordinatesXYZW, 4> others
195 ) const noexcept {
196 #if defined AUSAXS_USE_SSE2
197 return evaluate_4_sse(others);
198 #else
199 return evaluate_4_scalar(others);
200 #endif
201 }
202
203 template<bool vbw>
204 inline ausaxs::hist::detail::xyzw::QuadEvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_4(
205 std::span<const CompactCoordinatesXYZW, 4> others
206 ) const noexcept {
207 #if defined AUSAXS_USE_SSE2
208 return evaluate_rounded_4_sse(others);
209 #else
210 return evaluate_rounded_4_scalar(others);
211 #endif
212 }
213
214 template<bool vbw>
215 inline ausaxs::hist::detail::xyzw::OctoEvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_8(
216 std::span<const CompactCoordinatesXYZW, 8> others
217 ) const noexcept {
218 #if defined AUSAXS_USE_AVX2
219 return evaluate_8_avx(others);
220 #elif defined AUSAXS_USE_SSE2
221 xyzw::OctoEvaluatedResult result;
222 evaluate_4_sse_into(others.template first<4>(), result.distances.data(), result.distance_bins.data(), result.weights.data());
223 evaluate_4_sse_into(others.template last<4>(), result.distances.data()+4, result.distance_bins.data()+4, result.weights.data()+4);
224 return result;
225 #else
226 xyzw::OctoEvaluatedResult result;
227 evaluate_8_scalar_into(others, result.distances.data(), result.distance_bins.data(), result.weights.data());
228 return result;
229 #endif
230 }
231
232 template<bool vbw>
233 inline ausaxs::hist::detail::xyzw::OctoEvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_8(
234 std::span<const CompactCoordinatesXYZW, 8> others
235 ) const noexcept {
236 #if defined AUSAXS_USE_AVX2
237 return evaluate_rounded_8_avx(others);
238 #elif defined AUSAXS_USE_SSE2
239 xyzw::OctoEvaluatedResultRounded result;
240 evaluate_rounded_4_sse_into(others.template first<4>(), result.distances.data(), result.weights.data());
241 evaluate_rounded_4_sse_into(others.template last<4>(), result.distances.data()+4, result.weights.data()+4);
242 return result;
243 #else
244 xyzw::OctoEvaluatedResultRounded result;
245 evaluate_rounded_8_scalar_into(others, result.distances.data(), result.weights.data());
246 return result;
247 #endif
248 }
249
250 template<bool vbw>
251 inline ausaxs::hist::detail::xyzw::HexaEvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_16(
252 std::span<const CompactCoordinatesXYZW, 16> others
253 ) const noexcept {
254 #if defined AUSAXS_USE_AVX512
255 return evaluate_16_avx512(others);
256 #elif defined AUSAXS_USE_AVX2
257 xyzw::HexaEvaluatedResult result;
258 evaluate_8_avx_into(others.template first<8>(), result.distances.data(), result.distance_bins.data(), result.weights.data());
259 evaluate_8_avx_into(others.template last<8>(), result.distances.data()+8, result.distance_bins.data()+8, result.weights.data()+8);
260 return result;
261 #elif defined AUSAXS_USE_SSE2
262 xyzw::HexaEvaluatedResult result;
263 evaluate_4_sse_into(others.template first<4>(), result.distances.data(), result.distance_bins.data(), result.weights.data());
264 evaluate_4_sse_into(others.template subspan<4,4>(), result.distances.data()+4, result.distance_bins.data()+4, result.weights.data()+4);
265 evaluate_4_sse_into(others.template subspan<8,4>(), result.distances.data()+8, result.distance_bins.data()+8, result.weights.data()+8);
266 evaluate_4_sse_into(others.template last<4>(), result.distances.data()+12, result.distance_bins.data()+12, result.weights.data()+12);
267 return result;
268 #else
269 xyzw::HexaEvaluatedResult result;
270 evaluate_8_scalar_into(others.template first<8>(), result.distances.data(), result.distance_bins.data(), result.weights.data());
271 evaluate_8_scalar_into(others.template last<8>(), result.distances.data()+8, result.distance_bins.data()+8, result.weights.data()+8);
272 return result;
273 #endif
274 }
275
276 template<bool vbw>
277 inline ausaxs::hist::detail::xyzw::HexaEvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_16(
278 std::span<const CompactCoordinatesXYZW, 16> others
279 ) const noexcept {
280 #if defined AUSAXS_USE_AVX512
281 return evaluate_rounded_16_avx512(others);
282 #elif defined AUSAXS_USE_AVX2
283 xyzw::HexaEvaluatedResultRounded result;
284 evaluate_rounded_8_avx_into(others.template first<8>(), result.distances.data(), result.weights.data());
285 evaluate_rounded_8_avx_into(others.template last<8>(), result.distances.data()+8, result.weights.data()+8);
286 return result;
287 #elif defined AUSAXS_USE_SSE2
288 xyzw::HexaEvaluatedResultRounded result;
289 evaluate_rounded_4_sse_into(others.template first<4>(), result.distances.data(), result.weights.data());
290 evaluate_rounded_4_sse_into(others.template subspan<4,4>(), result.distances.data()+4, result.weights.data()+4);
291 evaluate_rounded_4_sse_into(others.template subspan<8,4>(), result.distances.data()+8, result.weights.data()+8);
292 evaluate_rounded_4_sse_into(others.template last<4>(), result.distances.data()+12, result.weights.data()+12);
293 return result;
294 #else
295 xyzw::HexaEvaluatedResultRounded result;
296 evaluate_rounded_8_scalar_into(others.template first<8>(), result.distances.data(), result.weights.data());
297 evaluate_rounded_8_scalar_into(others.template last<8>(), result.distances.data()+8, result.weights.data()+8);
298 return result;
299 #endif
300 }
301
302 template<bool vbw>
303 inline ausaxs::hist::detail::xyzw::EvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_scalar(const CompactCoordinatesXYZW& other) const noexcept {
304 float dist = std::sqrt(squared_dot_product(this->data.data(), other.data.data()));
305 int32_t dist_bin = std::round(get_inv_width() * dist);
306 return xyzw::EvaluatedResult(dist, dist_bin, value.w*other.value.w);
307 }
308
309 template<bool vbw>
310 inline ausaxs::hist::detail::xyzw::EvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_scalar(
311 const CompactCoordinatesXYZW& other
312 ) const noexcept {
313 int32_t dist = std::round(get_inv_width()*std::sqrt(squared_dot_product(this->data.data(), other.data.data())));
314 return xyzw::EvaluatedResultRounded(dist, value.w*other.value.w);
315 }
316
317 template<bool vbw>
318 inline void ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_4_scalar_into(
319 std::span<const CompactCoordinatesXYZW, 4> others,
320 float* dist_out, int32_t* bin_out, float* wt_out
321 ) const noexcept {
322 float inv_width = get_inv_width();
323 for (int i = 0; i < 4; ++i) {
324 float dist = std::sqrt(squared_dot_product(this->data.data(), others[i].data.data()));
325 dist_out[i] = dist;
326 bin_out[i] = static_cast<int32_t>(std::round(inv_width * dist));
327 wt_out[i] = value.w * others[i].value.w;
328 }
329 }
330
331 template<bool vbw>
332 inline ausaxs::hist::detail::xyzw::QuadEvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_4_scalar(
333 std::span<const CompactCoordinatesXYZW, 4> others
334 ) const noexcept {
335 xyzw::QuadEvaluatedResult result;
336 evaluate_4_scalar_into(others, result.distances.data(), result.distance_bins.data(), result.weights.data());
337 return result;
338 }
339
340 template<bool vbw>
341 inline void ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_4_scalar_into(
342 std::span<const CompactCoordinatesXYZW, 4> others,
343 int32_t* dist_out, float* wt_out
344 ) const noexcept {
345 float inv_width = get_inv_width();
346 for (int i = 0; i < 4; ++i) {
347 dist_out[i] = static_cast<int32_t>(std::round(inv_width * std::sqrt(squared_dot_product(this->data.data(), others[i].data.data()))));
348 wt_out[i] = value.w * others[i].value.w;
349 }
350 }
351
352 template<bool vbw>
353 inline ausaxs::hist::detail::xyzw::QuadEvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_4_scalar(
354 std::span<const CompactCoordinatesXYZW, 4> others
355 ) const noexcept {
356 xyzw::QuadEvaluatedResultRounded result;
357 evaluate_rounded_4_scalar_into(others, result.distances.data(), result.weights.data());
358 return result;
359 }
360
361 template<bool vbw>
362 inline void ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_8_scalar_into(
363 std::span<const CompactCoordinatesXYZW, 8> others,
364 float* dist_out, int32_t* bin_out, float* wt_out
365 ) const noexcept {
366 evaluate_4_scalar_into(others.template first<4>(), dist_out, bin_out, wt_out);
367 evaluate_4_scalar_into(others.template last<4>(), dist_out + 4, bin_out + 4, wt_out + 4);
368 }
369
370 template<bool vbw>
371 inline void ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_8_scalar_into(
372 std::span<const CompactCoordinatesXYZW, 8> others,
373 int32_t* dist_out, float* wt_out
374 ) const noexcept {
375 evaluate_rounded_4_scalar_into(others.template first<4>(), dist_out, wt_out);
376 evaluate_rounded_4_scalar_into(others.template last<4>(), dist_out + 4, wt_out + 4);
377 }
378
379 #if defined AUSAXS_USE_SSE2
380 #include <nmmintrin.h>
381 template<bool vbw>
382 inline void ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_4_sse_into(
383 std::span<const CompactCoordinatesXYZW, 4> others,
384 float* dist_out, int32_t* bin_out, float* wt_out
385 ) const noexcept {
386 const float* p = reinterpret_cast<const float*>(others.data());
387 __m128 r1 = _mm_loadu_ps(p);
388 __m128 r2 = _mm_loadu_ps(p + 4);
389 __m128 r3 = _mm_loadu_ps(p + 8);
390 __m128 r4 = _mm_loadu_ps(p + 12);
391
392 // extract w values (position 3) before computing differences
393 __m128 weights = _mm_mul_ps(_mm_set_ps1(value.w),
394 _mm_movehl_ps(_mm_unpackhi_ps(r3, r4), _mm_unpackhi_ps(r1, r2)));
395
396 __m128 sv = _mm_load_ps(this->data.data());
397 __m128 d1 = _mm_sub_ps(sv, r1);
398 __m128 d2 = _mm_sub_ps(sv, r2);
399 __m128 d3 = _mm_sub_ps(sv, r3);
400 __m128 d4 = _mm_sub_ps(sv, r4);
401 d1 = _mm_mul_ps(d1, d1);
402 d2 = _mm_mul_ps(d2, d2);
403 d3 = _mm_mul_ps(d3, d3);
404 d4 = _mm_mul_ps(d4, d4);
405
406 // 4x4 transpose: converts per-atom [dx²,dy²,dz²,dw²] to per-component rows, discarding w
407 __m128 t0 = _mm_unpacklo_ps(d1, d2); // [dx1²,dx2²,dy1²,dy2²]
408 __m128 t1 = _mm_unpackhi_ps(d1, d2); // [dz1²,dz2²,dw1²,dw2²]
409 __m128 t2 = _mm_unpacklo_ps(d3, d4); // [dx3²,dx4²,dy3²,dy4²]
410 __m128 t3 = _mm_unpackhi_ps(d3, d4); // [dz3²,dz4²,dw3²,dw4²]
411 __m128 row_x = _mm_movelh_ps(t0, t2); // [dx1²,dx2²,dx3²,dx4²]
412 __m128 row_y = _mm_movehl_ps(t2, t0); // [dy1²,dy2²,dy3²,dy4²]
413 __m128 row_z = _mm_movelh_ps(t1, t3); // [dz1²,dz2²,dz3²,dz4²]
414
415 __m128 dist2 = _mm_add_ps(_mm_add_ps(row_x, row_y), row_z);
416 __m128 dist_sqrt = _mm_sqrt_ps(dist2);
417 __m128 dist_binf = _mm_mul_ps(dist_sqrt, _mm_set_ps1(get_inv_width()));
418 __m128i dist_bin = _mm_cvtps_epi32(dist_binf);
419
420 _mm_store_ps(dist_out, dist_sqrt);
421 _mm_store_si128(reinterpret_cast<__m128i*>(bin_out), dist_bin);
422 _mm_store_ps(wt_out, weights);
423 }
424
425 template<bool vbw>
426 inline ausaxs::hist::detail::xyzw::QuadEvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_4_sse(
427 std::span<const CompactCoordinatesXYZW, 4> others
428 ) const noexcept {
429 xyzw::QuadEvaluatedResult result;
430 evaluate_4_sse_into(others, result.distances.data(), result.distance_bins.data(), result.weights.data());
431 return result;
432 }
433
434 template<bool vbw>
435 inline void ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_4_sse_into(
436 std::span<const CompactCoordinatesXYZW, 4> others,
437 int32_t* dist_out, float* wt_out
438 ) const noexcept {
439 const float* p = reinterpret_cast<const float*>(others.data());
440 __m128 r1 = _mm_loadu_ps(p);
441 __m128 r2 = _mm_loadu_ps(p + 4);
442 __m128 r3 = _mm_loadu_ps(p + 8);
443 __m128 r4 = _mm_loadu_ps(p + 12);
444
445 // extract w values (position 3) before computing differences
446 __m128 weights = _mm_mul_ps(_mm_set_ps1(value.w),
447 _mm_movehl_ps(_mm_unpackhi_ps(r3, r4), _mm_unpackhi_ps(r1, r2)));
448
449 __m128 sv = _mm_load_ps(this->data.data());
450 __m128 d1 = _mm_sub_ps(sv, r1);
451 __m128 d2 = _mm_sub_ps(sv, r2);
452 __m128 d3 = _mm_sub_ps(sv, r3);
453 __m128 d4 = _mm_sub_ps(sv, r4);
454 d1 = _mm_mul_ps(d1, d1);
455 d2 = _mm_mul_ps(d2, d2);
456 d3 = _mm_mul_ps(d3, d3);
457 d4 = _mm_mul_ps(d4, d4);
458
459 __m128 t0 = _mm_unpacklo_ps(d1, d2);
460 __m128 t1 = _mm_unpackhi_ps(d1, d2);
461 __m128 t2 = _mm_unpacklo_ps(d3, d4);
462 __m128 t3 = _mm_unpackhi_ps(d3, d4);
463 __m128 row_x = _mm_movelh_ps(t0, t2);
464 __m128 row_y = _mm_movehl_ps(t2, t0);
465 __m128 row_z = _mm_movelh_ps(t1, t3);
466
467 __m128 dist2 = _mm_add_ps(_mm_add_ps(row_x, row_y), row_z);
468 __m128 dist_sqrt = _mm_sqrt_ps(dist2);
469 __m128 dist_binf = _mm_mul_ps(dist_sqrt, _mm_set_ps1(get_inv_width()));
470 __m128i dist_bin = _mm_cvtps_epi32(dist_binf);
471
472 _mm_store_si128(reinterpret_cast<__m128i*>(dist_out), dist_bin);
473 _mm_store_ps(wt_out, weights);
474 }
475
476 template<bool vbw>
477 inline ausaxs::hist::detail::xyzw::QuadEvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_4_sse(
478 std::span<const CompactCoordinatesXYZW, 4> others
479 ) const noexcept {
480 xyzw::QuadEvaluatedResultRounded result;
481 evaluate_rounded_4_sse_into(others, result.distances.data(), result.weights.data());
482 return result;
483 }
484
485 #endif
486
487 #if defined AUSAXS_USE_AVX2
488 #include <immintrin.h>
489
490 template<bool vbw>
491 inline void ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_8_avx_into(
492 std::span<const CompactCoordinatesXYZW, 8> others,
493 float* dist_out, int32_t* bin_out, float* wt_out
494 ) const noexcept {
495 const float* p = reinterpret_cast<const float*>(others.data());
496 __m256 v12 = _mm256_loadu_ps(p);
497 __m256 v34 = _mm256_loadu_ps(p + 8);
498 __m256 v56 = _mm256_loadu_ps(p + 16);
499 __m256 v78 = _mm256_loadu_ps(p + 24);
500
501 // extract w values (position 3 in each lane) before computing differences
502 __m256 wt0 = _mm256_unpackhi_ps(v12, v34);
503 __m256 wt1 = _mm256_unpackhi_ps(v56, v78);
504 __m256 w_all = _mm256_shuffle_ps(wt0, wt1, _MM_SHUFFLE(3,2,3,2));
505 __m256 weights = _mm256_mul_ps(_mm256_set1_ps(value.w), w_all);
506
507 // compute squared differences (squaring before transpose gives better ILP:
508 // 4 independent mul ops overlap on separate execution ports)
509 __m256 svv = _mm256_broadcast_ps(reinterpret_cast<const __m128*>(this->data.data()));
510 __m256 d12 = _mm256_sub_ps(svv, v12);
511 __m256 d34 = _mm256_sub_ps(svv, v34);
512 __m256 d56 = _mm256_sub_ps(svv, v56);
513 __m256 d78 = _mm256_sub_ps(svv, v78);
514 d12 = _mm256_mul_ps(d12, d12);
515 d34 = _mm256_mul_ps(d34, d34);
516 d56 = _mm256_mul_ps(d56, d56);
517 d78 = _mm256_mul_ps(d78, d78);
518
519 // in-lane 4x4 transpose: lo lanes → atoms {1,3,5,7}, hi lanes → atoms {2,4,6,8}
520 __m256 t0 = _mm256_unpacklo_ps(d12, d34);
521 __m256 t1 = _mm256_unpackhi_ps(d12, d34);
522 __m256 t2 = _mm256_unpacklo_ps(d56, d78);
523 __m256 t3 = _mm256_unpackhi_ps(d56, d78);
524 __m256 row_x = _mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(1,0,1,0));
525 __m256 row_y = _mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(3,2,3,2));
526 __m256 row_z = _mm256_shuffle_ps(t1, t3, _MM_SHUFFLE(1,0,1,0));
527
528 __m256 dist2 = _mm256_add_ps(_mm256_add_ps(row_x, row_y), row_z);
529 __m256 dist_sqrt = _mm256_sqrt_ps(dist2);
530 __m256i dist_bin = _mm256_cvtps_epi32(_mm256_mul_ps(dist_sqrt, _mm256_set1_ps(get_inv_width())));
531
532 _mm256_store_ps(dist_out, dist_sqrt);
533 _mm256_store_si256(reinterpret_cast<__m256i*>(bin_out), dist_bin);
534 _mm256_store_ps(wt_out, weights);
535 }
536
537 template<bool vbw>
538 inline ausaxs::hist::detail::xyzw::OctoEvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_8_avx(
539 std::span<const CompactCoordinatesXYZW, 8> others
540 ) const noexcept {
541 xyzw::OctoEvaluatedResult result;
542 evaluate_8_avx_into(others, result.distances.data(), result.distance_bins.data(), result.weights.data());
543 return result;
544 }
545
546 template<bool vbw>
547 inline void ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_8_avx_into(
548 std::span<const CompactCoordinatesXYZW, 8> others,
549 int32_t* dist_out, float* wt_out
550 ) const noexcept {
551 const float* p = reinterpret_cast<const float*>(others.data());
552 __m256 v12 = _mm256_loadu_ps(p);
553 __m256 v34 = _mm256_loadu_ps(p + 8);
554 __m256 v56 = _mm256_loadu_ps(p + 16);
555 __m256 v78 = _mm256_loadu_ps(p + 24);
556
557 // extract w values (position 3 in each lane) before computing differences
558 __m256 wt0 = _mm256_unpackhi_ps(v12, v34);
559 __m256 wt1 = _mm256_unpackhi_ps(v56, v78);
560 __m256 w_all = _mm256_shuffle_ps(wt0, wt1, _MM_SHUFFLE(3,2,3,2));
561 __m256 weights = _mm256_mul_ps(_mm256_set1_ps(value.w), w_all);
562
563 __m256 svv = _mm256_broadcast_ps(reinterpret_cast<const __m128*>(this->data.data()));
564 __m256 d12 = _mm256_sub_ps(svv, v12);
565 __m256 d34 = _mm256_sub_ps(svv, v34);
566 __m256 d56 = _mm256_sub_ps(svv, v56);
567 __m256 d78 = _mm256_sub_ps(svv, v78);
568 d12 = _mm256_mul_ps(d12, d12);
569 d34 = _mm256_mul_ps(d34, d34);
570 d56 = _mm256_mul_ps(d56, d56);
571 d78 = _mm256_mul_ps(d78, d78);
572
573 __m256 t0 = _mm256_unpacklo_ps(d12, d34);
574 __m256 t1 = _mm256_unpackhi_ps(d12, d34);
575 __m256 t2 = _mm256_unpacklo_ps(d56, d78);
576 __m256 t3 = _mm256_unpackhi_ps(d56, d78);
577 __m256 row_x = _mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(1,0,1,0));
578 __m256 row_y = _mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(3,2,3,2));
579 __m256 row_z = _mm256_shuffle_ps(t1, t3, _MM_SHUFFLE(1,0,1,0));
580
581 __m256 dist2 = _mm256_add_ps(_mm256_add_ps(row_x, row_y), row_z);
582 __m256i dist_bin = _mm256_cvtps_epi32(_mm256_mul_ps(_mm256_sqrt_ps(dist2), _mm256_set1_ps(get_inv_width())));
583
584 _mm256_store_si256(reinterpret_cast<__m256i*>(dist_out), dist_bin);
585 _mm256_store_ps(wt_out, weights);
586 }
587
588 template<bool vbw>
589 inline ausaxs::hist::detail::xyzw::OctoEvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_8_avx(
590 std::span<const CompactCoordinatesXYZW, 8> others
591 ) const noexcept {
592 xyzw::OctoEvaluatedResultRounded result;
593 evaluate_rounded_8_avx_into(others, result.distances.data(), result.weights.data());
594 return result;
595 }
596 #endif
597
598 #if defined AUSAXS_USE_AVX512
599 #include <immintrin.h>
600
601 template<bool vbw>
602 inline ausaxs::hist::detail::xyzw::HexaEvaluatedResult ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_16_avx512(
603 std::span<const CompactCoordinatesXYZW, 16> others
604 ) const noexcept {
605 const float* p = reinterpret_cast<const float*>(others.data());
606 __m512 v03 = _mm512_loadu_ps(p);
607 __m512 v47 = _mm512_loadu_ps(p + 16);
608 __m512 v811 = _mm512_loadu_ps(p + 32);
609 __m512 v1215 = _mm512_loadu_ps(p + 48);
610
611 // extract weights from raw data before computing differences
612 const __m512i gather_w = _mm512_setr_epi32(3, 7, 11, 15, 19, 23, 27, 31, 0, 0, 0, 0, 0, 0, 0, 0);
613 __m256 w_lo = _mm512_castps512_ps256(_mm512_permutex2var_ps(v03, gather_w, v47));
614 __m256 w_hi = _mm512_castps512_ps256(_mm512_permutex2var_ps(v811, gather_w, v1215));
615 __m512 weights = _mm512_mul_ps(_mm512_set1_ps(value.w),
616 _mm512_insertf32x8(_mm512_castps256_ps512(w_lo), w_hi, 1));
617
618 // compute squared differences (square-first for ILP)
619 __m512 svv = _mm512_broadcast_f32x4(_mm_load_ps(this->data.data()));
620 __m512 d03 = _mm512_sub_ps(svv, v03);
621 __m512 d47 = _mm512_sub_ps(svv, v47);
622 __m512 d811 = _mm512_sub_ps(svv, v811);
623 __m512 d1215 = _mm512_sub_ps(svv, v1215);
624 d03 = _mm512_mul_ps(d03, d03);
625 d47 = _mm512_mul_ps(d47, d47);
626 d811 = _mm512_mul_ps(d811, d811);
627 d1215 = _mm512_mul_ps(d1215, d1215);
628
629 // gather x², y², z² from each pair, then combine into full 512-bit vectors
630 const __m512i gather_x = _mm512_setr_epi32(0, 4, 8, 12, 16, 20, 24, 28, 0, 0, 0, 0, 0, 0, 0, 0);
631 const __m512i gather_y = _mm512_setr_epi32(1, 5, 9, 13, 17, 21, 25, 29, 0, 0, 0, 0, 0, 0, 0, 0);
632 const __m512i gather_z = _mm512_setr_epi32(2, 6, 10, 14, 18, 22, 26, 30, 0, 0, 0, 0, 0, 0, 0, 0);
633
634 __m256 rx_lo = _mm512_castps512_ps256(_mm512_permutex2var_ps(d03, gather_x, d47));
635 __m256 rx_hi = _mm512_castps512_ps256(_mm512_permutex2var_ps(d811, gather_x, d1215));
636 __m512 row_x = _mm512_insertf32x8(_mm512_castps256_ps512(rx_lo), rx_hi, 1);
637
638 __m256 ry_lo = _mm512_castps512_ps256(_mm512_permutex2var_ps(d03, gather_y, d47));
639 __m256 ry_hi = _mm512_castps512_ps256(_mm512_permutex2var_ps(d811, gather_y, d1215));
640 __m512 row_y = _mm512_insertf32x8(_mm512_castps256_ps512(ry_lo), ry_hi, 1);
641
642 __m256 rz_lo = _mm512_castps512_ps256(_mm512_permutex2var_ps(d03, gather_z, d47));
643 __m256 rz_hi = _mm512_castps512_ps256(_mm512_permutex2var_ps(d811, gather_z, d1215));
644 __m512 row_z = _mm512_insertf32x8(_mm512_castps256_ps512(rz_lo), rz_hi, 1);
645
646 __m512 dist2 = _mm512_add_ps(_mm512_add_ps(row_x, row_y), row_z);
647 __m512 dist_sqrt = _mm512_sqrt_ps(dist2);
648 __m512i dist_bin = _mm512_cvtps_epi32(_mm512_mul_ps(dist_sqrt, _mm512_set1_ps(get_inv_width())));
649
650 xyzw::HexaEvaluatedResult result;
651 _mm512_store_ps(reinterpret_cast<float*>(result.distances.data()), dist_sqrt);
652 _mm512_store_si512(reinterpret_cast<__m512i*>(result.distance_bins.data()), dist_bin);
653 _mm512_store_ps(reinterpret_cast<float*>(result.weights.data()), weights);
654 return result;
655 }
656
657 template<bool vbw>
658 inline ausaxs::hist::detail::xyzw::HexaEvaluatedResultRounded ausaxs::hist::detail::CompactCoordinatesXYZW<vbw>::evaluate_rounded_16_avx512(
659 std::span<const CompactCoordinatesXYZW, 16> others
660 ) const noexcept {
661 const float* p = reinterpret_cast<const float*>(others.data());
662 __m512 v03 = _mm512_loadu_ps(p);
663 __m512 v47 = _mm512_loadu_ps(p + 16);
664 __m512 v811 = _mm512_loadu_ps(p + 32);
665 __m512 v1215 = _mm512_loadu_ps(p + 48);
666
667 const __m512i gather_w = _mm512_setr_epi32(3, 7, 11, 15, 19, 23, 27, 31, 0, 0, 0, 0, 0, 0, 0, 0);
668 __m256 w_lo = _mm512_castps512_ps256(_mm512_permutex2var_ps(v03, gather_w, v47));
669 __m256 w_hi = _mm512_castps512_ps256(_mm512_permutex2var_ps(v811, gather_w, v1215));
670 __m512 weights = _mm512_mul_ps(_mm512_set1_ps(value.w),
671 _mm512_insertf32x8(_mm512_castps256_ps512(w_lo), w_hi, 1));
672
673 __m512 svv = _mm512_broadcast_f32x4(_mm_load_ps(this->data.data()));
674 __m512 d03 = _mm512_sub_ps(svv, v03);
675 __m512 d47 = _mm512_sub_ps(svv, v47);
676 __m512 d811 = _mm512_sub_ps(svv, v811);
677 __m512 d1215 = _mm512_sub_ps(svv, v1215);
678 d03 = _mm512_mul_ps(d03, d03);
679 d47 = _mm512_mul_ps(d47, d47);
680 d811 = _mm512_mul_ps(d811, d811);
681 d1215 = _mm512_mul_ps(d1215, d1215);
682
683 const __m512i gather_x = _mm512_setr_epi32(0, 4, 8, 12, 16, 20, 24, 28, 0, 0, 0, 0, 0, 0, 0, 0);
684 const __m512i gather_y = _mm512_setr_epi32(1, 5, 9, 13, 17, 21, 25, 29, 0, 0, 0, 0, 0, 0, 0, 0);
685 const __m512i gather_z = _mm512_setr_epi32(2, 6, 10, 14, 18, 22, 26, 30, 0, 0, 0, 0, 0, 0, 0, 0);
686
687 __m256 rx_lo = _mm512_castps512_ps256(_mm512_permutex2var_ps(d03, gather_x, d47));
688 __m256 rx_hi = _mm512_castps512_ps256(_mm512_permutex2var_ps(d811, gather_x, d1215));
689 __m512 row_x = _mm512_insertf32x8(_mm512_castps256_ps512(rx_lo), rx_hi, 1);
690
691 __m256 ry_lo = _mm512_castps512_ps256(_mm512_permutex2var_ps(d03, gather_y, d47));
692 __m256 ry_hi = _mm512_castps512_ps256(_mm512_permutex2var_ps(d811, gather_y, d1215));
693 __m512 row_y = _mm512_insertf32x8(_mm512_castps256_ps512(ry_lo), ry_hi, 1);
694
695 __m256 rz_lo = _mm512_castps512_ps256(_mm512_permutex2var_ps(d03, gather_z, d47));
696 __m256 rz_hi = _mm512_castps512_ps256(_mm512_permutex2var_ps(d811, gather_z, d1215));
697 __m512 row_z = _mm512_insertf32x8(_mm512_castps256_ps512(rz_lo), rz_hi, 1);
698
699 __m512i dist_bin = _mm512_cvtps_epi32(_mm512_mul_ps(
700 _mm512_sqrt_ps(_mm512_add_ps(_mm512_add_ps(row_x, row_y), row_z)),
701 _mm512_set1_ps(get_inv_width())));
702
703 xyzw::HexaEvaluatedResultRounded result;
704 _mm512_store_si512(reinterpret_cast<__m512i*>(result.distances.data()), dist_bin);
705 _mm512_store_ps(reinterpret_cast<float*>(result.weights.data()), weights);
706 return result;
707 }
708 #endif