// Copyright 2018-2025 RnD Center "ELVEES", JSC

/*! \file
 *  \brief Тестирование функции minval
 *  \author Фролов Андрей
 */

#include "tests.h"

int main() {
  int failed_count = 0;
  int size[TEST_COUNT] = {512, 1024, 2048, 8192, 16384};
  int print = 0;

  print_table_header();

#ifndef LOCAL_MEM
  void* src0_pos = memalign(64, size[TEST_COUNT - 1] * sizeof(int64_t));
  void* src0_neg = memalign(64, size[TEST_COUNT - 1] * sizeof(int64_t));
  void* src0_rnd = memalign(64, size[TEST_COUNT - 1] * sizeof(int64_t));
#else
#ifdef BARE_METAL
  void* src0_pos = &__local_mem;
#else
  disable_l2_cache();
  void* src0_pos = &xyram_data;
#endif
  void* src0_neg = src0_pos + size[TEST_COUNT - 1] * sizeof(int64_t);
  void* src0_rnd = src0_neg + size[TEST_COUNT - 1] * sizeof(int64_t);
#endif

  create_vector_s16((int16_t*)src0_pos, size[TEST_COUNT - 1], 1);
  create_vector_s16((int16_t*)src0_neg, size[TEST_COUNT - 1], -1);
  create_vector_s16((int16_t*)src0_rnd, size[TEST_COUNT - 1], 0);
  for (int i = 0; i < TEST_COUNT; ++i) {
    int32_t input_bytes = size[i] * sizeof(int16_t) * 1;
    int32_t ti_tics = size[i] / 8 + 29;

#ifndef DISABLE_NEG_TEST_DATA
    printf("| minval_short_neg       | %14d |", size[i]);
    failed_count += test_minval((int16_t*)src0_neg, size[i], print, input_bytes, ti_tics);
#endif

    printf("| minval_short_rnd       | %14d |", size[i]);
    failed_count += test_minval((int16_t*)src0_rnd, size[i], print, input_bytes, ti_tics);

#ifndef DISABLE_POS_TEST_DATA
    printf("| minval_short_pos       | %14d |", size[i]);
    failed_count += test_minval((int16_t*)src0_pos, size[i], print, input_bytes, ti_tics);
#endif
  }

  create_vector_s32((int32_t*)src0_pos, size[TEST_COUNT - 1], 1);
  create_vector_s32((int32_t*)src0_neg, size[TEST_COUNT - 1], -1);
  create_vector_s32((int32_t*)src0_rnd, size[TEST_COUNT - 1], 0);
  for (int i = 0; i < TEST_COUNT; ++i) {
    int32_t input_bytes = size[i] * sizeof(int32_t) * 1;
    int32_t ti_tics = 0;

#ifndef DISABLE_NEG_TEST_DATA
    printf("| minval_int_neg         | %14d |", size[i]);
    failed_count += test_minval32((int32_t*)src0_neg, size[i], print, input_bytes, ti_tics);
#endif

    printf("| minval_int_rnd         | %14d |", size[i]);
    failed_count += test_minval32((int32_t*)src0_rnd, size[i], print, input_bytes, ti_tics);

#ifndef DISABLE_POS_TEST_DATA
    printf("| minval_int_pos         | %14d |", size[i]);
    failed_count += test_minval32((int32_t*)src0_pos, size[i], print, input_bytes, ti_tics);
#endif
  }

  create_vector_float((float*)src0_pos, size[TEST_COUNT - 1], 1);
  create_vector_float((float*)src0_neg, size[TEST_COUNT - 1], -1);
  create_vector_float((float*)src0_rnd, size[TEST_COUNT - 1], 0);
  for (int i = 0; i < TEST_COUNT; ++i) {
    int32_t input_bytes = size[i] * sizeof(float) * 1;
    int32_t ti_tics = (int)(0.5 * size[i] + 25);

#ifndef DISABLE_NEG_TEST_DATA
    printf("| minval_fl_neg          | %14d |", size[i]);
    failed_count += test_minval_fl((float*)src0_neg, size[i], print, input_bytes, ti_tics);
#endif

    printf("| minval_fl_rnd          | %14d |", size[i]);
    failed_count += test_minval_fl((float*)src0_rnd, size[i], print, input_bytes, ti_tics);

#ifndef DISABLE_POS_TEST_DATA
    printf("| minval_fl_pos          | %14d |", size[i]);
    failed_count += test_minval_fl((float*)src0_pos, size[i], print, input_bytes, ti_tics);
#endif
  }

  create_vector_double((double*)src0_pos, size[TEST_COUNT - 1], 1);
  create_vector_double((double*)src0_neg, size[TEST_COUNT - 1], -1);
  create_vector_double((double*)src0_rnd, size[TEST_COUNT - 1], 0);
  for (int i = 0; i < TEST_COUNT; ++i) {
    int32_t input_bytes = size[i] * sizeof(double) * 1;
    int32_t ti_tics = 0;

#ifndef DISABLE_NEG_TEST_DATA
    printf("| minval_db_neg          | %14d |", size[i]);
    failed_count += test_minval_db((double*)src0_neg, size[i], print, input_bytes, ti_tics);
#endif

    printf("| minval_db_rnd          | %14d |", size[i]);
    failed_count += test_minval_db((double*)src0_rnd, size[i], print, input_bytes, ti_tics);

#ifndef DISABLE_POS_TEST_DATA
    printf("| minval_db_pos          | %14d |", size[i]);
    failed_count += test_minval_db((double*)src0_pos, size[i], print, input_bytes, ti_tics);
#endif
  }

#ifndef LOCAL_MEM
  free(src0_rnd);
  free(src0_pos);
  free(src0_neg);
#else
#ifndef BARE_METAL
  enable_l2_cache(L2_CACHE_SIZE);
#endif
#endif

  return failed_count;
}

int test_minval(int16_t* src0, int size, int print, int32_t input_bytes, int32_t ti_tics) {
  int ret = 0;

  uint32_t tic_count[2], instruction_count[2];
  uint32_t ref_tic_count[2], ref_instruction_count[2];

  count_tics(ref_tic_count, ref_instruction_count);
  int16_t res = ref_minval(src0, size);
  count_tics(&ref_tic_count[1], &ref_instruction_count[1]);

  count_tics(tic_count, instruction_count);
  int16_t c = minval(src0, size);
  count_tics(&tic_count[1], &instruction_count[1]);

  if (print) {
    printf("vect1:");
    print_vector_s16(src0, size);
    printf("dsp_res:");
    printf("%d\n", c);
    printf("ref_res:");
    printf("%d\n", res);
  }

  if (c == res) {
    ret = 0;
  } else {
    ret = 1;
  }

  print_performance(ref_tic_count, ref_instruction_count, tic_count, instruction_count, input_bytes, ti_tics);

  if (ret == 0)
    printf(" passed |\n");
  else
    printf(" failed |\n");

  return ret;
}

int test_minval32(int32_t* src0, int size, int print, int32_t input_bytes, int32_t ti_tics) {
  int ret = 0;

  uint32_t tic_count[2], instruction_count[2];
  uint32_t ref_tic_count[2], ref_instruction_count[2];

  count_tics(ref_tic_count, ref_instruction_count);
  int32_t res = ref_minval32(src0, size);
  count_tics(&ref_tic_count[1], &ref_instruction_count[1]);

  count_tics(tic_count, instruction_count);
  int32_t c = minval32(src0, size);
  count_tics(&tic_count[1], &instruction_count[1]);

  if (print) {
    printf("vect1:");
    print_vector_s32(src0, size);
    printf("dsp_res:");
    printf("%d\n", c);
    printf("ref_res:");
    printf("%d\n", res);
  }

  if (c == res) {
    ret = 0;
  } else {
    ret = 1;
  }

  print_performance(ref_tic_count, ref_instruction_count, tic_count, instruction_count, input_bytes, ti_tics);

  if (ret == 0)
    printf(" passed |\n");
  else
    printf(" failed |\n");

  return ret;
}

int test_minval_fl(float* src0, int size, int print, int32_t input_bytes, int32_t ti_tics) {
  int ret = 0;

  uint32_t tic_count[2], instruction_count[2];
  uint32_t ref_tic_count[2], ref_instruction_count[2];

  count_tics(ref_tic_count, ref_instruction_count);
  float res = ref_minval_fl(src0, size);
  count_tics(&ref_tic_count[1], &ref_instruction_count[1]);

  count_tics(tic_count, instruction_count);
  float c = minval_fl(src0, size);
  count_tics(&tic_count[1], &instruction_count[1]);

  if (print) {
    printf("vect1:");
    print_vector_float(src0, size);
    printf("dsp_res:");
    printf("%f\n", c);
    printf("ref_res:");
    printf("%f\n", res);
  }

  if (c == res) {
    ret = 0;
  } else {
    ret = 1;
  }

  print_performance(ref_tic_count, ref_instruction_count, tic_count, instruction_count, input_bytes, ti_tics);

  if (ret == 0)
    printf(" passed |\n");
  else
    printf(" failed |\n");

  return ret;
}

int test_minval_db(double* src0, int size, int print, int32_t input_bytes, int32_t ti_tics) {
  int ret = 0;

  uint32_t tic_count[2], instruction_count[2];
  uint32_t ref_tic_count[2], ref_instruction_count[2];

  count_tics(ref_tic_count, ref_instruction_count);
  double res = ref_minval_db(src0, size);
  count_tics(&ref_tic_count[1], &ref_instruction_count[1]);

  count_tics(tic_count, instruction_count);
  double c = minval_db(src0, size);
  count_tics(&tic_count[1], &instruction_count[1]);

  if (print) {
    printf("vect1:");
    print_vector_double(src0, size);
    printf("dsp_res:");
    printf("%f\n", c);
    printf("ref_res:");
    printf("%f\n", res);
  }

  if (c == res) {
    ret = 0;
  } else {
    ret = 1;
  }

  print_performance(ref_tic_count, ref_instruction_count, tic_count, instruction_count, input_bytes, ti_tics);

  if (ret == 0)
    printf(" passed |\n");
  else
    printf(" failed |\n");

  return ret;
}
