Permalink
Browse files

Use xoroshiro128+ random generator (everywhere besides BSON code).

  • Loading branch information...
savask committed Apr 23, 2018
1 parent fbe81ba commit cda029ff4246ff116d0b2872199b24067a215f22
Showing with 481 additions and 406 deletions.
  1. +1 −5 src/Probability.cpp
  2. +0 −1 src/Probability.h
  3. +51 −0 src/common/tpt-rand.cpp
  4. +22 −0 src/common/tpt-rand.h
  5. +4 −3 src/graphics/Renderer.cpp
  6. +4 −2 src/gui/preview/PreviewView.cpp
  7. +2 −1 src/simulation/Air.cpp
  8. +1 −0 src/simulation/Elements.h
  9. +52 −51 src/simulation/Simulation.cpp
  10. +1 −0 src/simulation/Simulation.h
  11. +5 −5 src/simulation/elements/ACID.cpp
  12. +1 −1 src/simulation/elements/AMTR.cpp
  13. +2 −2 src/simulation/elements/ANAR.cpp
  14. +6 −6 src/simulation/elements/BANG.cpp
  15. +4 −4 src/simulation/elements/BCLN.cpp
  16. +3 −3 src/simulation/elements/BMTL.cpp
  17. +2 −2 src/simulation/elements/BOMB.cpp
  18. +2 −2 src/simulation/elements/BOYL.cpp
  19. +1 −1 src/simulation/elements/BREC.cpp
  20. +3 −3 src/simulation/elements/BRMT.cpp
  21. +2 −2 src/simulation/elements/C5.cpp
  22. +1 −1 src/simulation/elements/CAUS.cpp
  23. +9 −9 src/simulation/elements/CBNW.cpp
  24. +3 −3 src/simulation/elements/CLNE.cpp
  25. +1 −1 src/simulation/elements/CLST.cpp
  26. +5 −5 src/simulation/elements/CO2.cpp
  27. +1 −1 src/simulation/elements/COAL.cpp
  28. +5 −5 src/simulation/elements/DEST.cpp
  29. +4 −4 src/simulation/elements/DEUT.cpp
  30. +6 −6 src/simulation/elements/DSTW.cpp
  31. +4 −4 src/simulation/elements/ELEC.cpp
  32. +13 −13 src/simulation/elements/EMP.cpp
  33. +7 −7 src/simulation/elements/EXOT.cpp
  34. +11 −11 src/simulation/elements/Element.cpp
  35. +3 −3 src/simulation/elements/FILT.cpp
  36. +9 −9 src/simulation/elements/FIRE.cpp
  37. +7 −7 src/simulation/elements/FIRW.cpp
  38. +2 −2 src/simulation/elements/FOG.cpp
  39. +2 −2 src/simulation/elements/FRZW.cpp
  40. +1 −1 src/simulation/elements/FRZZ.cpp
  41. +3 −3 src/simulation/elements/FSEP.cpp
  42. +3 −3 src/simulation/elements/FUSE.cpp
  43. +12 −12 src/simulation/elements/FWRK.cpp
  44. +5 −5 src/simulation/elements/GEL.cpp
  45. +1 −1 src/simulation/elements/GLOW.cpp
  46. +3 −3 src/simulation/elements/GOLD.cpp
  47. +1 −1 src/simulation/elements/GOO.cpp
  48. +1 −1 src/simulation/elements/GRAV.cpp
  49. +6 −6 src/simulation/elements/H2.cpp
  50. +2 −2 src/simulation/elements/ICEI.cpp
  51. +5 −5 src/simulation/elements/IGNT.cpp
  52. +5 −5 src/simulation/elements/IRON.cpp
  53. +3 −3 src/simulation/elements/ISOZ.cpp
  54. +3 −3 src/simulation/elements/ISZS.cpp
  55. +12 −12 src/simulation/elements/LIGH.cpp
  56. +4 −4 src/simulation/elements/MERC.cpp
  57. +4 −4 src/simulation/elements/NBLE.cpp
  58. +20 −20 src/simulation/elements/NEUT.cpp
  59. +5 −5 src/simulation/elements/O2.cpp
  60. +3 −3 src/simulation/elements/PBCN.cpp
  61. +2 −2 src/simulation/elements/PCLN.cpp
  62. +10 −10 src/simulation/elements/PHOT.cpp
  63. +2 −2 src/simulation/elements/PIPE.cpp
  64. +5 −5 src/simulation/elements/PLNT.cpp
  65. +1 −1 src/simulation/elements/PLUT.cpp
  66. +2 −2 src/simulation/elements/POLO.cpp
  67. +3 −3 src/simulation/elements/PROT.cpp
  68. +6 −6 src/simulation/elements/PRTI.cpp
  69. +5 −5 src/simulation/elements/PRTO.cpp
  70. +4 −4 src/simulation/elements/QRTZ.cpp
  71. +1 −1 src/simulation/elements/RIME.cpp
  72. +2 −2 src/simulation/elements/RPEL.cpp
  73. +2 −2 src/simulation/elements/SHLD1.cpp
  74. +2 −2 src/simulation/elements/SHLD2.cpp
  75. +2 −2 src/simulation/elements/SHLD3.cpp
  76. +1 −1 src/simulation/elements/SHLD4.cpp
  77. +7 −7 src/simulation/elements/SING.cpp
  78. +4 −4 src/simulation/elements/SLTW.cpp
  79. +1 −1 src/simulation/elements/SNOW.cpp
  80. +7 −7 src/simulation/elements/SPNG.cpp
  81. +5 −5 src/simulation/elements/SPRK.cpp
  82. +5 −5 src/simulation/elements/STKM.cpp
  83. +2 −2 src/simulation/elements/THDR.cpp
  84. +2 −2 src/simulation/elements/TRON.cpp
  85. +7 −7 src/simulation/elements/TUNG.cpp
  86. +6 −6 src/simulation/elements/VIBR.cpp
  87. +1 −1 src/simulation/elements/VINE.cpp
  88. +3 −3 src/simulation/elements/VIRS.cpp
  89. +5 −5 src/simulation/elements/WARP.cpp
  90. +6 −6 src/simulation/elements/WATR.cpp
  91. +1 −1 src/simulation/elements/WTRV.cpp
  92. +2 −2 src/simulation/elements/YEST.cpp
  93. +3 −3 src/simulation/simtools/Mix.cpp
@@ -16,6 +16,7 @@
#include <numeric>
#include <cstdlib>
#include "Probability.h"
#include "common/tpt-rand.h"

namespace Probability
{
@@ -25,11 +26,6 @@ float binomial_gte1(int n, float p)
return 1.0f - std::pow(1.0f-p, n);
}

float randFloat()
{
return static_cast<float>(rand())/RAND_MAX;
}

SmallKBinomialGenerator::SmallKBinomialGenerator(unsigned int n, float p, unsigned int maxK_)
{
maxK = maxK_;
@@ -25,7 +25,6 @@ namespace Probability
// X ~ binomial(n,p), returns P(X>=1)
// e.g. If a reaction has n chances of occurring, each time with probability p, this returns the probability that it occurs at least once.
float binomial_gte1(int n, float p);
float randFloat();

class SmallKBinomialGenerator
{
@@ -0,0 +1,51 @@
#include "tpt-rand.h"
#include <cstdlib>

/* xoroshiro128+ by David Blackman and Sebastiano Vigna */

static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}

uint64_t RandomGen::next(void) {
const uint64_t s0 = s[0];
uint64_t s1 = s[1];
const uint64_t result = s0 + s1;

s1 ^= s0;
s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
s[1] = rotl(s1, 36); // c

return result;
}

unsigned int RandomGen::operator()()
{
return next()&0xFFFFFFFF;
}

unsigned int RandomGen::between(unsigned int lower, unsigned int upper)
{
unsigned int r = (*this)();

return r % (upper - lower + 1) + lower;
}

float RandomGen::uniform01()
{
return static_cast<float>(random_gen())/(float)0xFFFFFFFF;
}

RandomGen::RandomGen()
{
s[0] = 1;
s[1] = 2;
}

void RandomGen::seed(unsigned int sd)
{
s[0] = sd;
s[1] = sd;
}

RandomGen random_gen;
@@ -0,0 +1,22 @@
#ifndef TPT_RAND_
#define TPT_RAND_

#include <stdint.h>

class RandomGen
{
private:
uint64_t s[2];
uint64_t next(void);
public:
unsigned int operator()();
unsigned int between(unsigned int lower, unsigned int upper);
float uniform01();

RandomGen();
void seed(unsigned int sd);
};

extern RandomGen random_gen;

#endif /* TPT_RAND_ */
@@ -8,6 +8,7 @@
#include "Renderer.h"
#include "Graphics.h"
#include "common/tpt-math.h"
#include "common/tpt-rand.h"
#include "common/tpt-minmax.h"
#include "gui/game/RenderPreset.h"
#include "simulation/Elements.h"
@@ -1774,7 +1775,7 @@ void Renderer::render_parts()
}
if(pixel_mode & PMODE_SPARK)
{
flicker = rand()%20;
flicker = random_gen()%20;
#ifdef OGLR
//Oh god, this is awful
lineC[clineC++] = ((float)colr)/255.0f;
@@ -1838,7 +1839,7 @@ void Renderer::render_parts()
}
if(pixel_mode & PMODE_FLARE)
{
flicker = rand()%20;
flicker = random_gen()%20;
#ifdef OGLR
//Oh god, this is awful
lineC[clineC++] = ((float)colr)/255.0f;
@@ -1911,7 +1912,7 @@ void Renderer::render_parts()
}
if(pixel_mode & PMODE_LFLARE)
{
flicker = rand()%20;
flicker = random_gen()%20;
#ifdef OGLR
//Oh god, this is awful
lineC[clineC++] = ((float)colr)/255.0f;
@@ -17,6 +17,8 @@
#include "gui/interface/AvatarButton.h"
#include "gui/interface/Keys.h"
#include "gui/dialogues/ErrorMessage.h"
#include "common/tpt-math.h"
#include "common/tpt-rand.h"

class PreviewView::LoginAction: public ui::ButtonAction
{
@@ -297,7 +299,7 @@ void PreviewView::CheckComment()
{
if (!commentHelpText)
{
if (rand()%2)
if (random_gen()%2)
commentWarningLabel->SetText("Stolen? Report the save instead");
else
commentWarningLabel->SetText("Please report stolen saves");
@@ -313,7 +315,7 @@ void PreviewView::CheckComment()
{
if (!commentHelpText)
{
if (rand()%2)
if (random_gen()%2)
commentWarningLabel->SetText("Please do not swear");
else
commentWarningLabel->SetText("Bad language may be deleted");
@@ -6,6 +6,7 @@
//#include <powder.h>
//#include <defines.h>
#include "Gravity.h"
#include "common/tpt-rand.h"

/*float kernel[9];
@@ -373,7 +374,7 @@ void Air::RecalculateBlockAirMaps()
}
}
// mostly accurate insulator blocking, besides checking GEL
else if ((type == PT_HSWC && sim.parts[i].life != 10) || sim.elements[type].HeatConduct <= (rand()%250))
else if ((type == PT_HSWC && sim.parts[i].life != 10) || sim.elements[type].HeatConduct <= (random_gen()%250))
{
int x = ((int)(sim.parts[i].x+0.5f))/CELL, y = ((int)(sim.parts[i].y+0.5f))/CELL;
if (sim.InBounds(x, y) && !(bmap_blockairh[y][x]&0x8))
@@ -70,6 +70,7 @@
struct playerst;

#include "ElementClasses.h"
#include "common/tpt-rand.h"


#endif /* ELEMENTS_H_ */
Oops, something went wrong.

2 comments on commit cda029f

@moonheart08

This comment has been minimized.

Contributor

moonheart08 replied Apr 23, 2018

:)

@moonheart08

This comment has been minimized.

Contributor

moonheart08 replied Apr 23, 2018

Now that's nice. next(), when compiled in release mode, is 14 instructions (at least on my laptop)

mov    (%rdi),%rax
mov    0x8(%rdi),%rsi
mov    %rax,%rdx
rorx   $0x9,%rax,%rcx
add    %rsi,%rax
xor    %rsi,%rdx
mov    %rdx,%r8
xor    %rdx,%rcx
rorx   $0x1c,%rdx,%rdx
shl    $0xe,%r8
mov    %rdx,0x8(%rdi)
xor    %r8,%rcx
mov    %rcx,(%rdi)
retq   

All low level, high speed instructions. Plus, almost everything it does is directly operating on registers, not memory, making it even faster (1-2 uOPs).
Only thing that should be different for standard builds is RORX not being there, as RORX is a newer instruction and as such less systems have it.

Please sign in to comment.