base/dispatch.c, rand/rand.c, and asm: Support `rdseed' for quick noise.
[catacomb] / rand / rand-x86ish.S
index 829bc2c..b91a937 100644 (file)
 ///--------------------------------------------------------------------------
 /// 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 --------------------------------------------------