gitee.com/quant1x/num@v0.3.2/asm/src/floats_test.c (about)

     1  #include "munit.h"
     2  #include "math.h"
     3  
     4  const size_t kVectorLength = 63;
     5  const size_t kIteration = 1;
     6  
     7  /* no simd */
     8  
     9  void mul_const_add_to(float *a, float *b, float *c, int64_t n)
    10  {
    11    for (int64_t i = 0; i < n; i++)
    12    {
    13      c[i] += a[i] * (*b);
    14    }
    15  }
    16  
    17  void mul_const_to(float *a, float *b, float *c, int64_t n)
    18  {
    19    for (int64_t i = 0; i < n; i++)
    20    {
    21      c[i] = a[i] * (*b);
    22    }
    23  }
    24  
    25  void mul_const(float *a, float *b, int64_t n)
    26  {
    27    for (int64_t i = 0; i < n; i++)
    28    {
    29      a[i] *= *b;
    30    }
    31  }
    32  
    33  void mul_to(float *a, float *b, float *c, int64_t n)
    34  {
    35    for (int64_t i = 0; i < n; i++)
    36    {
    37      c[i] = a[i] * b[i];
    38    }
    39  }
    40  
    41  void dot(float *a, float *b, int64_t n, float *ret)
    42  {
    43    *ret = 0;
    44    for (int64_t i = 0; i < n; i++)
    45    {
    46      *ret += a[i] * b[i];
    47    }
    48  }
    49  
    50  int rand_float(float *a, int64_t n)
    51  {
    52    for (int i = 0; i < n; i++)
    53    {
    54      a[i] = munit_rand_double();
    55    }
    56  }
    57  
    58  #if defined(__x86_64__)
    59  
    60  void _mm256_mul_const_add_to(float *a, float *b, float *c, int64_t n);
    61  void _mm256_mul_const_to(float *a, float *b, float *c, int64_t n);
    62  void _mm256_mul_const(float *a, float *b, int64_t n);
    63  void _mm256_mul_to(float *a, float *b, float *c, int64_t n);
    64  void _mm256_dot(float *a, float *b, int64_t n, float *ret);
    65  
    66  void _mm512_mul_const_add_to(float *a, float *b, float *c, int64_t n);
    67  void _mm512_mul_const_to(float *a, float *b, float *c, int64_t n);
    68  void _mm512_mul_const(float *a, float *b, int64_t n);
    69  void _mm512_mul_to(float *a, float *b, float *c, int64_t n);
    70  void _mm512_dot(float *a, float *b, int64_t n, float *ret);
    71  
    72  MunitResult mm256_mul_const_add_to_test(const MunitParameter params[], void *user_data_or_fixture)
    73  {
    74    float a[kVectorLength], expect[kVectorLength], actual[kVectorLength];
    75    rand_float(a, kVectorLength);
    76    rand_float(expect, kVectorLength);
    77    memcpy(expect, actual, sizeof(float) * kVectorLength);
    78    float b = munit_rand_double();
    79  
    80    mul_const_add_to(a, &b, expect, kVectorLength);
    81    _mm256_mul_const_add_to(a, &b, actual, kVectorLength);
    82    munit_assert_floats_equal(kVectorLength, expect, actual);
    83    return MUNIT_OK;
    84  }
    85  
    86  MunitResult mm256_mul_const_to_test(const MunitParameter params[], void *user_data_or_fixture)
    87  {
    88    float a[kVectorLength], expect[kVectorLength], actual[kVectorLength];
    89    rand_float(a, kVectorLength);
    90    float b = munit_rand_double();
    91  
    92    mul_const_to(a, &b, expect, kVectorLength);
    93    _mm256_mul_const_to(a, &b, actual, kVectorLength);
    94    munit_assert_floats_equal(kVectorLength, expect, actual);
    95    return MUNIT_OK;
    96  }
    97  
    98  MunitResult mm256_mul_const_test(const MunitParameter params[], void *user_data_or_fixture)
    99  {
   100    float expect[kVectorLength], actual[kVectorLength];
   101    rand_float(expect, kVectorLength);
   102    memcpy(expect, actual, sizeof(float) * kVectorLength);
   103    float b = munit_rand_double();
   104  
   105    mul_const(expect, &b, kVectorLength);
   106    _mm256_mul_const(actual, &b, kVectorLength);
   107    munit_assert_floats_equal(kVectorLength, expect, actual);
   108    return MUNIT_OK;
   109  }
   110  
   111  MunitResult mm256_mul_to_test(const MunitParameter params[], void *user_data_or_fixture)
   112  {
   113    float a[kVectorLength], b[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   114    rand_float(a, kVectorLength);
   115    rand_float(b, kVectorLength);
   116  
   117    mul_to(a, b, expect, kVectorLength);
   118    _mm256_mul_to(a, b, actual, kVectorLength);
   119    munit_assert_floats_equal(kVectorLength, expect, actual);
   120    return MUNIT_OK;
   121  }
   122  
   123  MunitResult mm256_dot_test(const MunitParameter params[], void *user_data_or_fixture)
   124  {
   125    float a[kVectorLength], b[kVectorLength], expect, actual;
   126    rand_float(a, kVectorLength);
   127    rand_float(b, kVectorLength);
   128  
   129    dot(a, b, kVectorLength, &expect);
   130    _mm256_dot(a, b, kVectorLength, &actual);
   131    munit_assert_float_equal(expect, actual, 5);
   132    return MUNIT_OK;
   133  }
   134  
   135  MunitTest mm256_tests[] = {
   136      {"mul_const_add_to", mm256_mul_const_add_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   137      {"mul_const_to", mm256_mul_const_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   138      {"mul_const", mm256_mul_const_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   139      {"mul_to", mm256_mul_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   140      {"dot", mm256_dot_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   141      {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}};
   142  
   143  static const MunitSuite mm256_suite = {
   144      "mm256_", mm256_tests, NULL, kIteration, MUNIT_SUITE_OPTION_NONE};
   145  
   146  MunitResult mm512_mul_const_add_to_test(const MunitParameter params[], void *user_data_or_fixture)
   147  {
   148    float a[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   149    rand_float(a, kVectorLength);
   150    rand_float(expect, kVectorLength);
   151    memcpy(expect, actual, sizeof(float) * kVectorLength);
   152    float b = munit_rand_double();
   153  
   154    mul_const_add_to(a, &b, expect, kVectorLength);
   155    _mm512_mul_const_add_to(a, &b, actual, kVectorLength);
   156    munit_assert_floats_equal(kVectorLength, expect, actual);
   157    return MUNIT_OK;
   158  }
   159  
   160  MunitResult mm512_mul_const_to_test(const MunitParameter params[], void *user_data_or_fixture)
   161  {
   162    float a[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   163    rand_float(a, kVectorLength);
   164    float b = munit_rand_double();
   165  
   166    mul_const_to(a, &b, expect, kVectorLength);
   167    _mm512_mul_const_to(a, &b, actual, kVectorLength);
   168    munit_assert_floats_equal(kVectorLength, expect, actual);
   169    return MUNIT_OK;
   170  }
   171  
   172  MunitResult mm512_mul_const_test(const MunitParameter params[], void *user_data_or_fixture)
   173  {
   174    float expect[kVectorLength], actual[kVectorLength];
   175    rand_float(expect, kVectorLength);
   176    memcpy(expect, actual, sizeof(float) * kVectorLength);
   177    float b = munit_rand_double();
   178  
   179    mul_const(expect, &b, kVectorLength);
   180    _mm512_mul_const(actual, &b, kVectorLength);
   181    munit_assert_floats_equal(kVectorLength, expect, actual);
   182    return MUNIT_OK;
   183  }
   184  
   185  MunitResult mm512_mul_to_test(const MunitParameter params[], void *user_data_or_fixture)
   186  {
   187    float a[kVectorLength], b[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   188    rand_float(a, kVectorLength);
   189    rand_float(b, kVectorLength);
   190  
   191    mul_to(a, b, expect, kVectorLength);
   192    _mm512_mul_to(a, b, actual, kVectorLength);
   193    munit_assert_floats_equal(kVectorLength, expect, actual);
   194    return MUNIT_OK;
   195  }
   196  
   197  MunitResult mm512_dot_test(const MunitParameter params[], void *user_data_or_fixture)
   198  {
   199    float a[kVectorLength], b[kVectorLength], expect, actual;
   200    rand_float(a, kVectorLength);
   201    rand_float(b, kVectorLength);
   202  
   203    dot(a, b, kVectorLength, &expect);
   204    _mm512_dot(a, b, kVectorLength, &actual);
   205    munit_assert_float_equal(expect, actual, 5);
   206    return MUNIT_OK;
   207  }
   208  
   209  MunitTest mm512_tests[] = {
   210      {"mul_const_add_to", mm512_mul_const_add_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   211      {"mul_const_to", mm512_mul_const_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   212      {"mul_const", mm512_mul_const_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   213      {"mul_to", mm512_mul_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   214      {"dot", mm512_dot_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   215      {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}};
   216  
   217  static const MunitSuite mm512_suite = {
   218      "mm512_", mm512_tests, NULL, kIteration, MUNIT_SUITE_OPTION_NONE};
   219  
   220  int main(int argc, char *const argv[MUNIT_ARRAY_PARAM(argc + 1)])
   221  {
   222    munit_suite_main(&mm256_suite, NULL, argc, argv);
   223    munit_suite_main(&mm512_suite, NULL, argc, argv);
   224    return 0;
   225  }
   226  
   227  #elif defined(__aarch64__)
   228  
   229  void vmul_const_add_to(float *a, float *b, float *c, int64_t n);
   230  void vmul_const_to(float *a, float *b, float *c, int64_t n);
   231  void vmul_const(float *a, float *b, int64_t n);
   232  void vmul_to(float *a, float *b, float *c, int64_t n);
   233  void vdot(float *a, float *b, int64_t n, float *ret);
   234  
   235  MunitResult vmul_const_add_to_test(const MunitParameter params[], void *user_data_or_fixture)
   236  {
   237    float a[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   238    rand_float(a, kVectorLength);
   239    rand_float(expect, kVectorLength);
   240    memcpy(expect, actual, sizeof(float) * kVectorLength);
   241    float b = munit_rand_double();
   242  
   243    mul_const_add_to(a, &b, expect, kVectorLength);
   244    vmul_const_add_to(a, &b, actual, kVectorLength);
   245    munit_assert_floats_equal(kVectorLength, expect, actual);
   246    return MUNIT_OK;
   247  }
   248  
   249  MunitResult vmul_const_to_test(const MunitParameter params[], void *user_data_or_fixture)
   250  {
   251    float a[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   252    rand_float(a, kVectorLength);
   253    float b = munit_rand_double();
   254  
   255    mul_const_to(a, &b, expect, kVectorLength);
   256    vmul_const_to(a, &b, actual, kVectorLength);
   257    munit_assert_floats_equal(kVectorLength, expect, actual);
   258    return MUNIT_OK;
   259  }
   260  
   261  MunitResult vmul_const_test(const MunitParameter params[], void *user_data_or_fixture)
   262  {
   263    float expect[kVectorLength], actual[kVectorLength];
   264    rand_float(expect, kVectorLength);
   265    memcpy(expect, actual, sizeof(float) * kVectorLength);
   266    float b = munit_rand_double();
   267  
   268    mul_const(expect, &b, kVectorLength);
   269    vmul_const(actual, &b, kVectorLength);
   270    munit_assert_floats_equal(kVectorLength, expect, actual);
   271    return MUNIT_OK;
   272  }
   273  
   274  MunitResult vmul_to_test(const MunitParameter params[], void *user_data_or_fixture)
   275  {
   276    float a[kVectorLength], b[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   277    rand_float(a, kVectorLength);
   278    rand_float(b, kVectorLength);
   279  
   280    mul_to(a, b, expect, kVectorLength);
   281    vmul_to(a, b, actual, kVectorLength);
   282    munit_assert_floats_equal(kVectorLength, expect, actual);
   283    return MUNIT_OK;
   284  }
   285  
   286  MunitResult vdot_test(const MunitParameter params[], void *user_data_or_fixture)
   287  {
   288    float a[kVectorLength], b[kVectorLength], expect, actual;
   289    rand_float(a, kVectorLength);
   290    rand_float(b, kVectorLength);
   291  
   292    dot(a, b, kVectorLength, &expect);
   293    vdot(a, b, kVectorLength, &actual);
   294    munit_assert_float_equal(expect, actual, 5);
   295    return MUNIT_OK;
   296  }
   297  
   298  MunitTest vtests[] = {
   299      {"mul_const_add_to", vmul_const_add_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   300      {"mul_const_to", vmul_const_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   301      {"mul_const", vmul_const_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   302      {"mul_to", vmul_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   303      {"dot", vdot_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   304      {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}};
   305  
   306  static const MunitSuite vsuite = {
   307      "v", vtests, NULL, kIteration, MUNIT_SUITE_OPTION_NONE};
   308  
   309  void svmul_const_add_to(float *a, float *b, float *c, long n);
   310  void svmul_const_to(float *a, float *b, float *c, long n);
   311  void svmul_const(float *a, float *b, long n);
   312  void svmul_to(float *a, float *b, float *c, long n);
   313  
   314  MunitResult svmul_const_add_to_test(const MunitParameter params[], void *user_data_or_fixture)
   315  {
   316    float a[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   317    rand_float(a, kVectorLength);
   318    rand_float(expect, kVectorLength);
   319    memcpy(expect, actual, sizeof(float) * kVectorLength);
   320    float b = munit_rand_double();
   321  
   322    mul_const_add_to(a, &b, expect, kVectorLength);
   323    svmul_const_add_to(a, &b, actual, kVectorLength);
   324    munit_assert_floats_equal(kVectorLength, expect, actual);
   325    return MUNIT_OK;
   326  }
   327  
   328  MunitResult svmul_const_to_test(const MunitParameter params[], void *user_data_or_fixture)
   329  {
   330    float a[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   331    rand_float(a, kVectorLength);
   332    float b = munit_rand_double();
   333  
   334    mul_const_to(a, &b, expect, kVectorLength);
   335    svmul_const_to(a, &b, actual, kVectorLength);
   336    munit_assert_floats_equal(kVectorLength, expect, actual);
   337    return MUNIT_OK;
   338  }
   339  
   340  MunitResult svmul_const_test(const MunitParameter params[], void *user_data_or_fixture)
   341  {
   342    float expect[kVectorLength], actual[kVectorLength];
   343    rand_float(expect, kVectorLength);
   344    memcpy(expect, actual, sizeof(float) * kVectorLength);
   345    float b = munit_rand_double();
   346  
   347    mul_const(expect, &b, kVectorLength);
   348    svmul_const(actual, &b, kVectorLength);
   349    munit_assert_floats_equal(kVectorLength, expect, actual);
   350    return MUNIT_OK;
   351  }
   352  
   353  MunitResult svmul_to_test(const MunitParameter params[], void *user_data_or_fixture)
   354  {
   355    float a[kVectorLength], b[kVectorLength], expect[kVectorLength], actual[kVectorLength];
   356    rand_float(a, kVectorLength);
   357    rand_float(b, kVectorLength);
   358  
   359    mul_to(a, b, expect, kVectorLength);
   360    svmul_to(a, b, actual, kVectorLength);
   361    munit_assert_floats_equal(kVectorLength, expect, actual);
   362    return MUNIT_OK;
   363  }
   364  
   365  MunitTest svtests[] = {
   366      {"mul_const_add_to", svmul_const_add_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   367      {"mul_const_to", svmul_const_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   368      {"mul_const", svmul_const_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   369      {"mul_to", svmul_to_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
   370      {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}};
   371  
   372  static const MunitSuite svsuite = {
   373      "v", svtests, NULL, kIteration, MUNIT_SUITE_OPTION_NONE};
   374  
   375  int main(int argc, char *const argv[MUNIT_ARRAY_PARAM(argc + 1)])
   376  {
   377    munit_suite_main(&vsuite, NULL, argc, argv);
   378    munit_suite_main(&svsuite, NULL, argc, argv);
   379    return 0;
   380  }
   381  
   382  #endif