Skip to content

Instantly share code, notes, and snippets.

@vurtun
vurtun / obj_filter.c
Last active March 3, 2025 08:53
object string filter using sse2, avx2, neon
/* This algorithm uses as a predicate equality of the first and the last characters from the substring. These two characters are populated in two registers
* F and L respectively. Then in each iteration two chunks of strings are loaded. The first chunk (A) is read from offset i (where i is the current offset)
* and the second chunk (B) is read from offset i + k - 1, where k is sub string's length. Then we compute a vector expression F == A and B == L.
* This step yields a byte vector (or a bit mask), where "true" values denote position of potential substring occurrences.
* Finally, just at these positions an exact comparisons of sub strings are performed.
*
* Example: Let's assume 8-byte registers. We're searching for word "cat", thus:
*
* F = [ c | c | c | c | c | c | c | c ]
* L = [ t | t | t | t | t | t | t | t ]
@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 October 15, 2024 07:05
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)
*/
@vurtun
vurtun / sqrt.c
Last active December 1, 2022 09:08
/*
Enable FMA support:
clang -O2 -Wall -mfma
*/
static inline float
rsqrta(float x) {
/* Exact bits: 13.71 */
/* δ+max = 7.459289×10^−5 */
/* δ−max = −7.450387×10^−5 */
union { float f; int i; } v = {x};
@vurtun
vurtun / nn.c
Last active November 1, 2023 16:13
Neural Network
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <stdarg.h>
#include <math.h>
@vurtun
vurtun / bsearch.c
Last active October 30, 2024 10:28
static inline int
bin_search(const void *vals, int cnt, int siz, void *val,
int(*cmp_less)(const void *a, const void *b)) {
int half, nleft = cnt;
const unsigned char *base = vals;
while ((half = (nleft >> 1)) > 0) {
const unsigned char *mid = base + half * siz;
base = cmp_less(mid, val) ? mid : base;
nleft -= half;
}
// Symbols:
// Dropdowns rendering: [|----------------[v]|]
// Right Click -> New State Machine -> Name: "blnd_tree_loco_walk"
static struct anim_pose*
blnd_tree_loco_walk(struct anim_ctx *ctx, struct anim_sm *sm) {
struct anim_pose *res = 0;
{
// Right Click -> New Node -> Animation -> Walk Left
struct anim *walk_l = [|----------------[v]|];
@vurtun
vurtun / sort.c
Last active January 11, 2023 15:22
// ref: http://www.codercorner.com/RadixSortRevisited.htm
// http://stereopsis.com/radix.html
// int/float: https://github.com/lshamis/FloatRadixSort
// string: https://github.com/rantala/string-sorting/blob/master/src/msd_ce.cpp
struct str {
const char *str;
const char *end;
int len;
};
#include <stdio.h>
#define xglue(x, y) x##y
#define glue(x, y) xglue(x, y)
#define uniqid(name) glue(name, __LINE__)
#define scp_brk(name) goto xglue(___SCOPE___,name)
#define scp_file(name,f,p,fmt)\
xglue(___SCOPE___,name): for(int uniqid(_i_) = (f) ? 1 : ((((f) = fopen(p,fmt)) == 0) ? 1 : 0);\
(uniqid(_i_) == 0) ? 1 : ((f) ? (fclose(f), 0): 0); ++uniqid(_i_))