OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-dev/] [fsf-gcc-snapshot-1-mar-12/] [or1k-gcc/] [libgfortran/] [intrinsics/] [rand.c] - Diff between revs 733 and 783

Only display areas with differences | Details | Blame | View Log

Rev 733 Rev 783
/* Implementation of the IRAND, RAND, and SRAND intrinsics.
/* Implementation of the IRAND, RAND, and SRAND intrinsics.
   Copyright (C) 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
   Copyright (C) 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
   Contributed by Steven G. Kargl <kargls@comcast.net>.
   Contributed by Steven G. Kargl <kargls@comcast.net>.
 
 
This file is part of the GNU Fortran 95 runtime library (libgfortran).
This file is part of the GNU Fortran 95 runtime library (libgfortran).
 
 
Libgfortran is free software; you can redistribute it and/or
Libgfortran is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
version 3 of the License, or (at your option) any later version.
 
 
Libgfortran is distributed in the hope that it will be useful,
Libgfortran is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
Under Section 7 of GPL version 3, you are granted additional
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
3.1, as published by the Free Software Foundation.
 
 
You should have received a copy of the GNU General Public License and
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */
<http://www.gnu.org/licenses/>.  */
 
 
/* Simple multiplicative congruent algorithm.
/* Simple multiplicative congruent algorithm.
   The period of this generator is approximately 2^31-1, which means that
   The period of this generator is approximately 2^31-1, which means that
   it should not be used for anything serious.  The implementation here
   it should not be used for anything serious.  The implementation here
   is based of an algorithm from  S.K. Park and K.W. Miller, Comm. ACM,
   is based of an algorithm from  S.K. Park and K.W. Miller, Comm. ACM,
   31, 1192-1201 (1988).  It is also provided solely for compatibility
   31, 1192-1201 (1988).  It is also provided solely for compatibility
   with G77.  */
   with G77.  */
 
 
#include "libgfortran.h"
#include "libgfortran.h"
#include <gthr.h>
#include <gthr.h>
 
 
#define GFC_RAND_A      16807
#define GFC_RAND_A      16807
#define GFC_RAND_M      2147483647
#define GFC_RAND_M      2147483647
#define GFC_RAND_M1     (GFC_RAND_M - 1)
#define GFC_RAND_M1     (GFC_RAND_M - 1)
 
 
static GFC_UINTEGER_8 rand_seed = 1;
static GFC_UINTEGER_8 rand_seed = 1;
#ifdef __GTHREAD_MUTEX_INIT
#ifdef __GTHREAD_MUTEX_INIT
static __gthread_mutex_t rand_seed_lock = __GTHREAD_MUTEX_INIT;
static __gthread_mutex_t rand_seed_lock = __GTHREAD_MUTEX_INIT;
#else
#else
static __gthread_mutex_t rand_seed_lock;
static __gthread_mutex_t rand_seed_lock;
#endif
#endif
 
 
 
 
/* Set the seed of the irand generator.  Note 0 is a bad seed.  */
/* Set the seed of the irand generator.  Note 0 is a bad seed.  */
 
 
static void
static void
srand_internal (GFC_INTEGER_8 i)
srand_internal (GFC_INTEGER_8 i)
{
{
  rand_seed = i ? i : 123459876;
  rand_seed = i ? i : 123459876;
}
}
 
 
extern void PREFIX(srand) (GFC_INTEGER_4 *i);
extern void PREFIX(srand) (GFC_INTEGER_4 *i);
export_proto_np(PREFIX(srand));
export_proto_np(PREFIX(srand));
 
 
void
void
PREFIX(srand) (GFC_INTEGER_4 *i)
PREFIX(srand) (GFC_INTEGER_4 *i)
{
{
  __gthread_mutex_lock (&rand_seed_lock);
  __gthread_mutex_lock (&rand_seed_lock);
  srand_internal (*i);
  srand_internal (*i);
  __gthread_mutex_unlock (&rand_seed_lock);
  __gthread_mutex_unlock (&rand_seed_lock);
}
}
 
 
/* Return an INTEGER in the range [1,GFC_RAND_M-1].  */
/* Return an INTEGER in the range [1,GFC_RAND_M-1].  */
 
 
extern GFC_INTEGER_4 irand (GFC_INTEGER_4 *);
extern GFC_INTEGER_4 irand (GFC_INTEGER_4 *);
iexport_proto(irand);
iexport_proto(irand);
 
 
GFC_INTEGER_4
GFC_INTEGER_4
irand (GFC_INTEGER_4 *i)
irand (GFC_INTEGER_4 *i)
{
{
  GFC_INTEGER_4 j;
  GFC_INTEGER_4 j;
  if (i)
  if (i)
    j = *i;
    j = *i;
  else
  else
    j = 0;
    j = 0;
 
 
  __gthread_mutex_lock (&rand_seed_lock);
  __gthread_mutex_lock (&rand_seed_lock);
 
 
  switch (j)
  switch (j)
  {
  {
    /* Return the next RN. */
    /* Return the next RN. */
    case 0:
    case 0:
      break;
      break;
 
 
    /* Reset the RN sequence to system-dependent sequence and return the
    /* Reset the RN sequence to system-dependent sequence and return the
       first value.  */
       first value.  */
    case 1:
    case 1:
      srand_internal (0);
      srand_internal (0);
      break;
      break;
 
 
    /* Seed the RN sequence with j and return the first value.  */
    /* Seed the RN sequence with j and return the first value.  */
    default:
    default:
      srand_internal (j);
      srand_internal (j);
      break;
      break;
   }
   }
 
 
   rand_seed = GFC_RAND_A * rand_seed % GFC_RAND_M;
   rand_seed = GFC_RAND_A * rand_seed % GFC_RAND_M;
   j = (GFC_INTEGER_4) rand_seed;
   j = (GFC_INTEGER_4) rand_seed;
 
 
  __gthread_mutex_unlock (&rand_seed_lock);
  __gthread_mutex_unlock (&rand_seed_lock);
 
 
   return j;
   return j;
}
}
iexport(irand);
iexport(irand);
 
 
 
 
/*  Return a random REAL in the range [0,1).  */
/*  Return a random REAL in the range [0,1).  */
 
 
extern GFC_REAL_4 PREFIX(rand) (GFC_INTEGER_4 *i);
extern GFC_REAL_4 PREFIX(rand) (GFC_INTEGER_4 *i);
export_proto_np(PREFIX(rand));
export_proto_np(PREFIX(rand));
 
 
GFC_REAL_4
GFC_REAL_4
PREFIX(rand) (GFC_INTEGER_4 *i)
PREFIX(rand) (GFC_INTEGER_4 *i)
{
{
  GFC_UINTEGER_4 mask;
  GFC_UINTEGER_4 mask;
#if GFC_REAL_4_RADIX == 2
#if GFC_REAL_4_RADIX == 2
  mask = ~ (GFC_UINTEGER_4) 0u << (32 - GFC_REAL_4_DIGITS + 1);
  mask = ~ (GFC_UINTEGER_4) 0u << (32 - GFC_REAL_4_DIGITS + 1);
#elif GFC_REAL_4_RADIX == 16
#elif GFC_REAL_4_RADIX == 16
  mask = ~ (GFC_UINTEGER_4) 0u << ((8 - GFC_REAL_4_DIGITS) * 4 + 1);
  mask = ~ (GFC_UINTEGER_4) 0u << ((8 - GFC_REAL_4_DIGITS) * 4 + 1);
#else
#else
#error "GFC_REAL_4_RADIX has unknown value"
#error "GFC_REAL_4_RADIX has unknown value"
#endif
#endif
  return ((GFC_UINTEGER_4) (irand(i) -1) & mask) * (GFC_REAL_4) 0x1.p-31f;
  return ((GFC_UINTEGER_4) (irand(i) -1) & mask) * (GFC_REAL_4) 0x1.p-31f;
}
}
 
 
#ifndef __GTHREAD_MUTEX_INIT
#ifndef __GTHREAD_MUTEX_INIT
static void __attribute__((constructor))
static void __attribute__((constructor))
init (void)
init (void)
{
{
  __GTHREAD_MUTEX_INIT_FUNCTION (&rand_seed_lock);
  __GTHREAD_MUTEX_INIT_FUNCTION (&rand_seed_lock);
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.