This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Random seed initialization


Angelo Graziosi wrote:
A new GCC 4.7.2 has been released (http://cygwin.com/ml/cygwin/2013-04/msg00170.html),
but the problems described in this thread still remain.

Can you try the attached program? Run it a couple of time and check the output. On my system, I can simply use:

$ gcc test.c && ./a.out
gettimeofday:  err=0, secs=1365674085, usecs=67639
clock_gettime: err=0, secs=1365674085, usecs=67683
time:          err=0, secs=1365674085, usecs=0
clock_gettime: err=0, secs=9343, nsecs=0, tck=1000000000

However, depending on the system, you might need to add extra libraries (e.g. "-lrt" with older glibc) or you have to disable some of the tests if they are not supported.

I think the test covers all possibilities from libgfortran but I might have missed one. (One known exception is trying whether the weak binding for librt's function works.)

Tobias
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#define __USE_POSIX199309 1
#include <time.h>

/* #ifdef HAVE_GETTIMEOFDAY */
static int
gf_gettime_1 (time_t * secs, long * usecs)
{
  struct timeval tv;
  int err;
  err = gettimeofday (&tv, NULL);
  *secs = tv.tv_sec;
  *usecs = tv.tv_usec;
  return err;
}

/* #elif defined(HAVE_CLOCK_GETTIME) */
static int
gf_gettime_2 (time_t * secs, long * usecs)
{
  struct timespec ts;
  int err = clock_gettime (CLOCK_REALTIME, &ts);
  *secs = ts.tv_sec;
  *usecs = ts.tv_nsec / 1000;
  return err;
}

/* #else */
static int
gf_gettime_3 (time_t * secs, long * usecs)
{
  time_t t = time (NULL);
  *secs = t;
  *usecs = 0;
  if (t == ((time_t)-1))
    return -1;
  return 0;
}


/* #ifdef HAVE_CLOCK_GETTIME */
static int
gf_gettime_mono (time_t * secs, long * nanosecs, long * tck)
{
  int err;
  struct timespec ts;
  *tck = 1000000000;
  err = clock_gettime (CLOCK_MONOTONIC, &ts);
  *secs = ts.tv_sec;
  *nanosecs = ts.tv_nsec;
  return err;
}


int
main ()
{
  int err;
  time_t secs;
  long usecs;
  long nsecs, tck;


  err = gf_gettime_1 (&secs, &usecs);
  printf ("gettimeofday:  err=%d, secs=%ld, usecs=%ld\n",
	  err, (long) secs, usecs);

  err = gf_gettime_2 (&secs, &usecs);
  printf ("clock_gettime: err=%d, secs=%ld, usecs=%ld\n",
	  err, (long) secs, usecs);

  err = gf_gettime_3 (&secs, &usecs);
  printf ("time:          err=%d, secs=%ld, usecs=%ld\n",
	  err, (long) secs, usecs);

  err = gf_gettime_mono (&secs, &nsecs, &tck);
  printf ("clock_gettime: err=%d, secs=%ld, nsecs=%ld, tck=%ld\n",
	  err, (long) secs, usecs, tck);

  return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]