Skip to content

Instantly share code, notes, and snippets.

@jibsen
jibsen / bytes.md
Last active December 20, 2023 10:02
Ramblings about uint8_t and undefined behavior

Introduction

The C standard only specifies minimum limits for the values of character types and standard integer types. This makes it possible to generate efficient code on diverse architectures, but can pose problematic if your code expects the limits to match your development platform, or if you have to do low-level things.

Before C99, the usual way to solve this was to use typedef to declare synonyms

After solving the compaction puzzle for parallel processing of values in a partitioned stream, the path to a screenspace CSG quadtree kernel is now open.

Instead of separate tiles, products and factors, we now keep a single array of factors, of which each factor also has a product index and a tile coordinate. We operate on a 256x256 tile so our tile coordinates fit into 16 bit, and allow only a maximum of 65536 products for this tile, with 2^31 addressable brushes. A factor then requires only 8 bytes: 2 bytes for its product index, 2 bytes for its tile coordinate and 4 bytes for its signed brush id.

We seed the array with all factors that matter for this 256x256 tile, sorted by tile coordinate and product index so that factors which belong to the same product are packed together, and all products which belong to the same tile are packed together as well.

We also init a brush id image with tile size 1x1.

In the beginning, there is typically

@thibaudruelle
thibaudruelle / Add_reg_keys-Anaconda_Prompt_Here.bat
Created November 30, 2017 08:46
Run as admin to add "Anaconda Prompt Here" to context menu. See https://stackoverflow.com/a/46803585/1291711.
REG ADD HKCR\Directory\Background\shell\Anaconda\ /ve /f /d "Anaconda Prompt Here"
REG ADD HKCR\Directory\Background\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\Anaconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\Directory\Background\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%%USERPROFILE%%\\Anaconda3\\pythonw.exe %%USERPROFILE%%\\Anaconda3\\cwp2.py %%USERPROFILE%%\\Anaconda3 %%V cmd.exe /K %%USERPROFILE%%\\Anaconda3\\Scripts\\activate.bat %%USERPROFILE%%\\Anaconda3"
REG ADD HKCR\Directory\shell\Anaconda\ /ve /f /d "Anaconda Prompt Here"
REG ADD HKCR\Directory\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\Anaconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\Directory\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%%USERPROFILE%%\\Anaconda3\\pythonw.exe %%USERPROFILE%%\\Anaconda3\\cwp2.py %%USERPROFILE%%\\Anaconda3 %%V cmd.exe /K %%USERPROFILE%%\\Anaconda3\\Scripts\\activate.bat %%USERPROFILE%%\\Anaconda3"
@shafik
shafik / WhatIsStrictAliasingAndWhyDoWeCare.md
Last active October 21, 2025 15:16
What is Strict Aliasing and Why do we Care?

What is the Strict Aliasing Rule and Why do we care?

(OR Type Punning, Undefined Behavior and Alignment, Oh My!)

What is strict aliasing? First we will describe what is aliasing and then we can learn what being strict about it means.

In C and C++ aliasing has to do with what expression types we are allowed to access stored values through. In both C and C++ the standard specifies which expression types are allowed to alias which types. The compiler and optimizer are allowed to assume we follow the aliasing rules strictly, hence the term strict aliasing rule. If we attempt to access a value using a type not allowed it is classified as undefined behavior(UB). Once we have undefined behavior all bets are off, the results of our program are no longer reliable.

Unfortunately with strict aliasing violations, we will often obtain the results we expect, leaving the possibility the a future version of a compiler with a new optimization will break code we th

data Graph = Graph
{ pointsLeft :: Set.Set (V2 Double)
-- ^ All the points in the whole graph left to be connected
, branches :: Set.Set LineSegment
-- ^ All branches we have found, connecting two points
, currentPoints :: [V2 Double]
-- ^ Points that are currently being processed
, maxDist :: Double
-- ^ Maximum distance a thing can be away from a thing
}
@briandominick
briandominick / asciidoc-static.adoc
Last active June 16, 2025 16:12
Static Site Generators with AsciiDoc Support

There are 28 static site generators that support AsciiDoc sourcing.

from math import *
# a performant solution would store a prefix sum of line lengths to
# a sidechannel and then use that to do a bsearch; on the GPU,
# you'd do a sum tree / histopyramid as a preprocessing step
def find_point(points, d):
d = d
for i in range(1, len(points)):
x0,y0 = points[i-1]
x1,y1 = points[i]
@rygorous
rygorous / random_bracket_seq.py
Created March 8, 2019 01:55
Generate a random, sequence of correctly nested parentheses
import random
def random_bracket_sequence(n):
"""Generates a balanced sequence of n +1s and n -1s corresponding to correctly nested brackets."""
# "Generating binary trees at random", Atkinson & Sack, 1992
# Generate a randomly shuffled sequence of n +1s and n -1s
# These are steps 1 and 2 of the algorithm in the paper
seq = [-1, 1]*n
random.shuffle(seq)