Skip to content

Commit

Permalink
[fix] use correct floating point convertion
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMadsen committed Jan 2, 2016
1 parent d1594b4 commit 51d7ce3
Show file tree
Hide file tree
Showing 4 changed files with 471 additions and 216 deletions.
20 changes: 18 additions & 2 deletions reference.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

/* The state must be seeded so that it is not everywhere zero. */
uint64_t s[ 2 ];

uint64_t xorshift128plus(void) {
uint64_t xorshift128plus_int(void) {
uint64_t s1 = s[ 0 ];
const uint64_t s0 = s[ 1 ];
s[ 0 ] = s0;
s1 ^= s1 << 23;
return ( s[ 1 ] = ( s1 ^ s0 ^ ( s1 >> 17 ) ^ ( s0 >> 26 ) ) ) + s0;
}

double xorshift128plus_double(void) {
const uint64_t x = xorshift128plus_int();
const uint64_t x_doublefied = UINT64_C(0x3FF) << 52 | x >> 12;

return *((double *) &x_doublefied) - 1.0;
}

int main(int argc, char *argv[]) {
s[0] = 1L;
s[1] = 2L;
Expand All @@ -27,9 +35,17 @@ int main(int argc, char *argv[]) {
s[1] = atol(argv[3]);
}

const bool useDouble = (argc > 4);

printf("[\n");
for (int i = 0; i < length; i++) {
printf(" \"%016llX\"", xorshift128plus());

if (useDouble) {
printf(" \"%.20e\"", xorshift128plus_double());
} else {
printf(" \"%016llX\"", xorshift128plus_int());
}

if (i < length - 1) {
printf(",\n");
}
Expand Down
Loading

0 comments on commit 51d7ce3

Please sign in to comment.