Skip to content

Commit b9604fe

Browse files
committed
BUG: Fix the shifting of the emulated 128-bit increment.
1 parent 4bd9e40 commit b9604fe

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
*.a
1111

1212
test-pcg64-*
13+
test-native.txt
14+
test-emulated.txt

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ pcg64-emulated.o: pcg64.c pcg64.h
2929
test-pcg64-native: test-pcg64-native.o pcg64-native.o
3030
test-pcg64-emulated: test-pcg64-emulated.o pcg64-emulated.o
3131

32+
test: test-pcg64-native test-pcg64-emulated
33+
./test-pcg64-native > test-native.txt
34+
./test-pcg64-emulated > test-emulated.txt
35+
diff -u test-native.txt test-emulated.txt
36+
3237
clean:
3338
rm -f *.a *.o test-pcg64-native test-pcg64-emulated

pcg64.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,11 @@ inline void pcg_setseq_128_srandom_r(pcg_state_setseq_128* rng,
130130
pcg128_t initstate, pcg128_t initseq)
131131
{
132132
rng->state = PCG_128BIT_CONSTANT(0ULL, 0ULL);
133+
/* Ensure that the increment is odd. */
133134
rng->inc.high = initseq.high << 1u;
134-
rng->inc.high |= initseq.low & 0x800000000000ULL;
135+
/* Shift the highest bit from the low word up to the lowest bit of the high
136+
* word. */
137+
rng->inc.high |= initseq.low >> 63u;
135138
rng->inc.low = (initseq.low << 1u) | 1u;
136139
pcg_setseq_128_step_r(rng);
137140
rng->state = _pcg128_add(rng->state, initstate);

test-pcg64.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ void print_uint64_hex(uint64_t x)
3535
int main(int argc, char** argv)
3636
{
3737
pcg64_random_t rng;
38-
pcg64_srandom_r(&rng, PCG_128BIT_CONSTANT(0ULL, 42ULL), PCG_128BIT_CONSTANT(0ULL, 54ULL));
38+
pcg64_srandom_r(&rng, PCG_128BIT_CONSTANT(0ULL, 42ULL),
39+
PCG_128BIT_CONSTANT(0, 0x8000000000000054ULL));
3940
for (int i=0; i < 100; i++) {
4041
print_uint64_hex(pcg64_random_r(&rng));
4142
}

0 commit comments

Comments
 (0)