Skip to content

Instantly share code, notes, and snippets.

typedef union {
struct { char pad_x[31]; int x; } __attribute__((packed));
struct { char pad_y[512]; int y; } __attribute__((packed));
// ...
} __attribute__((packed)) overlay;
// Macrofied:
#define JOIN1(x, y) x##y
#define JOIN(x, y) JOIN1(x, y)
anonymous
anonymous / ds4.cpp
Created December 17, 2015 19:00
example of using libhid to read and write a playstation DS4 controller (wired)
// quick hack job by @mmalex
// thanks to libhid author and @johndrinkwater and https://github.com/chrippa/ds4drv
#include <stdio.h>
#include "hidapi.h" // using http://www.signal11.us/oss/hidapi/
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
#pragma pack(push, 1)
@dwilliamson
dwilliamson / Pimpl.cpp
Last active May 12, 2017 16:10
How I Pimpl
struct Data
{
Data()
{
}
~Data()
{
}
@bryanedds
bryanedds / PlatonicSolidsOfSoftwareConstruction.txt
Last active December 12, 2017 20:05
The Platonic Solids of Software Construction and Their Realization in C
No matter the business domain or programming language, programmers always end up needing support for ALL of these things
in some form -
* Resource Semantics (such as RAII in C++, or finalizers in C#)
* Error Semantics (such as exceptions in most languages, or conditions in Common Lisp)
* Algebraic Types (such as structs and unions in C, or records and DUs in F#)
* Data Abstraction (such as that which is often badly entangled within object systems in OO languages)
@rygorous
rygorous / dtoa.c
Last active November 23, 2019 07:23
RAD dtoa
// The original author of this software is David M. Gay.
// RAD modifications from here down to the copyright notice
// Actual RAD version of this includes rrCore.h which does
// (among other things) endianness detection - you'll have to
// do that yourself.
#include "dtoa.h"
// Again, we normally use our own assert macros here.
@rygorous
rygorous / box_pruning_notes.txt
Created February 17, 2017 00:41
Note on changes to the box pruning code.
Brief explanation what I did to get the speed-up, and the thought process behind it.
The original code went:
EnterLoop:
movaps xmm3, xmmword ptr [edx+ecx*2] // Box1YZ
cmpnltps xmm3, xmm2
movmskps eax, xmm3
cmp eax, 0Ch
Suppose you are writing a simple one-pass compiler in the Wirth style, and you want to
target non-RISC architectures like the x86 where conditional operations use shared condition state.
Case 1:
if (x <= y) z = w
CMP x, y
JGT skip
MOV z, w
skip:
Vex is a minimalistic markup language with the motto "form without meaning".
This is going to @italic=really hurt.
// @author: Per Vognsen
// @version: 1.3
The weather forecast for @date is @weather.
@list{
@dwilliamson
dwilliamson / Doc.md
Last active April 23, 2023 14:17
Minimal Code Generation for STL-like Containers

This is my little Christmas-break experiment trying to (among other things) reduce the amount of generated code for containers.

THIS CODE WILL CONTAIN BUGS AND IS ONLY PRESENTED AS AN EXAMPLE.

The C++ STL is still an undesirable library for many reasons I have extolled in the past. But it's also a good library. Demons lie in this here debate and I have no interest in revisiting it right now.

The goals that I have achieved with this approach are:

typedef int task_t; // Lower bits as table index, upper bits as generation counter for use-after-free detection.
typedef void (*task_function_t)(void *task_data);
task_t task_create(task_function_t task_function, void *task_data);
void task_depends(task_t task, task_t dependency);
void task_start(task_t task);
Upon completion, tasks signal their dependencies and are released.
Memory requirements for a minimal implementation: 16/24 bytes per task and 8 bytes per dependency edge.
Even so, you still don't want to have one task instance for every array entry in a gigabyte array. Always assign