Skip to content

Instantly share code, notes, and snippets.

View bit-hack's full-sized avatar

Aidan Dodds bit-hack

View GitHub Profile
// based on:
// blog.demofox.org/2017/06/20/simd-gpu-friendly-branchless-binary-search
#include <cstdint>
#include <cstddef>
template <typename type_t, size_t size>
size_t binary_search(type_t val, const type_t (& data)[size]) {
size_t pos = 0;
size_t i = size >> 1;
@bit-hack
bit-hack / heap.cpp
Last active January 5, 2019 23:19
Generate all possible permutations of n objects.
// non-recursive version of algorythm presented here:
// http://ruslanledesma.com/2016/06/17/why-does-heap-work.html
#include <algorithm>
#include <array>
#include <stddef.h>
#include <stdio.h>
#include <vector>
template <typename type_t>
@bit-hack
bit-hack / heaparray.cpp
Created July 22, 2017 00:52
heap allocated array wrapper
#pragma once
#include <memory>
#include <cstddef>
template <typename type_t>
struct dynarray_t {
dynarray_t(size_t elements)
: _elements(elements)
, _array(std::move(std::make_unique<type_t[]>(elements)))
{
@bit-hack
bit-hack / CMakeLists.txt
Last active July 23, 2017 23:02
Nice Recursive CMake globbing
project(residualvm)
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# recursively glob files
#
file(GLOB_RECURSE _FILES "*.cpp" "*.h")
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# file exclusion step
#
@bit-hack
bit-hack / Win32Blit.cpp
Last active July 25, 2017 20:53
Direct Window Pixel Blitting
#include <cassert>
#include <string>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
typedef unsigned int uint;
struct GDIDetail {
@bit-hack
bit-hack / poll-winmm.cpp
Created July 27, 2017 00:14
polling winmm playback
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <Windows.h>
#define WAVE_RATE 44100
#define WAVE_SAMPLES 1024
#define WAVE_CHANNELS 2
@bit-hack
bit-hack / thread-winmm.cpp
Created July 27, 2017 09:41
Threaded winmm audio playback
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <Windows.h>
#define MMOK(EXP) ((EXP) == MMSYSERR_NOERROR)
struct WaveInfo {
@bit-hack
bit-hack / tagval.h
Created July 28, 2017 16:48
Example tagged type structure
#ifndef TAGVAL_H__
#define TAGVAL_T__
#include <typeinfo>
struct tagval_t {
tagval_t(int v): type_(typeid(int)), int_(v) {}
tagval_t(float v): type_(typeid(float)), float_(v) {}
tagval_t(const char *v): type_(typeid(const char *)), cstr_(v) {}
@bit-hack
bit-hack / sdl_framework.cpp
Last active August 1, 2017 11:33
Minimal SDL 1 app skeleton
struct Framework {
Framework()
: _screen(NULL)
{
}
bool init(uint32_t w, uint32_t h)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
@bit-hack
bit-hack / cparser.cpp
Last active September 21, 2017 09:25
ANSI C Recursive Decent Parser generated from BNF
// Auto Generated From ANSI C BNF using grammar adapted from Section A13 of The
// C programming language, 2nd edition, by Brian W. Kernighan and Dennis M.
// Ritchie, Prentice Hall, 1988.
struct state_t {};
bool lexer_found(const char *);
state_t state_save();
void state_restore(state_t&);
bool parse_integer_constant();
bool parse_character_constant();