qemu/tests/tcg/loongarch64/test_bit.c
<<
>>
Prefs
   1#include <assert.h>
   2#include <inttypes.h>
   3
   4#define ARRAY_SIZE(X) (sizeof(X) / sizeof(*(X)))
   5#define TEST_CLO(N)                                     \
   6static uint64_t test_clo_##N(uint64_t rj)               \
   7{                                                       \
   8    uint64_t rd = 0;                                    \
   9                                                        \
  10    asm volatile("clo."#N" %0, %1\n\t"                  \
  11                 : "=r"(rd)                             \
  12                 : "r"(rj)                              \
  13                 : );                                   \
  14    return rd;                                          \
  15}
  16
  17#define TEST_CLZ(N)                                     \
  18static uint64_t test_clz_##N(uint64_t rj)               \
  19{                                                       \
  20    uint64_t rd = 0;                                    \
  21                                                        \
  22    asm volatile("clz."#N" %0, %1\n\t"                  \
  23                 : "=r"(rd)                             \
  24                 : "r"(rj)                              \
  25                 : );                                   \
  26    return rd;                                          \
  27}
  28
  29#define TEST_CTO(N)                                     \
  30static uint64_t test_cto_##N(uint64_t rj)               \
  31{                                                       \
  32    uint64_t rd = 0;                                    \
  33                                                        \
  34    asm volatile("cto."#N" %0, %1\n\t"                  \
  35                 : "=r"(rd)                             \
  36                 : "r"(rj)                              \
  37                 : );                                   \
  38    return rd;                                          \
  39}
  40
  41#define TEST_CTZ(N)                                     \
  42static uint64_t test_ctz_##N(uint64_t rj)               \
  43{                                                       \
  44    uint64_t rd = 0;                                    \
  45                                                        \
  46    asm volatile("ctz."#N" %0, %1\n\t"                  \
  47                 : "=r"(rd)                             \
  48                 : "r"(rj)                              \
  49                 : );                                   \
  50    return rd;                                          \
  51}
  52
  53TEST_CLO(w)
  54TEST_CLO(d)
  55TEST_CLZ(w)
  56TEST_CLZ(d)
  57TEST_CTO(w)
  58TEST_CTO(d)
  59TEST_CTZ(w)
  60TEST_CTZ(d)
  61
  62struct vector {
  63    uint64_t (*func)(uint64_t);
  64    uint64_t u;
  65    uint64_t r;
  66};
  67
  68static struct vector vectors[] = {
  69    {test_clo_w, 0xfff11fff392476ab, 0},
  70    {test_clo_d, 0xabd28a64000000, 0},
  71    {test_clz_w, 0xfaffff42392476ab, 2},
  72    {test_clz_d, 0xabd28a64000000, 8},
  73    {test_cto_w, 0xfff11fff392476ab, 2},
  74    {test_cto_d, 0xabd28a64000000, 0},
  75    {test_ctz_w, 0xfaffff42392476ab, 0},
  76    {test_ctz_d, 0xabd28a64000000, 26},
  77};
  78
  79int main()
  80{
  81    int i;
  82
  83    for (i = 0; i < ARRAY_SIZE(vectors); i++) {
  84        assert((*vectors[i].func)(vectors[i].u) == vectors[i].r);
  85    }
  86
  87    return 0;
  88}
  89