Skip to content

Instantly share code, notes, and snippets.

View johnmcfarlane's full-sized avatar

John McFarlane johnmcfarlane

View GitHub Profile
@johnmcfarlane
johnmcfarlane / stats.hpp
Created January 6, 2017 21:04
most basic statistics gatherer
#include <algorithm>
#include <limits>
template<typename Number = int, typename Counter = int>
class stats {
public:
void sample(Number const & n) {
_sum += n;
_count ++;
_minimum = std::min(_minimum, n);
@johnmcfarlane
johnmcfarlane / godbolt_average.cpp
Last active May 24, 2017 00:46
example of zero-cost abstraction using the fixed_point library
// Signed 15:16 Fixed-Point Square Function Using sg14::elastic
// Here's how to use the fixed_point library on Godbolt.org.
// Normally, you'd just add `#include <sg14/fixed_point>`.
#define SG14_GODBOLT_ORG
#include <https://raw.githubusercontent.com/johnmcfarlane/fixed_point/develop/include/sg14/bits/config.h>
#include <https://raw.githubusercontent.com/johnmcfarlane/fixed_point/develop/include/sg14/bits/config.h>
#include <https://raw.githubusercontent.com/johnmcfarlane/fixed_point/develop/include/sg14/cstdint>
#include <https://raw.githubusercontent.com/johnmcfarlane/fixed_point/develop/include/sg14/limits>
#include <https://raw.githubusercontent.com/johnmcfarlane/fixed_point/develop/include/sg14/type_traits>
@johnmcfarlane
johnmcfarlane / buffer.hpp
Created January 19, 2017 02:22
2-pointer sequence; like a vector without dynamic growth; somewhat standards-compliant
#include <algorithm>
#include <iterator>
#include <memory>
namespace sg14 {
template<typename T>
class buffer : public std::iterator_traits<T*> {
public:
using size_type = typename buffer<T>::difference_type;
#!/usr/bin/env python3
from argparse import ArgumentParser
from io import StringIO
from itertools import chain
from os import chdir, curdir, path
from subprocess import CalledProcessError, check_output, PIPE
from sys import argv, stderr
from report import benchmarks_from_buffer, csv_from_report, table_from_benchmarks
@johnmcfarlane
johnmcfarlane / kristerw.cpp
Last active July 26, 2017 02:05
Examples from Krister Walfridsson's [How undefined signed overflow enables optimizations in GCC](https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html).
// examples from Krister Walfridsson's "How undefined signed overflow enables optimizations in GCC":
// https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html
// https://godbolt.org/g/Hw797e
// Other relevant material:
// [CppCon 2016: Jon Kalb “unsigned: A Guideline for Better Code"](https://www.youtube.com/watch?v=wvtFGa6XJDU)
// [CppCon 2016: Chandler Carruth “Garbage In, Garbage Out: Arguing about Undefined Behavior..."](https://www.youtube.com/watch?v=yG1OZ69H_-o&t=2542s)
#include <algorithm>
@johnmcfarlane
johnmcfarlane / i like commonmark.md
Last active May 12, 2017 19:25
A very quick demonstration of commonmark

A quick vote for commonmark:

  • it's very easy to read and write - even in as raw text;
  • it's well supported by GitHub;
  • it does 90% of what you need to write technical documentation;
  • it allows you to casually drop snippets of HTML/JSON in for the other 10% and
  • there are plenty of tools for converting it to HTML and other formats (see Pandoc and cmark).

In fact the above rant is commonmark! https://gist.github.com/johnmcfarlane/aec9d1c55d25bbbc086469c0c897b580

@johnmcfarlane
johnmcfarlane / fixed_point.h
Last active June 28, 2017 14:47
Single-header version of fixed_point library
// Copyright John McFarlane 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file ../LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// mechanically retrieved, single-header version of fixed_point library
// https://github.com/johnmcfarlane/fixed_point
#if ! defined(SG14_FIXED_POINT_SINGLE_HEADER)
@johnmcfarlane
johnmcfarlane / test.h
Created June 29, 2017 06:47
A very small header
// here is a very small header
@johnmcfarlane
johnmcfarlane / jmcf.xml
Last active July 3, 2017 19:55
some CLion formatting settings that I think I might be able to tollerate
<code_scheme name="jmcf">
<Objective-C>
<option name="INDENT_NAMESPACE_MEMBERS" value="0" />
<option name="INDENT_C_STRUCT_MEMBERS" value="2" />
<option name="INDENT_CLASS_MEMBERS" value="2" />
<option name="INDENT_INSIDE_CODE_BLOCK" value="2" />
<option name="KEEP_STRUCTURES_IN_ONE_LINE" value="true" />
<option name="KEEP_CASE_EXPRESSIONS_IN_ONE_LINE" value="true" />
<option name="FUNCTION_NON_TOP_AFTER_RETURN_TYPE_WRAP" value="0" />
<option name="FUNCTION_TOP_AFTER_RETURN_TYPE_WRAP" value="0" />
@johnmcfarlane
johnmcfarlane / broken_header.h
Last active July 8, 2017 01:19
A broken header
#include <stdlib.h>
namespace a_namespace{
constexpr int x = 6;
#if defined TEST
#error The test passed (ironically)
#endif
}