X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/7b0d1a63587f3cb1ae3bb8b248bbb1b82bdca7bd..6c0946ef1f1fa9b75b8c6d65ea0554ff4e1ec4eb:/rand/rand-x86ish.S diff --git a/rand/rand-x86ish.S b/rand/rand-x86ish.S index 829bc2cd..b91a9373 100644 --- a/rand/rand-x86ish.S +++ b/rand/rand-x86ish.S @@ -37,28 +37,64 @@ ///-------------------------------------------------------------------------- /// Quick random generation. +// Common register allocation. +#if CPUFAM_X86 +# define COUNT ecx +#endif +#if CPUFAM_AMD64 && ABI_SYSV +# define COUNT ecx +#endif +#if CPUFAM_AMD64 && ABI_WIN +# define COUNT r8d +#endif + FUNC(rand_quick_x86ish_rdrand) // Enter with a pointer to the random context in the first argument. // Return zero on success, or -1 on error. #if CPUFAM_X86 - mov edx, [esp + 4] + mov edx, [SP + 4] stalloc 28 -# define COUNT ecx #endif #if CPUFAM_AMD64 && ABI_SYSV stalloc 8 -# define COUNT ecx #endif #if CPUFAM_AMD64 && ABI_WIN stalloc 40 -# define COUNT r8d #endif endprologue // Try to fetch a random number. mov COUNT, 16 -0: rdrand R_a(r) +0: rdrand AX + jc 1f + dec COUNT + jnz 0b + + // Failed. + mov eax, -1 + jmp 9f +ENDFUNC + +FUNC(rand_quick_x86ish_rdseed) + // Enter with a pointer to the random context in the first argument. + // Return zero on success, or -1 on error. + +#if CPUFAM_X86 + mov edx, [SP + 4] + stalloc 28 +#endif +#if CPUFAM_AMD64 && ABI_SYSV + stalloc 8 +#endif +#if CPUFAM_AMD64 && ABI_WIN + stalloc 40 +#endif + endprologue + + // Try to fetch a random number. + mov COUNT, 16 +0: rdseed AX jc 1f dec COUNT jnz 0b @@ -70,22 +106,22 @@ FUNC(rand_quick_x86ish_rdrand) // Success. 1: #if CPUFAM_X86 - mov [esp + 16], eax - lea ecx, [esp + 16] - mov dword ptr [esp + 12], 32 - mov dword ptr [esp + 8], 4 - mov [esp + 4], ecx - mov [esp + 0], edx + mov [SP + 16], AX + lea ecx, [SP + 16] + mov dword ptr [SP + 12], 32 + mov dword ptr [SP + 8], 4 + mov [SP + 4], ecx + mov [SP + 0], edx #endif #if CPUFAM_AMD64 && ABI_SYSV - mov [rsp + 0], rax - mov rsi, rsp + mov [SP + 0], AX + mov rsi, SP mov edx, 8 mov ecx, 64 #endif #if CPUFAM_AMD64 && ABI_WIN - mov [rsp + 32], rax - lea rdx, [rsp + 32] + mov [SP + 32], AX + lea rdx, [SP + 32] mov r8d, 8 mov r9d, 64 #endif @@ -106,4 +142,6 @@ FUNC(rand_quick_x86ish_rdrand) ret ENDFUNC +#undef COUNT + ///----- That's all, folks --------------------------------------------------