Skip to content

Instantly share code, notes, and snippets.

@rygorous
rygorous / gist:e0f055bfb74e3d5f0af20690759de5a7
Created May 8, 2016 06:54
A bit of background on compilers exploiting signed overflow
Why do compilers even bother with exploiting undefinedness signed overflow? And what are those
mysterious cases where it helps?
A lot of people (myself included) are against transforms that aggressively exploit undefined behavior, but
I think it's useful to know what compiler writers are accomplishing by this.
TL;DR: C doesn't work very well if int!=register width, but (for backwards compat) int is 32-bit on all
major 64-bit targets, and this causes quite hairy problems for code generation and optimization in some
fairly common cases. The signed overflow UB exploitation is an attempt to work around this.
@ajdavis
ajdavis / uncrustify_changed_lines.py
Created March 13, 2015 20:12
Put uncrustify and uncrustify.cfg in your ~/bin and run this to uncrustify changed lines in your file.
#!/usr/bin/env python
import difflib
import subprocess
import shutil
import sys
from os import getcwd
from os.path import normpath, expanduser, join, exists
from git import Repo # pip install gitpython
#include <iostream>
#include <functional>
#include <string>
#include <numeric>
#include <array>
template<typename T,typename R>
struct match_struct{
T t;
std::function<R(T&&)> f;
};
# Once done these will be defined:
#
# LIBSODIUM_FOUND
# LIBSODIUM_INCLUDE_DIRS
# LIBSODIUM_LIBRARIES
#
if(LIBSODIUM_INCLUDE_DIRS AND LIBSODIUM_LIBRARIES)
set(LIBSODIUM_FOUND TRUE)
else()
@dabrahams
dabrahams / constexpr_demo.cpp
Created December 11, 2011 01:29
Fun with C++11 constexpr
#include <iostream>
#include <iomanip>
//
// Utilities
//
// RETURNS() is used to avoid writing boilerplate "->decltype(x) { return x; }" phrases.
//
// USAGE: auto function(<arguments>) RETURNS(<some-expression>);
//
@jeetsukumaran
jeetsukumaran / custom_iterator.cpp
Created February 18, 2010 02:33
Sample C++/STL custom iterator
// Sample custom iterator.
// By perfectly.insane (http://www.dreamincode.net/forums/index.php?showuser=76558)
// From: http://www.dreamincode.net/forums/index.php?showtopic=58468
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cassert>