Skip to content

Commit e7ab04c

Browse files
committed
[perl #115928] use a consistent internal rand on all platforms
2 parents e3be4e3 + a7f3e46 commit e7ab04c

22 files changed

+235
-334
lines changed

Configure

Lines changed: 6 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -19489,26 +19489,7 @@ esac
1948919489

1949019490
: How can we generate normalized random numbers ?
1949119491
echo " "
19492-
echo "Looking for a random number function..." >&4
19493-
case "$randfunc" in
19494-
'')
19495-
if set drand48 val -f; eval $csym; $val; then
19496-
dflt="drand48"
19497-
echo "Good, found drand48()." >&4
19498-
elif set random val -f; eval $csym; $val; then
19499-
dflt="random"
19500-
echo "OK, found random()." >&4
19501-
else
19502-
dflt="rand"
19503-
echo "Yuck, looks like I have to use rand()." >&4
19504-
fi
19505-
echo " "
19506-
;;
19507-
*)
19508-
dflt="$randfunc"
19509-
;;
19510-
esac
19511-
cont=true
19492+
echo "Using our internal random number implementation..." >&4
1951219493

1951319494
case "$ccflags" in
1951419495
*-Dmy_rand=*|*-Dmy_srand=*)
@@ -19519,103 +19500,11 @@ case "$ccflags" in
1951919500
;;
1952019501
esac
1952119502

19522-
while $test "$cont"; do
19523-
rp="Use which function to generate random numbers?"
19524-
. ./myread
19525-
if $test "$ans" = "$dflt"; then
19526-
: null
19527-
else
19528-
randbits=''
19529-
fi
19530-
randfunc="$ans"
19531-
if set $ans val -f; eval $csym; $val; then
19532-
cont=''
19533-
else
19534-
dflt=y
19535-
rp="I cannot find function $ans. Use that name anyway?"
19536-
. ./myread
19537-
dflt=rand
19538-
case "$ans" in
19539-
[yY]*) cont='';;
19540-
esac
19541-
fi
19542-
case "$cont" in
19543-
'')
19544-
case "$randfunc" in
19545-
drand48)
19546-
drand01="drand48()"
19547-
seedfunc="srand48"
19548-
randbits=48
19549-
randseedtype=long
19550-
;;
19551-
rand|random)
19552-
case "$randbits" in
19553-
'')
19554-
echo "Checking to see how many bits your $randfunc() function produces..." >&4
19555-
$cat >try.c <<EOCP
19556-
#$i_unistd I_UNISTD
19557-
#$i_stdlib I_STDLIB
19558-
#include <stdio.h>
19559-
#ifdef I_UNISTD
19560-
# include <unistd.h>
19561-
#endif
19562-
#ifdef I_STDLIB
19563-
# include <stdlib.h>
19564-
#endif
19565-
int main()
19566-
{
19567-
int i;
19568-
unsigned long tmp;
19569-
unsigned long max = 0L;
19570-
19571-
for (i = 1000; i; i--) {
19572-
tmp = (unsigned long) $randfunc();
19573-
if (tmp > max) max = tmp;
19574-
}
19575-
for (i = 0; max; i++)
19576-
max /= 2;
19577-
printf("%d\n",i);
19578-
}
19579-
EOCP
19580-
set try
19581-
if eval $compile_ok; then
19582-
dflt=`try`
19583-
else
19584-
dflt='?'
19585-
echo "(I can't seem to compile the test program...)"
19586-
fi
19587-
;;
19588-
*)
19589-
dflt="$randbits"
19590-
;;
19591-
esac
19592-
rp="How many bits does your $randfunc() function produce?"
19593-
. ./myread
19594-
randbits="$ans"
19595-
$rm_try
19596-
drand01="($randfunc() / (double) ((unsigned long)1 << $randbits))"
19597-
seedfunc="s$randfunc"
19598-
randseedtype=unsigned
19599-
;;
19600-
*)
19601-
dflt="31"
19602-
rp="How many bits does your $randfunc() function produce?"
19603-
. ./myread
19604-
randbits="$ans"
19605-
seedfunc="s$randfunc"
19606-
drand01="($randfunc() / (double) ((unsigned long)1 << $randbits))"
19607-
if set $seedfunc val -f; eval $csym; $val; then
19608-
echo "(Using $seedfunc() to seed random generator)"
19609-
else
19610-
echo "(Warning: no $seedfunc() to seed random generator)"
19611-
seedfunc=rand
19612-
fi
19613-
randseedtype=unsigned
19614-
;;
19615-
esac
19616-
;;
19617-
esac
19618-
done
19503+
randfunc=Perl_drand48
19504+
drand01="Perl_drand48()"
19505+
seedfunc="Perl_drand48_init"
19506+
randbits=48
19507+
randseedtype=U32
1961919508

1962019509
: Check how to flush
1962119510
echo " "

Cross/config.sh-arm-linux

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ direntrytype='struct dirent'
556556
dlext='so'
557557
dlsrc='dl_dlopen.xs'
558558
doublesize='8'
559-
drand01='drand48()'
559+
drand01='Perl_drand48()'
560560
drand48_r_proto='0'
561561
dtrace=''
562562
dynamic_ext='B ByteLoader Cwd Data/Dumper Devel/DProf Devel/PPPort Devel/Peek Digest/MD5 Encode Fcntl File/Glob Filter/Util/Call I18N/Langinfo IO IPC/SysV List/Util MIME/Base64 Opcode POSIX PerlIO/encoding PerlIO/scalar PerlIO/via SDBM_File Socket Storable Sys/Hostname Sys/Syslog Time/HiRes Unicode/Collate Unicode/Normalize XS/APItest XS/Typemap attributes re threads threads/shared'
@@ -882,9 +882,9 @@ ptrsize='4'
882882
quadkind='3'
883883
quadtype='long long'
884884
randbits='48'
885-
randfunc='drand48'
885+
randfunc='Perl_drand48'
886886
random_r_proto='0'
887-
randseedtype='long'
887+
randseedtype='U32'
888888
ranlib=':'
889889
rd_nodata='-1'
890890
readdir64_r_proto='0'
@@ -915,7 +915,7 @@ sched_yield='sched_yield()'
915915
scriptdir='/usr/bin'
916916
scriptdirexp='/usr/bin'
917917
sed='sed'
918-
seedfunc='srand48'
918+
seedfunc='Perl_drand48_init'
919919
selectminbits='32'
920920
selecttype='fd_set *'
921921
sendmail=''

Cross/config.sh-arm-linux-n770

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ direntrytype='struct dirent'
529529
dlext='so'
530530
dlsrc='dl_dlopen.xs'
531531
doublesize='8'
532-
drand01='drand48()'
532+
drand01='Perl_drand48()'
533533
drand48_r_proto='0'
534534
dynamic_ext='B ByteLoader Cwd Data/Dumper Devel/DProf Devel/PPPort Devel/Peek Digest/MD5 Encode Fcntl File/Glob Filter/Util/Call I18N/Langinfo IO IPC/SysV List/Util MIME/Base64 Opcode POSIX PerlIO/encoding PerlIO/scalar PerlIO/via SDBM_File Socket Storable Sys/Hostname Sys/Syslog Time/HiRes Unicode/Collate Unicode/Normalize XS/APItest XS/Typemap attributes re threads threads/shared'
535535
eagain='EAGAIN'
@@ -852,9 +852,9 @@ ptrsize='4'
852852
quadkind='3'
853853
quadtype='long long'
854854
randbits='48'
855-
randfunc='drand48'
855+
randfunc='Perl_drand48'
856856
random_r_proto='0'
857-
randseedtype='long'
857+
randseedtype='U32'
858858
ranlib=':'
859859
rd_nodata='-1'
860860
readdir64_r_proto='0'
@@ -881,7 +881,7 @@ sched_yield='sched_yield()'
881881
scriptdir='/usr/bin'
882882
scriptdirexp='/usr/bin'
883883
sed='sed'
884-
seedfunc='srand48'
884+
seedfunc='Perl_drand48_init'
885885
selectminbits='32'
886886
selecttype='fd_set *'
887887
sendmail=''

NetWare/config.wc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ direntrytype='DIR'
547547
dlext='nlm'
548548
dlsrc='dl_netware.xs'
549549
doublesize='8'
550-
drand01='(rand()/(double)((unsigned)1<<RANDBITS))'
550+
drand01='Perl_drand48()'
551551
drand48_r_proto='0'
552552
dtrace=''
553553
dynamic_ext='Socket IO Fcntl Opcode SDBM_File attributes'
@@ -854,10 +854,10 @@ prototype='define'
854854
ptrsize='4'
855855
quadkind='5'
856856
quadtype='__int64'
857-
randbits='15'
858-
randfunc='rand'
857+
randbits='48'
858+
randfunc='Perl_drand48'
859859
random_r_proto='0'
860-
randseedtype='unsigned'
860+
randseedtype='U32'
861861
ranlib='rem'
862862
rd_nodata='-1'
863863
readdir64_r_proto='0'
@@ -887,7 +887,7 @@ sched_yield=''
887887
scriptdir='~INST_TOP~~INST_VER~\bin'
888888
scriptdirexp='~INST_TOP~~INST_VER~\bin'
889889
sed='sed'
890-
seedfunc='srand'
890+
seedfunc='Perl_drand48_init'
891891
selectminbits='32'
892892
selecttype='fd_set *'
893893
sendmail='blat'

config_h.SH

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,10 +3147,10 @@ sed <<!GROK!THIS! >$CONFIG_H -e 's!^#undef\(.*/\)\*!/\*#define\1 \*!' -e 's!^#un
31473147
* function used to generate normalized random numbers.
31483148
* Values include 15, 16, 31, and 48.
31493149
*/
3150-
#define Drand01() $drand01 /**/
3151-
#define Rand_seed_t $randseedtype /**/
3152-
#define seedDrand01(x) $seedfunc((Rand_seed_t)x) /**/
3153-
#define RANDBITS $randbits /**/
3150+
#define Drand01() $drand01 /**/
3151+
#define Rand_seed_t $randseedtype /**/
3152+
#define seedDrand01(x) $seedfunc((Rand_seed_t)x) /**/
3153+
#define RANDBITS $randbits /**/
31543154
31553155
/* Select_fd_set_t:
31563156
* This symbol holds the type used for the 2nd, 3rd, and 4th

embed.fnc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,8 @@ p |I32 |wait4pid |Pid_t pid|NN int* statusp|int flags
15711571
: Used in locale.c and perl.c
15721572
p |U32 |parse_unicode_opts|NN const char **popt
15731573
Ap |U32 |seed
1574+
Xpno |double |drand48_r |NN perl_drand48_t *random_state
1575+
Xpno |void |drand48_init_r |NN perl_drand48_t *random_state|U32 seed
15741576
: Only used in perl.c
15751577
p |void |get_hash_seed |NN unsigned char * const seed_buffer
15761578
: Used in doio.c, pp_hot.c, pp_sys.c

embedvar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
#define PL_psig_pend (vTHX->Ipsig_pend)
244244
#define PL_psig_ptr (vTHX->Ipsig_ptr)
245245
#define PL_ptr_table (vTHX->Iptr_table)
246+
#define PL_random_state (vTHX->Irandom_state)
246247
#define PL_reentrant_buffer (vTHX->Ireentrant_buffer)
247248
#define PL_reentrant_retint (vTHX->Ireentrant_retint)
248249
#define PL_reg_curpm (vTHX->Ireg_curpm)

intrpvar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,8 @@ PERLVARA(I, op_exec_cnt, OP_max+2, UV) /* Counts of executed OPs of the given ty
784784
DEBUGGING is enabled, too. */
785785
#endif
786786

787+
PERLVAR(I, random_state, PL_RANDOM_STATE_TYPE)
788+
787789
/* If you are adding a U8 or U16, check to see if there are 'Space' comments
788790
* above on where there are gaps which currently will be structure padding. */
789791

pp.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,10 +2712,6 @@ PP(pp_sin)
27122712
--Jarkko Hietaniemi 27 September 1998
27132713
*/
27142714

2715-
#ifndef HAS_DRAND48_PROTO
2716-
extern double drand48 (void);
2717-
#endif
2718-
27192715
PP(pp_rand)
27202716
{
27212717
dVAR;

proto.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,16 @@ PERL_CALLCONV void Perl_dounwind(pTHX_ I32 cxix);
10241024
PERL_CALLCONV I32 Perl_dowantarray(pTHX)
10251025
__attribute__warn_unused_result__;
10261026

1027+
PERL_CALLCONV void Perl_drand48_init_r(perl_drand48_t *random_state, U32 seed)
1028+
__attribute__nonnull__(1);
1029+
#define PERL_ARGS_ASSERT_DRAND48_INIT_R \
1030+
assert(random_state)
1031+
1032+
PERL_CALLCONV double Perl_drand48_r(perl_drand48_t *random_state)
1033+
__attribute__nonnull__(1);
1034+
#define PERL_ARGS_ASSERT_DRAND48_R \
1035+
assert(random_state)
1036+
10271037
PERL_CALLCONV void Perl_dump_all(pTHX);
10281038
PERL_CALLCONV void Perl_dump_all_perl(pTHX_ bool justperl);
10291039
PERL_CALLCONV void Perl_dump_eval(pTHX);

0 commit comments

Comments
 (0)