[OT] The Usual Arithmetic Confusions

Steven Schveighoffer schveiguy at gmail.com
Fri Feb 4 22:11:10 UTC 2022


On 2/4/22 4:54 PM, Paul Backus wrote:
> On Friday, 4 February 2022 at 21:13:10 UTC, Walter Bright wrote:
>> The integral promotion rules came about because of how the PDP-11 
>> instruction set worked, as C was developed on an -11. But this has 
>> carried over into modern CPUs. Consider:
>>
> [...]
>>
>> You're paying a 3 size byte penalty for using short arithmetic rather 
>> than int arithmetic. It's slower, too.
>>
>> Generally speaking, int should be used for most calculations, short 
>> and byte for storage.
> 
> Sure. That's a reason why I, the programmer, might want to use int 
> instead of short or byte in my code. But if, for whatever reason, I've 
> chosen to use short or byte in spite of the performance penalties, I 
> would rather not have the language second-guess me on that choice.

Yeah, the user doesn't care how the compiler does the instructions. They 
care about the outcome. If they want to assign it back to a byte, they 
probably don't care about losing the extra precision. Otherwise, they 
would assign to an int.

I don't think anyone is arguing that the result of the operation should 
be truncated to a byte, even if assigned to an int.

-Steve


More information about the Digitalmars-d mailing list