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

2

u/initial-algebra May 07 '26

This does NOT work as well with trapping. Trapping means that + is no longer associative (!), for example. Surprise!

It's a tradeoff. With wrapping, you get nice algebraic properties. With trapping, you get nice ordering properties. Sometimes you want one, sometimes you want the other, so it should be explicit. Though, in the case of dealing with sizes, offsets, pointers, data structures etc. you almost always want trapping, except for special cases like ring buffers, so I think it's a sane default for a systems language.

1

u/matthieum May 08 '26

I think it's a sane default for a systems language

Performance-minded folks would disagree. Trapping implementations are software emulations and are not auto-vectorizable :/

Instructions sets (x86/64) only natively support wrapping. Anything else has to be "emulated", and emulations are typically not vectorizable.

This leads to a chicken and egg problem:

  • Languages use wrapping semantics, because that's native speed.
  • Chips makers only offer wrapping semantics, because that's what languages use.

I was really looking forward to the Mill CPU, where the designers actually offered full-fledged integer operation behaviors -- wrapping, trapping, saturating, widening -- for both scalar & vector operations. WOOT. Been waiting for over a decade though, wonder how the guys are doing :'(


Even assuming native instructions, though, the fact that associativity is lost makes me look twice, to be honest. I wonder how much performance is lost to non-associative arithmetic.

In high-level languages it's likely the correct semantics, but systems languages nowadays are the languages you reach for when performance matters... so dinging their performance is a bit of a non-starter :/

2

u/initial-algebra May 08 '26

See, to me, systems programming is distinct from number crunching. There is obviously some overlap, as when you're implementing fundamental data structures and algorithms, you want them to be efficient, but correctness is even more important, and having to be a bit more verbose to go the other way is fine by me.

Of course, the ideal solution would be to be able to narrow the range of integer types down even further using refinement types. Then, you may not have to give up any properties, because you can know when the result of an operation can never overflow (barring pathological cases where memory would already be exhausted, for example), and it can be compiled to the most efficient implementation available.

1

u/matthieum May 09 '26

See, to me, systems programming is distinct from number crunching.

You are technically correct! In fact, did you know that when Graydon Hoare created Rust it had, among other things, built-in big integers? It was intended to be a systems programming language where correctness comes first.

In practice, however, systems programming language are the only languages available who can be used for high-performance (or low-overhead) tasks, and therefore seem to invariably be coopted for it.

Of course, the ideal solution would be to be able to narrow the range of integer types down even further using refinement types. [...]

Did you know Rust is getting generic integers? The idea is to have i<N> and u<N> where N is the bitwidth, and define the current crop of integers as synonyms, so type i32 = i<32>; for example.

Technically, this could be used to implement widening. Ie, the output of i<5> + i<5> is i<6>. Crude, but simple.

Anyway, whether full integer range or just bitwidth, I'm not sure how practical it could be. Multiplication really widens very quickly. i<32> * i<32> is i<64> already.

I guess you'd need checked narrowing regularly to keep things under control?

Hum... who knows, maybe it'd work.

2

u/initial-algebra May 09 '26

What I have in mind are integers that are bounded by actual memory usage, for example sizes/lengths, (valid) offsets/indices, reference counts etc. Now, I have no idea how that would actually be expressed, or the exact rules around using them in a way that would guarantee no overflow. Only being able to mention such a quantity at most once in an expression would be a start, but how to track/enforce this...