Skip to content

Instantly share code, notes, and snippets.

View ziap's full-sized avatar

Zap ziap

View GitHub Profile
@ziap
ziap / pcg32.zig
Created February 23, 2025 10:49
PCG32 RSH-RR with vectorized 64-bit generation
const Pcg32 = struct {
state: u64,
fn next_u32(self: *Pcg32) u32 {
const s = self.state;
self.state = s *% 0x5851f42d4c957f2d +% 0x14057b7ef767814f;
const xorshifted: u32 = @truncate((s ^ (s >> 18)) >> 27);
const rot: u5 = @intCast(s >> 59);
return (xorshifted >> rot) | (xorshifted << -% rot);
@ziap
ziap / zxhash.c
Last active February 23, 2025 07:24
A custom fast, non-cryptographic hash function
#include <stdint.h>
static inline uint64_t multiply_mix(uint64_t x, uint64_t y) {
__uint128_t m = (__uint128_t)x * (__uint128_t)y;
uint64_t hi = m >> 64;
uint64_t lo = m;
return lo ^ hi;
}
@ziap
ziap / example.zig
Last active February 27, 2025 14:20
Algorithm for initializing large RNGs from strings and other low-entropy sources
const std = @import("std");
const seeder = @import("seeder.zig");
const Xoshiro256 = struct {
s: [4]u64,
pub fn next(self: *Xoshiro256) u64 {
const S = struct {
inline fn rotl(data: u64, rot: u6) u64 {
import asyncio
from base64 import urlsafe_b64encode
from hashlib import sha384
import os
import aiofiles
from aiohttp.client import ClientSession
# Fake user agent to trick the google font api to give us the .woff2 fonts
# Retrive one by typing `navigator.userAgent` in the browser's console
@ziap
ziap / inverse-index.cpp
Last active October 17, 2024 07:34
Compile time inverse indexing in C++
#include <cstdint>
#include <string_view>
#include <memory>
constexpr uint32_t fnv_1a(std::string_view sv) {
uint32_t hash = 0x811c9dc5;
for (char c : sv) {
hash ^= (uint8_t)c;
hash *= 0x01000193;
}
@ziap
ziap / readline.c
Created October 14, 2024 07:59
Read a line from stdin into a malloc'd buffer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *read_line_malloc(void) {
size_t sz = 64;
char *buf = malloc(sz);
if (!fgets(buf, sz, stdin)) {
*buf = '\0';
@ziap
ziap / fixed-bigint.cpp
Created October 7, 2024 08:39
Fixed-width big integer in C++
#include <cstdint>
#include <cstring>
#include <charconv>
#include <iostream>
template<const int N>
struct BigInt {
static constexpr size_t digit_count = 9;
static constexpr uint32_t base = 1000000000;
uint32_t limbs[N];
@ziap
ziap / avl.c
Last active August 7, 2024 06:03
AVL tree implementation in C
#include <stdlib.h>
#include <stdint.h>
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
typedef enum {
SIDE_LEFT = 0,
SIDE_RIGHT,
SIDE_COUNT,
} AVL_Side;
@ziap
ziap / function.cpp
Last active May 5, 2024 07:54
Fat pointer based polymorphic function wrapper, `std::function` alternative
template<class>
class Function {};
template<class R, class ...Args>
class Function<R(Args...)> {
private:
template<class F>
static R type_erase(const void *fn, Args... args) {
return (*(const F*)fn)(args...);
}
@ziap
ziap / arena.h
Last active June 19, 2024 11:17
Zap's personal approach to arena allocation
/*
arena.h - Zap's personal approach to arena allocation
USAGE
This is an STB-style single-header library.
#define ARENA_IMPLEMENTATION // (in *one* C file)
#include "arena.h"
#define ARENA_ALLOC and ARENA_DEALLOC to avoid using malloc/free and create a
custom backend for the allocator.