Skip to content

Instantly share code, notes, and snippets.

View lossless_image_compression_filter.c
https://encode.su/threads/3015-Oodle-Lossless-Image-compression
https://aras-p.info/blog/2023/02/01/Float-Compression-3-Filters/
https://github.com/veluca93/fpnge/blob/926df95bce7bd1affaa0163572ac6f0ae692eb95/fpnge.cc#L619
https://www.investopedia.com/terms/a/autocorrelation.asp
https://ruby0x1.github.io/machinery_blog_archive/post/data-structures-part-2-indices/index.html
@vurtun
vurtun / aoc.c
Last active December 8, 2023 09:34
AoC 2023
View aoc.c
#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
View obj_filter.c
#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
View perm.c
#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
View day_2.c
/**
* 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
View diag.c
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
View sqrt.c
/*
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
View nn.c
#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>
View graph.c
// 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
View sort.c
// 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;
};