News for hackers. Stuff that matters.
Re:Catching integer overflows in C by Annatar on Wednesday August 22, 2018 @01:19AM (#17816493)
Using compiler built-ins makes one’s code instantly unportable and is therefore unacceptable.
Re:Catching integer overflows in C by exDM69 on Wednesday August 22, 2018 @02:29AM (#17816767)
On the contrary, compiler built-ins can make your code more portable (across different hw architectures) at the expense of some other compilers.

GCC and Clang have mostly compatible compiler intrinsics and with those two compilers you can compile your software to almost any platform out there.

If you need to get your code compiled on MSVC or ICC or some niche C compiler, you can work around this by adding some kind of wrapper around them with #ifdefs (not pretty).

With compiler built-ins you get SIMD, atomics, bitops (popcnt, clz, etc) that are portable to almost any CPU instruction set out there. Without them, you have to re-write that code for every instruction set you intend to support.

So your options are:

1) Stick to standard C and write code that compiles to suboptimal output code (such as using division to get multiply-with-overflow)

2) Use compiler built-ins and get portability across different HW at the expense of not supporting other compilers

3) Write wrappers around compiler built-ins with #ifdefs to support all the compilers you want (add fallback to standard C if you need to)

4) Write your code using HW-specific intrinsics or inline assembly

Out of these, option 2) will give you maximum portability with least amount of effort. The other options are more effort and the benefits are arguable. If you think there are other viable options, please do share.

Re:Catching integer overflows in C by BeeOnRope on Wednesday August 22, 2018 @02:59AM (#17816884)
A variant of (3) that seems close to ideal is to use conditionally defined functions as suggested, that someone else has already written and generously shared, such as portable-snippets [0]. It has a component specifically to offer cross platform builtins with fallback/emulation on plaforms without direct support.

[0] https://github.com/nemequ/portable-snippets

Re:Catching integer overflows in C by exDM69 on Wednesday August 22, 2018 @03:06AM (#17816915)
Thanks for the link, that looks quite useful if compiler portability is needed. It's still more effort than option #2, but quite an acceptable trade-off.
Re:Catching integer overflows in C by Annatar on Wednesday August 22, 2018 @03:02AM (#17816895)
“2) Use compiler built-ins and get portability across different HW at the expense of not supporting other compilers”

Writing code to two specific compilers (clang and gcc) is NOT portable code.

This is absolutely unacceptable behavior.

Re:Catching integer overflows in C by AstralStorm on Wednesday August 22, 2018 @03:45AM (#17817060)
The absolutely unacceptable thing is C still not having standard (optional) SIMD syntax. It at least finally got atomics.

But wait, that's not portable to Keil so you cannot use that ;)

The reaction is, if you dislike fast but non standard code, push for standardization of those features, not begrudge developers for doing the right thing and using them.

Re:Catching integer overflows in C by exDM69 on Wednesday August 22, 2018 @04:09AM (#17817148)
I agree that it's unacceptable that SIMD Vector extensions [0] are not standardized yet.

However, they already work acceptably in GCC and Clang. There are only minor differences (e.g. __builtin_shufflevector) but it's usable with very minimal #ifdeffing.

Example:

    typedef float vec4f __attribute__((vector_size(16)));
    // initializer
    vec4f a = { 1.0, 2.0, 3.0, 4.0 };
    // literal expression
    vec4f b = a + (vec4f){ 5.0, 6.0, 7.0, 8.0 };
    // standard infix operators
    vec4f c = -b * (a+b); // with --ffast-math, this will be a mad()
It's not perfect but it's quite usable and I've been using vector extensions in C for years.

[0] https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html

Re:Catching integer overflows in C by Annatar on Wednesday August 22, 2018 @06:30AM (#17817670)
SIMD is processor specific, and is important only in certain scenarios.

Trading portability for performance is a trade-off that most developers shouldn’t be willing to make if they are open sourcing their software, because it will prevent someone from running their software on that user’s desired platform, thereby showing the user the middle finger. That’s just wrong.

Re:Catching integer overflows in C by jchw on Wednesday August 22, 2018 @02:32AM (#17816784)
When it comes to anything that isn't embedded work, I only care about platforms that support GCC or Clang. I'd happily take an improvement to my development workflow at the cost of dropping support for fringe users. Making truly portable C or C++ code is an exercise in utter pointlessness, and is usually impractical for non-trivial software, be it a desktop application, an operating system kernel, and a lot of things in between. By marketshare an extreme majority of computers and phones in the world can be targeted with Clang alone. Giving up useful compiler extensions to gain compatibility for a platform used by 0.0001% of computer users seems very pointless. I'd rather be concerned with making my software accessible to those with visual impairments or other disabilities, a way, way huger demographic, one that you can't choose to not be a part of.
Re:Catching integer overflows in C by Annatar on Tuesday August 28, 2018 @04:13PM (#17862442)
Well we disagree vehemently: writing clean C code that compiles with any ANSI C compiler is pretty damn easy. Take BIND for example, a non-trivial piece of software which compiles with any compiler.
Re:Catching integer overflows in C by hossbeast on Wednesday August 22, 2018 @02:24AM (#17816746)
Not at all. You wrap it in an ifdef macro by platform.
Re:Catching integer overflows in C by Annatar on Wednesday August 22, 2018 @03:16AM (#17816953)
That’s a solution of last resort, when all else has failed, an act of desperation: whosoever does that is saying: “I don’t know how to express this in a generic fashion”, or “it’s too much work to do it that way and I don’t want to deal with it”. Since the very reason we get paid as programmers is to deal with it, I argue that such an act constitutes a breach of grounds on which we are being paid.
Re:Catching integer overflows in C by Mikhail_Edoshin on Wednesday August 22, 2018 @04:01AM (#17817127)
By generically you mean "in C"? Sometimes the functionality is there, but there's no way to express it in C. Integer overflows are exactly such a case; many processors provide a built-in overflow detection mechanism, but there's no way to access it in C. (It's not even a special mechanism, it's just the command itself, in assembly you clearly see the overflow.) I'd say that using compiler-specific functions (provided they plug into these processor commands) or assembly is exactly the right way to deal with this issue. Yes, parts of the code will be non-portable, but if they're clearly isolated and marked as machine-specific, then why not? The pros certainly outweigh the cons.
Re:Catching integer overflows in C by Annatar on Wednesday August 22, 2018 @06:36AM (#17817695)
I had exactly that happen when compiling PowerDNS: the developers relied on a GCC intrinsic, so I had to embark on an epic journey of building and packaging a newer version of GCC.

Just to be able to serve DNS answers.

That’s just so wrong.