// File: kernel.cpp
#include "loop_032.h"
#include <stdint.h>
#include <arm_sve.h>
#define restrict __restrict
#define SC_SVE_ATTR
#define FOR_COND(P, S, I, N) svptest_first(svptrue_b##S(), P = svwhilelt_b##S(I, N))
#define FOR_LOOP(T, I, M, N, P, S, W) for (T I = M; FOR_COND(P, S, I, N); I += svcnt##W())
#define FOR_LOOP_8(T, I, M, N, P)  FOR_LOOP(T, I, M, N, P, 8, b)
#define FOR_LOOP_16(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 16, h)
#define FOR_LOOP_32(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 32, w)
#define FOR_LOOP_64(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 64, d)
static inline uint32_t get_sve_vl(void) {
  uint64_t vl = 0; asm volatile("incb %[vl], all, mul #8" : [vl] "+r"(vl) : :);
  return (uint32_t)vl;
}
static inline uint32_t get_vl(void) { return get_sve_vl(); }

extern "C" void inner_loop_032(struct loop_032_data * input) {
  double *a = input->a;
  double *b = input->b;
  int n = input->n;

  svfloat64_t c = svdup_f64(0.0);
  svuint64_t idx = svindex_u64(4, 5);

  int m = (n - 4) / 5;

  svbool_t p;
  FOR_LOOP_64(int, i, 0, m, p) {
    svfloat64_t a_vec = svld1(p, a + i);
    svfloat64_t b_vec = svld1_gather_index(p, b + 5 * i, idx);
    c = svmls_m(p, c, a_vec, b_vec);
  }

  input->res = svaddv(svptrue_b64(), c);
}