Loading...
Searching...
No Matches
DistanceHwy.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifdef USE_HIGHWAY
6
7// clang-format off
8// Highway dynamic-dispatch: foreach_target.h re-includes this TU per
9// SIMD target; the local header MUST come after it or every target
10// namespace sees a stale single-target definition (redefinition errors).
11#undef HWY_TARGET_INCLUDE
12#define HWY_TARGET_INCLUDE "gpr/auxiliary/DistanceHwy.cpp"
13#include <hwy/foreach_target.h>
14#include <hwy/highway.h>
15
16#include "DistanceHwy.h"
17// clang-format on
18
19HWY_BEFORE_NAMESPACE();
20namespace gpr {
21namespace hwy_kernels {
22namespace HWY_NAMESPACE {
23
24namespace hn = hwy::HWY_NAMESPACE;
25
26void AccumulateInvrDiffSq(double* HWY_RESTRICT dist,
27 const double* HWY_RESTRICT src, double broadcast_val,
28 double scale, int64_t count)
29{
30 const hn::ScalableTag<double> d;
31 const size_t N = hn::Lanes(d);
32
33 const auto v_bcast = hn::Set(d, broadcast_val);
34 const auto v_scale = hn::Set(d, scale);
35
36 // Every lane computes dist + (scale * diff) * diff with separate
37 // roundings, the expression of the scalar tail below and of the
38 // non-Highway loops in Distance.cpp. MulAdd would round once on FMA
39 // targets, and scale * (diff * diff) rounds differently, so either would
40 // make the result follow the element count and the dispatched target.
41 size_t i = 0;
42 for (; i + N <= static_cast<size_t>(count); i += N) {
43 auto v_src = hn::LoadU(d, src + i);
44 auto v_dist = hn::LoadU(d, dist + i);
45 auto v_diff = hn::Sub(v_bcast, v_src);
46 v_dist = hn::Add(v_dist, hn::Mul(hn::Mul(v_scale, v_diff), v_diff));
47 hn::StoreU(v_dist, d, dist + i);
48 }
49
50 // Scalar tail
51 for (; i < static_cast<size_t>(count); ++i) {
52 double diff = broadcast_val - src[i];
53 dist[i] += scale * diff * diff;
54 }
55}
56
57void RecipSqrtInplace(double* HWY_RESTRICT r2, int64_t count)
58{
59 const hn::ScalableTag<double> d;
60 const size_t N = hn::Lanes(d);
61 const auto one = hn::Set(d, 1.0);
62
63 size_t i = 0;
64 for (; i + N <= static_cast<size_t>(count); i += N) {
65 const auto v = hn::LoadU(d, r2 + i);
66 hn::StoreU(hn::Div(one, hn::Sqrt(v)), d, r2 + i);
67 }
68 // Tail through the same operations. Div and Sqrt are correctly rounded
69 // under IEEE-754, so unlike the exp kernels the scalar form would agree
70 // here anyway; keeping one path makes that independent of the reader
71 // knowing which operations carry that guarantee.
72 if (i < static_cast<size_t>(count)) {
73 const size_t rem = static_cast<size_t>(count) - i;
74 const auto v = hn::LoadN(d, r2 + i, rem);
75 hn::StoreN(hn::Div(one, hn::Sqrt(v)), d, r2 + i, rem);
76 }
77}
78
79} // namespace HWY_NAMESPACE
80} // namespace hwy_kernels
81} // namespace gpr
82HWY_AFTER_NAMESPACE();
83
84#if HWY_ONCE
85namespace gpr {
86namespace hwy_kernels {
87
88HWY_EXPORT(AccumulateInvrDiffSq);
89HWY_EXPORT(RecipSqrtInplace);
90
91void recip_sqrt_inplace(double* HWY_RESTRICT r2, int64_t count)
92{
93 HWY_DYNAMIC_DISPATCH(RecipSqrtInplace)(r2, count);
94}
95
96void accumulate_invr_diff_sq(double* HWY_RESTRICT dist,
97 const double* HWY_RESTRICT src,
98 double broadcast_val, double scale, int64_t count)
99{
100 HWY_DYNAMIC_DISPATCH(AccumulateInvrDiffSq)
101 (dist, src, broadcast_val, scale, count);
102}
103
104} // namespace hwy_kernels
105} // namespace gpr
106#endif // HWY_ONCE
107
108#endif // USE_HIGHWAY
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16