Skip to content

Instantly share code, notes, and snippets.

@cairnc
cairnc / sat_full_simd.cpp
Created July 11, 2026 01:05
Brute force SAT optimized with SSE
// SoA and SSE separating axis test between two convex hulls. Face phases go through a SIMD
// getSupport. For the edge phase, B's verts and face normals are transformed into A's space once
// up front from the hull's stored SoA arrays so the inner loop does no transforms. Data is packed
// one array per component and the loop tests one B edge against four A edges at a time. Edge
// counts are bounded so every buffer is a fixed size stack array.
#include "sat.h"
#include <immintrin.h>
// a*b + c, fused when FMA is available.
static inline __m128 mad(__m128 a, __m128 b, __m128 c)
@cairnc
cairnc / portal.cpp
Created July 25, 2025 03:25
SIMD portal intersection
// A float which remains large when you modify its least significant bits
const Vec4f VEC4_LARGE_FLOAT = Vec4f::fromF32(8589934592);
const Vec4f VEC4_INDEX_MASK = Vec4f::fromI32(~1, ~1, 0, 0);
const Vec4f VEC4_INDEX_VALS = Vec4f::fromI32(0, 1, 0x50000000, 0x50000000);
const Vec4f VEC4_ZERO = Vec4f::fromF32(0.0f);
constexpr int BIG_FLOAT_INTEGER = 0x50000000;
template <size_t n, size_t i, size_t indexBits = 3>
@cairnc
cairnc / sat_blog.cpp
Last active July 19, 2025 13:21
sat_blog
void satCollideReference(const SatShape *a, Transform xfA, const SatShape *b, Transform xfB, SatResult *res)
{
ASSERT(b->numFaces != 2);
Transform bToA = xfA.inverse().mul(xfB);
Transform aToB = bToA.inverse();
res->vert = -1;
res->face = -1;
res->support = INFINITY;
// Test MD against planes of a