Skip to content

Instantly share code, notes, and snippets.

Jonathan Adamczewski twoscomplement

Block or report user

Report or block twoscomplement

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@twoscomplement
twoscomplement / array_size.h
Created Dec 18, 2018
A C++ macro to compute the number of elements in an array.
View array_size.h
/*
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)
@twoscomplement
twoscomplement / myeyes.cpp
Created May 26, 2018
Investigation of <random>
View myeyes.cpp
#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 / movoops.cpp
Last active Feb 7, 2018
Watch as the OS rewrites my buggy program. [Windows, SetErrorMode, SEM_NOALIGNMENTFAULTEXCEPT]
View movoops.cpp
/*
Watch as the OS rewrites my buggy program.
Compile with:
cl //Ox movoops.cpp
Output:
0f 28 01
xxxxxxxxxxxxxxx1
0f 10 01
@twoscomplement
twoscomplement / TransientFunction.h
Last active Apr 15, 2018
TransientFunction: A light-weight alternative to std::function [C++11]
View TransientFunction.h
// 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 / todo.h
Last active Apr 5, 2019
TODO(): A macro that will fire a static assert if compiled on or after a specified date. Requires C++11.
View todo.h
/*
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 / CrcTablesWithSlices.cpp
Created Sep 26, 2016
C++11/C++14: Generate CRC tables at compile time. With slices.
View CrcTablesWithSlices.cpp
// 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 / gist:80c164be5cad7f51939e512f0142a5eb
Last active Apr 10, 2017
CRC tables, generated at compile time using C++11 constexpr, C++14 utility library, variadic template, initializer list
View gist:80c164be5cad7f51939e512f0142a5eb
// 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) {
View xxhash32_constexpr.cpp
// 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)
{
You can’t perform that action at this time.