16 Commits
v1.1 ... main

Author SHA1 Message Date
210725ab52 create a macro to reduce the head strain of getting the width of a type 2025-06-20 00:26:56 +02:00
29d76880e3 improve memory allocation logic 2025-06-20 00:22:14 +02:00
0acc93d137 use an exit function, rather than manually freeing, since it's saver for when errors happen 2025-06-19 23:57:46 +02:00
b5b16be0ad mod does not need the width of ull, so set it to be int 2025-06-19 23:42:12 +02:00
8304c0620b fix: not adding -DNDEBUG flag to compilation 2025-06-19 23:32:38 +02:00
e18e9b0297 fix: was not multiplying by byte size of ull 2025-06-19 22:58:35 +02:00
65d91b2d00 add error handling for if we cannot get the random data 2025-06-19 18:33:25 +02:00
2fd839ae12 fix: integer overflow when giving a small value as an argument 2025-06-19 18:18:31 +02:00
fa19fc2789 fix: using rand() in single flips will always be heads 2025-06-19 18:14:33 +02:00
b31071bb91 fix: running against linux's stack size limit 2025-06-19 17:53:12 +02:00
598834255f optimize some more by using /dev/random 2025-06-19 16:57:15 +02:00
5f6f1bf720 make the macro more cross-compiler friendly 2025-06-19 16:41:09 +02:00
1e9ae43e52 update workflows (to also include ARM support) 2025-06-17 23:51:02 +02:00
95dce84d28 fully reoptimize 2025-06-17 23:51:02 +02:00
deabe97247 rework code to be more accurate and platform-independent 2025-06-17 12:55:20 +02:00
3bbfea7400 fix header file to be more platform-independent 2025-06-17 12:46:47 +02:00
4 changed files with 93 additions and 122 deletions

View File

@@ -9,72 +9,26 @@ on:
description: the tag to release for
default: ""
jobs:
compile:
runs-on: ubuntu-latest
release_bin:
runs-on: ${{matrix.os}}
strategy:
matrix:
os:
- ubuntu-24.04
- ubuntu-24.04-arm
steps:
- uses: actions/checkout@v4
- run: make compile
- uses: actions/upload-artifact@v4
with:
name: bin
path: bin/
# release_deb:
# runs-on: ubuntu-latest
# needs: compile
# steps:
# - uses: actions/download-artifact@v4
# with:
# name: bin
# path: ${{github.workspace}}/bin
# - if: ${{!env.ACT}}
# uses: softprops/action-gh-release@v2
# with:
# tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
# files: bin/coinflip
# fail_on_unmatched_files: true
# env:
# GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
# - if: ${{env.ACT}}
# uses: actions/upload-artifact@v4
# with:
# path: bin/coinflip
# release_aur-bin:
# runs-on: ubuntu-latest
# needs: compile
# steps:
# - uses: actions/download-artifact@v4
# with:
# name: bin
# path: ${{github.workspace}}/bin
# - if: ${{!env.ACT}}
# uses: softprops/action-gh-release@v2
# with:
# tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
# files: bin/coinflip
# fail_on_unmatched_files: true
# env:
# GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
# - if: ${{env.ACT}}
# uses: actions/upload-artifact@v4
# with:
# path: bin/coinflip
release_bin:
runs-on: ubuntu-latest
needs: compile
steps:
- if: ${{!env.ACT}}
uses: actions/download-artifact@v4
with:
name: bin
path: ${{github.workspace}}/bin
- run: echo "FNAME=coinflip-$(uname -s)-$(uname -m)" >>"$GITHUB_ENV"
- run: mv bin/coinflip "$FNAME"
- uses: softprops/action-gh-release@v2
with:
tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
files: bin/coinflip
files: ${{env.FNAME}}
fail_on_unmatched_files: true
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- if: ${{env.ACT}}
uses: actions/upload-artifact@v4
with:
path: bin/coinflip
path: ${{env.FNAME}}

View File

@@ -1,14 +0,0 @@
name: validate
on:
pull_request:
branches:
- '**'
push:
branches:
- '**'
jobs:
compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make compile

View File

@@ -1,6 +1,6 @@
CC := x86_64-linux-gnu-gcc
CFLAGS := -Wall -Wextra -Wpedantic -O2 -std=gnu17
LDFLAGS :=
CC ?= cc
CFLAGS += -Wall -Wextra -Wpedantic -O3 -MD -MP -std=gnu99 -DNDEBUG
LDFLAGS +=
SRC = $(shell find src/ -name '*.c')
OBJ := $(patsubst src/%,obj/%,$(SRC:.c=.o))
@@ -18,7 +18,7 @@ $(BIN): $(OBJ)
# compile .o files (w/ .d files)
obj/%.o: src/%.c
@mkdir -p $(@D)
$(CC) -c -MD -MP $(CFLAGS) -o $@ $<
$(CC) -c $(CFLAGS) -o $@ $<
ifneq ($(shell which bear),)
compile_commands.json: makefile

View File

@@ -1,60 +1,91 @@
#include <features.h>
#if _POSIX_C_SOURCE >= 199309L
# include <bits/time.h>
# include <errno.h>
# include <limits.h>
# include <stdarg.h>
# include <stdint.h>
# include <stdio.h>
# include <stdlib.h>
# include <stdnoreturn.h>
# include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/random.h>
#include <time.h>
noreturn static inline void error(char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(1);
#define error(ret, s, ...) \
do { \
fprintf(stderr, (s)__VA_OPT__(, __VA_ARGS__)); \
exit(ret); \
} while (0)
#define WIDTHOF(x) (sizeof(x) * 8)
typedef unsigned long long ull;
typedef struct dynrdat {
size_t cap;
ull dat[];
} dynrdat;
dynrdat* rdat = NULL;
static inline ull pow2_ceil(ull x) {
x -= !!x; // if x=0, remains 0; else x -= 1
return 1ULL << (WIDTHOF(ull) - __builtin_clzll(x | 1));
}
static void quit(void) {
free(rdat);
}
int main(int argc, char** argv) {
// set the seed to the number of seconds xor with nanoseconds
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srand((uint32_t)(ts.tv_nsec ^ ts.tv_sec)); // combine seconds and nanoseconds
}
// return the result if no input
if (argc <= 1) return printf("%s\n", (clock() & 1) ? "heads" : "tails");
atexit(quit);
// just print whether it's heads or tails
if (argc == 1) {
printf("%s\n", (rand() & 1) ? "heads" : "tails");
return 0;
}
// if we have input; perform coin flips for those times
for (uint32_t i = 1; i < (unsigned)argc; ++i) {
int n = 0; // contains the random data
uint64_t headsc = 0; // amount of heads
uint64_t tailsc = 0; // amount of tails
// get the integer from the string, return 1 if an error occurred
// loop through arguments
for (unsigned i = 1; i < (unsigned)argc; ++i) {
// parse the argument
errno = 0;
long const c = strtol(argv[i], NULL, 10);
if (errno == EINVAL) error("syntax error for string: %s\n", argv[i]);
if (c < 1 || c > (LONG_MAX - 1)) error("count may only be in between 1 and %l. Received %l\n", LONG_MAX, c);
ull c = strtoull(argv[i], NULL, 10);
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)
int mod = c % WIDTHOF(ull); // get the remainder of the available random bits
c = c / WIDTHOF(ull); // compute our "word count"
// dynamically scale the array to our needs, ensuring 2^n scaling
size_t cap = pow2_ceil(c + !!mod);
if (!rdat || rdat->cap < cap) {
void* ptr = realloc(rdat, sizeof(dynrdat) + sizeof(ull) * cap);
if (!ptr) error(1, "insufficient memory\n", );
rdat = ptr;
rdat->cap = cap;
}
// populate the array with random data
if (getrandom(rdat->dat, (c + !!mod) * sizeof(ull), 0) < 0)
error(1, "failed to acquire random data!\n", );
// perform for the input count
for (long j = 0; j < c; ++j) {
if (n == 0) n = rand(); // get a new random number if the random data ran out
(void)((n & 1) ? headsc++ : tailsc++); // increment the correct counter
n >>= 1; // shift the random integer right by 1
ull headsc = 0; // amount of heads
ull tailsc = 0; // amount of tails
// loop through how many full words there are available
ull* n = rdat->dat; // initialize the pointer to the start of our random data
for (; n < (rdat->dat + c); n++) {
int cnt = __builtin_popcountll(*n); // counts the set bits
headsc += cnt;
tailsc += WIDTHOF(ull) - cnt;
}
// if there is a remainder, use the last N to get this
if (mod) {
ull msk = (1 << mod) - 1; // get a mask with the set word size
int cnt = __builtin_popcountll(*n & msk);
headsc += cnt;
tailsc += mod - cnt;
}
// print the results of this cycle
printf("results:\n heads: %lu\n tails: %lu\n", headsc, tailsc);
printf("results:\n heads: %llu\n tails: %llu\n", headsc, tailsc);
}
return 0;
}
#else
# error "platform unsupported"
#endif