[OT] The Usual Arithmetic Confusions

Walter Bright newshound2 at digitalmars.com
Sat Feb 5 00:46:49 UTC 2022


On 2/4/2022 3:55 PM, Elronnd wrote:
> On Friday, 4 February 2022 at 23:43:28 UTC, Walter Bright wrote:
>> The penalty for byte arithmetic is the shortage of registers.
> On 64-bit, there are as many byte registers as word registers. (More, 
> technically, but the high-half registers should be avoided at all costs.)

Access to those byte registers requires an additional REX byte.


>> That's fine unless you're using a systems programming language, where the 
>> customers expect performance.
> If a customer wants int ops to be generated, they can use ints. There is nothing 
> preventing them from doing this, as has been pointed out else-thread.

Actually, they'd need to insert casts to int for subexpressions. This is not 
going to be appealing.


>>> 3. Your code example actually does exactly what you suggest--using short 
>>> arithmetic for storage.
>>
>> The load instructions still use the extra operand size override bytes.
> 
> I do not follow.  Your post said:
> 
>> Generally speaking, int should be used for most calculations, short and byte 
>> for storage.
> 
> How am I to store shorts without an operand-size override prefix?

Consider more complex expressions than load and store.


>>> 4. (continued from 3) in a larger, more interesting expression, regardless of 
>>> language semantics, the compiler will generally be free to use ints for 
>>> intermediates.
>>
>> If it does, then you'll have other truncation problems depending on how the 
>> optimization of the expression plays out. Unless you went the x87 route and 
>> slowed everything down by truncating every subexpression to short.
> 
> Example: ubyte x,y,z,w; w = x + y + z.
> 
> (((x + y) mod 2^32 mod 2^8) + z) mod 2^32 mod 2^8 is the same as (((x + y) mod 
> 2^32) + z) mod 2^32 mod 2^8.  The mod 2^32 are implicit in the use of 32-bit 
> registers; the mod 2^8 are explicit truncation.  The former form, with two 
> explicit truncations, can be rewritten as the latter form, getting rid of the 
> intermediate truncation, giving the exact same result as with promotion.

Consider:

     byte a, b;
     int d = a + b;

You're going to get surprising results with your proposal.


More information about the Digitalmars-d mailing list