Skip to content
This repository

Add Mersenne Twister implementation. #33

Merged
merged 1 commit into from over 2 years ago

3 participants

Nolan Lum Duke Leto Andrew Whitworth
Nolan Lum

No description provided.

Duke Leto
Collaborator

Closed GCI task: http://www.google-melange.com/gci/task/view/google/gci2011/7172206 , will merge this soon unless @whiteknight beats me to it

Duke Leto
Collaborator

Does this require a Parrot from within the last few days? I got a compile error about "emit" trying to compile Rosella on my current Parrot. Trying on the latest Parrot now.

Duke Leto
Collaborator
winxed  -o rosella/query.pir -c rosella/query.winxed
Method 'emit' not found for invocant of class 'Winxed;Compiler;FunctionId'
current instr.: 'parrot;Winxed;Compiler;IdentifierExpr;emit_assign_get' pc 19984 (ext/winxed/compiler.pir:8631) (winxedst1.winxed:3696)
called from Sub 'parrot;Winxed;Compiler;OpAssignExpr;emit_void' pc 23680 (ext/winxed/compiler.pir:10201) (winxedst1.winxed:4346)
called from Sub 'parrot;Winxed;Compiler;ExprStatement;emit' pc 16005 (ext/winxed/compiler.pir:6700) (winxedst1.winxed:2966)
called from Sub 'parrot;Winxed;Compiler;CompoundStatement;emit' pc 54675 (ext/winxed/compiler.pir:22546) (winxedst1.winxed:9263)
called from Sub 'parrot;Winxed;Compiler;IfStatement;emit' pc 45881 (ext/winxed/compiler.pir:19200) (winxedst1.winxed:7887)
called from Sub 'parrot;Winxed;Compiler;CompoundStatement;emit' pc 54675 (ext/winxed/compiler.pir:22546) (winxedst1.winxed:9263)
called from Sub 'parrot;Winxed;Compiler;FunctionBase;emit' pc 57512 (ext/winxed/compiler.pir:23804) (winxedst1.winxed:9796)
called from Sub 'parrot;Winxed;Compiler;emit_array' pc 11563 (ext/winxed/compiler.pir:4778) (winxedst1.winxed:2161)
called from Sub 'parrot;Winxed;Compiler;ClassStatement;emit' pc 60925 (ext/winxed/compiler.pir:25241) (winxedst1.winxed:10380)
called from Sub 'parrot;Winxed;Compiler;NamespaceBase;emit_base' pc 65195 (ext/winxed/compiler.pir:26978) (winxedst1.winxed:11066)
called from Sub 'parrot;Winxed;Compiler;NamespaceStatement;emit' pc 65633 (ext/winxed/compiler.pir:27192) (winxedst1.winxed:11160)
called from Sub 'parrot;Winxed;Compiler;NamespaceBase;emit_base' pc 65195 (ext/winxed/compiler.pir:26978) (winxedst1.winxed:11066)
called from Sub 'parrot;Winxed;Compiler;NamespaceStatement;emit' pc 65633 (ext/winxed/compiler.pir:27192) (winxedst1.winxed:11160)
called from Sub 'parrot;Winxed;Compiler;NamespaceBase;emit_base' pc 65195 (ext/winxed/compiler.pir:26978) (winxedst1.winxed:11066)
called from Sub 'parrot;Winxed;Compiler;RootNamespace;emit' pc 66119 (ext/winxed/compiler.pir:27440) (winxedst1.winxed:11280)
called from Sub 'parrot;Winxed;Compiler;WinxedCompileUnit;emit' pc 66733 (ext/winxed/compiler.pir:27705) (winxedst1.winxed:11398)
called from Sub 'parrot;Winxed;Compiler;WinxedHLL;__private_compile_tail' pc 66956 (ext/winxed/compiler.pir:27805) (winxedst1.winxed:11442)
called from Sub 'process_args' pc 1071 (ext/winxed/driver.pir:495) (winxed_installed.winxed:191)
called from Sub 'main' pc 1381 (ext/winxed/driver.pir:667) (winxed_installed.winxed:263)
exit status: 256
command: winxed  -o rosella/query.pir -c rosella/query.winxed
current instr.: 'parrot;Rosella;Winxed;Distutils;compile_to_pir' pc 536 (./src/winxed/Distutils.bootstrap.pir:337) (Distutils.winxed:132)
called from Sub 'parrot;Rosella;Winxed;Distutils;build_winxed_files' pc 248 (./src/winxed/Distutils.bootstrap.pir:144) (Distutils.winxed:61)
called from Sub 'run_step' pc 956 (runtime/parrot/library/distutils.pir:423)
called from Sub 'setup' pc 902 (runtime/parrot/library/distutils.pir:395)
called from Sub 'setup_main' pc 252 ((file unknown):63558474) (setup.winxed:39)
called from Sub 'main' pc 2173 ((file unknown):63559188) (setup.winxed:572)
called from Sub 'main' pc 1408 (ext/winxed/driver.pir:676) (winxed_installed.winxed:268)

current instr.: 'setup' pc 902 (runtime/parrot/library/distutils.pir:395)
called from Sub 'setup_main' pc 252 ((file unknown):63558474) (setup.winxed:39)
called from Sub 'main' pc 2196 ((file unknown):63559200) (setup.winxed:575)
called from Sub 'main' pc 1408 (ext/winxed/driver.pir:676) (winxed_installed.winxed:268)

This is on parrot 0af36791b3bbe and Rosella 2c86d47acb0a5a7

Nolan Lum

Whiteknight will have to comment on this. I haven't touched the query library at all.

Andrew Whitworth Whiteknight merged commit eb39b3f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 24, 2011
Nolan Lum nol888 Add Mersenne Twister implementation. 98ffa77
This page is out of date. Refresh to see the latest.
33 examples/mersenne.winxed
... ... @@ -0,0 +1,33 @@
  1 +// Mersenne Twister example.
  2 +
  3 +function main[main]()
  4 +{
  5 + var rosella = load_packfile("rosella/core.pbc");
  6 + var(Rosella.initialize_rosella)("random");
  7 +
  8 + var rnd = new Rosella.Random.MersenneTwister();
  9 +
  10 + print("20 random integers: ");
  11 + for(int i = 0; i < 20; i++)
  12 + {
  13 + print(rnd.get());
  14 + print(" ");
  15 + }
  16 + say("");
  17 +
  18 + print("20 random floats: ");
  19 + for(int i = 0; i < 20; i++)
  20 + {
  21 + print(rnd.get_float());
  22 + print(" ");
  23 + }
  24 + say("");
  25 +
  26 + print("20 random ints in [0, 10): ");
  27 + for(int i = 0; i < 20; i++)
  28 + {
  29 + print(rnd.get_range(0, 10));
  30 + print(" ");
  31 + }
  32 + say("");
  33 +}
5 setup.winxed
@@ -321,6 +321,11 @@ function setup_experimental_libraries(var rosella)
321 321 "commandline/Program",
322 322 "commandline/ProgramMode"
323 323 );
  324 +
  325 + setup_unstable_lib(rosella, "random", [],
  326 + "random/RandomNumber",
  327 + "random/MersenneTwister"
  328 + );
324 329 }
325 330
326 331 function setup_utilities(var rosella)
112 src/unstable/random/MersenneTwister.winxed
... ... @@ -0,0 +1,112 @@
  1 +namespace Rosella
  2 +{
  3 + namespace Random
  4 + {
  5 + class MersenneTwister : RandomNumber
  6 + {
  7 + // Period parameters
  8 + var N;
  9 + var M;
  10 + var MATRIX_A;
  11 + var UPPER_MASK;
  12 + var LOWER_MASK;
  13 +
  14 + // Tempering paramters
  15 + var TEMPERING_MASK_B;
  16 + var TEMPERING_MASK_C;
  17 +
  18 + var mt; // state vector
  19 + var mti; // mti == N + 1 means mt[N] is not initialized
  20 + var mag01;
  21 +
  22 + function MersenneTwister()
  23 + {
  24 + self.N = 624;
  25 + self.M = 397;
  26 + self.MATRIX_A = 0x9908b0df;
  27 + self.UPPER_MASK = 0x80000000; // most significant bit
  28 + self.LOWER_MASK = 0x7FFFFFFF; // the rest
  29 +
  30 + self.TEMPERING_MASK_B = 0x9D2C5680;
  31 + self.TEMPERING_MASK_C = 0xEFC60000;
  32 +
  33 + self.mt = [];
  34 + self.mag01 = [];
  35 +
  36 + int t;
  37 + ${ time t };
  38 + self.initialize(t);
  39 + }
  40 +
  41 + function initialize(int seed)
  42 + {
  43 + self.mag01[0] = 0;
  44 + self.mag01[1] = self.MATRIX_A;
  45 +
  46 + self.mt[0] = seed & 0xFFFFFFFF;
  47 + for (self.mti = 1; self.mti < self.N; self.mti++)
  48 + {
  49 + int y = self.mt[self.mti - 1];
  50 + int z;
  51 + ${ lsr z, y, 30 };
  52 +
  53 + self.mt[self.mti] = 1812433253 * (self.mt[self.mti - 1] ^ z) + self.mti;
  54 + self.mt[self.mti] = self.mt[self.mti] & 0xFFFFFFFF;
  55 + }
  56 + }
  57 +
  58 + function get()
  59 + {
  60 + int y;
  61 + int z;
  62 +
  63 + if (self.mti >= self.N)
  64 + {
  65 + int kk;
  66 + var mt = self.mt;
  67 + var mag01 = self.mag01;
  68 +
  69 + for (kk = 0; kk < self.N - self.M; kk++)
  70 + {
  71 + y = (mt[kk] & self.UPPER_MASK) | (mt[kk + 1] & self.LOWER_MASK);
  72 + ${ lsr z, y, 1 };
  73 + mt[kk] = mt[kk + self.M] ^ z ^ mag01[y & 1];
  74 + }
  75 + for (; kk < self.N - 1; kk++)
  76 + {
  77 + y = (mt[kk] & self.UPPER_MASK) | (mt[kk + 1] & self.LOWER_MASK);
  78 + ${ lsr z, y, 1 };
  79 + mt[kk] = mt[kk + (self.M - self.N)] ^ z ^ mag01[y & 1];
  80 + }
  81 +
  82 + y = (mt[self.N - 1] & self.UPPER_MASK) | (mt[0] & self.LOWER_MASK);
  83 + ${ lsr z, y, 1 };
  84 + mt[self.N - 1] = mt[self.M - 1] ^ z ^ mag01[y & 1];
  85 +
  86 + self.mti = 0;
  87 + }
  88 +
  89 + y = self.mt[self.mti];
  90 + self.mti++;
  91 +
  92 + ${ lsr z, y, 11 };
  93 + y = y ^ z; // TEMPERING_SHIFT_U(y)
  94 + y = y ^ (y << 7) & self.TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  95 + y = y ^ (y << 15) & self.TEMPERING_MASK_C; // TEMPERING_SHIFT_L(y)
  96 + ${ lsr z, y, 18 };
  97 + y = y ^ z; // TEMPERING_SHIFT_T(y)
  98 +
  99 + return y;
  100 + }
  101 +
  102 + function get_float()
  103 + {
  104 + float n = (1 << 24);
  105 + int y = self.get();
  106 + ${ lsr y, y, 8 };
  107 +
  108 + return y / n;
  109 + }
  110 + }
  111 + }
  112 +}
20 src/unstable/random/RandomNumber.winxed
... ... @@ -0,0 +1,20 @@
  1 +class Rosella.Random.RandomNumber
  2 +{
  3 + // Seed the random number generator with the given seed.
  4 + function initialize(int seed) { }
  5 +
  6 + // Gets a random signed integer from [INT_MIN, INT_MAX].
  7 + function get() { }
  8 +
  9 + // Gets a random floating-point number from [0, 1).
  10 + function get_float() { }
  11 +
  12 + // Gets a random signed integer from [low, high).
  13 + function get_range(int low, int high)
  14 + {
  15 + float f = self.get_float();
  16 + int i = f * (high - low) + low;
  17 +
  18 + return i;
  19 + }
  20 +}

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.