Skip to content

Instantly share code, notes, and snippets.

@vurtun
vurtun / str_split.c
Last active April 18, 2021 19:34
string split (line splitting)
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))
@vurtun
vurtun / cloth.c
Last active April 18, 2021 19:33
// 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 February 16, 2021 19:54
basic math function approximations
#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;
@vurtun
vurtun / gif.c
Last active December 25, 2020 21:04
#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;

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

@vurtun
vurtun / lay.c
Last active December 15, 2021 22:37
// 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;
@vurtun
vurtun / ui.c
Last active July 26, 2021 19:55
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}
@vurtun
vurtun / prng.c
Last active March 24, 2020 19:50
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)
{
@vurtun
vurtun / utf.c
Last active December 26, 2023 11:58
#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)
{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
static int
str_fzy(const char *str, const char *ptn)
{
const char *pat = ptn;