phobos/std
Joseph Rushton Wakeling dcd83ac02c Rework MersenneTwisterEngine using Ilya Yaroshenko's faster algorithm
This patch updates MersenneTwisterEngine's implementation to use the new
and much faster implementation Ilya prepared for `mir.random`.

The original Mersenne Twister algorithm (as per the reference C code by
Matsumoto and Nishimura) first regenerates the contents of the entire
state array, before looping over the resulting values and tempering them
in order to generate the actual variates.  Once the entire state array
has been iterated over, the array's values are then regenerated, and so
on and so forth.

Ilya's implementation reworks this idea to intertwine update of a single
entry in the state array with the tempering of another value to produce
the next variate.  This ensures that all the registers concerned stay
'hot' in the CPU's memory and hence ensures significant speedup.

(Just as a mention: as an experiment while adapting this code for phobos
I tried splitting apart the lines responsible for updating the internal
state array from those responsible for tempering the next variate, so
that they could be run sequentially instead of mixed up together; this
resulted in a fairly significant speed hit.)

In contrast to most (all?) other Mersenne Twister implementations, this
one iterates backwards through the state array, which allows for a few
extra compiler optimizations that speed up the algorithm.  It has thus
been necessary to rework the range-based `seed` method to take account
of this.

Besides the algorithmic change, this patch introduces two new template
variables: an extra tempering variable `d`, and the initialization
multiplier `f`, which brings the template implementation in line with
that of the C++11 standard.  These extra template variables are needed
in order to effectively implement the standard 64-bit version of the
Mersenne Twister, which will be added in a follow-up patch.

Finally, this implementation introduces corrections to the handling of
the word size `w` to ensure that the right sequences are produced when
the word size is less than the number of bits in `UIntType`.  Unittests
will be added for this in a follow-up patch.

------------------------------------------------------------------------

Where this implementation differs from that in `mir.random`:

Ilya's original design has been reworked as a drop-in replacement for
the existing Phobos implementation, which is currently implemented as a
forward range (although it should ideally be an input range, but that
design flaw should be fixed as a separate issue).

There appears to be no significant speed hit from reworking Ilya's code
as a range rather than as a functor.  However, some other aspects of the
original design have required rather intrusive changes in order to get
the full speed benefits without introducing breaking change.

The original MersenneTwisterEngine allows for implicit instantiation of
generator instances without providing a seed.  This is handled by
checking in the `front` and `popFront` method whether the generator has
been properly initialized, and seeding it with the default seed if not.
However, these runtime checks on every single call result in a massive
speed hit.  The current implementation therefore takes a number of steps
in order to ensure that the internal state of the generator can be set
to its default-seeded values at compile time:

  * all the internal state variables have been wrapped up in a nested
    `State` struct to facilitate generation;

  * the internals of the `seed` and `popFront` methods have been
    separated out into CTFE'able private static methods (`seedImpl`
    and `popFrontImpl`) which take a reference to a `State` instance
    as input;

  * a CTFE'able private static `defaultState` method has been added
    which generates a `State` instance matching that generated by
    instantiating the generator with the `defaultSeed`.

The `defaultState` method is then used to initialize the default value
of the internal `State` instance at compile-time, replicating the effect
of the original runtime seeding check.

These latter workarounds could be removed in future if the generator
were updated to `@disable this();` and therefore always require explicit
seeding, but this would be a breaking change and so is avoided for now.
2017-02-15 21:17:16 +01:00
..
algorithm swapRanges: Remove redundant constraint tests 2017-02-15 17:21:21 +00:00
c
container [doc] Fix argument naming. 2017-02-09 15:35:08 +01:00
digest Merge remote-tracking branch 'upstream/stable' into merge_stable 2017-01-15 01:30:10 +01:00
experimental Merge pull request #5092 from JackStouffer/logger-imports 2017-02-10 15:51:26 -08:00
internal Merge remote-tracking branch 'upstream/stable' into merge_stable 2017-01-15 01:30:10 +01:00
net Added immutable to never modified variables in std.net.email 2017-02-14 10:29:32 -05:00
range swapRanges: Remove redundant constraint tests 2017-02-15 17:21:21 +00:00
regex style, remove several useless usage of "final" 2017-02-06 09:34:28 +01:00
windows fix deprecation messages about core.bitop.bswap 2017-02-07 00:38:49 -08:00
array.d [scope] add @trusted and 'scope' annotations to std.array 2017-02-02 01:17:48 -08:00
ascii.d
base64.d Remove package wide std.algorithm imports from Phobos 2016-09-22 08:36:14 +01:00
bigint.d
bitmanip.d Move deprecations along. 2017-01-24 03:08:10 -08:00
compiler.d
complex.d
concurrency.d std.concurrency: remove false Ddoc comments 2017-02-11 02:09:19 -08:00
concurrencybase.d
conv.d Add checks for infinite ranges in many range function signitures 2017-02-14 14:19:04 -05:00
csv.d Merge pull request #5105 from JackStouffer/csv-const 2017-02-10 15:52:37 -08:00
datetime.d Merge pull request #5065 from WalterBright/datetime-scope 2017-02-11 08:08:35 -05:00
demangle.d
encoding.d fix Issue 16291 - EncodingScheme.create fails 2016-10-05 01:54:22 +02:00
exception.d ErrnoException: add a ctor overload that takes errno as an argument 2017-02-14 10:56:36 +02:00
file.d Add checks for infinite ranges in many range function signitures 2017-02-14 14:19:04 -05:00
format.d Merge pull request #5130 from JackStouffer/format-docs 2017-02-15 19:52:00 +01:00
functional.d
getopt.d Merge pull request #5010 from Chaosus/master 2017-01-05 14:55:09 -05:00
json.d std.json: use proper block comments 2017-02-09 12:39:55 -08:00
math.d std.math: use proper block comments 2017-02-09 13:28:49 -08:00
mathspecial.d
meta.d correct anySatisfy docs 2017-01-11 05:52:04 +01:00
mmfile.d Removed uses of the delete keyword from std.mmfile 2016-10-27 16:30:30 +01:00
numeric.d Make more modules publicly runnable on dlang.org 2017-01-06 23:29:55 +01:00
outbuffer.d Fix std.outbuffer.[v]printf() for Visual Studio 2015+ 2017-02-07 23:30:23 +01:00
parallelism.d [doc] Remove spurious data section. 2017-02-09 18:41:16 +01:00
path.d Add missing imports to public unittests 2016-12-15 23:23:35 +01:00
process.d remove trailing whitespace 2017-01-31 13:27:03 +00:00
random.d Rework MersenneTwisterEngine using Ilya Yaroshenko's faster algorithm 2017-02-15 21:17:16 +01:00
signals.d Follow-up style fixes for November 2016-12-08 01:46:47 +01:00
socket.d std.socket: use proper block comments 2017-02-11 02:05:27 -08:00
stdint.d [doc] Add emphasis for readability. 2017-02-10 23:46:12 +01:00
stdio.d Added example to std.stdio.writeln 2017-02-15 10:26:12 -05:00
stdiobase.d
string.d Add checks for infinite ranges in many range function signitures 2017-02-14 14:19:04 -05:00
system.d
traits.d [scope] add some 'scope' support to std.traits 2017-02-04 02:11:00 -08:00
typecons.d [doc] Typo. 2017-02-13 20:43:34 +01:00
typetuple.d
uni.d Add missing imports to public unittests 2016-12-15 23:23:35 +01:00
uri.d removed most global imports from std.uri 2017-01-22 11:13:20 -05:00
utf.d Merge pull request #5126 from JackStouffer/codeLength 2017-02-15 16:51:51 +01:00
uuid.d Remove package wide std.algorithm imports from Phobos 2016-09-22 08:36:14 +01:00
variant.d std.variant: Guard documented unittest members in VariantN 2016-12-08 10:53:57 +00:00
xml.d make std.xml toString() @safe 2017-02-06 21:08:32 -08:00
zip.d Remove package wide std.algorithm imports from Phobos 2016-09-22 08:36:14 +01:00
zlib.d Removed uses of the delete keyword from std.zlib 2016-10-27 16:30:33 +01:00