Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

// A C++11 constexpr implementation of xxhash32
// Jonathan Adamczewski / https://twitter.com/twoscomplement
// A reimplementation of code from https://github.com/Cyan4973/xxHash
// Foundation functions / commonly used patterns
// Read four chars and construct uint32_t (little endian)
constexpr uint32_t xxh_read32(const char* input)
{
@twoscomplement
twoscomplement / gist:80c164be5cad7f51939e512f0142a5eb
Last active April 10, 2017 07:50
CRC tables, generated at compile time using C++11 constexpr, C++14 utility library, variadic template, initializer list
// CRC tables, generated at compile time using C++11 constexpr, C++14 utility library, variadic template, initializer list
// Compiled and tested using msvc 2015, gcc 6.2, and clang 3.9.0.
// clang requires -std=c++14 -ftemplate-depth=512
#include <stdint.h>
#include <utility>
template<typename T, T Poly>
struct CrcTable {
static constexpr T Generate(T v, int r = 8) {
@twoscomplement
twoscomplement / CrcTablesWithSlices.cpp
Created September 26, 2016 16:25
C++11/C++14: Generate CRC tables at compile time. With slices.
// Generate CRC tables at compile time. With slices.
// Based on http://create.stephan-brumme.com/crc32/#slicing-by-8-overview
//
// C++11, C++14 utility library, constexpr, variadic templates, initializer lists.
//
// Tested using gcc-6.2, clang-3.9.0, and latest msvc 2015 compilers.
// clang: -std=c++14 -ftemplate-depth=512
//
// @twoscomplement 2016-09-26
@twoscomplement
twoscomplement / todo.h
Last active April 5, 2019 17:33
TODO(): A macro that will fire a static assert if compiled on or after a specified date. Requires C++11.
/*
todo.h
TODO():
A macro that will fire a static assert if compiled on or after
a specified date.
Requires C++11 constexpr support.
@twoscomplement
twoscomplement / TransientFunction.h
Last active August 19, 2023 08:32
TransientFunction: A light-weight alternative to std::function [C++11]
// TransientFuction: A light-weight alternative to std::function [C++11]
// Pass any callback - including capturing lambdas - cheaply and quickly as a
// function argument
//
// Based on:
// https://deplinenoise.wordpress.com/2014/02/23/using-c11-capturing-lambdas-w-vanilla-c-api-functions/
//
// - No instantiation of called function at each call site
// - Simple to use - use TransientFunction<> as the function argument
// - Low cost: cheap setup, one indirect function call to invoke
@twoscomplement
twoscomplement / movoops.cpp
Last active February 7, 2018 21:43
Watch as the OS rewrites my buggy program. [Windows, SetErrorMode, SEM_NOALIGNMENTFAULTEXCEPT]
/*
Watch as the OS rewrites my buggy program.
Compile with:
cl //Ox movoops.cpp
Output:
0f 28 01
xxxxxxxxxxxxxxx1
0f 10 01
@twoscomplement
twoscomplement / myeyes.cpp
Created May 26, 2018 06:36
Investigation of <random>
#include <random>
#include <stdlib.h>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <algorithm>
#include <string>
namespace __detail {
template<typename _UIntType, size_t __w,
@twoscomplement
twoscomplement / array_size.h
Created December 18, 2018 07:21
A C++ macro to compute the number of elements in an array.
/*
ARRAY_SIZE(a)
A macro to compute the number of elements in an array a.
Features & considerations:
- Will not compile if a is not an array (error message is not elegant).
- Able to be used with non-const a.
- Happens to produce very few (false-positive) compile-time warnings with msvc 19 when
the result is used in signed and truncating arithmetic - unlike std::size()
(see also https://t.co/NvhjvMUhA1)