Skip to content

Instantly share code, notes, and snippets.

View miyaokamarina's full-sized avatar
😶

Marina Miyaoka miyaokamarina

😶
View GitHub Profile
@miyaokamarina
miyaokamarina / README.md
Last active November 30, 2023 10:39
Random 32-bit magic number generator

Random 32-bit magic number generator

A magic number for a binary file format should satisfy the following requirements:

  • invalid in UTF-8,

    Must contain either invalid bytes or invalid sequences.

  • is not ascending,
  • has no equal bytes,
  • has no bytes that are common in binary data, > 00, 01, 80, FF
@miyaokamarina
miyaokamarina / tu01.c
Last active October 23, 2023 11:04
Running TestU01 BigCrush in parallel
// The worker binary.
// Compile as
// gcc -Wall -O3 -o ./OUT_DIR/tu01 ./tu01.c
// -Iinclude -Llib -ltestu01 -lprobdist -lmylib -lm
#include <TestU01.h>
// Should provide:
// • void reset(); -- Init/reset the state.
@miyaokamarina
miyaokamarina / inv.js
Last active July 11, 2023 21:51
Modular multiplicative inverse (mod 2^w) in JavaScript
/**
* Modular multiplicative inverse (mod 2^w).
*
* Find x, s.t.
*
* ax ≡ 1 (mod 2^w)
*
* Based on Hurchalla, J. (2022). “An Improved Integer Modular Multiplicative
* Inverse (modulo 2^w)”. https://arxiv.org/abs/2204.04342
*
@miyaokamarina
miyaokamarina / toyhash.js
Created July 11, 2023 21:11
Illustrative examples of hash functions and their inverses for 4, 8, and 16-bit inputs.
// @ts-check
// SPDX-License-Identifier: CC0-1.0
/**
* 4-bit hash.
*
* Single LCG step + xorshift.
*
* @param {number} x
*/
@miyaokamarina
miyaokamarina / desktop-entry-parsers.md
Last active February 19, 2023 12:41
Desktop Entry Parsers Comparison

Desktop Entry Parsers

KConfig

  • Only recognizes \n newlines.
  • Accepts [ \t\n\r] as whitespace:
    • At the start of line.
    • At the end of line.
    • Before =.
  • After =.
@miyaokamarina
miyaokamarina / grapheme-cluster-length.js
Created June 19, 2021 15:49
String length in Unicode grapheme clusters.
const segmenter = new Intl.Segmenter('en-us', {
granularity: 'grapheme',
});
const length = x => {
x = x.normalize('NFC');
const graphemes = segmenter.segment(x);
return Array.from(graphemes).length;
@miyaokamarina
miyaokamarina / chmod.pegjs
Last active June 8, 2021 22:39
Sorta kinda UNIX chmod command permissions notation parser.
{
const MASK = 0;
const COPY = 1;
const ADD = 0;
const SUB = 1;
const SET = 2;
const Symbolic = (s, o, v) => {
s ??= { class: 0o7, umasked: true };
@miyaokamarina
miyaokamarina / README.md
Created June 3, 2021 19:14
(Probably) The fastest Mersenne Twister for JavaScript/TypeScript

Yet Another Mersenne Twister

  • Hella fast.
  • Compact af.
  • Built with typed arrays.
  • Provides an API to save/restore the state.
  • Written in TypeScript.
import { MersenneTwister } from './mersenne-twister.ts';

Largest number less than one

IEEE 754 binary64

0 01111111110 1111111111111111111111111111111111111111111111111111 === 1 - 2**(-53)
@miyaokamarina
miyaokamarina / typescript-type-level-spread.ts
Last active June 1, 2021 11:57
Infers the type of object spread operation; like { ...a, ...b }, but for types.
declare type Refine<from, to> = from extends to ? from : never;
declare type ArraySize<t> = Refine<GetProperty<t, 'length'>, number>;
declare type GetProperty<t, k> = t[Refine<k, keyof t>];
declare type ArrayType<t> = t extends ReadonlyArray<infer u> ? u : never;
declare type ArrayHead<a> = a extends readonly [infer h] ? h : never;
declare type Rebuild<t> = { [k in keyof t]: t[k] };
declare type KeyofReq<o, k extends keyof o = keyof o> = k extends any ? (undefined extends o[k] ? never : k) : never;
// Binary spread
declare type ObjectSpread<