relinker: lower ADCX/ADOX under --to-rosetta? #7157

Open
opened 2026-10-17 14:23:44 +00:00 by Destroyers212x · 5 comments
Destroyers212x commented 2026-10-17 14:23:44 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Destroyers212x on 2026-10-17T14:23:44Z.


docs/dev/debt/relinker.md says ADCX/ADOX fault under Rosetta and aren't converted, because compilers don't emit them and code that checks CPUID sees no ADX. I confirmed they still raise SIGILL on an M1 with macOS 26.6.2 (details in #7143). Of the instructions I tested that fault there, they're the only ones the relinker doesn't handle.

Since the PS5's Zen 2 CPU has ADX, code built for that fixed target could use them without a CPUID check, for example a bignum or crypto routine compiled with -madx. I don't know of a title that does.

Would you accept a --to-rosetta lowering for them, following the ANDN lowering (Intel SDM semantics: ADCX reads and writes only CF, ADOX only OF, all other flags preserved, with converter tests and an execution test under Rosetta)? Or would you rather keep them unconverted until a title needs it?

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5899320029 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7157). Originally posted by [Destroyers212x](https://github.com/Destroyers212x) on 2026-10-17T14:23:44Z. --- `docs/dev/debt/relinker.md` says ADCX/ADOX fault under Rosetta and aren't converted, because compilers don't emit them and code that checks CPUID sees no ADX. I confirmed they still raise SIGILL on an M1 with macOS 26.6.2 (details in #7143). Of the instructions I tested that fault there, they're the only ones the relinker doesn't handle. Since the PS5's Zen 2 CPU has ADX, code built for that fixed target could use them without a CPUID check, for example a bignum or crypto routine compiled with `-madx`. I don't know of a title that does. Would you accept a `--to-rosetta` lowering for them, following the ANDN lowering (Intel SDM semantics: ADCX reads and writes only CF, ADOX only OF, all other flags preserved, with converter tests and an execution test under Rosetta)? Or would you rather keep them unconverted until a title needs it? AI-assisted: yes (Claude Code)
plokmklo commented 2026-10-18 05:45:49 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by plokmklo on 2026-10-18T05:45:49Z.


Since the question here was whether such a lowering would be accepted, I went ahead and implemented it on https://github.com/plokmklo/AnyPS5/tree/contrib/7157-adcx-adox (commit 5275b4016), following the ANDN lowering as the template.

ADCX (66 0F 38 F6, VEX pp=1) and ADOX (F3 0F 38 F6, VEX pp=3) are lowered under --to-rosetta with the Intel SDM contract: ADCX writes only CF, ADOX only OF, and every other flag is preserved. The stub captures the incoming flags with PUSHF before the arithmetic, stages the source into a scratch register, chains the carry with ADC, and moves the result carry to CF or OF through SETO/NEG plus AND/OR on the scratch, so the untouched flags come back from the POPF exactly as they were. An RSP destination is computed in a stack slot and written back with a final move, so the stub frame itself never moves. Legacy and VEX encodings, memory sources and 32-bit forms are covered.

Tests: a new converter/execution suite (adcx_adox_lowering) runs every register combination against four flag images and three operand patterns on Linux x86-64, checking the result, all sixteen registers, the untouched flags and the red zone — 195/195 relinker tests pass with it, and check_conventions.py is clean. The relinker debt entry now reflects the new handling.

I don't have a title that emits ADX, so the Rosetta execution run and a real fixed-target module are still untested — the lowering follows the SDM semantics the ANDN entry set as the house pattern. Happy to open the PR whenever the queue allows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6246230178 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7157#issuecomment-6246230178). Originally posted by [plokmklo](https://github.com/plokmklo) on 2026-10-18T05:45:49Z. --- Since the question here was whether such a lowering would be accepted, I went ahead and implemented it on `https://github.com/plokmklo/AnyPS5/tree/contrib/7157-adcx-adox` (commit `5275b4016`), following the ANDN lowering as the template. ADCX (`66 0F 38 F6`, VEX pp=1) and ADOX (`F3 0F 38 F6`, VEX pp=3) are lowered under `--to-rosetta` with the Intel SDM contract: ADCX writes only CF, ADOX only OF, and every other flag is preserved. The stub captures the incoming flags with PUSHF before the arithmetic, stages the source into a scratch register, chains the carry with ADC, and moves the result carry to CF or OF through SETO/NEG plus AND/OR on the scratch, so the untouched flags come back from the POPF exactly as they were. An RSP destination is computed in a stack slot and written back with a final move, so the stub frame itself never moves. Legacy and VEX encodings, memory sources and 32-bit forms are covered. Tests: a new converter/execution suite (`adcx_adox_lowering`) runs every register combination against four flag images and three operand patterns on Linux x86-64, checking the result, all sixteen registers, the untouched flags and the red zone — 195/195 relinker tests pass with it, and `check_conventions.py` is clean. The relinker debt entry now reflects the new handling. I don't have a title that emits ADX, so the Rosetta execution run and a real fixed-target module are still untested — the lowering follows the SDM semantics the ANDN entry set as the house pattern. Happy to open the PR whenever the queue allows.
MarcZX10 commented 2026-10-18 05:48:29 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T05:48:29Z.


Thanks for doing the independent implementation and test work. I already have open PR #7173 for the same --to-rosetta ADCX/ADOX lowering; its current head covers 32/64-bit forms, register and memory operands, extended registers, carry inputs, boundary cases and generated-stub execution, and its Linux/macOS/Windows relinker CI passed.

Your 195-case matrix may still expose a useful test gap. Before opening a second implementation PR, could you compare its cases with #7173 and point out any encoding or flag-preservation case not already exercised? If there is a distinct gap, we can coordinate a focused follow-up that credits your test contribution.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6246250226 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7157#issuecomment-6246250226). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T05:48:29Z. --- Thanks for doing the independent implementation and test work. I already have open PR #7173 for the same `--to-rosetta` ADCX/ADOX lowering; its current head covers 32/64-bit forms, register and memory operands, extended registers, carry inputs, boundary cases and generated-stub execution, and its Linux/macOS/Windows relinker CI passed. Your 195-case matrix may still expose a useful test gap. Before opening a second implementation PR, could you compare its cases with #7173 and point out any encoding or flag-preservation case not already exercised? If there is a distinct gap, we can coordinate a focused follow-up that credits your test contribution.
MarcZX10 commented 2026-10-18 06:24:28 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T06:24:28Z.


I compared your branch with #7173. The main distinct test gap I found is state preservation: #7173 checks results and arithmetic flags, but does not snapshot all guest GPRs or check the caller red zone after the generated trampoline. Your Execution harness checks both, and it exercises a broader set of register encodings. That looks like a useful test-only follow-up, independent of the lowering. Would you be comfortable with me carrying that preservation coverage into a separate PR while crediting your test contribution? I would leave the VEX-form cases out until their architectural validity is confirmed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6246532955 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7157#issuecomment-6246532955). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T06:24:28Z. --- I compared your branch with #7173. The main distinct test gap I found is state preservation: #7173 checks results and arithmetic flags, but does not snapshot all guest GPRs or check the caller red zone after the generated trampoline. Your `Execution` harness checks both, and it exercises a broader set of register encodings. That looks like a useful test-only follow-up, independent of the lowering. Would you be comfortable with me carrying that preservation coverage into a separate PR while crediting your test contribution? I would leave the VEX-form cases out until their architectural validity is confirmed.
MarcZX10 commented 2026-10-18 12:10:47 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T12:10:47Z.


Following up on the state-preservation gap I identified against #7173: its generated-stub tests do not currently snapshot all guest GPRs or the caller red zone, while your Execution harness checks both. If you agree, I can carry only that focused regression coverage into a separate PR with your test contribution credited. I will leave the lowering and unverified VEX cases out.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249753313 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7157#issuecomment-6249753313). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T12:10:47Z. --- Following up on the state-preservation gap I identified against #7173: its generated-stub tests do not currently snapshot all guest GPRs or the caller red zone, while your `Execution` harness checks both. If you agree, I can carry only that focused regression coverage into a separate PR with your test contribution credited. I will leave the lowering and unverified VEX cases out.
maximilianfv commented 2026-10-22 10:50:33 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-22T10:50:33Z.


ADCX/ADOX still fault on an M1 Pro with macOS 27.0.1

New data point for the Rosetta table in docs/dev/debt/relinker.md, which has M1 results for macOS 15.3.1 and 26.6.2 but none for macOS 27.

Host: Apple M1 Pro, macOS 27.0.1, x86_64 under Rosetta 2 (sysctl.proc_translated = 1). A probe ran each instruction in a forked child and compared its result with the architectural value (LZCNT 1 = 63 rather than BSR's 0, TZCNT 0 = 64, and so on). The results were identical over 3 runs.

Result Instructions
SIGILL ADCX, ADOX, RDSEED, RDPID, CLWB
Execute with correct results ANDN, BEXTR, BLSI, BLSMSK, BLSR, TZCNT, LZCNT, POPCNT, BZHI, MULX, PDEP, PEXT, RORX, SARX, SHLX, SHRX, MOVBE, RDRAND, AVX, AVX2, FMA, F16C
Execute, no observable result CLFLUSHOPT, PREFETCHW
RDTSCP executes, TSC_AUX = 0xaaaaaaab (matches the M5 / 27.0.1 value in the debt entry)

CPUID reports far less than what executes:

  • leaf 1: ecx=0298220f edx=0f8b8b15, so no MOVBE, FMA, AVX, F16C or RDRAND bit
  • leaf 7.0: ebx=ecx=edx=0, so no BMI1, BMI2, AVX2 or ADX bit
  • leaf 0x80000001: ecx=00000001, so no LZCNT (ABM) or PREFETCHW bit

The SIGILL and execute sets match the M1 / macOS 26.6.2 entry exactly. Moving from 26 to 27 did not add ADX on M1, so #7173's lowering is still needed on this host.

Probe source (x86_64, clang++ -arch x86_64 -std=c++20 -O1 probe.cpp)
#include <cpuid.h>
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <sys/wait.h>
#include <unistd.h>

using u64 = std::uint64_t;
using u32 = std::uint32_t;

// Each probe returns 0 when the result matches the architectural value, 1 when it executed with a wrong result.
static int andn() { u64 r, a = 0xF0F0, b = 0xFFFF; asm volatile("andn %2, %1, %0" : "=r"(r) : "r"(a), "r"(b)); return r == 0x0F0F ? 0 : 1; }
static int bextr() { u64 r, s = 0xABCD, c = (8 << 8) | 4; asm volatile("bextr %2, %1, %0" : "=r"(r) : "r"(s), "r"(c)); return r == 0xBC ? 0 : 1; }
static int blsi() { u64 r, s = 0b1100; asm volatile("blsi %1, %0" : "=r"(r) : "r"(s)); return r == 0b100 ? 0 : 1; }
static int blsmsk() { u64 r, s = 0b1100; asm volatile("blsmsk %1, %0" : "=r"(r) : "r"(s)); return r == 0b111 ? 0 : 1; }
static int blsr() { u64 r, s = 0b1100; asm volatile("blsr %1, %0" : "=r"(r) : "r"(s)); return r == 0b1000 ? 0 : 1; }
static int tzcnt() { u64 r, s = 0; asm volatile("tzcnt %1, %0" : "=r"(r) : "r"(s)); return r == 64 ? 0 : 1; }
static int lzcnt() { u64 r, s = 1; asm volatile("lzcnt %1, %0" : "=r"(r) : "r"(s)); return r == 63 ? 0 : 1; }
static int popcnt() { u64 r, s = 0xFF00FF; asm volatile("popcnt %1, %0" : "=r"(r) : "r"(s)); return r == 16 ? 0 : 1; }
static int bzhi() { u64 r, s = ~0ull, n = 8; asm volatile("bzhi %2, %1, %0" : "=r"(r) : "r"(s), "r"(n)); return r == 0xFF ? 0 : 1; }
static int mulx() { u64 lo, hi, b = 1ull << 63; asm volatile("mulx %3, %0, %1" : "=r"(lo), "=r"(hi) : "d"(4ull), "r"(b)); return lo == 0 && hi == 2 ? 0 : 1; }
static int pdep() { u64 r, s = 0b101, m = 0xF0; asm volatile("pdep %2, %1, %0" : "=r"(r) : "r"(s), "r"(m)); return r == 0x50 ? 0 : 1; }
static int pext() { u64 r, s = 0x50, m = 0xF0; asm volatile("pext %2, %1, %0" : "=r"(r) : "r"(s), "r"(m)); return r == 0b101 ? 0 : 1; }
static int rorx() { u64 r, s = 1; asm volatile("rorx $1, %1, %0" : "=r"(r) : "r"(s)); return r == (1ull << 63) ? 0 : 1; }
static int sarx() { u64 r, s = 0x8000000000000000ull, n = 4; asm volatile("sarx %2, %1, %0" : "=r"(r) : "r"(s), "r"(n)); return r == 0xF800000000000000ull ? 0 : 1; }
static int shlx() { u64 r, s = 1, n = 4; asm volatile("shlx %2, %1, %0" : "=r"(r) : "r"(s), "r"(n)); return r == 16 ? 0 : 1; }
static int shrx() { u64 r, s = 16, n = 4; asm volatile("shrx %2, %1, %0" : "=r"(r) : "r"(s), "r"(n)); return r == 1 ? 0 : 1; }
static int movbe() { u32 m = 0x11223344, r; asm volatile("movbe %1, %0" : "=r"(r) : "m"(m)); return r == 0x44332211 ? 0 : 1; }
static int adcx() { u64 a = ~0ull, b = 1; asm volatile("stc\n\tadcx %1, %0" : "+r"(a) : "r"(b) : "cc"); return a == 1 ? 0 : 1; }
static int adox() { u64 a = 5, b = 1; asm volatile("xor %%eax, %%eax\n\tadox %1, %0" : "+r"(a) : "r"(b) : "cc", "rax"); return a == 6 ? 0 : 1; }
static int rdrand() { u64 r; unsigned char ok; asm volatile("rdrand %0; setc %1" : "=r"(r), "=qm"(ok)); return ok ? 0 : 1; }
static int rdseed() { u64 r; unsigned char ok; asm volatile("rdseed %0; setc %1" : "=r"(r), "=qm"(ok)); return ok ? 0 : 1; }
static int rdpid() { u64 r; asm volatile("rdpid %0" : "=r"(r)); return 0; }
static int rdtscp() { u32 lo, hi, aux; asm volatile("rdtscp" : "=a"(lo), "=d"(hi), "=c"(aux)); std::printf("    TSC_AUX=%#x\n", aux); std::fflush(stdout); return 0; }
static int clwb() { static char line[64]; asm volatile("clwb %0" : "+m"(line)); return 0; }
static int clflushopt() { static char line[64]; asm volatile("clflushopt %0" : "+m"(line)); return 0; }
static int prefetchw() { static char line[64]; asm volatile("prefetchw %0" : : "m"(line)); return 0; }
static int avx() { alignas(32) float a[8] = {1, 2, 3, 4, 5, 6, 7, 8}, r[8]; asm volatile("vmovaps %1, %%ymm0\n\tvaddps %%ymm0, %%ymm0, %%ymm1\n\tvmovaps %%ymm1, %0\n\tvzeroupper" : "=m"(r) : "m"(a) : "xmm0", "xmm1"); return r[7] == 16 ? 0 : 1; }
static int avx2() { alignas(32) int a[8] = {1, 2, 3, 4, 5, 6, 7, 8}, r[8]; asm volatile("vmovdqa %1, %%ymm0\n\tvpaddd %%ymm0, %%ymm0, %%ymm1\n\tvmovdqa %%ymm1, %0\n\tvzeroupper" : "=m"(r) : "m"(a) : "xmm0", "xmm1"); return r[7] == 16 ? 0 : 1; }
static int fma() { float a = 2, b = 3, c = 4; asm volatile("vfmadd213ss %2, %1, %0" : "+x"(a) : "x"(b), "x"(c)); return a == 10 ? 0 : 1; }
static int f16c() { alignas(16) float in[4] = {1.5f, 0, 0, 0}; u64 half; asm volatile("vmovups %1, %%xmm0\n\tvcvtps2ph $0, %%xmm0, %%xmm1\n\tvmovq %%xmm1, %0" : "=r"(half) : "m"(in) : "xmm0", "xmm1"); return (half & 0xFFFF) == 0x3E00 ? 0 : 1; }

struct Probe { const char* name; int (*run)(); };

static const Probe probes[] = {
    {"ANDN", andn}, {"BEXTR", bextr}, {"BLSI", blsi}, {"BLSMSK", blsmsk}, {"BLSR", blsr},
    {"TZCNT", tzcnt}, {"LZCNT", lzcnt}, {"POPCNT", popcnt}, {"BZHI", bzhi}, {"MULX", mulx},
    {"PDEP", pdep}, {"PEXT", pext}, {"RORX", rorx}, {"SARX", sarx}, {"SHLX", shlx}, {"SHRX", shrx},
    {"MOVBE", movbe}, {"ADCX", adcx}, {"ADOX", adox}, {"RDRAND", rdrand}, {"RDSEED", rdseed},
    {"RDPID", rdpid}, {"RDTSCP", rdtscp}, {"CLWB", clwb}, {"CLFLUSHOPT", clflushopt},
    {"PREFETCHW", prefetchw}, {"AVX", avx}, {"AVX2", avx2}, {"FMA", fma}, {"F16C", f16c},
};

int main() {
    unsigned a, b, c, d;
    __cpuid(1, a, b, c, d);
    std::printf("CPUID 1: ecx=%08x edx=%08x\n", c, d);
    __cpuid_count(7, 0, a, b, c, d);
    std::printf("CPUID 7.0: ebx=%08x ecx=%08x edx=%08x\n", b, c, d);
    __cpuid(0x80000001, a, b, c, d);
    std::printf("CPUID 0x80000001: ecx=%08x edx=%08x\n", c, d);
    for (const auto& probe : probes) {
        std::fflush(stdout);
        const pid_t child = fork();
        if (child == 0) _exit(probe.run());
        int status = 0;
        waitpid(child, &status, 0);
        const char* verdict = WIFSIGNALED(status) ? strsignal(WTERMSIG(status)) : WEXITSTATUS(status) == 0 ? "executes" : "executes, wrong result";
        std::printf("%-11s %s\n", probe.name, verdict);
    }
}

AI-assisted: yes (Claude Code wrote and ran the probe and the write-up).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6338941182 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7157#issuecomment-6338941182). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-22T10:50:33Z. --- ### ADCX/ADOX still fault on an M1 Pro with macOS 27.0.1 New data point for the Rosetta table in `docs/dev/debt/relinker.md`, which has M1 results for macOS 15.3.1 and 26.6.2 but none for macOS 27. Host: Apple M1 Pro, macOS 27.0.1, x86_64 under Rosetta 2 (`sysctl.proc_translated` = 1). A probe ran each instruction in a forked child and compared its result with the architectural value (`LZCNT 1` = 63 rather than BSR's 0, `TZCNT 0` = 64, and so on). The results were identical over 3 runs. | Result | Instructions | |---|---| | SIGILL | **ADCX, ADOX**, RDSEED, RDPID, CLWB | | Execute with correct results | ANDN, BEXTR, BLSI, BLSMSK, BLSR, TZCNT, LZCNT, POPCNT, BZHI, MULX, PDEP, PEXT, RORX, SARX, SHLX, SHRX, MOVBE, RDRAND, AVX, AVX2, FMA, F16C | | Execute, no observable result | CLFLUSHOPT, PREFETCHW | | RDTSCP | executes, `TSC_AUX` = `0xaaaaaaab` (matches the M5 / 27.0.1 value in the debt entry) | CPUID reports far less than what executes: - leaf 1: `ecx=0298220f edx=0f8b8b15`, so no MOVBE, FMA, AVX, F16C or RDRAND bit - leaf 7.0: `ebx=ecx=edx=0`, so no BMI1, BMI2, AVX2 or ADX bit - leaf 0x80000001: `ecx=00000001`, so no LZCNT (ABM) or PREFETCHW bit The SIGILL and execute sets match the M1 / macOS 26.6.2 entry exactly. Moving from 26 to 27 did not add ADX on M1, so #7173's lowering is still needed on this host. <details><summary>Probe source (x86_64, <code>clang++ -arch x86_64 -std=c++20 -O1 probe.cpp</code>)</summary> ```cpp #include <cpuid.h> #include <csignal> #include <cstdint> #include <cstdio> #include <cstring> #include <sys/wait.h> #include <unistd.h> using u64 = std::uint64_t; using u32 = std::uint32_t; // Each probe returns 0 when the result matches the architectural value, 1 when it executed with a wrong result. static int andn() { u64 r, a = 0xF0F0, b = 0xFFFF; asm volatile("andn %2, %1, %0" : "=r"(r) : "r"(a), "r"(b)); return r == 0x0F0F ? 0 : 1; } static int bextr() { u64 r, s = 0xABCD, c = (8 << 8) | 4; asm volatile("bextr %2, %1, %0" : "=r"(r) : "r"(s), "r"(c)); return r == 0xBC ? 0 : 1; } static int blsi() { u64 r, s = 0b1100; asm volatile("blsi %1, %0" : "=r"(r) : "r"(s)); return r == 0b100 ? 0 : 1; } static int blsmsk() { u64 r, s = 0b1100; asm volatile("blsmsk %1, %0" : "=r"(r) : "r"(s)); return r == 0b111 ? 0 : 1; } static int blsr() { u64 r, s = 0b1100; asm volatile("blsr %1, %0" : "=r"(r) : "r"(s)); return r == 0b1000 ? 0 : 1; } static int tzcnt() { u64 r, s = 0; asm volatile("tzcnt %1, %0" : "=r"(r) : "r"(s)); return r == 64 ? 0 : 1; } static int lzcnt() { u64 r, s = 1; asm volatile("lzcnt %1, %0" : "=r"(r) : "r"(s)); return r == 63 ? 0 : 1; } static int popcnt() { u64 r, s = 0xFF00FF; asm volatile("popcnt %1, %0" : "=r"(r) : "r"(s)); return r == 16 ? 0 : 1; } static int bzhi() { u64 r, s = ~0ull, n = 8; asm volatile("bzhi %2, %1, %0" : "=r"(r) : "r"(s), "r"(n)); return r == 0xFF ? 0 : 1; } static int mulx() { u64 lo, hi, b = 1ull << 63; asm volatile("mulx %3, %0, %1" : "=r"(lo), "=r"(hi) : "d"(4ull), "r"(b)); return lo == 0 && hi == 2 ? 0 : 1; } static int pdep() { u64 r, s = 0b101, m = 0xF0; asm volatile("pdep %2, %1, %0" : "=r"(r) : "r"(s), "r"(m)); return r == 0x50 ? 0 : 1; } static int pext() { u64 r, s = 0x50, m = 0xF0; asm volatile("pext %2, %1, %0" : "=r"(r) : "r"(s), "r"(m)); return r == 0b101 ? 0 : 1; } static int rorx() { u64 r, s = 1; asm volatile("rorx $1, %1, %0" : "=r"(r) : "r"(s)); return r == (1ull << 63) ? 0 : 1; } static int sarx() { u64 r, s = 0x8000000000000000ull, n = 4; asm volatile("sarx %2, %1, %0" : "=r"(r) : "r"(s), "r"(n)); return r == 0xF800000000000000ull ? 0 : 1; } static int shlx() { u64 r, s = 1, n = 4; asm volatile("shlx %2, %1, %0" : "=r"(r) : "r"(s), "r"(n)); return r == 16 ? 0 : 1; } static int shrx() { u64 r, s = 16, n = 4; asm volatile("shrx %2, %1, %0" : "=r"(r) : "r"(s), "r"(n)); return r == 1 ? 0 : 1; } static int movbe() { u32 m = 0x11223344, r; asm volatile("movbe %1, %0" : "=r"(r) : "m"(m)); return r == 0x44332211 ? 0 : 1; } static int adcx() { u64 a = ~0ull, b = 1; asm volatile("stc\n\tadcx %1, %0" : "+r"(a) : "r"(b) : "cc"); return a == 1 ? 0 : 1; } static int adox() { u64 a = 5, b = 1; asm volatile("xor %%eax, %%eax\n\tadox %1, %0" : "+r"(a) : "r"(b) : "cc", "rax"); return a == 6 ? 0 : 1; } static int rdrand() { u64 r; unsigned char ok; asm volatile("rdrand %0; setc %1" : "=r"(r), "=qm"(ok)); return ok ? 0 : 1; } static int rdseed() { u64 r; unsigned char ok; asm volatile("rdseed %0; setc %1" : "=r"(r), "=qm"(ok)); return ok ? 0 : 1; } static int rdpid() { u64 r; asm volatile("rdpid %0" : "=r"(r)); return 0; } static int rdtscp() { u32 lo, hi, aux; asm volatile("rdtscp" : "=a"(lo), "=d"(hi), "=c"(aux)); std::printf(" TSC_AUX=%#x\n", aux); std::fflush(stdout); return 0; } static int clwb() { static char line[64]; asm volatile("clwb %0" : "+m"(line)); return 0; } static int clflushopt() { static char line[64]; asm volatile("clflushopt %0" : "+m"(line)); return 0; } static int prefetchw() { static char line[64]; asm volatile("prefetchw %0" : : "m"(line)); return 0; } static int avx() { alignas(32) float a[8] = {1, 2, 3, 4, 5, 6, 7, 8}, r[8]; asm volatile("vmovaps %1, %%ymm0\n\tvaddps %%ymm0, %%ymm0, %%ymm1\n\tvmovaps %%ymm1, %0\n\tvzeroupper" : "=m"(r) : "m"(a) : "xmm0", "xmm1"); return r[7] == 16 ? 0 : 1; } static int avx2() { alignas(32) int a[8] = {1, 2, 3, 4, 5, 6, 7, 8}, r[8]; asm volatile("vmovdqa %1, %%ymm0\n\tvpaddd %%ymm0, %%ymm0, %%ymm1\n\tvmovdqa %%ymm1, %0\n\tvzeroupper" : "=m"(r) : "m"(a) : "xmm0", "xmm1"); return r[7] == 16 ? 0 : 1; } static int fma() { float a = 2, b = 3, c = 4; asm volatile("vfmadd213ss %2, %1, %0" : "+x"(a) : "x"(b), "x"(c)); return a == 10 ? 0 : 1; } static int f16c() { alignas(16) float in[4] = {1.5f, 0, 0, 0}; u64 half; asm volatile("vmovups %1, %%xmm0\n\tvcvtps2ph $0, %%xmm0, %%xmm1\n\tvmovq %%xmm1, %0" : "=r"(half) : "m"(in) : "xmm0", "xmm1"); return (half & 0xFFFF) == 0x3E00 ? 0 : 1; } struct Probe { const char* name; int (*run)(); }; static const Probe probes[] = { {"ANDN", andn}, {"BEXTR", bextr}, {"BLSI", blsi}, {"BLSMSK", blsmsk}, {"BLSR", blsr}, {"TZCNT", tzcnt}, {"LZCNT", lzcnt}, {"POPCNT", popcnt}, {"BZHI", bzhi}, {"MULX", mulx}, {"PDEP", pdep}, {"PEXT", pext}, {"RORX", rorx}, {"SARX", sarx}, {"SHLX", shlx}, {"SHRX", shrx}, {"MOVBE", movbe}, {"ADCX", adcx}, {"ADOX", adox}, {"RDRAND", rdrand}, {"RDSEED", rdseed}, {"RDPID", rdpid}, {"RDTSCP", rdtscp}, {"CLWB", clwb}, {"CLFLUSHOPT", clflushopt}, {"PREFETCHW", prefetchw}, {"AVX", avx}, {"AVX2", avx2}, {"FMA", fma}, {"F16C", f16c}, }; int main() { unsigned a, b, c, d; __cpuid(1, a, b, c, d); std::printf("CPUID 1: ecx=%08x edx=%08x\n", c, d); __cpuid_count(7, 0, a, b, c, d); std::printf("CPUID 7.0: ebx=%08x ecx=%08x edx=%08x\n", b, c, d); __cpuid(0x80000001, a, b, c, d); std::printf("CPUID 0x80000001: ecx=%08x edx=%08x\n", c, d); for (const auto& probe : probes) { std::fflush(stdout); const pid_t child = fork(); if (child == 0) _exit(probe.run()); int status = 0; waitpid(child, &status, 0); const char* verdict = WIFSIGNALED(status) ? strsignal(WTERMSIG(status)) : WEXITSTATUS(status) == 0 ? "executes" : "executes, wrong result"; std::printf("%-11s %s\n", probe.name, verdict); } } ``` </details> AI-assisted: yes (Claude Code wrote and ran the probe and the write-up).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#7157
No description provided.