]> git.sesse.net Git - vlc/commitdiff
nrand48 replacement
authorRémi Denis-Courmont <remi@remlab.net>
Fri, 5 Mar 2010 22:03:27 +0000 (00:03 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Fri, 5 Mar 2010 22:03:27 +0000 (00:03 +0200)
This is the POSIX linear congruential pseudo-random generator with
48-bits of state. It is non-secure/reproducible and thread-safe.
rand() is not thread-safe and *ahem* must not be used in VLC code.
rand_r() is thread-safe but obsoleted in POSIX.2008.

For secure random numbers, use the slower vlc_rand_bytes().
vlc_rand_bytes() can also be used to generate the seed if you are short
on idea for a 48-bits pseudo-random value.

compat/nrand48.c [new file with mode: 0644]
configure.ac
include/vlc_fixups.h

diff --git a/compat/nrand48.c b/compat/nrand48.c
new file mode 100644 (file)
index 0000000..9498ef0
--- /dev/null
@@ -0,0 +1,46 @@
+/*****************************************************************************
+ * nrand48.c: POSIX nrand48() replacement
+ *****************************************************************************
+ * Copyright © 2010 Rémi Denis-Courmont
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <inttypes.h>
+
+long nrand48 (unsigned short subi[3])
+{
+    const uint64_t a = UINT64_C(0x5DEECE66D16);
+    const unsigned c = 13;
+    const uint64_t mask = UINT64_C(0xFFFFFFFFFFFF); // 48 bits
+
+    uint64_t x = ((uint64_t)subi[0] << 32)
+               | ((uint32_t)subi[1] << 16)
+               | subi[2];
+
+    x *= a;
+    x += c;
+    x &= mask;
+
+    subi[0] = (x >> 32) & 0xFFFF;
+    subi[1] = (x >> 16) & 0xFFFF;
+    subi[2] = (x >>  0) & 0XFFFF;
+
+    return x >> 16;
+}
index 6344b7d601770729ebbf374edeb7a7c33cc459bb..e1004403043278458ec031cb0889bfd44220eda0 100644 (file)
@@ -548,7 +548,7 @@ need_libc=false
 
 dnl Check for usual libc functions
 AC_CHECK_FUNCS([ctime_r daemon fcntl fdopendir fork getenv getpwuid_r gettimeofday isatty lstat memalign posix_fadvise posix_madvise posix_memalign putenv setenv setlocale stricmp strnicmp tdestroy uselocale])
-AC_REPLACE_FUNCS([asprintf atof atoll getcwd getpid gmtime_r lldiv localtime_r rewind strcasecmp strcasestr strdup strlcpy strncasecmp strndup strnlen strsep strtof strtok_r strtoll swab vasprintf])
+AC_REPLACE_FUNCS([asprintf atof atoll getcwd getpid gmtime_r lldiv localtime_r nrand48 rewind strcasecmp strcasestr strdup strlcpy strncasecmp strndup strnlen strsep strtof strtok_r strtoll swab vasprintf])
 AC_CHECK_FUNCS(fdatasync,,
   [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
 ])
index b05c8097812e900282201e9ff388366794b5e177..8e24e0f3c1acbd70e894ddb17ebe1e8ef09e5330 100644 (file)
@@ -239,4 +239,9 @@ struct pollfd
 # define tdestroy vlc_tdestroy
 #endif
 
+/* Random numbers */
+#ifndef HAVE_NRAND48
+long nrand48 (unsigned short subi[3]);
+#endif
+
 #endif /* !LIBVLC_FIXUPS_H */