Skip to content

Commit

Permalink
Add some glue to handle zeros being returned from the Philox 4x32 pse…
Browse files Browse the repository at this point in the history
…udo-RNG.
  • Loading branch information
tpn committed Jan 9, 2021
1 parent c96ee22 commit 96b44fa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/PerfectHash/Rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef struct DECLSPEC_ALIGN(16) _RNG_STATE_PHILOX43210 {
UINT4 Output;
UINT2 Key;
ULONG CurrentCount;
ULONG Padding;
ULONG NumberOfZeroLongsEncountered;
ULONGLONG TotalCount;
ULONGLONG TotalBytes;
} RNG_STATE_PHILOX43210, *PRNG_STATE_PHILOX43210;
Expand Down
43 changes: 40 additions & 3 deletions src/PerfectHash/RngPhilox4x32.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ RngPhilox43210Init(
State->Key.X = (ULONG)Rng->Seed;
State->Key.Y = (ULONG)(Rng->Seed >> 32);
State->CurrentCount = 0;
State->NumberOfZeroLongsEncountered = 0;
State->TotalCount = 0;
State->TotalBytes = 0ULL;
SkipaheadSubsequence(State, Rng->Subsequence);
Expand All @@ -352,9 +353,45 @@ RngPhilox43210GenerateRandomBytes(
Long = (PLONG)Buffer;

for (Index = 0; Index < NumberOfLongs; Index++) {
Random = GetRandomLong(State);
ASSERT(Random != 0);
*Long++ = Random;
do {
Random = GetRandomLong(State);

//
// I encountered a zero being returned under the following
// conditions for State:
//
// Counter.X = 0x01c922d1
// Counter.Y = 0x0
// Counter.Z = 0x2
// Counter.W = 0x0
// Key.X = 0x19811025
// Key.Y = 0x20190903
// CurrentCount = 2
// TotalCount = 119835462
// TotalBytes = 479341848
//
// This was using our default seed, and occurred on the
// normaliz-1.keys file in sys32, with the following command line
// parameters:
// --FindBestGraph
// --BestCoverageType=LowestNumberOfEmptyCacheLines
// --BestCoverageAttempts=2
// --GraphImpl=1
//

if (Random != 0) {
*Long++ = Random;
break;
}

State->NumberOfZeroLongsEncountered++;

//
// I picked 10 arbitrarily.
//

ASSERT(State->NumberOfZeroLongsEncountered < 10);
} while (1);
}

return S_OK;
Expand Down

0 comments on commit 96b44fa

Please sign in to comment.