Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
b31071bb91
|
|||
|
598834255f
|
|||
|
5f6f1bf720
|
|||
|
1e9ae43e52
|
|||
|
95dce84d28
|
|||
|
deabe97247
|
|||
|
3bbfea7400
|
68
.github/workflows/publish.yaml
vendored
68
.github/workflows/publish.yaml
vendored
@@ -9,72 +9,26 @@ on:
|
|||||||
description: the tag to release for
|
description: the tag to release for
|
||||||
default: ""
|
default: ""
|
||||||
jobs:
|
jobs:
|
||||||
compile:
|
release_bin:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ${{matrix.os}}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os:
|
||||||
|
- ubuntu-24.04
|
||||||
|
- ubuntu-24.04-arm
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- run: make compile
|
- run: make compile
|
||||||
- uses: actions/upload-artifact@v4
|
- run: echo "FNAME=coinflip-$(uname -s)-$(uname -m)" >>"$GITHUB_ENV"
|
||||||
with:
|
- run: mv bin/coinflip "$FNAME"
|
||||||
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
|
|
||||||
- uses: softprops/action-gh-release@v2
|
- uses: softprops/action-gh-release@v2
|
||||||
with:
|
with:
|
||||||
tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
|
tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
|
||||||
files: bin/coinflip
|
files: ${{env.FNAME}}
|
||||||
fail_on_unmatched_files: true
|
fail_on_unmatched_files: true
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||||
- if: ${{env.ACT}}
|
- if: ${{env.ACT}}
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
path: bin/coinflip
|
path: ${{env.FNAME}}
|
||||||
|
|||||||
14
.github/workflows/validate.yaml
vendored
14
.github/workflows/validate.yaml
vendored
@@ -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
|
|
||||||
8
makefile
8
makefile
@@ -1,6 +1,6 @@
|
|||||||
CC := x86_64-linux-gnu-gcc
|
CC ?= cc
|
||||||
CFLAGS := -Wall -Wextra -Wpedantic -O2 -std=gnu17
|
CFLAGS += -Wall -Wextra -Wpedantic -O3 -MD -MP -std=gnu99
|
||||||
LDFLAGS :=
|
LDFLAGS +=
|
||||||
|
|
||||||
SRC = $(shell find src/ -name '*.c')
|
SRC = $(shell find src/ -name '*.c')
|
||||||
OBJ := $(patsubst src/%,obj/%,$(SRC:.c=.o))
|
OBJ := $(patsubst src/%,obj/%,$(SRC:.c=.o))
|
||||||
@@ -18,7 +18,7 @@ $(BIN): $(OBJ)
|
|||||||
# compile .o files (w/ .d files)
|
# compile .o files (w/ .d files)
|
||||||
obj/%.o: src/%.c
|
obj/%.o: src/%.c
|
||||||
@mkdir -p $(@D)
|
@mkdir -p $(@D)
|
||||||
$(CC) -c -MD -MP $(CFLAGS) -o $@ $<
|
$(CC) -c $(CFLAGS) -o $@ $<
|
||||||
|
|
||||||
ifneq ($(shell which bear),)
|
ifneq ($(shell which bear),)
|
||||||
compile_commands.json: makefile
|
compile_commands.json: makefile
|
||||||
|
|||||||
121
src/main.c
121
src/main.c
@@ -1,60 +1,89 @@
|
|||||||
#include <features.h>
|
#include <assert.h>
|
||||||
#if _POSIX_C_SOURCE >= 199309L
|
#include <errno.h>
|
||||||
# include <bits/time.h>
|
#include <limits.h>
|
||||||
# include <errno.h>
|
#include <stdint.h>
|
||||||
# include <limits.h>
|
#include <stdio.h>
|
||||||
# include <stdarg.h>
|
#include <stdlib.h>
|
||||||
# include <stdint.h>
|
#include <sys/random.h>
|
||||||
# include <stdio.h>
|
#include <time.h>
|
||||||
# include <stdlib.h>
|
|
||||||
# include <stdnoreturn.h>
|
|
||||||
# include <time.h>
|
|
||||||
|
|
||||||
noreturn static inline void error(char* fmt, ...) {
|
#define error(ret, s, ...) \
|
||||||
va_list args;
|
do { \
|
||||||
va_start(args, fmt);
|
fprintf(stderr, (s)__VA_OPT__(, __VA_ARGS__)); \
|
||||||
vfprintf(stderr, fmt, args);
|
exit(ret); \
|
||||||
va_end(args);
|
} while (0)
|
||||||
exit(1);
|
|
||||||
|
typedef unsigned long long ull;
|
||||||
|
|
||||||
|
typedef struct dynrdat {
|
||||||
|
size_t cap;
|
||||||
|
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) {
|
int main(int argc, char** argv) {
|
||||||
// set the seed to the number of seconds xor with nanoseconds
|
// return the result if no input
|
||||||
{
|
if (argc <= 1) return printf("%s\n", (rand() & 1) ? "heads" : "tails");
|
||||||
struct timespec ts;
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
||||||
srand((uint32_t)(ts.tv_nsec ^ ts.tv_sec)); // combine seconds and nanoseconds
|
|
||||||
}
|
|
||||||
|
|
||||||
// just print whether it's heads or tails
|
dynrdat* rdat = NULL;
|
||||||
if (argc == 1) {
|
|
||||||
printf("%s\n", (rand() & 1) ? "heads" : "tails");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we have input; perform coin flips for those times
|
// loop through arguments
|
||||||
for (uint32_t i = 1; i < (unsigned)argc; ++i) {
|
for (unsigned i = 1; i < (unsigned)argc; ++i) {
|
||||||
int n = 0; // contains the random data
|
// parse the argument
|
||||||
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
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
long const c = strtol(argv[i], NULL, 10);
|
ull c = strtoull(argv[i], NULL, 10);
|
||||||
if (errno == EINVAL) error("syntax error for string: %s\n", argv[i]);
|
if (errno != 0) error(errno, "parse 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);
|
|
||||||
|
// 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", );
|
||||||
|
}
|
||||||
|
|
||||||
|
rdat = ptr;
|
||||||
|
rdat->cap = cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
// populate the array with random data
|
||||||
|
getrandom(rdat->dat, c + !!mod, 0);
|
||||||
|
|
||||||
// perform for the input count
|
// perform for the input count
|
||||||
for (long j = 0; j < c; ++j) {
|
ull headsc = 0; // amount of heads
|
||||||
if (n == 0) n = rand(); // get a new random number if the random data ran out
|
ull tailsc = 0; // amount of tails
|
||||||
(void)((n & 1) ? headsc++ : tailsc++); // increment the correct counter
|
|
||||||
n >>= 1; // shift the random integer right by 1
|
// 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 += c - cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
// print the results of this cycle
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(rdat);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
# error "platform unsupported"
|
|
||||||
#endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user