+/* --- @rand_quick@ --- *
+ *
+ * Arguments: @rand_pool *r@ = pointer to a randomness pool
+ *
+ * Returns: Zero on success; @-1@ on failure.
+ *
+ * Use Attempts to use some machine-specific `quick' source of
+ * entropy to top up @r@. This may not do anything at all on
+ * many systems.
+ */
+
+CPU_DISPATCH(static, return, int, quick, (rand_pool *r), (r),
+ pick_quick, trivial_quick);
+
+static int trivial_quick(rand_pool *r) { return (-1); }
+
+#if __GNUC__ && (CPUFAM_X86 || CPUFAM_AMD64)
+static int rdrand_quick(rand_pool *r)
+{
+ unsigned long rr;
+ unsigned char w;
+ int i;
+
+ for (i = 0; i < 16; i++) {
+ __asm__ ("rdrand %0; setc %1" : "=r" (rr), "=g" (w) : : "cc");
+ if (w) {
+ rand_add(r, &rr, sizeof(rr), 8*sizeof(rr));
+ return (0);
+ }
+ }
+ return (-1);
+}
+#endif
+
+static quick__functype *pick_quick(void)
+{
+#if __GNUC__ && (CPUFAM_X86 || CPUFAM_AMD64)
+ DISPATCH_PICK_COND(rand_quick, rdrand_quick,
+ cpu_feature_p(CPUFEAT_X86_RDRAND));
+#endif
+ DISPATCH_PICK_FALLBACK(rand_quick, trivial_quick);
+}
+
+int rand_quick(rand_pool *r) { RAND_RESOLVE(r); return (quick(r)); }
+