diff options
author | ondrej <ondrej@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2020-11-27 04:53:06 +0000 |
---|---|---|
committer | ondrej <ondrej@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2020-11-27 04:53:06 +0000 |
commit | 8d35ff545602abbf7cbef5b7685982676552668a (patch) | |
tree | 94c79015c6943549b99ee25dcaf64bfdee385e6a /rtl/inc | |
parent | d7830451e02a0547cd27da2166771e126b957e89 (diff) | |
download | fpc-8d35ff545602abbf7cbef5b7685982676552668a.tar.gz |
* revert r47598: implement TRandomGenerator
git-svn-id: https://svn.freepascal.org/svn/fpc/trunk@47605 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'rtl/inc')
-rw-r--r-- | rtl/inc/system.inc | 72 | ||||
-rw-r--r-- | rtl/inc/systemh.inc | 45 |
2 files changed, 27 insertions, 90 deletions
diff --git a/rtl/inc/system.inc b/rtl/inc/system.inc index 0b5e2600be..9b1f7932e1 100644 --- a/rtl/inc/system.inc +++ b/rtl/inc/system.inc @@ -51,6 +51,8 @@ const {$else} STACK_MARGIN = 16384; { Stack size margin for stack checking } {$endif} +{ Random / Randomize constants } + OldRandSeed : Cardinal = 0; { For Error Handling.} ErrorBase : Pointer = nil;public name 'FPC_ERRORBASE'; @@ -601,12 +603,29 @@ type {$R-} {range checking off} {$Q-} {overflow checking off} -function TRandomGenerator.MTWIST_MIXBITS(u, v: cardinal): cardinal; inline; +const + MTWIST_N = 624; + MTWIST_M = 397; + + MT_STATIC_SEED = 5489; + + MTWIST_UPPER_MASK = cardinal($80000000); + MTWIST_LOWER_MASK = cardinal($7FFFFFFF); + + MTWIST_MATRIX_A = cardinal($9908B0DF); + +var + mt_state: array[0..MTWIST_N-1] of cardinal; + +const + mt_index: cardinal = MTWIST_N+1; + +function MTWIST_MIXBITS(u, v: cardinal): cardinal; inline; begin result:=(u and MTWIST_UPPER_MASK) or (v and MTWIST_LOWER_MASK); end; -function TRandomGenerator.MTWIST_TWIST(u, v: cardinal): cardinal; inline; +function MTWIST_TWIST(u, v: cardinal): cardinal; inline; begin { the construct at the end is equivalent to if odd(v) then @@ -617,7 +636,7 @@ begin result:=(MTWIST_MIXBITS(u,v) shr 1) xor (cardinal(-(v and 1)) and MTWIST_MATRIX_A); end; -procedure TRandomGenerator.mtwist_init(seed: cardinal); +procedure mtwist_init(seed: cardinal); var i: longint; begin @@ -628,7 +647,7 @@ begin mt_index:=MTWIST_N; end; -procedure TRandomGenerator.mtwist_update_state; +procedure mtwist_update_state; var count: longint; begin @@ -649,7 +668,7 @@ begin end; -function TRandomGenerator.mtwist_u32rand: cardinal; +function mtwist_u32rand: cardinal; var l_index :cardinal; begin @@ -683,7 +702,7 @@ begin end; -function TRandomGenerator.Random(l:longint): longint; +function random(l:longint): longint; begin { otherwise we can return values = l (JM) } if (l < 0) then @@ -691,7 +710,7 @@ begin random := longint((int64(mtwist_u32rand)*l) shr 32); end; -function TRandomGenerator.Random(l:int64): int64; +function random(l:int64): int64; var a, b, c, d: cardinal; q, bd, ad, bc, ac: qword; @@ -726,49 +745,12 @@ begin end; {$ifndef FPUNONE} -function TRandomGenerator.Random: extended; -begin - random := mtwist_u32rand * (extended(1.0)/(int64(1) shl 32)); -end; -{$endif} - -procedure TRandomGenerator.Randomize(ARandSeed: cardinal); -begin - mt_index:=MTWIST_N+1; - OldRandSeed:=0; - Self.RandSeed:=ARandSeed; -end; - -procedure TRandomGenerator.Randomize; -begin - mt_index:=MTWIST_N+1; - OldRandSeed:=0; - System.Randomize(Self.RandSeed); -end; - - -function random(l:longint): longint; -begin - Result:=RandGenerator.Random(l); -end; - -function random(l:int64): int64; -begin - Result:=RandGenerator.Random(l); -end; - -{$ifndef FPUNONE} function random: extended; begin - Result:=RandGenerator.Random; + random := mtwist_u32rand * (extended(1.0)/(int64(1) shl 32)); end; {$endif} -Procedure Randomize; -begin - RandGenerator.Randomize; -end; - {$else FPC_USE_SIMPLE_RANDOM} { A simple implementation of random. TP/Delphi compatible. } diff --git a/rtl/inc/systemh.inc b/rtl/inc/systemh.inc index 81f03fb97b..3898399f17 100644 --- a/rtl/inc/systemh.inc +++ b/rtl/inc/systemh.inc @@ -786,51 +786,7 @@ const var ExitCode : TExitCode; public name 'operatingsystem_result'; - -{ Random / Randomize definitions and variables } -{$if defined(FPC_HAS_FEATURE_RANDOM)} -{$ifndef FPC_USE_SIMPLE_RANDOM} -type - TRandomGenerator = record - private const - MTWIST_N = 624; - MTWIST_M = 397; - - MTWIST_UPPER_MASK = cardinal($80000000); - MTWIST_LOWER_MASK = cardinal($7FFFFFFF); - - MTWIST_MATRIX_A = cardinal($9908B0DF); - - private - mt_index: cardinal; - RandSeed: cardinal; - OldRandSeed: cardinal; - mt_state: array[0..MTWIST_N-1] of cardinal; - - function MTWIST_MIXBITS(u, v: cardinal): cardinal; inline; - function MTWIST_TWIST(u, v: cardinal): cardinal; inline; - procedure mtwist_init(seed: cardinal); - procedure mtwist_update_state; - function mtwist_u32rand: cardinal; - public - procedure Randomize; - procedure Randomize(ARandSeed: cardinal); - - function Random(l:longint): longint; - function Random(l: int64): int64; - {$ifndef FPUNONE} - function Random: extended; - {$endif} - end; -var - RandGenerator: TRandomGenerator = (mt_index:TRandomGenerator.MTWIST_N+1; RandSeed:0; OldRandSeed:0); - RandSeed: Cardinal absolute RandGenerator.RandSeed; -{$else FPC_USE_SIMPLE_RANDOM} -var RandSeed : Cardinal; -{$endif FPC_USE_SIMPLE_RANDOM} -{$endif FPC_HAS_FEATURE_RANDOM} - { Delphi compatibility } {$ifdef FPC_HAS_FEATURE_DYNLIBS} @@ -976,7 +932,6 @@ Function Random(l:int64):int64; Function Random: extended; {$endif} Procedure Randomize; -Procedure Randomize(var RandSeed: cardinal); {$endif FPC_HAS_FEATURE_RANDOM} {$if defined(CPUINT8)} |