News for hackers. Stuff that matters.
Re:C and C++ Prioritize Performance over Correctness by philosopher1234 on Friday August 18, 2023 @12:56PM (#37178515)
>For example, a common thing programmers expect is that you can test for signed integer overflow by checking whether the result is less than one of the operands, as in this program:

    #include <stdio.h>

    int f(int x) {
        if(x+100 < x)
            printf("overflow\n");
        return x+100;
    }
>Clang optimizes away the if statement. The justification is that since signed integer overflow is undefined behavior, the compiler can assume it never happens, so x+100 must never be less than x. Ironically, this program would correctly detect overflow on both ones'-complement and two's-complement machines if the compiler would actually emit the check.

My god…

Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @01:27PM (#37179089)
I think what really annoys me is that this looks like actual malice on the part of the standards writers, and less severe malice on the part of the compiler authors.

I know I should attribute it to stupidity instead but ...

The standards writers could have made all UB implementation defined, but they didn't. The compiler authors could have made all Uab implementation defined, and they didn't.

Take uninitialised memory as an example, or integer overflow:

The standard could have said "will have a result as documented by the implementation". They didn't.

The implementation can choose a specific behaviour such as "overflow results depend on the underlying representation" or "uninitialised values will evaluate to an unpredictable result."

But nooooo... The standard keeps adding more instances of UB in each revision, and the compiler authors refuse to take a stand on what overflow or uninitialised values should result in.

Changing the wording so that all UB is now implementation defined does not affect blegacy code at all, except by forcing the implementation to document the behaviour and preventing a compiler from simply optimising out code.

Re:C and C++ Prioritize Performance over Correctness by nottorp on Friday August 18, 2023 @01:45PM (#37179385)
> less severe malice on the part of the compiler authors.

This is optimization run amok and I'd call it malice. If C/C++ are designed to let me shoot myself in the foot, the compiler should let me shoot myself in the foot instead of erasing my code.

Re:C and C++ Prioritize Performance over Correctness by torstenvl on Friday August 18, 2023 @01:51PM (#37179484)
The standard does make all definable UB implementation defined. The compiler writers intentionally misread the standard to allow these kinds of optimizations.

C17 Draft, § 3.4.3: "Possible undefined behavior ranges from...

. . . ignoring the situation completely with unpredictable results, to . . .

. . . behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message) . . .

. . . to terminating a translation or execution (with the issuance of a diagnostic message)."

Compiler writers like to language-lawyer this in two ways. First, they interpret "possible" to mean that this is a non-exhaustive list, but that isn't how interpretation of such a document typically works. Expressio unius est exclusio alterius -- the express inclusion of one is the exclusion of others -- means that any list is exhaustive unless explicitly indicated otherwise. In other words, the reality is that, according to the standard, these are the only possible options.

Second, they interpret "ignoring the situation completely" to mean "actively seeking out the situation and deleting whole sections of code based on that." This is quite self-evidently a dishonest interpretation.

Re:C and C++ Prioritize Performance over Correctness by cesarb on Friday August 18, 2023 @02:40PM (#37180189)
The optimization opportunities do indeed come from the first option, "ignoring the situation completely with unpredictable results". That is: the compiler assumes that the undefined behavior will not happen, and optimizes based on it. They do not "actively seek out the situation", it's in fact the opposite, they assume that the situation simply won't happen. And "deleting whole sections of code" is just the normal "unreachable code" optimization: code which cannot be reached on any feasible execution path will be deleted.
Re:C and C++ Prioritize Performance over Correctness by torstenvl on Friday August 18, 2023 @03:11PM (#37180628)
That is an obvious misreading.

Assume you have an unsigned char array r[] declared with 100 elements and the compiler encounters the following line of code:

r[200] = 0xff;

"ignoring the situation completely with unpredictable results" means translating that to the appropriate object code without bounds checking and no guarantees are made about whether that's a segfault or what your environment does.

"assum[ing] that the situation simply won't happen" means eliding the code entirely.

The first is required by the standard. The second is prohibited by the standard.

There is no section of any version of the standard that permits compilers to assume that anything which might be UB or predicated on UB is dead code.

Compilers must either ignore the fact that it's UB, or behave in a documented implementation-defined manner, or stop translation.

Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @02:05PM (#37179689)
While you have convinced me that the compiler authors are more culpable than the standards committee, I still feel that they are ultimately responsible - the buck stops with them.

They're free to, and easily able to, add the word "exhaustively" when listing possible behaviours, but they don't. They can even do away with it entirely and replace it with implementation-defined, which makes compilers non-conformant if they optimise out code.

Re:C and C++ Prioritize Performance over Correctness by pdw on Friday August 18, 2023 @03:23PM (#37180785)
You're quoting from a footnote. The footnotes are not part of the normative text, so how you choose to interpret it is irrelevant.

The actual definition of undefined behavior given by § 3.4.3 is: "behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements."

Since no requirement is imposed, compilers can do as they want.

Re:C and C++ Prioritize Performance over Correctness by torstenvl on Friday August 18, 2023 @03:32PM (#37180920)
> You're quoting from a footnote. The footnotes are not part of the normative text

I am not quoting from a footnote.

Footnote 1 pertains to § 1.

Footnote 2 pertains to § 3.19.5.

There are no footnotes pertaining to § 3.4.3.

And do you have a citation for the proposition that footnotes or notes of any other kind are not normative?

Re:C and C++ Prioritize Performance over Correctness by pdw on Friday August 18, 2023 @04:05PM (#37181361)
Apologies, it's a note, not a footnote. But notes in ISO standards aren't normative either.

This is a general principle of ISO standards, see https://share.ansi.org/Shared%20Documents/Standards%20Activi...:

> 6.5 Other informative elements

> 6.5.1 Notes and examples integrated in the text

> Notes and examples integrated in the text of a document shall only be used for giving additional information intended to assist the understanding or use of the document. They shall not contain requirements ("shall"; see 3.3.1 and Table H.1) or any information considered indispensable for the use of the document, e.g. instructions (imperative; see Table H.1), recommendations ("should"; see 3.3.2 and Table H.2) or permission ("may"; see Table H.3). Notes may be written as a statement of fact.

Re:C and C++ Prioritize Performance over Correctness by torstenvl on Saturday August 19, 2023 @12:26AM (#37185503)
I don't view "You shall not do this" and "If you do this, it's meaningless" as identical statements.

Besides, clang does the same thing with -std=c89, where this provision was emphatically not a note.

Re:C and C++ Prioritize Performance over Correctness by kelnos on Friday August 18, 2023 @01:51PM (#37179491)
I think it's a bit out of line to call this malice.

The C standard was written when people wanted a language that was easy to read and write, but could have similar performance to hand-written assembly. Performance, as the article title points out, was a bigger priority than correctness or eliminating foot guns.

Most of us here weren't around when many/most programs were written in assembly, and when machines were very constrained in how much memory they had and how many CPU cycles they could spare. Nowadays, performance concerns usually come behind speed and correctness of development. So it's hard to truly feel what trade offs the original designers of C (and the original C89 standards committee) had to make.

I'm not convinced implementation-defined behavior is really any better, though. Packagers and end-users should not have to ensure that the code their compiling is "compatible" with the compiler or hardware architecture they want to use. Developers shouldn't have to put a "best compiled with FooC compiler" or "only known to work on x86-64". That would not be an improvement.

I do agree that, these days, there's no excuse for writing language specifications that include undefined behavior. But C has a lot of baggage.

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Friday August 18, 2023 @02:14PM (#37179829)
That is usually a myth.

From the people that were there at the time.

"Oh, it was quite a while ago. I kind of stopped when C came out. That was a big blow. We were making so much good progress on optimizations and transformations. We were getting rid of just one nice problem after another. When C came out, at one of the SIGPLAN compiler conferences, there was a debate between Steve Johnson from Bell Labs, who was supporting C, and one of our people, Bill Harrison, who was working on a project that I had at that time supporting automatic optimization...The nubbin of the debate was Steve's defense of not having to build optimizers anymore because the programmer would take care of it. That it was really a programmer's issue.... Seibel: Do you think C is a reasonable language if they had restricted its use to operating-system kernels? Allen: Oh, yeah. That would have been fine. And, in fact, you need to have something like that, something where experts can really fine-tune without big bottlenecks because those are key problems to solve. By 1960, we had a long list of amazing languages: Lisp, APL, Fortran, COBOL, Algol 60. These are higher-level than C. We have seriously regressed, since C developed. C has destroyed our ability to advance the state of the art in automatic optimization, automatic parallelization, automatic mapping of a high-level language to the machine. This is one of the reasons compilers are ... basically not taught much anymore in the colleges and universities."

-- Fran Allen interview, Excerpted from: Peter Seibel. Coders at Work: Reflections on the Craft of Programming

Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @02:21PM (#37179937)
I fail to see to the point of your post in context of this thread.

What, in the parent post, do you consider a myth?

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Friday August 18, 2023 @02:28PM (#37180028)
C's original performance.

Optimization taking advantage of UB had to be introduced during the 1990's to make it into a reality.

Until the widespread of 32 bit hardware, even junior Assembly devs could relatively easy outperform C compilers.

Which is what the interview is about, originally C lacked the optimization tools, relying on developers, with tons of inline Assembly.

Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @02:44PM (#37180261)
> Which is what the interview is about, originally C lacked the optimization tools, relying on developers, with tons of inline Assembly.

I'm afraid that is not the conclusion I draw from the snippet you posted.

It's very clear to me that the HLL under question at that time prevented the programmer from performing the low-level optimisations that the C programmer could optionally do. The debate appeared to be be whether to let the language exclusively optimise the code, or just do no optimisation and let the programmer do it.

This is why the sentence ": Oh, yeah. That would have been fine. And, in fact, you need to have something like that, something where experts can really fine-tune without big bottlenecks because those are key problems to solve. " is in there - it's because the HLL languages literally wouldn't let the programmer optimise at the level that C would let the programmer optimise.

Honestly, if the proponent for the HLL language didn't add in "Use the HLL, but not for OS and low-level code where you really need to fine-tune", you'd have a point, but they said it, and so you don't.

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Friday August 18, 2023 @04:03PM (#37181337)
Right, that is why C code was polluted with inline Assembly until optimizers started taking advantage of UB.

Inline Assembly isn't C.

Re:C and C++ Prioritize Performance over Correctness by memefrog on Friday August 18, 2023 @08:44PM (#37184168)
Of course inline assembly is C. Most languages don't let you do inline assembly. That isn't because they don't like it, but because the way those languages get translated to machine code is highly variable. C is the only language where the semantic model is simple enough that you can see how every language construct would translate to assembly language and write your own inline assembly language in between.
Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @02:59AM (#37186275)
It definitely isn't.

What page of C standard describes it?

As for the rest of your comment, unless you are using a PDP-11, you will be surprised.

Re:C and C++ Prioritize Performance over Correctness by memefrog on Saturday August 19, 2023 @03:21AM (#37186374)
C predates the C standard, which describes a common subset of C implementations. It is useful for writing code which works across a variety of implementations. For example, you can write code that works on the Byzantine optimising compilers, which will attempt to render your code incorrectly if at all possible, but will make strictly conforming programs run very quickly. But it is not necessary to write code to the C standard, and many C implementations do not conform to it. The Plan9 C compiler, for example, expects 'void main(void)' for a program's main function, while the standard requires 'int main(void)'.
Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @06:19AM (#37187267)
Lets make it easier then, what page of K&R C describes inline Assembly?
Re:C and C++ Prioritize Performance over Correctness by memefrog on Saturday August 19, 2023 @06:36AM (#37187360)
[delayed]
Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @10:04AM (#37188900)
Likewise compiler specific extensions don't define universal truths about languages, that aren't part of the respective standard.

Had you been clever, you would have pointed out that inline Assembly is defined in the ANSI C++ standard, with the mention of asm (content) language construct, leaving the meaning of content implementation defined.

As it is also defined in the Ada language standard, Modula-2 standard revision, and Oakwood Guidelines for Oberon-2 implementation.

That is the thing mixing up C with compiler specific features, confusing the language for its standard and assuming no other language in the planet offers similar capabilities.

Re:C and C++ Prioritize Performance over Correctness by memefrog on Saturday August 19, 2023 @10:16AM (#37189021)
I am not remotely interested in what the ISO standard says about C, and I am even less interested in what the ANSI standard says about C++, a completely different language.

C is not defined by or constrained to the standard. It predates it and every implementation extends it, as is intended by the standard.

The person confusing the standard with the language is you.

Re:C and C++ Prioritize Performance over Correctness by defrost on Saturday August 19, 2023 @06:36AM (#37187361)
The more important detail is which "standard C" recognises inline asm as a common as dirt C extension.

Page 514 (of version n1256) of the ISO C Standard

Moving target, to be, sure, but it's been there for 20+ years now.

Annex J.5.10 The asm keyword

to quote a stack overflow answer (that sounds a lot like an answer commonly given back in the day on Usenet and #C IRC channels)

     informative, not normative, so an implementation need not provide inline assembly, and if it does it's not prescribed in which form. But it's a widespread extension, though not portable since compilers do indeed implement it differently.
https://www.open-std.org/JTC1/SC22/WG14/

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

Re:C and C++ Prioritize Performance over Correctness by aidenn0 on Saturday August 19, 2023 @01:22AM (#37185786)
My recollection is it was trying to keep up with Fortran's performance on numeric code as an impetus for taking advantage of UB (variations on what eventually became the "restrict" keyword were discussed but rejected for c89 and that idea comes directly from Fortran, which can assume arguments do not alias)
Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @02:01PM (#37179630)
> I think it's a bit out of line to call this malice.

I apologise, but note (warning: weasel words follow) I was careful to say it looks like malice to me, not that I have any indication that it actually was malice.

> I'm not convinced implementation-defined behavior is really any better, though. Packagers and end-users should not have to ensure that the code their compiling is "compatible" with the compiler or hardware architecture they want to use.

I think it is better, purely because then the resulting code can't simply be omitted when an integer may overflow, the code still has to be emitted by the compiler.

Right now all the worst bits of UB has to do with the compiler optimising out code. With IB replacing UB, the implementation will have to pick a behaviour and then stick with it. Much safer.

Re:C and C++ Prioritize Performance over Correctness by layer8 on Friday August 18, 2023 @03:37PM (#37181004)
> The standards writers could have made all UB implementation defined, but they didn't.

This is not possible without performance impact, because, in the general case, whether a program constitutes UB depends on data and/or previous program flow (halting problem), and thus would require additional runtime checks even for code that happens to never run into UB.

Re:C and C++ Prioritize Performance over Correctness by AlotOfReading on Friday August 18, 2023 @01:16PM (#37178874)
What the article misses is that the code wouldn't work on all hardware. Take MIPS for example, where the signed overflow would generate a hardware exception that the OS might have implemented to do anything from nothing to killing the generating process.

C was never standardized solely on the basis of what's most performant. The vast majority of explicit UB is there because someone knew of a system or situation where that assumption wasn't true.

Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @01:32PM (#37179149)
> C was never standardized solely on the basis of what's most performant. The vast majority of explicit UB is there because someone knew of a system or situation where that assumption wasn't true.

So? They could have called it implementation defined. The reasoning you present was broken then, as it is broken now.

Your MIPS example displays no reason for UB to exist.

Re:C and C++ Prioritize Performance over Correctness by AlotOfReading on Friday August 18, 2023 @02:01PM (#37179629)
Implementation defined means it has a defined behavior on every implementation. In the MIPS case defining that behavior would force the compiler to generate strictly signed instructions for signed values and define how the runtime platform handles these interrupts, rather than leaving the compiler free to generate "whatever works".

Look, a lot of UB is frankly stupid and shouldn't be in a modern language spec, including signed overflow. I'm not defending that, only giving an example where you have to break compatibility to eliminate it.

Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @02:14PM (#37179819)
> In the MIPS case defining that behavior would force the compiler to generate strictly signed instructions for signed values and define how the runtime platform handles these interrupts,

I respectfully disagree: in this particular scenario, it's enough for the compiler vendor to write "generates an interrupt on overflow" with no indication of what the handling should be, and still be well within implementation-defined behaviour.

After all, the standard specifies what `raise()` does, and what `signal()` does, but doesn't specify how the runtime is going to handle `raise(signum)` when the program has not yet called `signal(signum, fptr)`.

This scenario you presented displays exactly why UB should be replaced with IB in the standard. I've yet to see one good reason (including performance reasons) for why the dereferencing of a NULL pointer (for example) cannot be documented for the implementation.

With UB, we have real-world examples of a NULL pointer dereference causing compilers to omit code resulting in a program that continued to run but with security implications. If changed to IB, the compiler would be forced to emit that code and let the dereference crash the program (much better).

Re:C and C++ Prioritize Performance over Correctness by tempodox on Friday August 18, 2023 @01:15PM (#37178861)
This has to be the worst part of UB: That the compiler can assume it never happens. This way UB can affect your program even if the code that would exhibit the behavior is never executed.
Re:C and C++ Prioritize Performance over Correctness by gavinhoward on Friday August 18, 2023 @01:00PM (#37178586)
This is why I implemented two's-complement with unsigned types, that don't have UB on overflow.
Re:C and C++ Prioritize Performance over Correctness by kragen on Friday August 18, 2023 @02:15PM (#37179840)
like x > 2147483547u in this case?
Re:C and C++ Prioritize Performance over Correctness by gavinhoward on Friday August 18, 2023 @02:42PM (#37180212)
Correct. It will still overflow, but it's not UB and won't be optimized away.
Re:C and C++ Prioritize Performance over Correctness by kragen on Friday August 18, 2023 @03:04PM (#37180526)
by 'overflow' do you mean 'set the high bit'
Re:C and C++ Prioritize Performance over Correctness by gavinhoward on Friday August 18, 2023 @03:17PM (#37180696)
Correct.

I have routines that take the unsigned values and interpret them as signed, two's-complement values.

So you would write this:

    val = temp1 + temp2;
    if (y_ssize_lt(val, temp1)) y_trap();
That `y_ssize_lt()` function computes whether `val` is less than `temp1` as though they were signed, two's-complement integers. But because they are actually unsigned, the compiler cannot be malicious and delete that code.
Re:C and C++ Prioritize Performance over Correctness by kragen on Friday August 18, 2023 @03:55PM (#37181226)
i see, thanks

maybe we should develop a nonmalicious compiler

Re:C and C++ Prioritize Performance over Correctness by gavinhoward on Friday August 18, 2023 @09:41PM (#37184593)
Re:C and C++ Prioritize Performance over Correctness by pclmulqdq on Friday August 18, 2023 @01:08PM (#37178716)
This is why people who put "C/C++" on their resume don't usually know either language. In C++, integers are defined to be two's complement now, so that check is acceptable.
Re:C and C++ Prioritize Performance over Correctness by woodruffw on Friday August 18, 2023 @01:15PM (#37178844)
That, or they know that C23 includes two’s complement[1].

[1]: https://en.cppreference.com/w/c/23

Re:C and C++ Prioritize Performance over Correctness by pdw on Friday August 18, 2023 @01:41PM (#37179310)
Integer overflow is still undefined behavior in C23.
Re:C and C++ Prioritize Performance over Correctness by rsc on Friday August 18, 2023 @01:09PM (#37178741)
Not true, as the post explains.
Re:C and C++ Prioritize Performance over Correctness by pclmulqdq on Friday August 18, 2023 @01:17PM (#37178901)
C++20 made the switch. See P1236. It appears C23 has followed suit.
Re:C and C++ Prioritize Performance over Correctness by uecker on Friday August 18, 2023 @01:40PM (#37179299)
Two's complement does not mean signed overflow became defined. So the check is still wrong. And no, I do not think making it defined would lead to more correct programs. You then simply have really difficult wrap-around bugs instead of signed overflow which you can be found more easily with UB sanitizer or turn into traps at run-time.
Re:C and C++ Prioritize Performance over Correctness by rsc on Friday August 18, 2023 @01:32PM (#37179167)
If P1236 (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p12...) is really what it says ("alternative wording") then I don't believe that's true. Certainly P0907R3 (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p09...) is clear:

> Status-quo If a signed operation would naturally produce a value that is not within the range of the result type, the behavior is undefined. The author had hoped to make this well-defined as wrapping (the operations produce the same value bits as for the corresponding unsigned type), but WG21 had strong resistance against this.

My understanding is that the 2s complement change in C++20 ended up defining bitwise operations but not arithmetic.

Re:C and C++ Prioritize Performance over Correctness by e4m2 on Friday August 18, 2023 @01:28PM (#37179102)
It made the switch, sure, but overflow remains undefined.
Re:C and C++ Prioritize Performance over Correctness by boryas on Friday August 18, 2023 @01:00PM (#37178582)
-fwrapv and it's all good :)
Re:C and C++ Prioritize Performance over Correctness by not2b on Friday August 18, 2023 @01:08PM (#37178721)
It really isn't optional as a C developer to turn on warnings and get your code warning-free. This will eliminate the most common unbounded behavior issues, like uninitialized variables, though unfortunately not all of them.
Re:C and C++ Prioritize Performance over Correctness by boryas on Thursday August 24, 2023 @05:10PM (#37254594)
linux, qemu, postgres and many other projects written by "C developers" use and rely on fwrapv, fno-strict-aliasing and others.
Re:C and C++ Prioritize Performance over Correctness by not2b on Friday August 25, 2023 @01:07AM (#37258285)
They also turn on warnings, so they catch uninitialized variables and the like.
Re:C and C++ Prioritize Performance over Correctness by pdw on Friday August 18, 2023 @01:47PM (#37179419)
This is why I hate it when people describe C as "portable assembler". The Usenet comp.lang.c FAQ was already thirty years ago warning people not to write overflow checks like this.
Re:C and C++ Prioritize Performance over Correctness by coliveira on Friday August 18, 2023 @01:47PM (#37179422)
You cannot check for overflow like this, because you're causing it. There are other ways to do this without adding first:

  #include <limits.h>

  int safe_add(int a, int b) {
    if (a > 0 && b > INT_MAX - a) {
        /* deal with overflow... */
    } else if (a < 0 && b < INT_MIN - a) {
        /* deal underflow.. */
    }
    return a + b;
  }
Re:C and C++ Prioritize Performance over Correctness by zer8k on Friday August 18, 2023 @02:28PM (#37180027)
I'm not convinced a "common" C program would ever do such a thing. Even the "Effective C" book (one of the best books on C, imo) discusses why this is wrong and why you should take advantage of `limits.h` for bounds checking.

This is just bad programming. The compiler, like usual, is correct because you're not in reality checking anything. You've made an obviously non-sensical statement that the overflowed value will be less than the value. Compiler optimizes it away. You can argue the semantics of UB here but this particular UB is borderline a solved problem in any practical sense.

To be fair, a static analyzer should be able to catch this.

Re:C and C++ Prioritize Performance over Correctness by _dain_ on Friday August 18, 2023 @03:01PM (#37180495)
>You've made an obviously non-sensical statement—

Therefore it should fail to compile.

You can even spin it as a performance enhancement: the whole program can be optimized away!

Re:C and C++ Prioritize Performance over Correctness by layer8 on Friday August 18, 2023 @03:32PM (#37180918)
The problem is, that’s not how the logical inference in a compiler/optimizer works. It’s very difficult to translate such an optimization back to “this statement has been optimized away”, in the general case.
Re:C and C++ Prioritize Performance over Correctness by _dain_ on Friday August 18, 2023 @03:36PM (#37180994)
If it's so difficult to figure out if the consequences of their optimization game are sensible or not, then they shouldn't play it in the first place.

Who actually wants it to behave this way, other than compiler engineers competing on microbenchmarks? Who is this language even for?

Re:C and C++ Prioritize Performance over Correctness by layer8 on Friday August 18, 2023 @03:40PM (#37181027)
It’s a side effect of desirable optimizations for UB-free code. You can’t have both at the same time.
Re:C and C++ Prioritize Performance over Correctness by _dain_ on Friday August 18, 2023 @03:48PM (#37181131)
Desirable for whom? Are the beneficiaries of it going to pay for the externalities they are generating, like polluters should?

You can say "ordinary workaday programmers benefit from speed improvements en passant", but they weren't really given a choice in the matter, were they? When programmers are given an explicit binary choice of "correct, but slightly slower", and "wrong, but slightly faster", they pick the former in practically all cases (or they should, at any rate). But they can't make this choice; the compiler and spec writers go behind their backs and construct these inscrutable labyrinths, then blame everyone else for getting lost in them.

Re:C and C++ Prioritize Performance over Correctness by aidenn0 on Saturday August 19, 2023 @01:18AM (#37185761)
-fwrapv is there for people to use. People don't use it.

Yes defaults matter, but they matter both ways. People benchmark with the default options and a compiler with -fwrapv turned on by default will lose those benchmarks and the "ordinary workaday programmers" still end up with trading correctness for speed, since one person at their workplace ran one benchmark once and picked the compiler that won in speed.

Re:C and C++ Prioritize Performance over Correctness by raverbashing on Saturday August 19, 2023 @01:18AM (#37185762)
If anyone tried to pull this kind of language lawyering in my job they would be fired

Maybe it's about time to fire the C compiler developers