// Copyright 2023 Google LLC

#include <cstdio>

#include "crt/printf_traits.h"
#include "tests/kelvin_isa/kelvin_test.h"

#define vpadd_v(T, Vd, Vs)          \
  {                                 \
    if (sizeof(T) == 2) {           \
      if (std::is_signed<T>::value) \
        vpadd_h_v(Vd, Vs);          \
      else                          \
        vpadd_h_u_v(Vd, Vs);        \
    }                               \
    if (sizeof(T) == 4) {           \
      if (std::is_signed<T>::value) \
        vpadd_w_v(Vd, Vs);          \
      else                          \
        vpadd_w_u_v(Vd, Vs);        \
    }                               \
  }

#define vpadd_v_m(T, Vd, Vs)        \
  {                                 \
    if (sizeof(T) == 2) {           \
      if (std::is_signed<T>::value) \
        vpadd_h_v_m(Vd, Vs);        \
      else                          \
        vpadd_h_u_v_m(Vd, Vs);      \
    }                               \
    if (sizeof(T) == 4) {           \
      if (std::is_signed<T>::value) \
        vpadd_w_v_m(Vd, Vs);        \
      else                          \
        vpadd_w_u_v_m(Vd, Vs);      \
    }                               \
  }

template <typename T1, typename T2>
static void test_vpadd_v() {
  constexpr int n = sizeof(T1);
  int lanes;
  getmaxvl(T2, lanes);

  T1 inp[lanes * 2] __attribute__((aligned(64)));
  T2 ref[lanes] __attribute__((aligned(64)));
  T2 dut[lanes] __attribute__((aligned(64)));

  for (int i = 0; i < lanes * 2; ++i) {
    inp[i] = ((i & 1 ? 0xc0 : 0x40) << (8 * (n - 1))) + i - 4;
  }

  for (int i = 0; i < lanes; ++i) {
    ref[i] = T2(inp[2 * i + 0]) + T2(inp[2 * i + 1]);
  }

  vld_x(T1, v16, inp);
  vpadd_v(T2, v0, v16);
  vst_x(T2, v0, dut);

  for (int i = 0; i < lanes; ++i) {
    if (ref[i] != dut[i]) {
      printf("**error vpadd_v[%d] ", i);
      printf(PrintfTraits<T2>::kFmt, ref[i]);
      printf(" ");
      printf(PrintfTraits<T2>::kFmt, dut[i]);
      printf("\n");
      printf("  inputs: ");
      printf(PrintfTraits<T1>::kFmt, inp[2 * i + 0]);
      printf(", ");
      printf(PrintfTraits<T1>::kFmt, inp[2 * i + 1]);
      printf("\n");
      exit(-1);
    }
  }
}

template <typename T1, typename T2>
static void test_vpadd_v_m() {
  constexpr int n = sizeof(T1);
  int lanes;
  getmaxvl_m(T2, lanes);

  T1 inp[lanes * 2] __attribute__((aligned(64)));
  T2 ref[lanes] __attribute__((aligned(64)));
  T2 dut[lanes] __attribute__((aligned(64)));

  for (int i = 0; i < lanes * 2; ++i) {
    inp[i] = ((i & 1 ? 0xc0 : 0x40) << (8 * (n - 1))) + i - 4;
  }

  for (int i = 0; i < lanes; ++i) {
    ref[i] = T2(inp[2 * i + 0]) + T2(inp[2 * i + 1]);
  }

  vld_x_m(T1, v16, inp);
  vpadd_v_m(T2, v0, v16);
  vst_x_m(T2, v0, dut);

  for (int i = 0; i < lanes; ++i) {
    if (ref[i] != dut[i]) {
      printf("**error vpadd_v_m[%d] ", i);
      printf(PrintfTraits<T2>::kFmtHex, ref[i]);
      printf(" ");
      printf(PrintfTraits<T2>::kFmtHex, dut[i]);
      printf("\n");
      exit(-1);
    }
  }
}

int main() {
  test_vpadd_v<int8_t, int16_t>();
  test_vpadd_v<int16_t, int32_t>();
  test_vpadd_v<uint8_t, uint16_t>();
  test_vpadd_v<uint16_t, uint32_t>();

  test_vpadd_v_m<int8_t, int16_t>();
  test_vpadd_v_m<int16_t, int32_t>();
  test_vpadd_v_m<uint8_t, uint16_t>();
  test_vpadd_v_m<uint16_t, uint32_t>();

  return 0;
}
