Skip to content

Instantly share code, notes, and snippets.

struct quat
quat_from_two_vectors(vec3 u, vec3 v)
{
float m = sqrt(2.f + 2.f * dot(u, v));
vec3 w = (1.f / m) * cross(u, v);
return quat(0.5f * m, w.x, w.y, w.z);
}
@vurtun
vurtun / api.md
Last active August 6, 2020 06:40
API Design: Modular Data-oriented processes

API Design: Modular Data-oriented processes (June-2017)

This is the second entry in my series about API design. While I wrote about granularity and request based APIs in my last post I want to write about API design and code architecture for modular data-oriented process this time. This is basically the write up of my findings while writing and finishing the core of my current GUI research quarks.

Actually sparking my interest for writing up my findings however was rasmusbarr@github (rasmusbarr@twitter) releasing his small data-oriented and SIMD-optimized 3D rigid body physics library. I noticed a lot of overlap in design between his physics and my GUI library and wanted to write up some general thoughts about what seems

#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#define tag(x) ((ptrdiff_t)&Tag##x)
#define is(x, T) ((x)->tag == tag(T))
#define as(x, T) ((struct T *)variant_cast((void *)(x), tag(T)))
static inline void*
#include <stdio.h>
#include <string.h>
enum jstates {JFAILED, JNXT, JSEP, JUP, JDWN, JQUP, JQDWN, JESC, JUNESC, JBARE, JUNBARE, JUTF8_2,
JUTF8_3, JUTF8_4, JUTF8_N, JSTATE_CNT};
enum jtype {JEOS,JERR,JTSEP,JOBJB,JOBJE,JARRB,JARRE,JNUM,JSTR,JTRUE,JFALSE,JNULL};
struct jtok {int type, len; const char *str;};
struct jctx {const unsigned char *tbl; const char *cur, *end;};
static const unsigned char jtbl[256] = {
/* ===========================================================================
*
* LIBRARY
*
* =========================================================================== */
/* Proof of Concept GUI:
* - PoC UI implementation in ~2.5kLOC of C89 (ANSI C)
* => Core solutions has no external dependencies (removing standard library dependency is trival)
* => Does not use or require any special language constructs or macro constructs just pointers and enums
* - Combines both "retained" and "immediate mode" UI by providing control over update frequency
@vurtun
vurtun / compr.c
Last active April 17, 2018 23:28
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
@vurtun
vurtun / defl.c
Last active June 13, 2023 21:30
Full deflate/inflate implementation in ~300 LoC
/* ===============================================================
* SDEFL
* ===============================================================
* public domain - no warranty implied; use at your own risk
* References:
https://bitbucket.org/rmitton/tigr/src/be3832bee7fb2f274fe5823e38f8ec7fa94e0ce9/src/tigr_inflate.c?at=default&fileviewer=file-view-default
https://github.com/github/putty/blob/49fb598b0e78d09d6a2a42679ee0649df482090e/sshzlib.c
https://www.ietf.org/rfc/rfc1951.txt
*/
#include <stdlib.h>
@vurtun
vurtun / bit_trick.c
Last active December 25, 2019 10:55
static char*
str_chr(char * str, int chr)
{
char *s = str;
int c = chr & 0xFF;
unsigned m = (c << 24)|(c << 16)|(c << 8)|c;
for (;;) {
while (((uintptr_t)s) & 3) {
chk1: if (s[0] == c) return s;
chk2: if (s[0] == 0) return s;
/* Iterative average/Iterative centroid computation from:
https://blog.demofox.org/2016/08/23/incremental-averaging/ by Alan Wolfe and
http://realtimecollisiondetection.net/blog/?p=48 by Christer Ericson
Example:
int n = 0;
float avg = 0;
avg = avg_add(avg, n++, rand());
avg = avg_add(avg, n++, rand());
*/
static float