5 Commits
v1.3.1 ... main

2 changed files with 16 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
CC ?= cc CC ?= cc
CFLAGS += -Wall -Wextra -Wpedantic -O3 -MD -MP -std=gnu99 CFLAGS += -Wall -Wextra -Wpedantic -O3 -MD -MP -std=gnu99 -DNDEBUG
LDFLAGS += LDFLAGS +=
SRC = $(shell find src/ -name '*.c') SRC = $(shell find src/ -name '*.c')

View File

@@ -13,6 +13,8 @@
exit(ret); \ exit(ret); \
} while (0) } while (0)
#define WIDTHOF(x) (sizeof(x) * 8)
typedef unsigned long long ull; typedef unsigned long long ull;
typedef struct dynrdat { typedef struct dynrdat {
@@ -20,17 +22,21 @@ typedef struct dynrdat {
ull dat[]; ull dat[];
} dynrdat; } dynrdat;
dynrdat* rdat = NULL;
static inline ull pow2_ceil(ull x) { static inline ull pow2_ceil(ull x) {
x -= !!x; // if x=0, remains 0; else x -= 1 x -= !!x; // if x=0, remains 0; else x -= 1
int lz = __builtin_clzll(x | 1); // get leading zeroes return 1ULL << (WIDTHOF(ull) - __builtin_clzll(x | 1));
return (~0ULL >> lz) + 1; // bit-shift the maximum value by this amount of leading zeroes }
static void quit(void) {
free(rdat);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
// return the result if no input // return the result if no input
if (argc <= 1) return printf("%s\n", (clock() & 1) ? "heads" : "tails"); if (argc <= 1) return printf("%s\n", (clock() & 1) ? "heads" : "tails");
atexit(quit);
dynrdat* rdat = NULL;
// loop through arguments // loop through arguments
for (unsigned i = 1; i < (unsigned)argc; ++i) { for (unsigned i = 1; i < (unsigned)argc; ++i) {
@@ -40,17 +46,14 @@ int main(int argc, char** argv) {
if (errno != 0) error(errno, "parse error for string: '%s'\n", argv[i]); if (errno != 0) error(errno, "parse error for string: '%s'\n", argv[i]);
// acquire random data (compiler will optimize MOD and DIV away since they're both base-2 constant values) // acquire random data (compiler will optimize MOD and DIV away since they're both base-2 constant values)
ull mod = c % (sizeof(ull) * 8); // get the remainder of the available random bits int mod = c % WIDTHOF(ull); // get the remainder of the available random bits
c = c / (sizeof(ull) * 8); // compute our "word count" c = c / WIDTHOF(ull); // compute our "word count"
// dynamically scale the array to our needs, ensuring 2^n scaling // dynamically scale the array to our needs, ensuring 2^n scaling
size_t cap = pow2_ceil(c + !!mod); size_t cap = pow2_ceil(c + !!mod);
if (!rdat || rdat->cap < cap) { if (!rdat || rdat->cap < cap) {
void* ptr = realloc(rdat, sizeof(dynrdat) + sizeof(ull) * cap); void* ptr = realloc(rdat, sizeof(dynrdat) + sizeof(ull) * cap);
if (!ptr) { if (!ptr) error(1, "insufficient memory\n", );
free(rdat);
error(1, "insufficient memory\n", );
}
rdat = ptr; rdat = ptr;
rdat->cap = cap; rdat->cap = cap;
@@ -69,7 +72,7 @@ int main(int argc, char** argv) {
for (; n < (rdat->dat + c); n++) { for (; n < (rdat->dat + c); n++) {
int cnt = __builtin_popcountll(*n); // counts the set bits int cnt = __builtin_popcountll(*n); // counts the set bits
headsc += cnt; headsc += cnt;
tailsc += sizeof(ull) * 8 - cnt; tailsc += WIDTHOF(ull) - cnt;
} }
// if there is a remainder, use the last N to get this // if there is a remainder, use the last N to get this
@@ -84,7 +87,5 @@ int main(int argc, char** argv) {
printf("results:\n heads: %llu\n tails: %llu\n", headsc, tailsc); printf("results:\n heads: %llu\n tails: %llu\n", headsc, tailsc);
} }
free(rdat);
return 0; return 0;
} }