Skip to content

Instantly share code, notes, and snippets.

@vurtun
vurtun / str_split.c
Last active Apr 18, 2021
string split (line splitting)
View str_split.c
static int*
str_split(const char *in, int siz, char delim) {
int *eol = 0;
__m128i msk = _mm_set1_epi8(0x80);
__m128i sub = _mm_set1_epi8(0x01);
__m128i neg = _mm_set1_epi64(-1LL);
__m128i sep = _mm_set1_epi8(delim);
__m128i endmsk = _mm_set1_epi32(-1);
if (siz & 15) {
endmsk = _mm_slli_si128(endmsk, 16 - (siz & 15))
View cloth.c
// https://github.com/dianedelallee/cloth-simulation
/* ---------------------------------------------------------------------------
* Vector3
* ---------------------------------------------------------------------------
*/
#define op(r,e,a,p,b,i,s) ((r) e (a) p ((b) i (s)))
#define lerp(r,a,b,t) ((r)=(a)+(t)*((b)-(a)))
#define rad(x) ((x)*3.141592653f/180.0f)
#define op3(r,e,a,p,b,i,s)\
@vurtun
vurtun / math.c
Last active Feb 16, 2021
basic math function approximations
View math.c
#include <stdio.h>
#include <stdlib.h>
#define sgn(v) ((0 < (v)) - ((v) < 0))
#define abs(a) (((a) < 0) ? -(a) : (a))
static int
ilog2(int n) {
#ifdef _MSC_VER
unsigned long msbp = 0;
View gif.c
#ifndef JO_GIF_H
#define JO_GIF_H
#include <stdio.h>
typedef struct jo_gif_kd_node_s {
unsigned char pnt[3];
unsigned char lhs, rhs;
unsigned char idx;
} jo_gif_kd_node_t;
View API Design:Builder-APIs.md

API Design: Builder APIs (October-2020)

Some time has past (three years!) since I last wrote about API specifically about coroutines style APIs so I thought why not write another one about a different API type I encounter relatively often. The builder API.

Now first let me take a step back and put this into 20,000 feet view on where builder APIs are located in the grant scheme. In general everything in computing is separated into input, processing and finally output. In its most basic form I am currently typing on my keyboard. All pressed keys are processed from the OS up to the browser I am writing this in and finally rendered and displayed on the screen as output. Of course this example is very user centric

View hash.c
#include <emmintrin.h>
#include <wmmintrin.h>
// https://godotengine.org/article/x11-display-server-progress-report
// https://www.youtube.com/watch?v=kQfF-P70V2Q
// https://www.youtube.com/watch?v=Fwi1qgSaxZY
static const unsigned char hash_default_seed[16] = {
0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D,
0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34};
#define hash128eq(v0, v1) (_mm_movemask_epi8(_mm_cmpeq_epi8(v0, v1)) == 0xffff)
View lay.c
// gui_lay_row(ctx, 3, (int[]) { 30, -90, -1 }, 0);
/* Layout */
enum gui_lay_dir {
GUI_ROW,
GUI_COL,
};
struct gui_lay_sol {
int fix_siz;
int fix_cnt;
View ui.c
struct gui_bnd {int min, mid, max, ext;};
struct gui_box {
struct gui_bnd x;
struct gui_bnd y;
};
#define gui_box(x,y,w,h) (struct gui_box){{x,x+(w>>1),x+w,w},{y,y+(h>>1),y+h,h}}
#define gui_min_max(a,b) (struct gui_bnd){a,a+((b-a)>>1),b,b-a}
#define gui_min_ext(m,e) (struct gui_bnd){m,m+(e>>1),m+e,e}
#define gui_max_ext(m,e) (struct gui_bnd){m-e,m-(e>>1),m,e}
#define gui_mid_min(c,m) (struct gui_bnd){m,c,m+c-m,(m-c)<<1}
View prng.c
static unsigned
wang_hash(unsigned u)
{
u = ((u ^ 61) ^ (u >> 16)) * 9;
u = (u ^ (u >> 4)) * 0x27d4eb2d;
return u ^ (u >> 15);
}
static unsigned
rand_lcg(unsigned *rng)
{
View utf.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define UTF_INVALID 0xFFFD
static const char*
utf_dec(unsigned *dst, const char *p, const char *e)
{