Skip to content

Instantly share code, notes, and snippets.

View willeccles's full-sized avatar
🖤
probably out driving

Will Eccles willeccles

🖤
probably out driving
View GitHub Profile
#ifndef ONE_OF_H
#define ONE_OF_H
#include <tuple>
#include <utility>
template <class... T>
struct one_of {
std::tuple<T...> t;
constexpr one_of(T&&... values) : t{std::forward<T>(values)...} {}
@willeccles
willeccles / KahanSum.h
Created March 16, 2023 14:04
C++ Kahan summation utility class
// TODO concepts/constraints/SFINAE to keep T as FP
// TODO make this more robust than the very basics
template <class T>
class KahanSum {
public:
KahanSum() : sum_{}, err_{} {}
KahanSum(T init) : sum_{init}, err_{} {}
constexpr KahanSum& Add(T val) {
@willeccles
willeccles / regmap.cpp
Created March 3, 2022 15:41
C++ register mapping
/*
* Basic register map implementation in C++. Makes reading/writing registers
* safer, should be just as efficient as doing things the old fashioned way.
* The setup is a little ugly, but such is life. It was never going to be pretty
* anyway.
*
* Requires C++20, though that could be changed with a little SFINAE trickery.
*
* Example code uses GPIO peripherals on an AM3358.
*
@willeccles
willeccles / gen_incremental.cpp
Last active January 3, 2022 18:19
Proof of concept: generating arrays of non-moveable, non-copyable, non-default-constructible types at compile-time using increasing integer values as constructor arguments
// in action: https://godbolt.org/z/8z8r37rGP
#include <algorithm>
#include <array>
#include <iostream>
#include <tuple>
#include <utility>
class L {
public:
@willeccles
willeccles / throwerrno.h
Last active December 15, 2021 20:59
throw std::system_error with errno values quickly
#ifndef THROW_ERRNO_H
#define THROW_ERRNO_H
#include <cerrno>
#include <string>
#include <system_error>
[[noreturn]]
inline void throw_errno() {
throw std::system_error(errno, std::system_category());
@willeccles
willeccles / __cplusplus values.md
Last active March 25, 2024 23:38
__cplusplus values for each C++ standard version

__cplusplus Values

Since it's impossible to find one concise source anywhere, as far as I can tell:

ISO C++ Standard Value
1998/2003 199711L
2011 201103L
2014 201402L
2017 201703L
@willeccles
willeccles / jedcrc.c
Created October 4, 2021 15:19
Calculate the checksum of the transmission data in a JEDEC .jed file.
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define STX 0x02
#define ETX 0x03
int main(int argc, char** argv) {
FILE* src = NULL;
@willeccles
willeccles / git-changelog.sh
Last active September 28, 2021 18:17
Generate markdown changelogs from git tags and the commits between them
#!/bin/sh
set -e
# For each tag in the repository (sorted in reverse chronological order), print
# something like the following:
#
# ## Version <TAG>
# <Tag body, if it has one, but not the body of the pointed-to commit>
#

Keybase proof

I hereby claim:

  • I am willeccles on github.
  • I am willeccles (https://keybase.io/willeccles) on keybase.
  • I have a public key whose fingerprint is 956D 8570 AA24 7CEC 5515 8652 CAEB C3A8 A8B8 E9E8

To claim this, I am signing this object:

@willeccles
willeccles / helloworld.c
Created March 12, 2021 00:37
Just a standard hello world program in C
#include <stdio.h>
#include <stdlib.h>
char arr[65536] = {0};
char* ptr = arr;
int main(int argc, char** argv) {
while (*ptr) {
*ptr = getchar();
putchar(*ptr);