[Info-vax] Rust as a HS language, was: Re: Quiet?
Arne Vajhøj
arne at vajhoej.dk
Fri Apr 8 12:46:59 EDT 2022
On 4/8/2022 12:13 PM, Johnny Billquist wrote:
> On 2022-04-08 15:33, Arne Vajhøj wrote:
>> But I was talking about something different. That the
>> C code and the generated instructions sort of "match".
>>
>> If you in C write:
>>
>> c = a + b;
>>
>> then you expect that it does an addition of
>> two pieces of data (integer or FP of some size
>> determined by declaration) and not anything else.
>>
>> In languages with operator overload and extension methods
>> that code may download a bitcoin mining program and
>> run it for 10 minutes.
>
> Actually, you might get surprised about what C compilers might do with
> this in some circumstances. If the size of the destination (c) is
> smaller than a and/or b, and the compiler knows that the result is going
> to be too large to fit, then you are again in UB. Which means the
> compiler is allowed to do anything, including do nothing at all (even
> though it might just be simple integer operations).
> So that "c = a + b;" might actually end up being optimized away totally.
> Which probably would surprise quite a number of people who wrote such a
> line.
>
> Or, as I had to explain to a colleague of mine at one point.
> If you have two unsigned values multiplied, and the resulting value is
> stored in a signed destination, any check for a value less than zero can
> be optimzied away, because obviously the value can never be less than
> zero. Disregarding the fact that the actual hardware, if giving such a
> multiplication, might wrap, and end up with a value that is actually
> less than zero.
> The colleague had the test in his code, and it never triggered, but
> printing the value out for sure showed that it was less than zero.
>
> The compiler was technically allowed to do that optimization, since it's
> UB. The writer of the code wanted to know if the value had wrapped.
> Sorry, C does not actually allow you to find that out.
Yes.
But my point was not about something being optimized away
but about too much happening under the hood.
Arne
More information about the Info-vax
mailing list