aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjoern A. Zeeb <bz@FreeBSD.org>2023-05-16 21:03:30 +0000
committerBjoern A. Zeeb <bz@FreeBSD.org>2023-08-07 00:52:41 +0000
commit1dcd1a539275fe69173bfc4dfc32c963fc7dcdbe (patch)
treec57d127640275b8dfe9fcdd346b61a97f4998633
parent676f02a4823f8a4a6e10a1cba73c6324c19b651b (diff)
downloadsrc-1dcd1a539275fe69173bfc4dfc32c963fc7dcdbe.tar.gz
src-1dcd1a539275fe69173bfc4dfc32c963fc7dcdbe.zip
LinuxKPI: implement get_random_u32_inclusive()
Implementation of get_random_u32_inclusive(). This is needed by an update for wireless drivers. Sponsored by: The FreeBSD Foundation MFC after: 10 days Differential Revision: https://reviews.freebsd.org/D40121
-rw-r--r--sys/compat/linuxkpi/common/include/linux/random.h30
1 files changed, 28 insertions, 2 deletions
diff --git a/sys/compat/linuxkpi/common/include/linux/random.h b/sys/compat/linuxkpi/common/include/linux/random.h
index fafb87cae9fe..561953be277a 100644
--- a/sys/compat/linuxkpi/common/include/linux/random.h
+++ b/sys/compat/linuxkpi/common/include/linux/random.h
@@ -4,6 +4,10 @@
* Copyright (c) 2010 Panasas, Inc.
* Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
* All rights reserved.
+ * Copyright 2023 The FreeBSD Foundation
+ *
+ * Portions of this software was developed by Björn Zeeb
+ * under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -36,8 +40,6 @@
#include <sys/random.h>
#include <sys/libkern.h>
-#define get_random_u32() get_random_int()
-
static inline void
get_random_bytes(void *buf, int nbytes)
{
@@ -54,6 +56,30 @@ get_random_int(void)
return (val);
}
+#define get_random_u32() get_random_int()
+
+/*
+ * See "Fast Random Integer Generation in an Interval" by Daniel Lemire
+ * [https://arxiv.org/pdf/1805.10941.pdf] for implementation insights.
+ */
+static inline uint32_t
+get_random_u32_inclusive(uint32_t floor, uint32_t ceil)
+{
+ uint64_t x;
+ uint32_t t, v;
+
+ MPASS(ceil >= floor);
+
+ v = get_random_u32();
+ t = ceil - floor + 1;
+ x = (uint64_t)t * v;
+ while (x < t)
+ x = (uint64_t)t * get_random_u32();
+ v = x >> 32;
+
+ return (floor + v);
+}
+
static inline u_long
get_random_long(void)
{