From 82be2ea8122a93b0d69d1ecea6f04b01825133f5 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 20 Aug 2019 18:16:16 +0100 Subject: [PATCH] replaced longs with uint64_ts --- Makefile | 2 +- src/frng.c | 2 ++ src/main.c | 52 +++++++++++++++++++++++++++------------------------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index a94f6aa..7ecfcba 100644 --- a/Makefile +++ b/Makefile @@ -6,5 +6,5 @@ clean: rm -f build/* shuffle3: - gcc src/*.c -Iinclude/ -g -Wall -pedantic --std=gnu11 -o build/$@ -lm + gcc src/*.c -Iinclude/ -g -Wall -pedantic --std=gnu11 -o build/$@ -lm ./build/$@ diff --git a/src/frng.c b/src/frng.c index 9ddc150..d0af1b0 100644 --- a/src/frng.c +++ b/src/frng.c @@ -2,6 +2,8 @@ #include #include +_Static_assert(sizeof(double)==8, "Double must be 64-bits."); + double vec[2]; static void _seed(const void* from) diff --git a/src/main.c b/src/main.c index ed17d47..f405b3a 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,8 @@ #include #include +_Static_assert(sizeof(float)==4, "Float must be 32 bits."); + /*void test_destructor(object* ptr) { printf("die\n"); @@ -50,12 +52,12 @@ void unshuffle(RNG with, array_t data) printf(" Okay\n"); } -void minmax_longs(long* min, long* max, const array_t data) +void minmax_int64_ts(int64_t* min, int64_t* max, const array_t data) { for(register int i=0;i *max) *max = ar_get_v(data, long, i); - if(ar_get_v(data, long, i) < *min) *min = ar_get_v(data, long, i); + if(ar_get_v(data, int64_t, i) > *max) *max = ar_get_v(data, int64_t, i); + if(ar_get_v(data, int64_t, i) < *min) *min = ar_get_v(data, int64_t, i); } } int valid_float(float f) @@ -73,12 +75,12 @@ void minmax_floats(float* min, float* max, const array_t data) } } -void minmax_sbytes(signed char* min, signed char* max, const array_t data) +void minmax_sbytes(int8_t* min, int8_t* max, const array_t data) { for(register int i=0;i *max) *max = ar_get_v(data, signed char, i); - if(ar_get_v(data, signed char, i) < *min) *min = ar_get_v(data, signed char, i); + if(ar_get_v(data, int8_t, i) > *max) *max = ar_get_v(data, int8_t, i); + if(ar_get_v(data, int8_t, i) < *min) *min = ar_get_v(data, int8_t, i); } } @@ -92,16 +94,16 @@ void print_array(const array_t data) void unshuffle3(S_LEXENV, array_t data) { - if(ar_type(data)!=sizeof(signed char)) - ar_reinterpret(data, sizeof(signed char)); + if(ar_type(data)!=sizeof(int8_t)) + ar_reinterpret(data, sizeof(int8_t)); RNG xoro = S_LEXCALL(rng_object, rng_new(RNG_ALGO(xoroshiro128plus))); RNG frng = S_LEXCALL(rng_object, rng_new(RNG_ALGO(frng))); RNG drng = S_LEXCALL(rng_object, rng_new(RNG_ALGO(drng))); - signed char bmax = INT8_MIN; - signed char bmin = INT8_MAX; + int8_t bmax = INT8_MIN; + int8_t bmin = INT8_MAX; minmax_sbytes(&bmin, &bmax, data); - unsigned int seed = (0xfffa << 16) | (bmin<<8) | bmax; + uint32_t seed = (0xfffa << 16) | (bmin<<8) | bmax; rng_seed(drng, &seed); unshuffle(drng, data); @@ -115,10 +117,10 @@ void unshuffle3(S_LEXENV, array_t data) rng_seed(frng, fseed); unshuffle(frng, data); - long min = INT64_MAX; - long max = INT64_MIN; - ar_reinterpret(data, sizeof(long)); - minmax_longs(&min, &max, data); + int64_t min = INT64_MAX; + int64_t max = INT64_MIN; + ar_reinterpret(data, sizeof(int64_t)); + minmax_int64_ts(&min, &max, data); uint64_t xseed[2]; xseed[0] = min; xseed[1] = max; @@ -127,22 +129,22 @@ void unshuffle3(S_LEXENV, array_t data) } void shuffle3(S_LEXENV, array_t data) { - if(ar_type(data)!=sizeof(long)) - ar_reinterpret(data, sizeof(long)); + if(ar_type(data)!=sizeof(int64_t)) + ar_reinterpret(data, sizeof(int64_t)); RNG xoro = S_LEXCALL(rng_object, rng_new(RNG_ALGO(xoroshiro128plus))); RNG frng = S_LEXCALL(rng_object, rng_new(RNG_ALGO(frng))); RNG drng = S_LEXCALL(rng_object, rng_new(RNG_ALGO(drng))); - long min = INT64_MAX; - long max = INT64_MIN; + int64_t min = INT64_MAX; + int64_t max = INT64_MIN; float fmin = (float)DBL_MAX; float fmax = (float)-DBL_MAX; - signed char bmax = INT8_MIN; - signed char bmin = INT8_MAX; + int8_t bmax = INT8_MIN; + int8_t bmin = INT8_MAX; - minmax_longs(&min, &max, data); + minmax_int64_ts(&min, &max, data); uint64_t xseed[2]; xseed[0] = min; xseed[1] = max; @@ -159,9 +161,9 @@ void shuffle3(S_LEXENV, array_t data) rng_seed(frng, fseed); shuffle(frng, data); - ar_reinterpret(data,sizeof(signed char)); + ar_reinterpret(data,sizeof(int8_t)); minmax_sbytes(&bmin, &bmax, data); - unsigned int seed = (0xfffa << 16) | (bmin<<8) | bmax; + uint32_t seed = (0xfffa << 16) | (bmin<<8) | bmax; rng_seed(drng, &seed); shuffle(drng, data); } @@ -179,7 +181,7 @@ void print_usage(char** argv) array_t read_whole_file(S_NAMED_LEXENV(base), FILE* fp) { fseek(fp,0,SEEK_END); - long sz = ftell(fp); + int64_t sz = ftell(fp); fseek(fp,0,SEEK_SET); array_t ar;