31 Commits
v1.0 ... 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
Quinn
a37f47d9f6 fix: no such thing as $default-branch 2025-04-09 15:42:25 +02:00
Quinn
42264b6402 fix: compiler wasn't available 2025-04-09 15:35:53 +02:00
Quinn
6189bdcf92 fix: bear not available 2025-04-09 15:35:53 +02:00
Quinn
10d1aab05f add new gh workflows / most of it 2025-04-09 15:35:11 +02:00
Quinn
ef1dcf7c39 small fixes in the code, mainly to ensure things work properly. 2025-04-09 14:57:14 +02:00
Quinn
40c48980b4 use makefile build system 2025-04-09 14:56:24 +02:00
Quinn
28e8aeb707 fix: forgot newlime innlog message 2025-04-06 17:34:01 +02:00
Quinn
4607afed9a use 64 bit integers for count, rather than 32 bit.
Was facing issues with integer wrapping
2025-04-06 17:32:26 +02:00
Quinn
6ef3d4a660 add bounds to numbered input 2025-04-06 17:27:55 +02:00
Quinn
40aa1b59e6 add error management script 2025-04-06 17:23:22 +02:00
Quinn
50964df129 refactor and pedantic fixes 2025-04-06 17:22:56 +02:00
Quinn
8c8386740b Fix: no need for the if condition there
Instead check if no arguments were given and exit sooner.
2025-04-06 16:55:54 +02:00
Quinn
6acaff36f7 remove package directory 2025-04-06 15:55:47 +02:00
Quinn
1da9e4243e allow for parsing command line arguments for multiple coinflips at once 2025-04-06 15:55:33 +02:00
unset
c8dce9dc0e improved random seed to be less predictable 2025-01-30 16:09:23 +01:00
5 changed files with 159 additions and 21 deletions

34
.github/workflows/publish.yaml vendored Normal file
View File

@@ -0,0 +1,34 @@
name: publish
on:
release:
types: published
workflow_dispatch:
inputs:
release_tag:
required: true
description: the tag to release for
default: ""
jobs:
release_bin:
runs-on: ${{matrix.os}}
strategy:
matrix:
os:
- ubuntu-24.04
- ubuntu-24.04-arm
steps:
- uses: actions/checkout@v4
- run: make compile
- 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: ${{env.FNAME}}
fail_on_unmatched_files: true
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- if: ${{env.ACT}}
uses: actions/upload-artifact@v4
with:
path: ${{env.FNAME}}

11
.gitignore vendored
View File

@@ -1,3 +1,8 @@
.vscode
build
package/usr
.*
!.github/
!.gitignore
/bin/
/obj/
/compile_commands.json

32
makefile Normal file
View File

@@ -0,0 +1,32 @@
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))
BIN := bin/coinflip
compile: compile_commands.json $(BIN)
clean:
rm -rf obj/ bin/ compile_commands.json
# link the .o files
$(BIN): $(OBJ)
@mkdir -p $(@D)
$(CC) $(LDFLAGS) $(OBJ) -o $(BIN)
# compile .o files (w/ .d files)
obj/%.o: src/%.c
@mkdir -p $(@D)
$(CC) -c $(CFLAGS) -o $@ $<
ifneq ($(shell which bear),)
compile_commands.json: makefile
$(MAKE) clean
@touch compile_commands.json
bear -- make compile
else
compile_commands.json:
endif
-include $(OBJ:.o=.d)

View File

@@ -1,6 +0,0 @@
Package: coinflip
Version: 1.0
Architecture: all
Priority: optional
Maintainer: Quinn
Description: adds a coinflip command which has a 50% chance of printing "heads" and 50% chance of printing "tails"

View File

@@ -1,18 +1,91 @@
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/random.h>
#include <time.h>
int main(void) {
srand(time(NULL)); // set the random seed to the system time
char* result = NULL;
#define error(ret, s, ...) \
do { \
fprintf(stderr, (s)__VA_OPT__(, __VA_ARGS__)); \
exit(ret); \
} while (0)
if (rand() & (~1) != 0) {
result = "heads";
}
else {
result = "tails";
}
#define WIDTHOF(x) (sizeof(x) * 8)
printf("%s\n", result);
return 0;
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) {
// return the result if no input
if (argc <= 1) return printf("%s\n", (clock() & 1) ? "heads" : "tails");
atexit(quit);
// loop through arguments
for (unsigned i = 1; i < (unsigned)argc; ++i) {
// parse the argument
errno = 0;
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
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: %llu\n tails: %llu\n", headsc, tailsc);
}
return 0;
}