base/dispatch.c, rand/rand.c, and asm: Support `rdseed' for quick noise.
[catacomb] / rand / rand-x86ish.S
index 70907fe..b91a937 100644 (file)
@@ -74,6 +74,34 @@ FUNC(rand_quick_x86ish_rdrand)
        // 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
+
+       // Failed.
+       mov     eax, -1
+       jmp     9f
 
        // Success.
 1: