Special Relativity in Financial Modeling 1.0.0
Lorentz transforms, spacetime classification, and geodesic price paths for quantitative finance
Loading...
Searching...
No Matches
beta_scalar.cpp
Go to the documentation of this file.
1/**
2 * @file beta_scalar.cpp
3 * @brief Scalar reference implementation of the beta batch kernel.
4 *
5 * Module: src/simd/
6 * Owner: AGT-08 — 2026-03-01
7 *
8 * Algorithm: Batch-max
9 * --------------------
10 * 1. Compute batch_max = max(|velocities[i]|) over all i in [0, n).
11 * 2. Update running_max = max(running_max, batch_max).
12 * 3. For each i: beta_i = |velocities[i]| / running_max.
13 * 4. Clamp each beta_i to BETA_CLAMP_LIMIT.
14 *
15 * Why batch-max (not element-wise max)?
16 * --------------------------------------
17 * Using the maximum of the *entire batch* as the denominator allows all SIMD
18 * variants to produce bit-identical results: AVX-512, AVX2, and scalar all
19 * compute the same running_max and perform identical divisions.
20 * Element-wise running_max updates introduce a serial dependency that breaks
21 * SIMD parallelism AND causes different betas depending on vector width.
22 *
23 * Correctness: beta_i = |v_i| / running_max ≤ 1.0 because running_max ≥ batch_max ≥ |v_i|.
24 * The invariant beta_i ∈ [0, BETA_MAX_SAFE) is always preserved.
25 */
26
27#include "simd_batch_detail.hpp"
28#include "momentum/momentum.hpp"
29
30#include <cmath>
31#include <algorithm>
32
33namespace srfm::simd::detail {
34
35static constexpr double BETA_CLAMP_LIMIT =
37
39 const double* SRFM_RESTRICT velocities,
40 std::size_t n,
41 double& running_max,
42 double* SRFM_RESTRICT out) noexcept
43{
44 if (n == 0) return;
45
46 // ── Pass 1: find batch max ───────────────────────────────────────────────
47 double batch_max = 0.0;
48 for (std::size_t i = 0; i < n; ++i) {
49 const double abs_v = std::abs(velocities[i]);
50 if (abs_v > batch_max) batch_max = abs_v;
51 }
52
53 // ── Update running max ───────────────────────────────────────────────────
54 if (batch_max > running_max) running_max = batch_max;
55
56 // ── Pass 2: compute betas ────────────────────────────────────────────────
57 // If running_max is 0, all velocities are 0, so all betas are 0.
58 const double denom = (running_max > 0.0) ? running_max : 1.0;
59 for (std::size_t i = 0; i < n; ++i) {
60 const double abs_v = std::abs(velocities[i]);
61 double beta = abs_v / denom;
62 if (beta > BETA_CLAMP_LIMIT) beta = BETA_CLAMP_LIMIT;
63 out[i] = beta;
64 }
65}
66
67} // namespace srfm::simd::detail
constexpr double BETA_MAX_SAFE
Definition momentum.hpp:62
static constexpr double BETA_CLAMP_LIMIT
Definition beta_avx2.cpp:30
void compute_beta_scalar(const double *__restrict__ velocities, std::size_t n, double &running_max, double *__restrict__ out) noexcept
Scalar reference implementation of the beta batch kernel.
Internal raw-double compute signatures for SIMD batch kernels.
#define SRFM_RESTRICT
Momentum-Velocity Signal Processor (AGT-03 / SRFM)