News for hackers. Stuff that matters.
Re:Catch-23: The New C Standard Sets the World on Fire by Gibbon1 on Sunday April 02, 2023 @03:19AM (#35408041)
That's not an argument to keep live grenades laying around, it's an argument to remove them from the spec.

Like signed int being UB. Define it to have 2 complement semantics. Problem solved. I'm sure the nutters trying to extend C++ with templates will howl but this is C not C++. And seriously C++ is dead man walking at this point.

Re:Catch-23: The New C Standard Sets the World on Fire by chongli on Sunday April 02, 2023 @04:59AM (#35408562)
C23 does make two’s complement standard. It also adds checked arithmetic so you can safely avoid signed overflow.

It does not make signed overflow defined behaviour. This would prevent integer operation reordering as an optimization, leading to slower code.

Re:Catch-23: The New C Standard Sets the World on Fire by Gibbon1 on Sunday April 02, 2023 @02:04PM (#35413343)
Yeah but it's reversed signed overflow shouldn't be UB by default. You should have to explicitly opt in for that.

The reason of course why they refuse to do that if because if that were that case most shops would up and ban unsafe signed.

Re:Catch-23: The New C Standard Sets the World on Fire by properparity on Sunday April 02, 2023 @02:29PM (#35413631)
>This would prevent integer operation reordering as an optimization, leading to slower code.

The sane way to address that is to add explicit opt-in annotations like 'restrict'.

  #push_optimize(assume_no_integer_overflow)
  int x = a + b;
  // more performance orientated code
  #pop_optimize
  // back to sane C

  #push_optimize(assume_no_alias(a, b), assume_stride(a, 16), assume_stride(b, 16))
  void compute(float *a, float *b, int index)
  {
   // here the compiler can assume a and b do not alias
   // and it can assume it can always load 16 bytes at a time
   // the programmer has made sure it's aligned and padded to so with any index
   // there's always 16 bytes to load
   // so go on, use any vectorized simd instruction you want
  }
  #pop_optimize
  // back to sane C
Re:Catch-23: The New C Standard Sets the World on Fire by chongli on Sunday April 02, 2023 @02:41PM (#35413757)
That’s a lot uglier and clunkier than just using the ckd_add, ckd_mul etc. safe checked arithmetic. Plus if an overflow occurs you still get an incorrect result which you probably don’t want.

Or maybe I’m wrong? Do people actually want overflows to occur and incorrect results? If they’re willing to tolerate incorrect results, why would they also want optimizations disabled?

Re:Catch-23: The New C Standard Sets the World on Fire by Gibbon1 on Sunday April 02, 2023 @04:45PM (#35415016)
The thing is it's ugly in the rare case that absolute performance is worth fighting for. And not ugly in the majority case where it isn't in the top three important things.
Re:Catch-23: The New C Standard Sets the World on Fire by chongli on Sunday April 02, 2023 @05:13PM (#35415282)
No, GP's proposal is ugly in the majority case. If you're going to make signed overflow defined behaviour then every time you write:

    int c = a + b;
You have to assume it will overflow and give an incorrect result. So now you need to check everything, everywhere, and you don't get any optimizations unless you explicitly ask for them with those ugly #push_optimize annotations. I completely fail to see how this is an advantage.

The way C works right now, the assumption is that you want optimization by default and safety is opt-in. The GP's proposal takes away the optimization by default. It then makes incorrect results the default, but it does not make safety the default. To make safety the default you would have to force people to write conditionals all over the place to check for the overflows with ckd_add, ckd_mul etc. Merely writing:

    int c = a + b;
Does not give you any assurances that your answer will be correct.
Re:Catch-23: The New C Standard Sets the World on Fire by Gibbon1 on Sunday April 02, 2023 @10:15PM (#35417749)
"So now you need to check everything, everywhere"

If you want to write robust code in C that what you need to do. UB doesn't give you runtime checks nor compile time checks for overflow.

"Does not give you any assurances that your answer will be correct."

Your problem is you think C's int is a mathematical integer when it is not. It's an ordered set.

Re:Catch-23: The New C Standard Sets the World on Fire by chongli on Monday April 03, 2023 @12:09AM (#35418455)
You misunderstood what I was saying. Those statements are from the perspective of the hypothetical language proposal I was critiquing. That proposal turns off all the optimizations by default and forces you to add annotations to turn them back on. At the same time, it does not actually give you anything useful for your trouble because it still doesn't solve the problem of signed overflow giving incorrect results.

The way C is now, you get the performance by default and safety is opt-in. That's the tradeoff C makes and it's a good one. Other languages give safety by default and make performance opt-in. The proposal I was responding to gives neither.

Re:Catch-23: The New C Standard Sets the World on Fire by pclmulqdq on Sunday April 02, 2023 @02:58PM (#35413909)
C++ 20 did that too.
Re:Catch-23: The New C Standard Sets the World on Fire by pjmlp on Sunday April 02, 2023 @05:29AM (#35408720)
Until LLVM, GCC, key game engines and GPGPU SDK get rewritten into something else, it is going to be Resident Evil day for a looong time.