logoalt Hacker News

Retr0idtoday at 11:09 AM2 repliesview on HN

GCC -O1 and clang -O1 will both optimize this function under the assumption that inputs that cause signed integer overflow are never passed:

    int will_overflow(int a, int b) {
        int sum = a + b;
        if (b > 0 && sum < a)
            return 1;
        return 0;
    }

Replies

mbrocktoday at 11:23 AM

Right, good example, and both GCC and Clang offer well understood parameters for deciding, per compilation unit, what behavior you want for signed overflow (-fwrapv, -fno-strict-overflow, etc), so in reality it's quite far from spooky arbitrary nasal demons.

skydhashtoday at 12:17 PM

Wouldn’t be better to check both inputs before against the max value of that type instead of actually doing the overflow?

show 1 reply