I'm afraid you might not understand how promotion works in C. Promotion
has nothing do to with the word size of the machine it's running on.
Within the expression, all intermediate values and literals are promoted
to the smallest type that can contain the largest value/literal in the
expression. As a general rule numeric literals (actual numbers typed
in) are the default integer size of the machine (32 bits on most modern
processors and compilers). This can be changed with pragmas or command
line options on most C compilers.
This may or may not be promoted or demoted to store the result in its
final destination.
For example:
#include "stdint.h"
int foo( void )
{
uint32_t Long1 = 10;
uint32_t Long2 = 20;
uint16_t Short1 = 10;
unit16_t Short2 = 20;
uint8_t Byte1 = 10;
uint8_t Byte2 = 20;
//
// Everything to the right of the equals is promoted to a uint32,
the math will be performed and then the result will be truncated to a
uint8_t when assigned.
//
// This may also generate a compiler warning due to not
typecasting the result on the right side of the equals.
//
Byte1 = Short1 + Long1;
//
// Everything to the right of the equals will be promoted to a
uint16, the math will be performed and then the result will be promoted
to a uint32 when assigned
//
Long1 = Short1 + Byte1;
//
// Everything to the right of the equals will not be promoted at
all, the math will be performed and the result will be promoted to a
uint16 when assigned.
//
Short1 = Byte1 + Byte2;
Generally numeric literals (actual numbers typed in) are the default
integer size of the machine (32 bits on most modern processors and
compilers). This can be changed with pragmas or command line options on
most C compilers.
I hope this clears things up?
On 8/15/2024 2:41 PM, ben via cctalk wrote:
On 2024-08-15 11:00 a.m., Paul Koning via cctalk
wrote:
The short answer is "it's historic and
manufacturers have done it in
different ways".
You might read the original paper on the topic, "On holy wars and a
plea for peace" by Danny Cohen (IEN-137, 1 april 1980):
https://www.rfc-editor.org/ien/ien137.txt Not reading the paper, I would say it is
more the case having short
data types (little) and FORTRAN packing 4 characters in word (big).
I don't know about the VAX,but my gripe is the x86 and the 68000 don't
automaticaly promote smaller data types to larger ones. What little
programming I have done was in C never cared about that detail.
Now I can see way it is hard to generate good code in C when all the
CPU's are brain dead in that aspect.
char *foo, long bar;
... foobar = *foo + bar
is r1 = foo
r3 = * r1
r2 = bar
sex byte r3
sex word r3
r4 = r3 + r2
foobar = r3
what I want is
bar = * foo + bar
nice easy coding.
And yes, different computers have used different ordering, not just
characters-in-word ordering but bit position numbering. For example,
very confusingly there are computers where the conventional numbering
has the lowest bit number (0 or 1) assigned to the most significant
bit. The more common numbering of 0 for the LSB gives the property
that setting bit n in a word produces the value 2^n, which is more
convenient than, say, 2^(59-n).
Real computers are 2^36 from the 50's.
Big iron is the 60's. :)
paul