Skip to content

Commit

Permalink
Introduce loadable pseudo-random number generators.
Browse files Browse the repository at this point in the history
N.B. The PRNG interface introduced here is subject to review and may change.

PRNGs are per-thread and may be changed with the new loadrng() standard
function. Two PRNGs are present:
   IconEx   A reimplementation of the traditional generator
            as a loadable library.
   Rabbit   A cryptographically strong PRNG from the eSTREAM project.
Other PRNGs -- probably RanLux and the Mersenne Twister -- will follow.
  • Loading branch information
Don-Ward committed Mar 15, 2022
1 parent df79234 commit 6be1d9d
Show file tree
Hide file tree
Showing 73 changed files with 10,730 additions and 24 deletions.
6 changes: 4 additions & 2 deletions Makedefs.in
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ datarootdir=@datarootdir@
docdir=@docdir@
mandir=@mandir@
htmldir=@htmldir@

SQLITE=@SQLITE@

# Used by plugins
SQLITE=@SQLITE@
GSLRNG=@GSLRNG@
RNGLIBS=@RNGLIBS@

# configuration parameters

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ install Install:
$(INST) -m 644 uni/$$d/*.* $(DESTDIR)$(ULB)/$$d; \
done
# plugins
@$(MAKE) -C plugins/rngLibraries install
@$(INST) -m 644 plugins/lib/*.* $(DESTDIR)$(UPLUGINS)/ || true
# docs and man
@echo "Installing $(DESTDIR)$(mandir)/man1/unicon.1 ..."
Expand Down
1 change: 1 addition & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ install Install:
$(INST) -m 644 uni/$$d/*.* $(DESTDIR)$(ULB)/$$d; \
done
# plugins
@$(MAKE) -C plugins/rngLibraries install
@$(INST) -m 644 plugins/lib/*.* $(DESTDIR)$(UPLUGINS)/ || true
# docs and man
@echo "Installing $(DESTDIR)$(mandir)/man1/unicon.1 ..."
Expand Down
87 changes: 86 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,9 @@ XL
GRAPHICS
XLIBS
CXXCPP
GSLRNG
SQLITE
RNGLIBS
LIBOBJS
HTMLDOC
UDOC
Expand Down Expand Up @@ -763,6 +765,7 @@ enable_iconcurrency
enable_ovld
enable_udbtools
enable_progs
enable_rnglibs
enable_verbosebuild
enable_doc
enable_htmldoc
Expand Down Expand Up @@ -1444,6 +1447,7 @@ Optional Features:
--enable-ovld Enable operator overloading
--enable-udbtools Enable Unicon debugger tools
--enable-progs Enable Unicon programs
--enable-rnglibs Enable loadable random number generators
--enable-verbosebuild Show full CC build lines with all compiler arguments
--enable-doc Add makefile rule to build documentation
--enable-htmldoc Add makefile rule to html documentation
Expand Down Expand Up @@ -3216,6 +3220,13 @@ else
verifyheap=no
fi

# Check whether --enable-rnglibs was given.
if test "${enable_rnglibs+set}" = set; then :
enableval=$enable_rnglibs; rnglibs=yes
else
rnglibs=no
fi

# Check whether --enable-devmode was given.
if test "${enable_devmode+set}" = set; then :
enableval=$enable_devmode; devmode=yes
Expand Down Expand Up @@ -4695,6 +4706,7 @@ else
CFLAGS="$ac_c_flag_save"
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }

ARCHFLAG=""


Expand Down Expand Up @@ -4755,6 +4767,14 @@ $as_echo "#define VerifyHeap 1" >>confdefs.h

fi

if test x"$rnglibs" = x"yes" ; then

$as_echo "#define RngLibrary 1" >>confdefs.h

RNGLIBS=1

fi

if test x"$udbtools" = x"yes" ; then
UDBTOOLS=udbtools
fi
Expand Down Expand Up @@ -7624,7 +7644,7 @@ _ACEOF
fi


# we don't need sqlite to be added to $LIBS, not yet at least
# Check for libs for plugin use, don't add to these to $LIBS
saved_libs="$LIBS"
for ac_header in sqlite3.h
do :
Expand Down Expand Up @@ -7683,6 +7703,63 @@ _ACEOF

fi

for ac_header in gsl/gsl_rng.h
do :
ac_fn_c_check_header_mongrel "$LINENO" "gsl/gsl_rng.h" "ac_cv_header_gsl_gsl_rng_h" "$ac_includes_default"
if test "x$ac_cv_header_gsl_gsl_rng_h" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_GSL_GSL_RNG_H 1
_ACEOF

fi

done

{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for gsl_rng_set in -lgsl" >&5
$as_echo_n "checking for gsl_rng_set in -lgsl... " >&6; }
if ${ac_cv_lib_gsl_gsl_rng_set+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lgsl $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char gsl_rng_set ();
int
main ()
{
return gsl_rng_set ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_gsl_gsl_rng_set=yes
else
ac_cv_lib_gsl_gsl_rng_set=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_gsl_gsl_rng_set" >&5
$as_echo "$ac_cv_lib_gsl_gsl_rng_set" >&6; }
if test "x$ac_cv_lib_gsl_gsl_rng_set" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_LIBGSL 1
_ACEOF

LIBS="-lgsl $LIBS"

fi


LIBS="$saved_libs"

if test "x$ac_cv_header_sqlite3_h" = "xyes" -a "x$ac_cv_lib_sqlite3_sqlite3_config" = "xyes"; then
Expand All @@ -7691,6 +7768,14 @@ else
SQLITE=""
fi

if test "x$ac_cv_header_gsl_gsl_rng_h" = "xyes" -a "x$ac_cv_lib_gsl_gsl_rng_set" = "xyes"; then
GSLRNG="gsl"
else
GSLRNG=""
fi




{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing getaddrinfo" >&5
$as_echo_n "checking for library containing getaddrinfo... " >&6; }
Expand Down
23 changes: 22 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ AC_ARG_ENABLE([progs],
[AS_HELP_STRING([--enable-progs], [Enable Unicon programs])],
[progs=yes], [progs=no])

AC_ARG_ENABLE([rnglibs],
[AS_HELP_STRING([--enable-rnglibs], [Enable loadable random number generators])],
[rnglibs=yes], [rnglibs=no])

AC_ARG_ENABLE([verbosebuild],
[AS_HELP_STRING([--enable-verbosebuild], [Show full CC build lines with all compiler arguments])],
[verbosebuild=yes], [verbosebuild=no])
Expand Down Expand Up @@ -259,6 +263,11 @@ if test x"$verifyheap" = x"yes" ; then
AC_DEFINE([VerifyHeap], [1], [Verify Heap])
fi

if test x"$rnglibs" = x"yes" ; then
AC_DEFINE([RngLibrary], [1], [Rng Library])
AC_SUBST(RNGLIBS,1)
fi

if test x"$udbtools" = x"yes" ; then
UDBTOOLS=udbtools
fi
Expand Down Expand Up @@ -545,18 +554,30 @@ AC_FUNC_FORK()
AC_CHECK_LIB(dbm,main)
AC_CHECK_LIB(ndbm,main)

# we don't need sqlite to be added to $LIBS, not yet at least
# Check for libs for plugin use, don't add to these to $LIBS
saved_libs="$LIBS"
AC_CHECK_HEADERS([sqlite3.h])
AC_CHECK_LIB(sqlite3, sqlite3_config)

AC_CHECK_HEADERS([gsl/gsl_rng.h])
AC_CHECK_LIB(gsl, gsl_rng_set)

LIBS="$saved_libs"

if test "x$ac_cv_header_sqlite3_h" = "xyes" -a "x$ac_cv_lib_sqlite3_sqlite3_config" = "xyes"; then
SQLITE="sqlite"
else
SQLITE=""
fi

if test "x$ac_cv_header_gsl_gsl_rng_h" = "xyes" -a "x$ac_cv_lib_gsl_gsl_rng_set" = "xyes"; then
GSLRNG="gsl"
else
GSLRNG=""
fi

AC_SUBST(SQLITE)
AC_SUBST(GSLRNG)

AC_SEARCH_LIBS([getaddrinfo],[socket],
[AC_DEFINE([HAVE_GETADDRINFO], [1], [getaddinfo])])
Expand Down
42 changes: 39 additions & 3 deletions doc/book/experimental.tex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,42 @@ \section{Extensions to \texttt{\&random}}
This feature allows the programmer to choose from a portfolio of different
random number generators (in addition to the one provided by Icon). It is also
possible to implement other generators and use them without rebuilding Unicon.
More than one generator may be in use at the same time.
It may be enabled by defining the C preprocessor symbol \texttt{RngLibrary}
before rebuilding the Unicon system.
More than one generator may be in use at the same time. It may be enabled by
using the \texttt{-{}-enable-rnglibs} option to \texttt{configure} before
rebuilding the Unicon system.

If the extension is enabled four new standard functions are defined:
\bigskip\hrule\vspace{0.1cm}
\noindent
{\bf loadrng(s) : string? } \hfill {\bf load PRNG}

\noindent
\texttt{loadrng(s)} replaces the generator in use by the current thread with the generator
denoted by \texttt{s}. \texttt{loadrng("rngIcon")} will reload the default generator.
\texttt{loadrng()} returns the name of the generator in use. The \texttt{?} operator will
use the loaded generator to produce random numbers.

\bigskip\hrule\vspace{0.1cm}
\noindent
{\bf rngbits(i) : array? } \hfill {\bf return random bits}

\noindent
\texttt{rngbits(n)} returns an integer array that contains at least \texttt{n} randomly
generated bits. If \texttt{n} is negative no bits are returned but the generator will skip
ahead as if the bits were actually returned. If \texttt{n} is zero it is an invitation to
supply the ``natural size'' of the generator (which is probably a power of two).

\bigskip\hrule\vspace{0.1cm}
\noindent
{\bf rngbitstring(i) : string? } \hfill {\bf return random bits}

\noindent
\texttt{rngbitstring(n)} returns a string that contains \texttt{n}
randomly generated \texttt{0} and \texttt{1} characters.

\bigskip\hrule\vspace{0.1cm}
\noindent
{\bf rngval() : integer? } \hfill {\bf return random value}

\noindent
\texttt{rngval()} returns a randomly generated integer value.
Loading

0 comments on commit 6be1d9d

Please sign in to comment.