Skip to content

Instantly share code, notes, and snippets.

View clausecker's full-sized avatar

Robert Clausecker clausecker

View GitHub Profile

UNIX famously uses fork+exec to create processes, a simple API that is nevertheless quite tricky to use correctly and that comes with a bunch of problems. The alternative, spawn, as used by VMS, Windows NT and recently POSIX, fixes many of these issues but it overly complex and makes it hard to add new features.

prepare() is a proposed API to simplify process creation. When calling prepare(), the current thread enters “preparation state.” That means, a nascent process is created and the current thread is moved to the context of this process, but without changing memory maps (this is similar to how vfork() works). Inside the nascent process, you can configure the environment as desired and then call prep_execve() to execute a new program. On success, prep_execve() leaves preparation state, moving the current thread back to the parent's process context and returns (!) the pid of the now grownup child. You can also use prep_exit() to abort the child without executing a new process, it similarly returns the pid

@clausecker
clausecker / biastest.c
Last active November 27, 2024 16:12
Bias test for arc4random_uniform()
/*
* biastest.c -- bias test for arc4ranom_uniform()
* written by Robert Clausecker <[email protected]>
*/
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdatomic.h>
@clausecker
clausecker / rdffrtest.c
Created October 19, 2023 21:56
Test the behaviour of rdffr with extending loads
/* test the behaviour of rdffr with extending loads */
#include <assert.h>
#include <arm_sve.h>
#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
@clausecker
clausecker / caesar.s
Created October 8, 2020 18:01
Caesar cipher in x86 ASM with MMX
# MMX caeasar chiffre implementation
# for i686 with MMX
# signature:
# caesar(out, in, len, key)
# key is between 0 and 25
.section .text
.globl caesar
.type caesar,@function
.align 16
@clausecker
clausecker / getnumericvalue.S
Created September 2, 2020 14:19
summing decimal digits with different approaches
// getnumericvalue(ptr)
.section .text
.type getnumericvalue, @function
.globl getnumericvalue
getnumericvalue:
xor %eax, %eax // digit counter
// process string until we reach cache-line alignment
test $64-1, %dil // is ptr aligned to 64 byte?
jz 0f
@clausecker
clausecker / count8asm15.s
Created August 18, 2020 23:51
ARM64 8-bit position population count prototype
// b:a = a+b+c, v31.16b used for scratch space
.macro csa, a, b, c
eor v31.16b, \a\().16b, \b\().16b
eor \a\().16b, v31.16b, \c\().16b
bit \b\().16b, \c\().16b, v31.16b
.endm
// d:a = a+b+c
.macro csac a, b, c, d
eor \d\().16b, \a\().16b, \b\().16b
@clausecker
clausecker / mem.s
Created August 4, 2020 18:18
Vectorised positional popcount for Go
#include "textflag.h"
// func PospopcntMem(counts *[8]int32, buf []byte)
TEXT ·PospopcntMem(SB),NOSPLIT,$0-32
MOVQ counts+0(FP), DI
MOVQ buf_base+8(FP), SI // SI = &buf[0]
MOVQ buf_len+16(FP), CX // CX = len(buf)
SUBQ $32, CX // pre-subtract 32 bit from CX
JL scalar
@clausecker
clausecker / harness.c
Last active August 4, 2020 16:00
8 bit positional popcount with AVX2 without too much code
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
extern void pospopcnt_reg(int accum[8], const char *buf, size_t len);
extern void pospopcnt_mem(int accum[8], const char *buf, size_t len);
extern void
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
extern int
main(int argc, char *argv[])
{
ssize_t count;
int flags;
@clausecker
clausecker / wc.c
Created October 17, 2019 14:41
parallel wc example
/* parallel wc(1) demo program */
/* cc -O3 -fopenmp -o wc wc.c */
#include <ctype.h>
#include <unistd.h>
#include <omp.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>