Skip to content

Instantly share code, notes, and snippets.

View sophec's full-sized avatar
🖤
[[maybe_unused]]

Sophie Eccles sophec

🖤
[[maybe_unused]]
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)...} {}
@sophec
sophec / 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) {
@sophec
sophec / 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.
*
@sophec
sophec / 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:
@sophec
sophec / 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());
@sophec
sophec / __cplusplus values.md
Last active May 29, 2026 18:49
__cplusplus values for each C++ standard version

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

  • C++98: 199711L
  • C++11: 201103L
  • C++14: 201402L
  • C++17: 201703L
  • C++20: 202002L
  • C++23: 202302L
@sophec
sophec / 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;
@sophec
sophec / 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>
#
@sophec
sophec / helloworld.c
Last active May 28, 2026 17:39
Just a standard hello world program in C
#include <stdio.h>
static char a[65535] = {0};
static char* p = a;
int main() {
*p += 8;
for (; *p;) {
*++p += 4;
for (; *p;) {
*++p += 2;
*++p += 3;
@sophec
sophec / hex2float.c
Last active February 22, 2021 18:35
convert hex to float, optionally swapping byte order
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <ctype.h>
#include <stdbool.h>
#define SWAP(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))