Re:Porting to GCC 14: C language issues
by hgs3 on Monday February 19, 2024 @11:34AM
(#39431684)
I was always a little surprised C never had an integer type the size of a native word. Probably the closest thing would be intptr_t since pointers presumably use a single word to represent addressable memory.
[ Parent
| on HN ]
Re:Porting to GCC 14: C language issues
by flohofwoe on Monday February 19, 2024 @12:29PM
(#39432325)
AFAIK until the switch to 64-bit architectures, int actually was the natural word size. Keeping int at 32-bits was probably done to simplify porting code to 64-bits (since all structs with ints in them would change their memory layout - but that's what the fixed-width integer types are for anyway, e.g. int32_t).
In hindsight it would probably have been better to bite the bullet and make int 64 bits wide.
Re:Porting to GCC 14: C language issues
by dataflow on Monday February 19, 2024 @02:38PM
(#39433780)
> AFAIK until the switch to 64-bit architectures, int actually was the natural word size
32-bit int is still arguably the native word size on x64. 32-bit is the fastest integer type there. 64-bit at the very least often consumes an extra prefix byte in the instruction. And that prefix is literally called an "extension" prefix... very much the opposite of native!
32-bit also often has the prefix byte (if one of the operands is r8-r15 or, for extending moves from 8-bit registers, r4-r15)
Re:Porting to GCC 14: C language issues
by Karellen on Tuesday February 20, 2024 @07:07AM
(#39440286)
Aren't 32-bit registers/operations also called "extensions" of their 16-bit counterparts on the x86 line, due to the ISA's 16-bit 8086/80286 lineage?
So could one make the argument that a 16-bit int ought to be the native word size on x64?
Re:Porting to GCC 14: C language issues
by dataflow on Tuesday February 20, 2024 @08:57AM
(#39441299)
No. This isn't about dictionary pedantry. 16-bit is actually frequently more expensive than 32-bit on x86.
Re:Porting to GCC 14: C language issues
by garaetjjte on Monday February 19, 2024 @12:56PM
(#39432662)
int being originally native word size is the reason for weird integer promotion rules.
Re:Porting to GCC 14: C language issues
by flohofwoe on Tuesday February 20, 2024 @06:08AM
(#39439918)
IMHO it's only weird because the promotion is to 32-bit integers on 64-bit platforms. If all math would happen on the natural word size (e.g. 64-bits), and specific integer widths would only matter for memory loads/stores, it would be fine.
Re:Porting to GCC 14: C language issues
by edflsafoiewq on Monday February 19, 2024 @05:20PM
(#39435528)
int is the smallest type you can do ALU ops on, so as long as x64 can still do 32-bit arithmetic, it's "natural" for it to remain 32 bits.
Re:Porting to GCC 14: C language issues
by zozbot234 on Monday February 19, 2024 @11:57AM
(#39431979)
> pointers presumably use a single word to represent addressable memory.
Only in flat address spaces, which excludes platforms like old 16-bit x86 or modern CHERI. There, "pointer difference within a single object" need not be the same size as "pointer reference".
Re:Porting to GCC 14: C language issues
by WalterBright on Monday February 19, 2024 @11:40AM
(#39431776)
size_t and ptrdiff_t works for me.