Skip to content

Instantly share code, notes, and snippets.

View johnmcfarlane's full-sized avatar

John McFarlane johnmcfarlane

View GitHub Profile
@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 / godbold_square.cpp
Last active June 24, 2017 09:30
example of zero-cost abstraction using the fixed_point library
// Signed 15:16 Fixed-Point Square Function Using sg14::elastic
#include <https://gist.githubusercontent.com/johnmcfarlane/23b8bc5fefb77d482306c5bc837b5df1/raw/ed6cdfc3de42ae3c140bbbdd0ce5b8bc131d1f02/fixed_point.h>
using namespace sg14;
// square a nunber using 15:16 fixed-point arithmetic
// without using a fixed-point library
float square_int(float input) {
// user must scale values by the correct amount
@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
}
@johnmcfarlane
johnmcfarlane / functional.h
Created July 14, 2017 20:35
Fixing `std::functional`.
#include <functional>
#include <memory>
#include <stdlib.h>
#if defined(USE_ALTERNATIVE)
template<class FunctionSignature>
class function;
template<class Result, class...Args>
@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 / mandelbrot.h
Created August 1, 2017 14:15
Minimal Mandelbrot generator using CNL
#include <cnl/fixed_point.h>
#include <array>
using namespace cnl;
using namespace std;
constexpr int mandelbrot(float f_x, float f_y, int const limit) {
using scalar = fixed_point<int64_t, -28>;
auto c_x = scalar{f_x};
auto c_y = scalar{f_y};
@johnmcfarlane
johnmcfarlane / 20170726_accu_bay_area_signed_ub.html
Last active August 9, 2017 18:58
Signed UB Lightning Talk
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);