News for hackers. Stuff that matters.
Re:Undefined behavior in C is a reading error by vyodaiken on Friday May 21, 2021 @01:40PM (#27237272)
And switch the i to a size_t and get vector code without the possibility of writing to random memory because your int overflows and GCC wants to pretend it cannot.

This is a poorly written loop. C design model is that if it is not critical, we don't care, and if it is, the programmer should fix it so optimization can work. https://gcc.godbolt.org/z/ErMP4cn6s

Re:Undefined behavior in C is a reading error by tom_mellior on Friday May 21, 2021 @04:45PM (#27239739)
You changed writes to indices offset..offset+15 to writes to indices 0..15.
Re:Undefined behavior in C is a reading error by vyodaiken on Friday May 21, 2021 @04:59PM (#27239898)
the offset is used to compute the index, not the count.
Re:Undefined behavior in C is a reading error by tom_mellior on Friday May 21, 2021 @05:08PM (#27239991)
But you're not using the offset to compute the index in bar().

    void foo(int offset, int *arr) {
        for(int i = offset; i < (offset + 16); i++) {
            arr[i] = i + 32;
        }
    }
If you call this with offset = 100, the arr[i] in the loop will write to arr[100], arr[101], ..., arr[115].

    void bar(unsigned int offset, int *arr) {
        if(offset+16 < offset)fail();
        for(size_t i = 0; i < 16; i++) {
            arr[i] = i+ offset + 32;
        }
    }
If you call this with offset = 100, the arr[i] in the loop will write to arr[0], arr[1], ..., arr[15].
Re:Undefined behavior in C is a reading error by vyodaiken on Friday May 21, 2021 @05:59PM (#27240499)
Fixed it, but same kind of result

https://gcc.godbolt.org/z/hx1zjE5xW

Re:Undefined behavior in C is a reading error by tom_mellior on Friday May 21, 2021 @06:09PM (#27240596)
Nice. If you like, could you explain again what the perceived difference is to adding

    if (offset >= INT_MAX - 16) fail();
in foo()?

(I mean, besides the fact that the size of the buffer pointed to by arr is highly unlikely to agree exactly with either INT_MAX or UNIT_MAX.)

Re:Undefined behavior in C is a reading error by vyodaiken on Friday May 21, 2021 @07:43PM (#27241379)
I wanted to show that we don't need UB justified deletes to get good code generation. There was no need to break all that working code when we could have just told people that size_t counters worked better in loops on x86-64 than ints. A lot of C optimization could work that way - relying on cooperation between the compiler and programmers. Java can't do that because Java programmers rely on complex abstractions that need a lot of compiler work to run fast.
Re:Undefined behavior in C is a reading error by tom_mellior on Saturday May 22, 2021 @04:58AM (#27244823)
> A lot of C optimization could work that way - relying on cooperation between the compiler and programmers.

That is precisely how it works already. The reason your code has no bounds checks is exactly because the compiler can assume that you have done your part and ensured that all indices are in bounds. This is what "the compiler can ignore UB" is all about.

The signed integer kerfuffle is just the same: The compiler assumes your cooperation in ensuring, beforehand, that your signed arithmetic never overflows. Its part of the bargain is generating the best possible code it can. Another part of its bargain is offering you the -fwrapv flag to communicate more about your expectations. A third part of the bargain is offering you sanitizers that can inform you that you have done something you probably didn't want.

Re:Undefined behavior in C is a reading error by vyodaiken on Saturday May 22, 2021 @10:57AM (#27247118)
The problems with that argument are 1) The expectations changed with no notice. You can say it always was that way, but that's just not correct. The bounds check worked and then didn't, no matter what you think the standard "always said" (and the UB experts on the WG14 often find it impossible to say exactly what provisions mean, so claims that all this was ever clear are also wrong.) 2) deleting overflow check reduces the power of the language. The supposed work arounds are painful and have edge cases. 3) the example, and others, show that much UB "we assume it can't happen" "optimization" is unnecessary. You make the language more difficult to use, more prone to unpleasant surprise, and in return you provide an "optmization" that could easily be produced by other means. You're insisting on using a hammer as a fork and annoyed when people don't find it convenient.