On custom-width integer types

submited by
Style Pass
2024-07-05 12:00:14

Before we start: by "custom-width integer types" I'm referring to a language feature that lets users specify integer types with any fixed number of bits (e.g. u7 , u12 ) - instead of being provided a handful of integers that correspond to common xword sizes (e.g. u8 ,u16 , u32 ,u64 ). The first time I've encountered this idea was in the Zig language, although it also exists in Clang & LLVM IR.

Now, Zig is an odd one. The language was founded on pure systems programming pragmatism - an antithesis of ivory-tower category theory shenanigans, and a place where Curry is just an earthy Indian spice. But time and time again, through pragmatic, real-world justifications, we witness a collision of these two worlds.

How come? It turns out that accurately modeling your program's intended behavior is in everyone's best interest. As long as this model is programatically enforced (e.g. by language semantics), we can get two big benefits: (1) we improve correctness and safety of our programs and (2) we open the door for aggressive compiler optimizations. And custom-width integer types are a perfect example of this.

Leave a Comment