Skip to content

Instantly share code, notes, and snippets.

@michaeljclark
michaeljclark / pythag.c
Created March 4, 2024 08:26
find largest 32-bit Pythagorean integer triple with a 64-bit square
#include <stdio.h>
/*
* cc -O2 pythag.c -o pythag
*
* find largest 32-bit Pythagorean integer triple with a 64-bit square
*/
typedef unsigned long long ulong;
@michaeljclark
michaeljclark / pattern-synth.py
Last active January 11, 2024 18:57
combinatorial bit pattern synthesizer in python
#!/usr/bin/env python3
from math import log
from random import randint
from functools import reduce
from itertools import product
from itertools import permutations
# Random bit utilities
@michaeljclark
michaeljclark / refcounting.md
Last active October 18, 2023 19:40
Work in progress draft on the design of a reference counting system.

References

Work in progress draft on the design of a reference counting system.

Primitive operations on reference counters

Summary of atomic operations required to implement reference counting. All reference counter operations take a pointer to a reference counter object. Reference to relaxed, acquire and release memory ordering on the primitive operations primarily refer to the barrier that protects the

@michaeljclark
michaeljclark / cpuident.c
Last active July 30, 2022 08:17
cmake configure time CPU feature detection for x86 processors
#include <stdio.h>
#include <stdlib.h>
#if defined __GNUC__ && (defined __i386__ || defined __x86_64__)
#define HAS_X86_CPUID 1
#include <cpuid.h>
static inline void __x86_cpuidex(int reg[], int level, int count)
{ __cpuid_count(level, count, reg[0], reg[1], reg[2], reg[3]); }
#elif defined _MSC_VER && (defined _M_IX86 || defined _M_X64)
#define HAS_X86_CPUID 1
@michaeljclark
michaeljclark / vf128.txt
Last active February 26, 2022 21:05
table of single-byte encodings for the vf128 variable-length floating-point number format
0 ▄▄▄▄ 0.0000 1 ▄▄▄▟ 0.0625 2 ▄▄▄▙ 0.1250 3 ▄▄▄█ 0.1875 4 ▄▄▟▄ 0.2500 5 ▄▄▟▟ 0.3125 6 ▄▄▟▙ 0.3750 7 ▄▄▟█ 0.4375
8 ▄▄▙▄ 0.5000 9 ▄▄▙▟ 0.5625 10 ▄▄▙▙ 0.6250 11 ▄▄▙█ 0.6875 12 ▄▄█▄ 0.7500 13 ▄▄█▟ 0.8125 14 ▄▄█▙ 0.8750 15 ▄▄██ 0.9375
16 ▄▟▄▄ 1.0000 17 ▄▟▄▟ 1.0625 18 ▄▟▄▙ 1.1250 19 ▄▟▄█ 1.1875 20 ▄▟▟▄ 1.2500 21 ▄▟▟▟ 1.3125 22 ▄▟▟▙ 1.3750 23 ▄▟▟█ 1.4375
24 ▄▟▙▄ 1.5000 25 ▄▟▙▟ 1.5625 26 ▄▟▙▙ 1.6250 27 ▄▟▙█ 1.6875 28 ▄▟█▄ 1.7500 29 ▄▟█▟ 1.8125 30 ▄▟█▙ 1.8750 31 ▄▟██ 1.9375
32 ▄▙▄▄ 2.0000 33 ▄▙▄▟ 2.1250 34 ▄▙▄▙ 2.2500 35 ▄▙▄█ 2.3750 36 ▄▙▟▄ 2.5000 37 ▄▙▟▟ 2.6250 38 ▄▙▟▙ 2.7500 39 ▄▙▟█ 2.8750
40 ▄▙▙▄ 3.0000 41 ▄▙▙▟ 3.1250 42 ▄▙▙▙ 3.2500 43 ▄▙▙█ 3.3750 44 ▄▙█▄ 3.5000 45 ▄▙█▟ 3.6250 46 ▄▙█
@michaeljclark
michaeljclark / bench_asn1.txt
Created February 25, 2022 23:41
benchmark results from crefl bench_asn1 benchmark for strtod, snprintf, vlu, leb, vf128, asn1 float and integer encoding
# crefl serialization benchmarks run on Intel Core i9-7980XE CPU
benchmark count time(s) op(ns) ops/s MiB/s
------------------------ ------- ------- ------- ------------- ---------
[ 0] f64-read-strtod-dec 1M 0.11 109.65 9,119,975 69.580
[ 1] f32-read-strtof-dec 1M 0.09 86.43 11,570,658 44.139
[ 2] f32-write-snprintf-dec 1M 0.14 143.62 6,962,688 26.561
[ 3] f64-write-snprintf-dec 1M 0.20 203.81 4,906,604 37.434
[ 4] f64-asn.1-read-byptr 1M 0.03 30.61 32,667,961 249.237
[ 5] f64-asn.1-read-byval 1M 0.03 28.96 34,532,112 263.459
@michaeljclark
michaeljclark / phydate128.py
Last active November 20, 2022 20:53
phydate128 — a 128-bit date format with 4-bit prefix and 124-bit counter
#!/usr/bin/env python3
# phydate128 — a 128-bit variable width datetime format
#
# struct { int<4> timebase; uint<4> length; bytes<length> counter; }
#
# prefix byte is composed of a 4-bit signed frequency base and 4-bit length
# frequency base is signed two's complement where 0b0000 is femtoseconds
# length indicates the size of the variable length counter field in bytes.
#
@michaeljclark
michaeljclark / llvlir.md
Last active January 23, 2022 21:15
Low Level Variable Length Intermediate Representation

Low Level Variable Length Intermediate Representation

Note: this an unofficial early draft of a work in progress specification. The LLVLIR specification has moved...

Introduction

Low Level Variable Length Intermediate Representation is a succinct target independent byte-code for deferred translation.

@michaeljclark
michaeljclark / ftload.cc
Created December 21, 2021 22:59
loads TrueType Bézier curves for codepoint using FreeType
/*
* ftload.cc loads TrueType Bézier curves for codepoint using FreeType
*
* Compile: c++ -std=c++17 -o ftload ftload.cc -Ithird_party/glm \
* $(pkg-config --cflags --libs freetype2)
*
* Usage: ./ftload <ttf-file> <dpi> <size> <code-point>
*
* Example: ./ftload DejaVuSans.ttf 72 32 99
*/
@michaeljclark
michaeljclark / maj2_random.glsl
Last active December 24, 2021 07:15
maj2_random is a simplified floating point hash function derived from SHA-2
/*
* maj2_random
*
* maj2_random is a simplified floating point hash function derived from SHA-2,
* retaining its high quality entropy compression function modified to permute
* entropy from a vec2 (designed for UV coordinates) returning float values
* between 0.0 and 1.0. since maj2_random is a hash function it will return
* coherent noise. vector argument can be truncated prior to increase grain.
*/