Skip to content

Instantly share code, notes, and snippets.

@vurtun
vurtun / math.c
Last active September 29, 2024 22:25
pure C vector math
/* 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)
#define mapI(r,fn,a,I) (r)[I]=fn((a)[I])
#define map2I(r,fn,a,b,I) (r)[I]=fn((a)[I], (b)[I])
#define map3I(r,fn,a,b,c,I) (r)[I]=fn((a)[I], (b)[I], (c)[I])
@vurtun
vurtun / rdp.c
Last active September 24, 2024 12:32
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 April 15, 2024 16:41
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
@vurtun
vurtun / aoc.c
Last active December 8, 2023 09:34
AoC 2023
#include <stdio.h>
#include <limits.h>
#define byte4_dup(c) (((c)<<24u)|((c)<<16u)|((c)<<8u)|(c))
#define byte4_cmp_lt(x,n) (((x)-~0UL/255u*(n))&~(x)&~0UL/255u*128u) // x>=0; 0<=n<=128
#define byte4_cmp_gt(x,n) (((x)+~0UL/255*(127-(n))|(x))&~0UL/255*128) // x>=0; 0<=n<=127
#ifdef _MSC_VER
static int bit_ffs32(unsigned int u) {_BitScanForward(&u, u); return (int)(u);}
static int bit_fls32(unsigned int u) {_BitScanBackward(&u, u); return 31-(int)(u);}
@vurtun
vurtun / obj_filter.c
Last active November 8, 2023 10:57
object string filter using sse2
#ifdef _MSC_VER
#define alignto(x) __declspec(align(x))
#define bit_cnt(u) __popcnt(u)
#define bit_cnt64(u) __popcnt64(u)
static int bit_ffs32(unsigned int u) {_BitScanForward(&u, u); return casti(u);}
static int bit_ffs64(unsigned long long u) {_BitScanForward64(&u, u); return casti(u);}
#else /* GCC, CLANG */
#define alignto(x) __attribute__((aligned(x)))
#define bit_cnt(u) __builtin_popcount(u)
#define bit_cnt64(u) __builtin_popcountll(u)
@vurtun
vurtun / perm.c
Last active October 13, 2023 08:58
Array shuffle
#define xglue(x, y) x##y
#define glue(x, y) xglue(x, y)
#define uniqid(name) glue(name, __LINE__)
#ifdef _MSC_VER
#define swap(a,b) do { decltype((a) + 0) _t = (a); (a) = (b); (b) = _t; } while(0)
#else
#define swap(a,b) do {__typeof__((a) + 0) _t = (a); (a) = (b); (b) = _t; } while(0)
#endif
@vurtun
vurtun / day_2.c
Last active July 14, 2023 21:47
Advent of Code 2022
/**
* Advent of Code 2022
* Day 2: Rock Paper Scissors
* https://adventofcode.com/2022/day/2
*
* http://codercorner.com/Modulo3.htm
* (Index+1)%3 = (1 << Index1) & 3
*/
#include <stdlib.h>
#include <stdio.h>
@vurtun
vurtun / diag.c
Last active June 15, 2023 21:31
static void
qdiag(float *restrict qres, const float *restrict A) {
/* Diagonalizer: http://melax.github.io/diag.html
'A' must be a symmetric matrix.
Returns quaternion q such that corresponding matrix Q
can be used to Diagonalize 'A'
Diagonal matrix D = Q * A * Transpose(Q); and A = QT*D*Q
The rows of q are the eigenvectors D's diagonal is the eigenvalues
As per 'row' convention if float3x3 Q = q.getmatrix(); then v*Q = q*v*conj(q)
*/