Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
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"
|
|
||||||
62
src/main.c
62
src/main.c
@@ -1,18 +1,60 @@
|
|||||||
|
#include <bits/time.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/types.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_ARGS__); \
|
||||||
|
exit(ret); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
if (rand() & (~1) != 0) {
|
typedef unsigned long long ull;
|
||||||
result = "heads";
|
|
||||||
}
|
|
||||||
else {
|
int main(int argc, char** argv) {
|
||||||
result = "tails";
|
// get the current time to set as the seed
|
||||||
|
struct timespec ts;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
|
srandom(ts.tv_nsec);
|
||||||
|
|
||||||
|
// return the result if no input
|
||||||
|
if (argc <= 1)
|
||||||
|
return printf("%s\n", (rand() & 1) ? "heads" : "tails");
|
||||||
|
|
||||||
|
// 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]);
|
||||||
|
|
||||||
|
// perform for the input count
|
||||||
|
ull headsc = 0; // amount of heads
|
||||||
|
ull tailsc = 0; // amount of tails
|
||||||
|
|
||||||
|
while (c >= 31) {
|
||||||
|
c -= 31;
|
||||||
|
long n = random(); // generate a random number for this batch
|
||||||
|
int cnt = __builtin_popcountl(n); // counts the set bits
|
||||||
|
headsc += cnt;
|
||||||
|
tailsc += 31 - cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", result);
|
if (c > 0) {
|
||||||
|
long n = random();
|
||||||
|
long msk = (1 << c) - 1;
|
||||||
|
int cnt = __builtin_popcountl(n & msk);
|
||||||
|
headsc += cnt;
|
||||||
|
tailsc += c - cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// print the results of this cycle
|
||||||
|
printf("results:\n heads: %llu\n tails: %llu\n", headsc, tailsc);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user