Skip to content

Commit

Permalink
Use srand48_deterministic if it is available.
Browse files Browse the repository at this point in the history
OpenBSD requires a special call to activate the standard, deterministic
behavior of `lrand48'.  Check for the special function and use it if it
is found.  (As far as I know, it is only available on OpenBSD.)

In addition to the principle change above, remove some old,
commented-out calls to `srand48' --- to avoid somebody uncommenting them
in the future and forgetting to call `srnd48_deterministic' instead!
  • Loading branch information
eeide committed May 26, 2017
1 parent f305ae8 commit 9b80960
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -77,6 +77,8 @@ check_include_file("sys/types.h" HAVE_SYS_TYPES_H)
check_include_file("unistd.h" HAVE_UNISTD_H)

check_symbol_exists(lrand48 "stdlib.h" HAVE_LRAND48)
check_symbol_exists(srand48_deterministic
"stdlib.h" HAVE_SRAND48_DETERMINISTIC)

set(csmith_PACKAGE "csmith")
set(csmith_PACKAGE_BUGREPORT "csmith-bugs@flux.utah.edu")
Expand Down
3 changes: 3 additions & 0 deletions cmake_config.h.in
Expand Up @@ -10,6 +10,9 @@
/* Define to 1 if you have the <memory.h> header file. */
#cmakedefine HAVE_MEMORY_H 1

/* Define to 1 if you have the `srand48_deterministic' function. */
#cmakedefine HAVE_SRAND48_DETERMINISTIC

/* Define to 1 if you have the <stdint.h> header file. */
#cmakedefine HAVE_STDINT_H 1

Expand Down
3 changes: 3 additions & 0 deletions config.h.in
Expand Up @@ -12,6 +12,9 @@
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H

/* Define to 1 if you have the `srand48_deterministic' function. */
#undef HAVE_SRAND48_DETERMINISTIC

/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H

Expand Down
15 changes: 15 additions & 0 deletions configure
Expand Up @@ -15840,6 +15840,21 @@ else
fi


# OpenBSD requires a special call to activate the standard, deterministic
# behavior of `lrand48'.
#
for ac_func in srand48_deterministic
do :
ac_fn_c_check_func "$LINENO" "srand48_deterministic" "ac_cv_func_srand48_deterministic"
if test "x$ac_cv_func_srand48_deterministic" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_SRAND48_DETERMINISTIC 1
_ACEOF

fi
done


# If we're using g++, set the default `CXXFLAGS' to something more pedantic.
#
if test "$ac_test_CXXFLAGS" = set; then
Expand Down
5 changes: 5 additions & 0 deletions configure.ac
Expand Up @@ -63,6 +63,11 @@ have_lrand48="no"
AC_CHECK_FUNCS([lrand48], [have_lrand48="yes"], [have_lrand48="no"])
AM_CONDITIONAL([HAVE_LRAND48], [test "$have_lrand48" = "yes"])

# OpenBSD requires a special call to activate the standard, deterministic
# behavior of `lrand48'.
#
AC_CHECK_FUNCS([srand48_deterministic])

# If we're using g++, set the default `CXXFLAGS' to something more pedantic.
#
if test "$ac_test_CXXFLAGS" = set; then
Expand Down
10 changes: 8 additions & 2 deletions src/AbsRndNumGenerator.cpp
Expand Up @@ -43,7 +43,7 @@

using namespace std;

#ifdef WIN32
#ifndef HAVE_LRAND48
extern "C" {
extern void srand48(long seed);
extern long lrand48(void);
Expand Down Expand Up @@ -94,7 +94,13 @@ AbsRndNumGenerator::make_rndnum_generator(RNDNUM_GENERATOR impl, const unsigned
void
AbsRndNumGenerator::seedrand(const unsigned long seed )
{
srand48 (seed);
#ifdef HAVE_SRAND48_DETERMINISTIC
// OpenBSD requires a special call to activate the standard,
// deterministic behavior of `lrand48'.
srand48_deterministic(seed);
#else
srand48(seed);
#endif
}

/*
Expand Down
15 changes: 0 additions & 15 deletions src/DefaultRndNumGenerator.cpp
Expand Up @@ -45,13 +45,6 @@
#include "CGOptions.h"
#include "DeltaMonitor.h"

#ifdef WIN32
extern "C" {
extern void srand48(long seed);
extern long lrand48(void);
}
#endif

DefaultRndNumGenerator *DefaultRndNumGenerator::impl_ = 0;

/*
Expand Down Expand Up @@ -181,14 +174,6 @@ DefaultRndNumGenerator::rnd_flipcoin(const unsigned int p, const Filter *f, cons
return rv;
}

#if 0
void
DefaultRndNumGenerator::seedrand( unsigned long seed )
{
srand48 (seed);
}
#endif

std::string &
DefaultRndNumGenerator::trace_depth()
{
Expand Down
2 changes: 0 additions & 2 deletions src/SimpleDeltaRndNumGenerator.cpp
Expand Up @@ -81,8 +81,6 @@ SimpleDeltaRndNumGenerator::make_rndnum_generator(const unsigned long /*seed*/)

impl_ = new SimpleDeltaRndNumGenerator(seq);

//srand48(seed);

impl_->random_point_ = SimpleDeltaRndNumGenerator::pure_rnd_upto(seq->sequence_length());

assert(impl_);
Expand Down

0 comments on commit 9b80960

Please sign in to comment.