Skip to content

Instantly share code, notes, and snippets.

View snowclipsed's full-sized avatar
๐Ÿ—ก๏ธ

snow snowclipsed

๐Ÿ—ก๏ธ
View GitHub Profile
@snowclipsed
snowclipsed / fast_tokenizer.zig
Created January 31, 2025 21:03
Fast Tokenizer in Zig
const std = @import("std");
const mem = std.mem;
const json = std.json;
const Allocator = mem.Allocator;
const Thread = std.Thread;
// Constants
pub const MIN_PARALLEL_TEXT_SIZE = 10_000;
pub const DEFAULT_CHUNK_SIZE = 1024 * 64;
pub const MAX_THREADS = 8;
const std = @import("std");
const builtin = @import("builtin");
const time = std.time;
const atomic = std.atomic;
const CACHE_LINE_SIZE: usize = atomic.cache_line;
const CHUNK_SIZE: usize = 1;
const NEON_ALIGNMENT = 16;
// For ARM NEON, we use 4-wide vectors as that's the native SIMD width
const Vec4f = @Vector(4, f32);
@snowclipsed
snowclipsed / matmul.zig
Last active February 26, 2025 23:41
Highly Performant Matrix Multiplication in Zig (and Grid Search to find the most optimal parameters!)
// To run tests, do :
// zig test -OReleaseSafe -Dcpu=native -Dtarget=native matmul.zig
// To run main, do :
// zig build-exe -OReleaseFast -Dcpu=native -Dtarget=native matmul.zig
// ./matmul
//
// โ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•— โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—
// โ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•—โ•šโ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘ โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ•šโ•โ•โ–ˆโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•
@snowclipsed
snowclipsed / CyberpunkLorenz.tsx
Last active January 12, 2025 20:59
3D Lorenz System Simulation rendered using ASCII/UTF-8 in React
import React, { useState, useEffect, useCallback, useRef } from 'react';
/**
* Represents a point in 3D space within the Lorenz system.
*/
interface Point {
x: number;
y: number;
z: number;
}
@snowclipsed
snowclipsed / CyberpunkPerlin.tsx
Created December 31, 2024 22:20
Perlin Noise and Terrain Generation using ASCII/UTF-8
import React, { useState, useEffect, useCallback, useRef } from 'react';
type ColoredChar = {
char: string;
color: string;
};
const defaultConfig = {
scale: 0.05,
@snowclipsed
snowclipsed / resize.zig
Created November 20, 2024 16:24
Resize Kernel using Bicubic Interpolation
const std = @import("std");
const c = @cImport({
@cInclude("stb_image.h");
});
fn cubic(x: f32) f32 {
const a = -0.5;
const abs_x = @abs(x);
if (abs_x <= 1.0) {
return (a + 2.0) * abs_x * abs_x * abs_x - (a + 3.0) * abs_x * abs_x + 1.0;
@snowclipsed
snowclipsed / matmul_FP32.zig
Last active January 15, 2025 19:00
Fast Matrix Multiplication in ZIG in FP32.
const std = @import("std");
// Can also try:
// 8 x 64
// 16 x 64
// Top GFLOPs/s on an Intelยฎ Coreโ„ข i7-13620H Processor = 300.9 GFLOPs/s
// Comments were added using Claude.
// To run simply run zig build-exe -O ReleaseFast matmul_FP32.zig, then run the binary ./matmul_FP32
// To test simply run zig test -O ReleaseFast matmul_FP32.zig
// To test performance on a generated binary, run : sudo perf stat -e cache-misses,cache-references,instructions,cycles ./matmul_FP32