// File: kernel.cpp
#include "loop_028.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_028(struct loop_028_data * data)

{
  double * input1 = data->input1;
  double * input2 = data->input2;
  double * output = data->output;
  int64_t size = data->size;

  svbool_t p;
  FOR_LOOP_64(int64_t, i, 0, size, p) {
    svfloat64_t inp1 = svld1(p, input1 + i);
    svfloat64_t inp2 = svld1(p, input2 + i);

    svfloat64_t inv2 = svrecpe(inp2);
    svfloat64_t stp2;
    for (int j = 0; j < 3; j++) {
      stp2 = svrecps(inp2, inv2);
      inv2 = svmul_m(p, inv2, stp2);
    }

    svfloat64_t out = svmul_m(p, inv2, inp1);
    svst1(p, output + i, out);
  }
}