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