Skip to content

Instantly share code, notes, and snippets.

View justinmeiners's full-sized avatar

Justin Meiners justinmeiners

View GitHub Profile

Multi-dimensional array views for systems programmers

As C programmers, most of us think of pointer arithmetic for multi-dimensional arrays in a nested way:

The address for a 1-dimensional array is base + x. The address for a 2-dimensional array is base + x + y*x_size for row-major layout and base + y + x*y_size for column-major layout. The address for a 3-dimensional array is base + x + (y + z*y_size)*x_size for row-column-major layout. And so on.

@pervognsen
pervognsen / rad.py
Last active January 18, 2024 02:30
# Reverse-mode automatic differentiation
import math
# d(-x) = -dx
def func_neg(x):
return -x, [-1]
# d(x + y) = dx + dy
def func_add(x, y):
//
// Author: Jonathan Blow
// Version: 1
// Date: 31 August, 2018
//
// This code is released under the MIT license, which you can find at
//
// https://opensource.org/licenses/MIT
//
//
typedef struct intern_t {
intern_t *next;
uint32_t length; // can be narrower if you want to limit internable string length, which is a good idea.
char str[1];
} intern_t;
hashtable_t string_table;
const char *string_intern(const char *str, uint32_t length) {
uint64_t key = string_hash(str, length);
; syntax objects are true AST nodes, so they are an algebraic data type with data constructors and deconstructors:
;
; syntax = (syntax-null props) | (syntax-cons props syntax syntax) | (syntax-identifier props symbol) | (syntax-atom props datum)
;
; the constructors have dual deconstructors with names like syntax-car, syntax-cdr, syntax-identifier-symbol,
; and type predicates like syntax-null?, syntax-pair?, syntax-identifier?.
;
; props is just an open-ended plist of key-value pairs with optional information about things like source location
; and anything else you want to track. you can call syntax-props on any of the syntax variants to get the plist.
; as a convenience when calling the constructor functions, you can either pass in a props plist directly, or you can
@jacobtomlinson
jacobtomlinson / .gitignore_global
Created August 18, 2017 10:08
An example global gitignore file
# An example global gitignore file
#
# Place a copy if this at ~/.gitignore_global
# Run `git config --global core.excludesfile ~/.gitignore_global`
# Compiled source #
###################
*.com
*.class
*.dll
@bkaradzic
bkaradzic / why_i_think_immediate_mode_gui_is_way_to_go_for_gamedev_tools.md
Last active April 5, 2024 05:40
Why I think Immediate Mode GUI is way to go for GameDev tools

Why I think Immediate Mode GUI is way to go for GameDev tools

Prerequisites

Before you continue, if you don't know what IMGUI is don't bother reading this post, just ignore it, don't write anything in comments section, etc. If you're curious about IMGUI see bottom of this post, otherwise continue whatever you were doing, this post it's not for you. Thanks!

If you know what IMGUI is, for context read following presentations and blog posts:

  • Insomniac’s Web Tools Postmortem

API Design: Coroutines APIs (Janurary-2017)

I am currently dealing with a lot of libraries at work. Both third party as well as libraries written or being currently in process of being written by me. I absolutely love writing and working with libraries. Especially if they present or bring me to either a new or different approach to solve a problem. Or at least provide a different view.

Over time I noticed however that quite regulary we had to decide that we cannot use a third party library. Often it is the usual reason.

/* Lightweight module based templates in standard C
===================================================
This is a proof of concept example of lightweight module based templates in C and
is loosely based on https://gist.github.com/pervognsen/c56d4ddce94fbef3c80e228b39efc028 from Per Vognsen.
While his approach (at least as far as I understood) is based on a python script to generate
the .h/.c files for you is this implementation contained in one single header file.
This is an outline to show how you can use the single header approach
and bend it to its absolute extrems. I tend to write specialized datastructures
for most of my problems but sometimes it happens that I have to use a particular
@mpneuried
mpneuried / Makefile
Last active April 19, 2024 21:06
Simple Makefile to build, run, tag and publish a docker containier to AWS-ECR
# import config.
# You can change the default config with `make cnf="config_special.env" build`
cnf ?= config.env
include $(cnf)
export $(shell sed 's/=.*//' $(cnf))
# import deploy config
# You can change the default deploy config with `make cnf="deploy_special.env" release`
dpl ?= deploy.env
include $(dpl)