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