Skip to content

Instantly share code, notes, and snippets.

@kevinmoran
kevinmoran / cubicSinf.h
Created October 9, 2019 15:49
Cubic approximation of sinf(), if you only need something sinusoid-ish
// Source: https://www.youtube.com/watch?v=1xlCVBIF_ig
float cubicSinf(float x)
{
float t = x * 0.15915f;
t = t - (int)t;
return 20.785f * t * (t - 0.5f) * (t - 1.f);
}
@kevinmoran
kevinmoran / SinApprox.h
Last active October 9, 2019 15:50
Sin Approximation
// From: https://web.archive.org/web/20080228213915/http://www.devmaster.net/forums/showthread.php?t=5784
float sinApprox(float x)
{
const float kPI32 = 3.14159265359f;
const float B = 4/kPI32;
const float C = -4/(kPI32*kPI32);
float y = B * x + C * x * abs(x);
@kevinmoran
kevinmoran / Murmur3.cpp
Created May 27, 2020 09:26
Simple MurmurHash3 Implementation (from Demetri Spanos)
// Minimal Murmur3 implementation shared by Demetri Spanos on Handmade Network Discord
//
// Code is deliberately terse and simplistic
// Intended to be the first hash function you reach for e.g. a simple hash table
// *** NB THIS IS NOT A CRYPTOGRAPHIC HASH ***
//
// @demetrispanos:
// "yes let me reiterate the moral of this story
// there is never any reason to use a dumb made up hash function
// use murmur3 or jenkins-one-at-a-time for a 0-effort version
@kevinmoran
kevinmoran / EntityProperties.cpp
Created January 21, 2021 23:11
Stretchy bitflags - Nifty code snippet from Ryan Fleury shared on Handmade Network discord
enum EntityProperty
{
EntityProperty_IsActive,
EntityProperty_RenderModel,
EntityProperty_Hostile,
EntityProperty_OnFire,
EntityProperty_SomethingElse,
EntityProperty_Count
};
#include <windows.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <assert.h>
#define _USE_MATH_DEFINES
#include <math.h> // for sin()
#include <stdint.h>
@kevinmoran
kevinmoran / rand_float.cpp
Created March 31, 2021 15:49
Converting an integer to a float between 0 and 1 with bithacks (for PRNG)
// Source: "xoshiro / xoroshiro generators and the PRNG shootout"
// https://prng.di.unimi.it/
#include <stdint.h>
static inline double u64ToDoubleBetween01(uint64_t x) {
return (x >> 11) * 0x1.0p-53;
}
// I came up with the following function for 32-bit floats based on the above, let me know if it's wrong
@kevinmoran
kevinmoran / Programming Wisdom.md
Last active November 2, 2022 18:47
List of useful programming resources I've found
@kevinmoran
kevinmoran / BitTwiddling.h
Created February 1, 2023 12:31
Bit Twiddling Tricks From Game Engine Gems 2
// https://twitter.com/EricLengyel/status/1620683606216835073
int clearLowestSetBit(int x)
{
return x & (x-1);
}
int setLowestUnsetBit(int x)
{
return x | (x+1);