Skip to content

Instantly share code, notes, and snippets.

View elricmann's full-sized avatar

Elric Neumann elricmann

View GitHub Profile
@elricmann
elricmann / vector_dot_product.cc
Created March 10, 2025 08:39
Vectorized dot product with std::experimental SIMD
// Copyright (c) 2025 Elric Neumann. All rights reserved. MIT license.
// Compile with: g++ -std=c++23 -O3 -march=native -lstdc++exp -o vector_dot_product vector_dot_product.cc
// Width is 4 elements for SSE 4.2 (128-bit registers).
//
// Expected output:
//
// simd width: 4 elements
// simd dot product result: 1.65952e+20
// scalar dot product result: 1.70002e+20
// simd time: 8 ms
#include <chrono>
#include <iostream>
#include <vector>
struct aligned_struct {
double a;
int b;
char c;
};
@elricmann
elricmann / regalloc.c
Created December 6, 2024 15:15
Basic register allocator in C
// Copyright (c) 2024 Elric Neumann. All rights reserved. MIT license.
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_NODES 1000
#define MAX_COLORS 32
#define MAX_NEIGHBORS 100
@elricmann
elricmann / masked.c
Created December 1, 2024 22:24
Pointer compression in C
// Copyright (c) 2024 Elric Neumann. All rights reserved. MIT license.
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
struct compressed_ptr {
int32_t offset;
uintptr_t base;
};
@elricmann
elricmann / lanczos_resample.c
Created November 27, 2024 21:16
Lanczos resampling of pixel maps
// Copyright (c) 2024 Elric Neumann. All rights reserved. MIT license.
// Compile and run with: clang -O3 -o main lanczos_resample.c -lm && ./main
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
@elricmann
elricmann / tracing-gc.rs
Created November 21, 2024 17:17
Basic Tracing GC implementation in Rust
// Copyright (c) 2024 Elric Neumann. All rights reserved. MIT license.
#![allow(unused)]
use std::rc::Rc;
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
pub trait GcObject {
fn trace(&self, tracer: &TracingGC);
}
@elricmann
elricmann / arch-abi-conventions.md
Created November 5, 2024 23:59
Cross-architecture ABI convention for system calls

Every ABI convention requires register usage that is in line with said convention, the C compiler you're using may force ad-hoc register allocation so it's important to suffix with __volatile__ when intending to escape user space.

arch instr syscall no reg arg reg sysret reg clobbered regs
x86-64 (Linux) syscall rax rdi, rsi, rdx, r10, r8, r9 rax rax, rcx, r11
x86 (Linux) int 0x80 eax ebx, ecx, edx, esi, edi, ebp eax eax, ebx, ecx, edx
ARM aarch32 svc 0 r7 r0, r1, r2, r3, r4, r5, r6 r0 r0-r3, r7
ARM aarch64 `s
@elricmann
elricmann / de-casteljau-cubic-bezier.glsl
Created October 28, 2024 15:50
Fragment shader for non-recursive De Casteljau's algorithm & cubic Bézier spline
// Copyright (c) 2024 Elric Neumann. All rights reserved. MIT license.
// Title: non-recursive De Casteljau's algorithm & cubic Bézier spline
#ifdef GL_ES
precision mediump float;
#endif
uniform float u_time;
uniform vec2 u_resolution;
@elricmann
elricmann / noise.glsl
Created October 27, 2024 18:59
Fragment shader for random noise layers, FBM procedure & amplitude pertubation
// Copyright (c) 2024 Elric Neumann. All rights reserved. MIT license.
// Title: random noise layers with fractal brownian motion & amplitude pertubation
#ifdef GL_ES
precision mediump float;
#endif
uniform float u_time;
uniform vec2 u_resolution;
@elricmann
elricmann / fresnel-ripple.glsl
Created October 26, 2024 22:12
Fragment shader for multi-source ripple with Fresnel effect and point reflection
// Copyright (c) 2024 Elric Neumann. All rights reserved. MIT license.
// Title: multi-source ripple with fresnel effect and point reflection
#ifdef GL_ES
precision mediump float;
#endif
uniform float u_time;
uniform vec2 u_resolution;