hachyderm.io is one of the many independent Mastodon servers you can use to participate in the fediverse.
Hachyderm is a safe space, LGBTQIA+ and BLM, primarily comprised of tech industry professionals world wide. Note that many non-user account types have restrictions - please see our About page.

Administered by:

Server stats:

9K
active users

Computer Systems: A Programmer’s Perspective, a textbook by Bryant and O’Halloran, is in its third edition and also its third decade of teaching students falsehoods about C

switching over to codegen for overflow checking for addition, we don't appear to get optimal code for any of the obvious ways to do it, from latest GCC or LLVM, unless we use the intrinsic

gcc.godbolt.org/z/9bcPdzYcY

gcc.godbolt.orgCompiler Explorer - C typedef int32_t int_t; typedef uint32_t uint_t; typedef int64_t wideint_t; typedef uint64_t wideuint_t; #define MAX INT32_MAX #define MIN INT32_MIN #define BITS (8 * sizeof(int_t)) int checked_add_0(int_t a, int_t b) { int_t res; return __builtin_add_overflow(a, b, &res); } int checked_add_1(int_t a, int_t b) { wideint_t lr = (wideint_t)a + (wideint_t)b; return lr > MAX || lr < MIN; } int checked_add_2(int_t a, int_t b) { int_t r = (uint_t)a + (uint_t)b; return (a < 0 && b < 0 && r >= 0) || (a >= 0 && b >= 0 && r < 0); } int checked_add_3(int_t a, int_t b) { uint_t ur = (uint_t)a + (uint_t)b; uint_t sr = ur >> (BITS - 1); uint_t sa = (uint_t)a >> (BITS - 1); uint_t sb = (uint_t)b >> (BITS - 1); return (sa && sb && !sr) || (!sa && !sb && sr); } int checked_add_3b(int_t a, int_t b) { const uint_t mask = (uint_t)1 << (BITS - 1); int_t rp = (uint_t)a + (uint_t)b; return ((uint_t)a & mask) == ((uint_t)b & mask) && ((uint_t)a & mask) != ((uint_t)rp & mask); } int checked_add_3c(int_t a, int_t b) { const uint_t r = (uint_t)a + (uint_t)b; return (((uint_t)a ^ r) & ((uint_t)b ^ r)) >> (BITS - 1); } int checked_add_4(int_t a, int_t b) { return (b > 0 && a > MAX - b) || (b < 0 && a < MIN - b); }
Cassandrich

@regehr @uecker And it can be provided by a dropin if you don't have a C23 compiler.