1 /* See if various BMI2 instructions give expected results */
7 typedef uint64_t reg_t
;
9 typedef uint32_t reg_t
;
12 #define insn1q(name, arg0) \
13 static inline reg_t name##q(reg_t arg0) \
16 asm volatile (#name "q %1, %0" : "=r"(result64) : "rm"(arg0)); \
20 #define insn1l(name, arg0) \
21 static inline reg_t name##l(reg_t arg0) \
24 asm volatile (#name "l %k1, %k0" : "=r"(result32) : "rm"(arg0)); \
28 #define insn2q(name, arg0, c0, arg1, c1) \
29 static inline reg_t name##q(reg_t arg0, reg_t arg1) \
32 asm volatile (#name "q %2, %1, %0" : "=r"(result64) : c0(arg0), c1(arg1)); \
36 #define insn2l(name, arg0, c0, arg1, c1) \
37 static inline reg_t name##l(reg_t arg0, reg_t arg1) \
40 asm volatile (#name "l %k2, %k1, %k0" : "=r"(result32) : c0(arg0), c1(arg1)); \
45 insn2q(pext
, src
, "r", mask
, "rm")
46 insn2q(pdep
, src
, "r", mask
, "rm")
47 insn2q(andn
, clear
, "rm", val
, "r")
48 insn2q(bextr
, range
, "rm", val
, "r")
49 insn2q(bzhi
, pos
, "rm", val
, "r")
50 insn2q(rorx
, val
, "r", n
, "i")
51 insn2q(sarx
, val
, "rm", n
, "r")
52 insn2q(shlx
, val
, "rm", n
, "r")
53 insn2q(shrx
, val
, "rm", n
, "r")
58 insn2l(pext
, src
, "r", mask
, "rm")
59 insn2l(pdep
, src
, "r", mask
, "rm")
60 insn2l(andn
, clear
, "rm", val
, "r")
61 insn2l(bextr
, range
, "rm", val
, "r")
62 insn2l(bzhi
, pos
, "rm", val
, "r")
63 insn2l(rorx
, val
, "r", n
, "i")
64 insn2l(sarx
, val
, "rm", n
, "r")
65 insn2l(shlx
, val
, "rm", n
, "r")
66 insn2l(shrx
, val
, "rm", n
, "r")
71 int main(int argc
, char *argv
[]) {
72 uint64_t ehlo
= 0x202020204f4c4845ull
;
73 uint64_t mask
= 0xa080800302020001ull
;
78 result
= andnq(mask
, ehlo
);
79 assert(result
== 0x002020204d4c4844);
81 result
= pextq(ehlo
, mask
);
82 assert(result
== 133);
84 result
= pdepq(result
, mask
);
85 assert(result
== (ehlo
& mask
));
87 result
= pextq(-1ull, mask
);
88 assert(result
== 511); /* mask has 9 bits set */
90 result
= pdepq(-1ull, mask
);
91 assert(result
== mask
);
93 result
= bextrq(mask
, 0x3f00);
94 assert(result
== (mask
& ~INT64_MIN
));
96 result
= bextrq(mask
, 0x1038);
97 assert(result
== 0xa0);
99 result
= bextrq(mask
, 0x10f8);
102 result
= bextrq(0xfedcba9876543210ull
, 0x7f00);
103 assert(result
== 0xfedcba9876543210ull
);
105 result
= blsiq(0x30);
106 assert(result
== 0x10);
108 result
= blsiq(0x30ull
<< 32);
109 assert(result
== 0x10ull
<< 32);
111 result
= blsmskq(0x30);
112 assert(result
== 0x1f);
114 result
= blsrq(0x30);
115 assert(result
== 0x20);
117 result
= blsrq(0x30ull
<< 32);
118 assert(result
== 0x20ull
<< 32);
120 result
= bzhiq(mask
, 0x3f);
121 assert(result
== (mask
& ~INT64_MIN
));
123 result
= bzhiq(mask
, 0x1f);
124 assert(result
== (mask
& ~(-1 << 30)));
126 result
= bzhiq(mask
, 0x40);
127 assert(result
== mask
);
129 result
= rorxq(0x2132435465768798, 8);
130 assert(result
== 0x9821324354657687);
132 result
= sarxq(0xffeeddccbbaa9988, 8);
133 assert(result
== 0xffffeeddccbbaa99);
135 result
= sarxq(0x77eeddccbbaa9988, 8 | 64);
136 assert(result
== 0x0077eeddccbbaa99);
138 result
= shrxq(0xffeeddccbbaa9988, 8);
139 assert(result
== 0x00ffeeddccbbaa99);
141 result
= shrxq(0x77eeddccbbaa9988, 8 | 192);
142 assert(result
== 0x0077eeddccbbaa99);
144 result
= shlxq(0xffeeddccbbaa9988, 8);
145 assert(result
== 0xeeddccbbaa998800);
149 result
= andnl(mask
, ehlo
);
150 assert(result
== 0x04d4c4844);
152 result
= pextl((uint32_t) ehlo
, mask
);
155 result
= pdepl(result
, mask
);
156 assert(result
== (uint32_t)(ehlo
& mask
));
158 result
= pextl(-1u, mask
);
159 assert(result
== 7); /* mask has 3 bits set */
161 result
= pdepl(-1u, mask
);
162 assert(result
== (uint32_t)mask
);
164 result
= bextrl(mask
, 0x1f00);
165 assert(result
== (mask
& ~INT32_MIN
));
167 result
= bextrl(ehlo
, 0x1018);
168 assert(result
== 0x4f);
170 result
= bextrl(mask
, 0x1038);
173 result
= bextrl((reg_t
)0x8f635a775ad3b9b4ull
, 0x3018);
174 assert(result
== 0x5a);
176 result
= bextrl((reg_t
)0xfedcba9876543210ull
, 0x7f00);
177 assert(result
== 0x76543210u
);
179 result
= bextrl(-1, 0);
182 result
= blsil(0xffff);
185 result
= blsmskl(0x300);
186 assert(result
== 0x1ff);
188 result
= blsrl(0xffc);
189 assert(result
== 0xff8);
191 result
= bzhil(mask
, 0xf);
194 result
= rorxl(0x65768798, 8);
195 assert(result
== 0x98657687);
197 result
= sarxl(0xffeeddcc, 8);
198 assert(result
== 0xffffeedd);
200 result
= sarxl(0x77eeddcc, 8 | 32);
201 assert(result
== 0x0077eedd);
203 result
= shrxl(0xffeeddcc, 8);
204 assert(result
== 0x00ffeedd);
206 result
= shrxl(0x77eeddcc, 8 | 128);
207 assert(result
== 0x0077eedd);
209 result
= shlxl(0xffeeddcc, 8);
210 assert(result
== 0xeeddcc00);