From a09f50de7fcde45ec4e9561d9155614e2e7d6065 Mon Sep 17 00:00:00 2001 From: bmprovan Date: Sat, 21 Jan 2017 04:54:19 -0600 Subject: [PATCH 1/2] Do float math with floats, avoid needless casting and double math. --- PCGSharp/Source/Pcg.cs | 7 ++++--- PCGSharp/Source/PcgExtended.cs | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/PCGSharp/Source/Pcg.cs b/PCGSharp/Source/Pcg.cs index e854194..a0b1b4d 100644 --- a/PCGSharp/Source/Pcg.cs +++ b/PCGSharp/Source/Pcg.cs @@ -68,6 +68,7 @@ public class Pcg { const ulong ShiftedIncrement = 721347520444481703ul; ulong _increment = 1442695040888963407ul; const ulong Multiplier = 6364136223846793005ul; + const float ToFloat01 = 1.0f / 4294967296.0f; const double ToDouble01 = 1.0 / 4294967296.0; // This attribute ensures that every thread will get its own instance of PCG. @@ -215,21 +216,21 @@ public uint[] NextUInts(int count, uint minInclusive, uint maxExclusive) { } public float NextFloat() { - return (float)(NextUInt() * ToDouble01); + return NextUInt() * ToFloat01; } public float NextFloat(float maxInclusive) { if(maxInclusive <= 0) throw new ArgumentException("MaxInclusive must be larger than 0"); - return (float)(NextUInt() * ToDouble01) * maxInclusive; + return NextUInt() * ToFloat01 * maxInclusive; } public float NextFloat(float minInclusive, float maxInclusive) { if(maxInclusive < minInclusive) throw new ArgumentException("Max must be larger than min"); - return (float)(NextUInt() * ToDouble01) * (maxInclusive-minInclusive) + minInclusive; + return NextUInt() * ToFloat01 * (maxInclusive-minInclusive) + minInclusive; } public float[] NextFloats(int count) { diff --git a/PCGSharp/Source/PcgExtended.cs b/PCGSharp/Source/PcgExtended.cs index f3b7693..87fe621 100644 --- a/PCGSharp/Source/PcgExtended.cs +++ b/PCGSharp/Source/PcgExtended.cs @@ -94,6 +94,7 @@ public class PcgExtended { const uint McgUnmultiplier = 2897767785u; // 1 / (uint.MaxValue + 1) + const float ToFloat01 = 1.0f / 4294967296.0f; const double ToDouble01 = 1.0 / 4294967296.0; uint[] _data; @@ -265,21 +266,21 @@ public uint[] NextUInts(int count, uint minInclusive, uint maxExclusive) { } public float NextFloat() { - return (float)(NextUInt() * ToDouble01); + return (NextUInt() * ToFloat01); } public float NextFloat(float maxInclusive) { if(maxInclusive <= 0) throw new ArgumentException("Max must be larger than 0"); - return (float)(NextUInt() * ToDouble01) * maxInclusive; + return (NextUInt() * ToFloat01) * maxInclusive; } public float NextFloat(float minInclusive, float maxInclusive) { if(maxInclusive < minInclusive) throw new ArgumentException("Max must be larger than min"); - return (float)(NextUInt() * ToDouble01) * (maxInclusive-minInclusive) + minInclusive; + return (NextUInt() * ToFloat01) * (maxInclusive-minInclusive) + minInclusive; } public float[] NextFloats(int count) { From d1ee4f21721a4743e2a886beece8686ce4821471 Mon Sep 17 00:00:00 2001 From: bmprovan Date: Sat, 21 Jan 2017 05:18:28 -0600 Subject: [PATCH 2/2] Dodge a few unnecessary operations in NextBool. --- PCGSharp/Source/Pcg.cs | 4 ++-- PCGSharp/Source/PcgExtended.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/PCGSharp/Source/Pcg.cs b/PCGSharp/Source/Pcg.cs index a0b1b4d..6f311b5 100644 --- a/PCGSharp/Source/Pcg.cs +++ b/PCGSharp/Source/Pcg.cs @@ -70,6 +70,7 @@ public class Pcg { const ulong Multiplier = 6364136223846793005ul; const float ToFloat01 = 1.0f / 4294967296.0f; const double ToDouble01 = 1.0 / 4294967296.0; + const uint HalfUint = uint.MaxValue / 2; // This attribute ensures that every thread will get its own instance of PCG. // An alternative, since PCG supports streams, is to use a different stream per @@ -334,8 +335,7 @@ public byte[] NextBytes(int count) { } public bool NextBool() { - var result = NextUInt(); - return result % 2 == 1; + return NextUInt() < HalfUint; } public bool[] NextBools(int count) { diff --git a/PCGSharp/Source/PcgExtended.cs b/PCGSharp/Source/PcgExtended.cs index 87fe621..f0422d4 100644 --- a/PCGSharp/Source/PcgExtended.cs +++ b/PCGSharp/Source/PcgExtended.cs @@ -96,6 +96,7 @@ public class PcgExtended { // 1 / (uint.MaxValue + 1) const float ToFloat01 = 1.0f / 4294967296.0f; const double ToDouble01 = 1.0 / 4294967296.0; + const uint HalfUint = uint.MaxValue / 2; uint[] _data; // This attribute ensures that every thread will get its own instance of PCG. @@ -384,8 +385,7 @@ public byte[] NextBytes(int count) { } public bool NextBool() { - uint result = NextUInt(); - return ((result % 2) == 1); + return NextUInt() < HalfUint; } public bool[] NextBools(int count) {