Skip to content

Instantly share code, notes, and snippets.

View mtwilliams's full-sized avatar
👺
small engines & big data

Michael Williams mtwilliams

👺
small engines & big data
View GitHub Profile
<table id="t312001195970" cellspacing="0" cellpadding="0"><style style="display: none;">table#t312001195970,#t312001195970 tbody,#t312001195970 tr,#t312001195970 td{margin:0;padding:0;}#t312001195970 td{width:1px; height:1px;border:none;}</style><tr><td style="background-color: rgb(161, 93, 75);"></td><td style="background-color: rgb(171, 97, 77);"></td><td style="background-color: rgb(185, 108, 80);"></td><td style="background-color: rgb(203, 120, 90);"></td><td style="background-color: rgb(215, 127, 99);"></td><td style="background-color: rgb(213, 128, 98);"></td><td style="background-color: rgb(213, 128, 99);"></td><td style="background-color: rgb(221, 130, 100);"></td><td style="background-color: rgb(236, 138, 107);"></td><td style="background-color: rgb(250, 145, 114);"></td><td style="background-color: rgb(251, 147, 114);"></td><td style="background-color: rgb(252, 147, 115);"></td><td style="background-color: rgb(247, 141, 111);"></td><td style="background-color: rgb(238, 133, 101);"></td><td style="ba
@mtwilliams
mtwilliams / compile-time-hash.cpp
Created January 17, 2021 09:07
Compile-time hashing of string literals with xxHash32.
#define XXHASH32_PRIME_1 0x9e3779b1ul
#define XXHASH32_PRIME_2 0x85ebca77ul
#define XXHASH32_PRIME_3 0xc2b2ae3dul
#define XXHASH32_PRIME_4 0x27d4eb2ful
#define XXHASH32_PRIME_5 0x165667b1ul
struct _compile_time_hasher {
static constexpr uint32_t hash(const char *const data, size_t len, uint32_t seed = 0) {
return avalanche(
  • Global manifest that is always in memory.
    • Stores indices and metadata.
    • Read in at start.
      • Or embedded into executable as R/W.
        • Further trickery can be used to embedded resources as well.
          • Maps well to Windows + Mac + iOS + Android.
    • Mutated at runtime.
      • There are no dynamic allocations for management.
    • Resources have sequential identifiers.
      • Assigned through an "append only" database.
#: Bits: Count @ Position High? Low? = Entry
00: 00000000: 00 @ 00 0 0 = 0x000
01: 00000001: 01 @ 00 0 1 = 0x101
02: 00000010: 01 @ 01 0 0 = 0x011
03: 00000011: 02 @ 00 0 1 = 0x102
04: 00000100: 01 @ 02 0 0 = 0x021
05: 00000101: 01 @ 00 0 1 = 0x101
06: 00000110: 02 @ 01 0 0 = 0x012
07: 00000111: 03 @ 00 0 1 = 0x103
08: 00001000: 01 @ 03 0 0 = 0x031
// Brute-force call using world-space bounding spheres.
for (buc_uint32_t frustum = 0; frustum < n_frusta; ++frustum) {
__m128 p_x0x1x2x3; __m128 p_y0y1y2y3; __m128 p_z0z1z2z3; __m128 p_d0d1d2d3;
__m128 p_x4x5x4x5; __m128 p_y4y5y4y5; __m128 p_z4z5z4z5; __m128 p_d4d5d4d5;
// Transpose frustum planes so we can test against them in parallel.
{
const __m128 x0y0z0d0 = _mm_load_ps((const float *)&frusta[frustum].planes[0]);
const __m128 x1y1z1d1 = _mm_load_ps((const float *)&frusta[frustum].planes[1]);
const __m128 x2y2z2d2 = _mm_load_ps((const float *)&frusta[frustum].planes[2]);
#include <cstdint>
#include <cstdio>
#define LABEL(Label) \
(((uint64_t)(#Label"\0" )[0] << 0)\
|((uint64_t)(#Label"\0\0" )[1] << 8)\
|((uint64_t)(#Label"\0\0\0" )[2] << 16)\
|((uint64_t)(#Label"\0\0\0\0" )[3] << 24)\
|((uint64_t)(#Label"\0\0\0\0\0" )[4] << 32)\
|((uint64_t)(#Label"\0\0\0\0\0\0" )[5] << 40)\
A vertex buffer conceptually encodes a chunk of voxel data
which has some finite x/y extent, but fully covers z (where
is height).
Ignorning chunks which are at same x/y of the chunk viewer
is in, it's possible to skip two of the four n/s/e/w faces
in the chunk. (I.e. if the chunk is to the NW of viewer,
the N and W faces of the chunk cannot be visible.)
If we sort the faces in the index buffer to separate each
@mtwilliams
mtwilliams / pretty-colors.c
Last active September 1, 2017 12:14
Generates a table of pretty colors.
//
// Generates a table of evenly distributed and colors at fixed saturation and
// value.
//
// https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
//
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
@mtwilliams
mtwilliams / pearson-hashing.c
Last active August 15, 2017 05:15
Generates a table for Pearson hashing, using Fisher-Yates.
//
// Generates a table for Pearson hashing.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
// Uniformly distributed random value between [min, max].