This is the mail archive of the
newlib@sourceware.cygnus.com
mailing list for the newlib project.
Re: bug in sys/reent.h
- To: medp at primag dot co dot uk
- Subject: Re: bug in sys/reent.h
- From: DJ Delorie <dj at delorie dot com>
- Date: Wed, 21 Jun 2000 10:22:56 -0400
- CC: nikulin at actsw dot amat dot com, newlib at sourceware dot cygnus dot com
- References: <NDBBLNOCICLBEIMMLFGEKEHMCFAA.medp@primag.co.uk>
> as an open source thing we have used newlib in a number of embedded
> products, one of which is DSP-based and does not use gcc.
What about this patch, then? It falls back to a 32-bit generator if
it's being built with something other than GCC. Please let me know if
this would work for your compiler.
2000-06-21 DJ Delorie <dj@cygnus.com>
* libc/include/sys/reent.h: support non-gcc compilers
* libc/stdlib/rand.c: ditto
Index: include/sys/reent.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/sys/reent.h,v
retrieving revision 1.3
diff -p -2 -r1.3 reent.h
*** reent.h 2000/06/20 18:34:57 1.3
--- reent.h 2000/06/21 14:21:59
*************** struct _reent
*** 183,188 ****
struct tm _localtime_buf;
int _gamma_signgam;
__extension__ unsigned long long _rand_next;
!
} _reent;
/* Two next two fields were once used by malloc. They are no longer
--- 183,191 ----
struct tm _localtime_buf;
int _gamma_signgam;
+ #ifdef __GNUC__
__extension__ unsigned long long _rand_next;
! #else
! int _rand_next[2];
! #endif
} _reent;
/* Two next two fields were once used by malloc. They are no longer
Index: stdlib/rand.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/rand.c,v
retrieving revision 1.3
diff -p -2 -r1.3 rand.c
*** rand.c 2000/06/20 18:34:57 1.3
--- rand.c 2000/06/21 14:21:59
*************** on two different systems.
*** 70,77 ****
#include <reent.h>
void
_DEFUN (srand, (seed), unsigned int seed)
{
! _REENT->_new._reent._rand_next = seed;
}
--- 70,83 ----
#include <reent.h>
+ #ifdef __GNUC__
+ #define N (_REENT->_new._reent._rand_next)
+ #else
+ #define N (_REENT->_new._reent._rand_next[0])
+ #endif
+
void
_DEFUN (srand, (seed), unsigned int seed)
{
! N = seed;
}
*************** int
*** 79,88 ****
_DEFUN_VOID (rand)
{
/* This multiplier was obtained from Knuth, D.E., "The Art of
Computer Programming," Vol 2, Seminumerical Algorithms, Third
Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */
! _REENT->_new._reent._rand_next =
! _REENT->_new._reent._rand_next * __extension__ 6364136223846793005LL + 1;
! return (int)((_REENT->_new._reent._rand_next >> 32) & RAND_MAX);
}
--- 85,98 ----
_DEFUN_VOID (rand)
{
+ #ifdef __GNUC__
/* This multiplier was obtained from Knuth, D.E., "The Art of
Computer Programming," Vol 2, Seminumerical Algorithms, Third
Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */
! N = N * __extension__ 6364136223846793005LL + 1;
! return (int)((N >> 32) & RAND_MAX);
! #else
! N = N * 1103515245 + 12345;
! return N & RAND_MAX;
! #endif
}