Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
e18e9b0297
|
|||
|
65d91b2d00
|
|||
|
2fd839ae12
|
|||
|
fa19fc2789
|
|||
|
b31071bb91
|
|||
|
598834255f
|
|||
|
5f6f1bf720
|
|||
|
1e9ae43e52
|
|||
|
95dce84d28
|
|||
|
deabe97247
|
|||
|
3bbfea7400
|
|||
|
|
a37f47d9f6 | ||
|
|
42264b6402 | ||
|
|
6189bdcf92 | ||
|
|
10d1aab05f | ||
|
|
ef1dcf7c39 | ||
|
|
40c48980b4 | ||
|
|
28e8aeb707 | ||
|
|
4607afed9a | ||
|
|
6ef3d4a660 | ||
|
|
40aa1b59e6 | ||
|
|
50964df129 | ||
|
|
8c8386740b | ||
|
|
6acaff36f7 | ||
|
|
1da9e4243e | ||
|
|
c8dce9dc0e |
34
.github/workflows/publish.yaml
vendored
Normal file
34
.github/workflows/publish.yaml
vendored
Normal 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
11
.gitignore
vendored
@@ -1,3 +1,8 @@
|
|||||||
.vscode
|
.*
|
||||||
build
|
|
||||||
package/usr
|
!.github/
|
||||||
|
!.gitignore
|
||||||
|
|
||||||
|
/bin/
|
||||||
|
/obj/
|
||||||
|
/compile_commands.json
|
||||||
|
|||||||
32
makefile
Normal file
32
makefile
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
CC ?= cc
|
||||||
|
CFLAGS += -Wall -Wextra -Wpedantic -O3 -MD -MP -std=gnu99
|
||||||
|
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)
|
||||||
@@ -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"
|
|
||||||
92
src/main.c
92
src/main.c
@@ -1,18 +1,90 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/random.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
int main(void) {
|
#define error(ret, s, ...) \
|
||||||
srand(time(NULL)); // set the random seed to the system time
|
do { \
|
||||||
char* result = NULL;
|
fprintf(stderr, (s)__VA_OPT__(, __VA_ARGS__)); \
|
||||||
|
exit(ret); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
if (rand() & (~1) != 0) {
|
typedef unsigned long long ull;
|
||||||
result = "heads";
|
|
||||||
}
|
typedef struct dynrdat {
|
||||||
else {
|
size_t cap;
|
||||||
result = "tails";
|
ull dat[];
|
||||||
|
} dynrdat;
|
||||||
|
|
||||||
|
static inline ull pow2_ceil(ull x) {
|
||||||
|
x -= !!x; // if x=0, remains 0; else x -= 1
|
||||||
|
int lz = __builtin_clzll(x | 1); // get leading zeroes
|
||||||
|
return (~0ULL >> lz) + 1; // bit-shift the maximum value by this amount of leading zeroes
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
// return the result if no input
|
||||||
|
if (argc <= 1) return printf("%s\n", (clock() & 1) ? "heads" : "tails");
|
||||||
|
|
||||||
|
dynrdat* rdat = NULL;
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
ull mod = c % (sizeof(ull) * 8); // get the remainder of the available random bits
|
||||||
|
c = c / (sizeof(ull) * 8); // 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) {
|
||||||
|
free(rdat);
|
||||||
|
error(1, "insufficient memory\n", );
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", result);
|
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 += sizeof(ull) * 8 - 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(rdat);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user