Skip to content

Instantly share code, notes, and snippets.

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <arm_neon.h>
#define cast(t,p) ((t)p)
#define casts(p) cast(short,p)
#define castus(p) cast(unsigned short,p)
#define casti(p) cast(int,p)
@vurtun
vurtun / chain.c
Last active April 28, 2025 17:45
chain simulation
#include <immintrin.h>
#include <pthread.h>
#include <assert.h>
#include <string.h>
enum {
MAX_CHAINS = 256,
PARTICLES_PER_CHAIN = 16,
SPHERES_PER_CHAIN = 8,
MAX_PARTICLES = (MAX_CHAINS * PARTICLES_PER_CHAIN),
@vurtun
vurtun / pdf.c
Last active May 8, 2025 11:55
Probability Density Function (PDF) Tree data structure
The PdfTree is a hierarchical data structure that stores values and their associated probabilities, organized in a way that resembles a binary tree or mip-map pyramid. It allows for efficient random selection of values proportional to their probabilities (e.g., for Monte Carlo sampling in rendering or procedural generation). The tree is built as a "summed probability tree," where each level aggregates probabilities from the level below it, enabling fast traversal for sampling.
The tree is laid out linearly in probabilities, with each level concatenated. For example:
Mip 0: Original probabilities [p0, p1, p2, p3]
Mip 1: Pairwise sums [p0+p1, p2+p3]
Mip 2: Sum of mip 1 [p0+p1+p2+p3]
Mip Level Calculation:
Computes the total number of elements across all mip levels.
@vurtun
vurtun / anim_blend.c
Last active April 29, 2025 05:51
animation sampling
#include <immintrin.h> // AVX, includes SSE2-SSE4.2
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
// Constants
#define MAX_MDLS 256
@vurtun
vurtun / bit_set.c
Last active February 5, 2025 09:50
#include <immintrin.h> // For AVX2
#define MAX_SIZE (64*1024)
#define BITS_PER_WORD 64
#define WORD_CNT ((MAX_SIZE + BITS_PER_WORD - 1) / BITS_PER_WORD) // 1024 words
#define BIT_MAP_CNT ((WORD_CNT + BITS_PER_WORD - 1) / BITS_PER_WORD) // 16 words
#define WORD_SHIFT 6 // log2(BITS_PER_WORD)
#define WORD_MSK (BITS_PER_WORD - 1) // 63
#define CHUNK_CNT (BIT_MAP_CNT/4)
#define CACHE_LINE_SIZE 64
@vurtun
vurtun / math.c
Last active November 21, 2024 10:37
pure C vector math
/* ---------------------------------------------------------------------------
* Vector
* ---------------------------------------------------------------------------
*/
/* vector */
#define opI(r,e,a,p,b,i,s,I)((r)[I]e(a)[I]p((b)[I]i(s)))
#define opsI(r,e,a,p,s,I) ((r)[I]e(a)[I]p(s))
#define setI(d,x,I) (d)[I]=(x)
#define dotI(a,b,I) (a)[I]*(b)[I]
#define lerpI(r,a,b,t,I) lerp((r)[I],(a)[I],(b)[I],t)
@vurtun
vurtun / rdp.c
Last active October 15, 2024 07:03
Ramer-Douglas-Peucker line simplification
#include <assert.h>
#include <math.h>
#include <stdio.h>
/* Alternative design: Iterate over path and at each point i check distance to line with points
i + 1 and i + 2 and skip i + 1 when distance is small than epsilon. */
static float
line_dist(const float *p, const float *p1, const float *p2) {
float dx = p2[0] - p1[0];
float dy = p2[1] - p1[1];
@vurtun
vurtun / kth.c
Last active May 23, 2024 18:34
print K largest(or smallest) elements in an array
/* Problem: Directory file view ui. Files can be sorted by different properties (name, size, ...). Ui itself only needs elements
that are currently in the visible section of the list view from x and count k. So I want to use a fixed size buffer with up to k elements
and walk through all files in the directory and only have the final elements in the end in the fixed size buffer.
Temp buffers are fine for me as long as they have a fixed at compile time size. For those familiar with SQL basically this is
a SORT BY LIMIT begin, count.
Idea: Use Floyd–Rivest algorithm with average O(n) to find the element at index x. Walk over list again and skip all elements smaller than x
then use heap to sort for the k smallest elements afterwards. So we have for Floyd–Rivest and heap combined O(n*log(k))
Problem: Find x is not directly possible since not all elements are in memory because we have only fixed size buffer.
@vurtun
vurtun / fold.c
Last active October 13, 2024 16:57
unicode case folding
// Selection algorithm
// Partial sorting
// http://www.unicode.org/Public/UCD/latest/ucd/CaseFolding.txt
switch(rune) {
case 0x0041: return 0x0061; // LATIN CAPITAL LETTER A
case 0x0042: return 0x0062; // LATIN CAPITAL LETTER B
case 0x0043: return 0x0063; // LATIN CAPITAL LETTER C
case 0x0044: return 0x0064; // LATIN CAPITAL LETTER D
case 0x0045: return 0x0065; // LATIN CAPITAL LETTER E
@vurtun
vurtun / burg.c
Last active March 13, 2024 08:22
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define szof(a) ((int)sizeof(a))
#define cntof(a) ((int)(sizeof(a) / sizeof((a)[0])))
static void