r/ProgrammingLanguages • C3 - http://c3-lang.org • May 03 '26

Blog post Unsigned Sizes: A Five Year Mistake

https://c3-lang.org/blog/unsigned-sizes-a-five-year-mistake/
91 Upvotes

115 comments sorted by

View all comments

Show parent comment

6

u/QuaternionsRoll May 04 '26

I’ve always wondered what it would be like if casting were performed on outputs rather than inputs. Define arithmetic operations such that they can never overflow (e.g. i32 + u32 -> i34) and then require the user to deal with casting the result (i34) to the type they actually need (i32, u32, etc.). Compilers are generally smart enough to eliminate the extra operations when the result is subject to a wrapping cast anyway.

2

u/ExplodingStrawHat May 04 '26

How would exponentials work? Would xy for two u8s produce a u263?

2

u/QuaternionsRoll May 04 '26

No, it would produce a u2039 (ceil(log2(255**255))). It doesn’t really matter though, the upper 2031 bits will never be computed if the result is cast to u8.

1

u/Bobbias May 05 '26

Yeah exponential growth is fast. You'd need something like 128 exabytes worth of bits to handle 2 64 bit numbers.

1

u/WittyStick May 04 '26 edited May 04 '26

My language is dynamically typed where this is a bit simpler - the result is whatever type is sufficient to fit the resulting value without loss of precision - though I don't have any separate dedicated types beyond int128 other than arbitrary precision integers. The arithmetic mode can be overridden with a dynamic binding - which can be one of modular, strict (aka checked, trap on overflow), precise (no information loss) or saturating.

In terms of encoding it in a static type system, it can get quite complex, requiring refinement or dependant types, and some operations like pow aren't trivial and could nuke your compile times. Basic addition and multiplication are fairly simple - for addition/subtraction you need at most 1 bit more that the widest input type, and for multiplication you need up to twice as many bits as the widest argument type. Div only needs the size of the first input as the smallest integer divisor is 1.

add, sub : _BitInt(N), _BitInt(M) -> _BitInt(MAX(N, M) + 1)
mul : _BitInt(N), _BitInt(M) -> _BitInt(MAX(N, M) * 2)
div : _BitInt(N), _BitInt(M) -> _BitInt(N)
pow : _BitInt(N), _BitInt(M) -> _BitInt((N+1) * 2^M)

There's more complexity if you were mixing signed and unsigned arguments, but I would advise against doing this and use the technique I previously described of permitting unsigned to be implicitly converted to a wider signed type first.

2

u/QuaternionsRoll May 04 '26

Your mul bound could be tightened from MAX(N, M) * 2 to N + M, and your div bound should be loosened from N to N + 1, as INT{N}_MIN / -1 == INT{N}_MAX + 1.

1

u/renozyx May 05 '26

Yes, I also wondered if this would be useful, with the addition that I would limit the size to 64bit:

  1. 64bit is big enough that it shouldn't overflow in normal code, so I think that it should be handled as "trap on overflow/wrap/UB" trifecta depending on compilation flags.
  2. 64bit are still very efficient to use if you have a 64bit CPU.

1

u/QuaternionsRoll May 05 '26

I’m gonna go out on a limb and say that most systems languages (or their implementations) have 128-bit integers for a reason. MSVC is the only major exception I can think of.

3

u/renozyx May 06 '26

OK, which reason? SIMD is the only thing I can think about..

1

u/TheChief275 Jul 10 '26 edited Jul 10 '26

Lmao this. Imagine thinking you need 128-bits for normal calculations or allocation sizes. 64-bits is plenty.

Want to go beyond that for scientific calculations? Well then 128-bits is likely not enough either and you want arbitrary precision integers