Skip to content

Instantly share code, notes, and snippets.

View hamsham's full-sized avatar

Miles Lacey hamsham

View GitHub Profile
@hamsham
hamsham / bn_strassen.cpp
Last active February 16, 2023 09:30
Bignum multiplication of numbers with arbitrary bases, using the Schönhage-Strassen algorithm.
/**
* Bignum multiplication of numbers with arbitrary bases, using the
* Schonhage-Strassen algorithm.
*
* g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors bn_strassen.cpp -o bn_strassen
*
* usage: bn_strassen.exe 123456789 987654321
*
* Sources:
@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 / 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 / endian.cpp
Last active March 1, 2021 00:31
A simple compile-time endianness checking system for programs written in C++11.
/*
* A simple compile-time endian test
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors endian.cpp -o endian
*
* This can be used with specialized template functions, classes, and class
* methods in order better tailor code and reduce reliance on runtime
* checking systems.
*/
#include <cstdint>
@hamsham
hamsham / cuda_raymarcher.cu
Last active December 23, 2019 15:19
A basic ray marcher using CUDA
/*
* A small raymarcher using CUDA
*
* This program renders a dynamically-generated image to a PPM file.
*
* nvcc cuda_raymarcher.cu -arch=sm_20 -o cuda_raymarcher
*/
#include <assert.h>
@hamsham
hamsham / sorts.c
Last active November 22, 2018 02:14
Implementations of various sorting methods in ANSI C.
/**
* Testing implementations of different sorting methods.
*
* gcc -ansi -Wall -Werror -Wextra -static-libgcc -pedantic-errors sorts.c -o sorts
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
@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()