Re:Checked integer arithmetic in the prospect of C23
by dooglius on Monday December 19, 2022 @12:14PM
(#34054609)
This is already present as a builtin in in GNU C (as indicated in TFA) and it already results in the optimal code: https://godbolt.org/z/qc4zvav7E
Re:Checked integer arithmetic in the prospect of C23
by tinglymintyfrsh on Monday December 19, 2022 @01:27PM
(#34055648)
Also, Hacker's Delight and OpenBSD probably have clever solutions for these.
Re:Checked integer arithmetic in the prospect of C23
by wahern on Monday December 19, 2022 @10:15PM
(#34061108)
Surprisingly, OpenBSD does not have a library (neither a public API nor even just routines which are copied project-to-project as is common with OpenBSD utilities and daemons) to handle arithmetic overflow. The closest might be malloc/realloc extensions, like reallocarray, that handle common scenarios where arithmetic overflow is seen.
Re:Checked integer arithmetic in the prospect of C23
by dezgeg on Monday December 19, 2022 @01:44PM
(#34055887)
Hmm, isn't the Apple-specific magic only for parity(PF) and aux carry (AF)? aarch64 does have a 'V' flag for signed overflow.
Re:Checked integer arithmetic in the prospect of C23
by raphlinus on Monday December 19, 2022 @02:31PM
(#34056512)
Oops, you're right. Too late to edit, sorry about the confusion.
Re:Checked integer arithmetic in the prospect of C23
by jcranmer on Monday December 19, 2022 @02:17PM
(#34056311)
Checked overflow operations are kind of the goto operation for "it's easy in assembly, hard in programming languages"--in hardware terms, it's usually check a flag, but since flag registers are not provided for in high-level languages, it becomes a game of try to write it in a pattern that the compiler can recognize, which is never a fun game to play. Even worse than addition is multiplication. Thankfully, C23 has finally added these operations.
Although, recently, I noticed I wanted a case where I wanted checked (u32 - u32) -> i32 and (u32 + i32) -> u32 operations, which even Rust's standard library doesn't provide. (The use case is keeping track of a running delta between two lists of u32 values--the delta can go positive or negative, so it has to be signed, but the values in the lists can never be negative).
Re:Checked integer arithmetic in the prospect of C23
by raphlinus on Monday December 19, 2022 @04:16PM
(#34057827)
The addition operator just landed in Rust 1.66 (checked_add_signed[1]), but the subtraction one it looks like you'd need to roll your own.
Re:Checked integer arithmetic in the prospect of C23
by tialaramex on Monday December 19, 2022 @07:32PM
(#34059910)
You are probably looking at it wrong. You can now write
i32::checked_add_unsigned(some_u32) and
i32::checked_sub_unsigned(some_u32)
... which I think are exactly what your parent needs.
Re:Checked integer arithmetic in the prospect of C23
by jcranmer on Monday December 19, 2022 @09:20PM
(#34060739)
u32::checked_add_signed solves one of the pairs (u32 + i32 -> u32), but there's nothing for the other one (u32 - u32 -> i32).
Re:Checked integer arithmetic in the prospect of C23
by tialaramex on Tuesday December 20, 2022 @04:27AM
(#34063604)
Good point, I'm sure this made sense to me when I wrote it, but I can't ask past me why.
Re:Checked integer arithmetic in the prospect of C23
by TazeTSchnitzel on Monday December 19, 2022 @05:10PM
(#34058450)
> it becomes a game of try to write it in a pattern that the compiler can recognize
Worse, in C or C++ you also need to find a way to do it without undefined behaviour. You can't just do the operation and see if the result matches expectations…
Re:Checked integer arithmetic in the prospect of C23
by tinus_hn on Monday December 19, 2022 @06:54PM
(#34059586)
C is supposed to be reasonably portable so it doesn’t make too much sense to depend on flags that are not available on all platforms.
Re:Checked integer arithmetic in the prospect of C23
by int_19h on Monday December 19, 2022 @07:17PM
(#34059789)
It's easier to find a platform that doesn't support floating-point numbers than one that doesn't have some kind of overflow flag. Some kind of platform-independent abstraction is long (as in, decades) overdue.
Re:Checked integer arithmetic in the prospect of C23
by noobermin on Monday December 19, 2022 @07:19PM
(#34059811)
Sure, but it kind of is insane that there are platforms (like ARM apparently, did not know) that do NOT have a carry flag.
Re:Checked integer arithmetic in the prospect of C23
by derefr on Monday December 19, 2022 @08:26PM
(#34060345)
Why can’t it be a flag on some platforms and complex hereafter branching logic on other platforms? Like the division shim on platforms that don’t have a division instruction?
I expect the C23 built-in functions will perform as well as Rust here, which is a win both for ergonomics (you can't really consider the current state of "will a+b overflow" to be discoverable) and performance.
[1]: https://mastodon.online/@raph/109535617953722719
[2]: https://godbolt.org/z/17zMsWjYv
[3]: https://rust.godbolt.org/z/36Ta9oP1P
[4]: https://news.ycombinator.com/item?id=33635720