Skip to content

Instantly share code, notes, and snippets.

View hamsham's full-sized avatar

Miles Lacey hamsham

View GitHub Profile
@hamsham
hamsham / LruCache.hpp
Created January 12, 2022 05:46
Calculation of an LRU index (from 0-8) using bitwise instructions based on Hacker's Delight.
#ifdef _MSC_VER
#include <intrin.h> // _BitScanForward
#endif
#include <cassert>
#include <cstdint>
@hamsham
hamsham / build_clang.sh
Last active January 6, 2022 07:49
Build script to check-out and compile LLVM/Clang
#!/usr/bin/env bash
set -euo pipefail
# This script follows the instructions for building Clang, from the LLVM
# documentation here: https://llvm.org/docs/GettingStarted.html#requirements
CLANG_RELEASE_LATEST=13.0.0
CLANG_GIT_REPO=https://github.com/llvm/llvm-project.git
@hamsham
hamsham / build_gcc.sh
Last active May 10, 2022 05:23
A simple script to check-out and cross-compile GCC.
#!/usr/bin/env bash
set -euo pipefail
# This script follows the instructions for building GCC, from the GNU
# documentation here: https://gcc.gnu.org/install/index.html
# x86_64: x86_64-pc-linux-gnu
# ARM: arm-none-eabi-linux
# Aarch64: aarch64-linux-gnu
# Vocore2: mipsel-openwrt-linux-uclibc
@hamsham
hamsham / work_thread.cpp
Created March 31, 2018 00:37
Simple worker thread/task queue test.
/*
* Simple worker thread/task queue test.
*
* g++ -std=c++11 -Wall Werror -Wextra -pedantic-errors work_thread.cpp -lpthread -o work_thread
*/
#include <atomic>
#include <cassert>
#include <chrono> // std::seconds
@hamsham
hamsham / line_drawing.cpp
Last active January 6, 2018 23:24
Benchmark of different drawing algorithms. EFLA 5 wins.
/*
* g++ --std=c++11 -O3 -DNUM_TEST_RUNS=50000 -pthread -Wall -Werror -Wextra -pedantic -pedantic-errors line_drawing.cpp -o line_drawing
*/
#include <cstdint> // uint8_t
#include <fstream> // std::ofstream
#include <iostream> // std::cout, std::cerr
#include <limits> // std::numeric_limits<>
#include <memory> // std::unique_ptr
@hamsham
hamsham / parse_args.c
Created August 5, 2017 02:48
Simple example of how to parse arguments from a single string, with quote-handling.
/*
* Test for parsing command-line arguments from a single string
*
* gcc -std=c11 -Wall -Werror -Wextra -pedantic -pedantic-errors parse_args.c -o parse_args
*/
#include <stdio.h> // printf(), fprintf()
#include <string.h> // strlen()
#include <stdbool.h> // bool, true, false
#include <ctype.h> // isspace()
@hamsham
hamsham / .emacs
Last active May 16, 2017 00:05
My Emacs Config
;; Added by Package.el. This must come before configurations of
;; installed packages. Don't delete this line. If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
(package-initialize)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
@hamsham
hamsham / align_nums.c
Created May 10, 2016 06:08
Get the next multiple of an unsigned integer to assist with memory allocations and data alignment.
/**
* Simple program to find the next multiple of a number (AMD, NVIDIA, & Intel share 16-byte alignments for cache lines).
*
* gcc -std=c11 -Wall -Wextra -Werror -pedantic-errors align_nums.c -o align_nums
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
@hamsham
hamsham / glsl_regex.cpp
Created February 4, 2016 06:04
Test to retrieve GLSL outputs from a fragment shader.
/**
* Testing a parser to grab the outputs of a fragment shader
*
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors glsl_regex.cpp -o glsl_regex
*/
#include <iostream>
#include <string>
#include <vector>
@hamsham
hamsham / camera_clip.cpp
Last active February 3, 2016 08:56
FOV-based camera clipping in 3D
bool is_point_in_fov(const ls::draw::Camera& cam, math::vec3 point, const float fovReduction = 1.f) {
// Get the local camera's absolute position and direction in world-space
const math::vec3&& eyePos = cam.get_abs_position();
const math::vec3&& eyeDir = math::normalize(cam.get_eye_direction());
// translate the input point using a model-view matrix (no projection
// matrix should be used).
const math::mat4&& mvMat = modelMatrix * cam.get_view_matrix();
const math::vec4&& temp = math::vec4{-point[0], -point[1],-point[2], 0.f} * mvMat;