2020-11-23 13:17:47 +01:00
|
|
|
/* See if various BMI2 instructions give expected results */
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
2022-08-25 12:58:18 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
#ifdef __x86_64
|
|
|
|
typedef uint64_t reg_t;
|
|
|
|
#else
|
|
|
|
typedef uint32_t reg_t;
|
|
|
|
#endif
|
|
|
|
|
2022-08-25 12:58:18 +02:00
|
|
|
#define insn1q(name, arg0) \
|
2023-01-15 00:05:41 +01:00
|
|
|
static inline reg_t name##q(reg_t arg0) \
|
2022-08-25 12:58:18 +02:00
|
|
|
{ \
|
2023-01-15 00:05:41 +01:00
|
|
|
reg_t result64; \
|
2022-08-25 12:58:18 +02:00
|
|
|
asm volatile (#name "q %1, %0" : "=r"(result64) : "rm"(arg0)); \
|
|
|
|
return result64; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define insn1l(name, arg0) \
|
2023-01-15 00:05:41 +01:00
|
|
|
static inline reg_t name##l(reg_t arg0) \
|
2022-08-25 12:58:18 +02:00
|
|
|
{ \
|
2023-01-15 00:05:41 +01:00
|
|
|
reg_t result32; \
|
2022-08-25 12:58:18 +02:00
|
|
|
asm volatile (#name "l %k1, %k0" : "=r"(result32) : "rm"(arg0)); \
|
|
|
|
return result32; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define insn2q(name, arg0, c0, arg1, c1) \
|
2023-01-15 00:05:41 +01:00
|
|
|
static inline reg_t name##q(reg_t arg0, reg_t arg1) \
|
2022-08-25 12:58:18 +02:00
|
|
|
{ \
|
2023-01-15 00:05:41 +01:00
|
|
|
reg_t result64; \
|
2022-08-25 12:58:18 +02:00
|
|
|
asm volatile (#name "q %2, %1, %0" : "=r"(result64) : c0(arg0), c1(arg1)); \
|
|
|
|
return result64; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define insn2l(name, arg0, c0, arg1, c1) \
|
2023-01-15 00:05:41 +01:00
|
|
|
static inline reg_t name##l(reg_t arg0, reg_t arg1) \
|
2022-08-25 12:58:18 +02:00
|
|
|
{ \
|
2023-01-15 00:05:41 +01:00
|
|
|
reg_t result32; \
|
2022-08-25 12:58:18 +02:00
|
|
|
asm volatile (#name "l %k2, %k1, %k0" : "=r"(result32) : c0(arg0), c1(arg1)); \
|
|
|
|
return result32; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __x86_64
|
|
|
|
insn2q(pext, src, "r", mask, "rm")
|
|
|
|
insn2q(pdep, src, "r", mask, "rm")
|
|
|
|
insn2q(andn, clear, "rm", val, "r")
|
|
|
|
insn2q(bextr, range, "rm", val, "r")
|
|
|
|
insn2q(bzhi, pos, "rm", val, "r")
|
|
|
|
insn2q(rorx, val, "r", n, "i")
|
|
|
|
insn2q(sarx, val, "rm", n, "r")
|
|
|
|
insn2q(shlx, val, "rm", n, "r")
|
|
|
|
insn2q(shrx, val, "rm", n, "r")
|
|
|
|
insn1q(blsi, src)
|
|
|
|
insn1q(blsmsk, src)
|
|
|
|
insn1q(blsr, src)
|
|
|
|
#endif
|
|
|
|
insn2l(pext, src, "r", mask, "rm")
|
|
|
|
insn2l(pdep, src, "r", mask, "rm")
|
|
|
|
insn2l(andn, clear, "rm", val, "r")
|
|
|
|
insn2l(bextr, range, "rm", val, "r")
|
|
|
|
insn2l(bzhi, pos, "rm", val, "r")
|
|
|
|
insn2l(rorx, val, "r", n, "i")
|
|
|
|
insn2l(sarx, val, "rm", n, "r")
|
|
|
|
insn2l(shlx, val, "rm", n, "r")
|
|
|
|
insn2l(shrx, val, "rm", n, "r")
|
|
|
|
insn1l(blsi, src)
|
|
|
|
insn1l(blsmsk, src)
|
|
|
|
insn1l(blsr, src)
|
2020-11-23 13:17:47 +01:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
uint64_t ehlo = 0x202020204f4c4845ull;
|
|
|
|
uint64_t mask = 0xa080800302020001ull;
|
2023-01-15 00:05:41 +01:00
|
|
|
reg_t result;
|
2020-11-23 13:17:47 +01:00
|
|
|
|
|
|
|
#ifdef __x86_64
|
|
|
|
/* 64 bits */
|
2023-01-15 00:05:41 +01:00
|
|
|
result = andnq(mask, ehlo);
|
|
|
|
assert(result == 0x002020204d4c4844);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = pextq(ehlo, mask);
|
|
|
|
assert(result == 133);
|
2020-11-23 13:17:47 +01:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = pdepq(result, mask);
|
|
|
|
assert(result == (ehlo & mask));
|
2020-11-23 13:17:47 +01:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = pextq(-1ull, mask);
|
|
|
|
assert(result == 511); /* mask has 9 bits set */
|
2020-11-23 13:17:47 +01:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = pdepq(-1ull, mask);
|
|
|
|
assert(result == mask);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bextrq(mask, 0x3f00);
|
|
|
|
assert(result == (mask & ~INT64_MIN));
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bextrq(mask, 0x1038);
|
|
|
|
assert(result == 0xa0);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bextrq(mask, 0x10f8);
|
|
|
|
assert(result == 0);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:42 +01:00
|
|
|
result = bextrq(0xfedcba9876543210ull, 0x7f00);
|
|
|
|
assert(result == 0xfedcba9876543210ull);
|
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = blsiq(0x30);
|
|
|
|
assert(result == 0x10);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = blsiq(0x30ull << 32);
|
|
|
|
assert(result == 0x10ull << 32);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = blsmskq(0x30);
|
|
|
|
assert(result == 0x1f);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = blsrq(0x30);
|
|
|
|
assert(result == 0x20);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = blsrq(0x30ull << 32);
|
|
|
|
assert(result == 0x20ull << 32);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bzhiq(mask, 0x3f);
|
|
|
|
assert(result == (mask & ~INT64_MIN));
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bzhiq(mask, 0x1f);
|
|
|
|
assert(result == (mask & ~(-1 << 30)));
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:32:06 +01:00
|
|
|
result = bzhiq(mask, 0x40);
|
|
|
|
assert(result == mask);
|
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = rorxq(0x2132435465768798, 8);
|
|
|
|
assert(result == 0x9821324354657687);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = sarxq(0xffeeddccbbaa9988, 8);
|
|
|
|
assert(result == 0xffffeeddccbbaa99);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = sarxq(0x77eeddccbbaa9988, 8 | 64);
|
|
|
|
assert(result == 0x0077eeddccbbaa99);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = shrxq(0xffeeddccbbaa9988, 8);
|
|
|
|
assert(result == 0x00ffeeddccbbaa99);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = shrxq(0x77eeddccbbaa9988, 8 | 192);
|
|
|
|
assert(result == 0x0077eeddccbbaa99);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = shlxq(0xffeeddccbbaa9988, 8);
|
|
|
|
assert(result == 0xeeddccbbaa998800);
|
2020-11-23 13:17:47 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 32 bits */
|
2023-01-15 00:05:41 +01:00
|
|
|
result = andnl(mask, ehlo);
|
|
|
|
assert(result == 0x04d4c4844);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = pextl((uint32_t) ehlo, mask);
|
|
|
|
assert(result == 5);
|
2020-11-23 13:17:47 +01:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = pdepl(result, mask);
|
|
|
|
assert(result == (uint32_t)(ehlo & mask));
|
2020-11-23 13:17:47 +01:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = pextl(-1u, mask);
|
|
|
|
assert(result == 7); /* mask has 3 bits set */
|
2020-11-23 13:17:47 +01:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = pdepl(-1u, mask);
|
|
|
|
assert(result == (uint32_t)mask);
|
2020-11-23 13:17:47 +01:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bextrl(mask, 0x1f00);
|
|
|
|
assert(result == (mask & ~INT32_MIN));
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bextrl(ehlo, 0x1018);
|
|
|
|
assert(result == 0x4f);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bextrl(mask, 0x1038);
|
|
|
|
assert(result == 0);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:42 +01:00
|
|
|
result = bextrl((reg_t)0x8f635a775ad3b9b4ull, 0x3018);
|
|
|
|
assert(result == 0x5a);
|
|
|
|
|
|
|
|
result = bextrl((reg_t)0xfedcba9876543210ull, 0x7f00);
|
|
|
|
assert(result == 0x76543210u);
|
|
|
|
|
|
|
|
result = bextrl(-1, 0);
|
|
|
|
assert(result == 0);
|
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = blsil(0xffff);
|
|
|
|
assert(result == 1);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = blsmskl(0x300);
|
|
|
|
assert(result == 0x1ff);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = blsrl(0xffc);
|
|
|
|
assert(result == 0xff8);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = bzhil(mask, 0xf);
|
|
|
|
assert(result == 1);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = rorxl(0x65768798, 8);
|
|
|
|
assert(result == 0x98657687);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = sarxl(0xffeeddcc, 8);
|
|
|
|
assert(result == 0xffffeedd);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = sarxl(0x77eeddcc, 8 | 32);
|
|
|
|
assert(result == 0x0077eedd);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = shrxl(0xffeeddcc, 8);
|
|
|
|
assert(result == 0x00ffeedd);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = shrxl(0x77eeddcc, 8 | 128);
|
|
|
|
assert(result == 0x0077eedd);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2023-01-15 00:05:41 +01:00
|
|
|
result = shlxl(0xffeeddcc, 8);
|
|
|
|
assert(result == 0xeeddcc00);
|
2022-08-25 12:58:18 +02:00
|
|
|
|
2020-11-23 13:17:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|