Hacker News Viewer

Building Your Own Efficient uint128 in C++

by PaulHoule on 2/1/2026, 8:40:45 PM

https://solidean.com/blog/2026/building-your-own-u128/

Comments

by: beached_whale

I am so happy that MSVC added 128 bit integers to their standard library in order to do ranges distance of uint64_t iota views. One type alias away from int128's on most machines running gcc/clang/msvc

2/2/2026, 3:06:21 AM


by: Joker_vD

&gt; On division: There is no neat codegen for division.<p>Wait, what? I&#x27;m fairly certain that you can do a 128-bit by 128-bit division using a x64&#x27;s 128-bit by 64-bit division instruction that gives you only 64-bit quotient and remainder. The trick is to pre-multiply both dividend and divisor by a large enough power of 2 so that the &quot;partial&quot; quotient and remainders that the hardware instruction would need to produce will fit into 64 bits. On the whole, IIRC you need either 1 or 2 division instructions, depending on how large the divisor is (if it&#x27;s too small, you need two divisions).

2/2/2026, 3:19:22 AM


by: PaulHoule

Makes me think of the bad old days where the platform gave you 8-bit ints and you built everything else yourself... or AVR-8.

2/2/2026, 1:52:02 AM


by: reactordev

Tangential. A long time ago at a company far far away, this is how we did UUIDs that made up a TenantId and a UserId, using this exact same logic, minus the arithmetic. Great stuff.<p>(We wanted something UUID like but deterministic that we could easily decompose and do RBAC with, this was prior to the invention of JWT’s, OAuth, and scopes, worked at the time).

2/2/2026, 1:16:11 AM


by: azhenley

<i>&gt; we use 256-bit integers in our hot paths and go up to 564 bits for certain edge cases.</i><p>Why 564 bits? That’s 70.5 bytes.

2/2/2026, 2:07:13 AM