🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #1110

Re: reassociation and undefined behaviour

#1110
From: Anton Ertl
Date: Sat, 05 Dec 2026 18:47
74 lines
4457 bytes
David Brown <david.brown@hesbynett.no> writes:
>On 04/12/2026 21:38, MitchAlsup wrote:
>> So, I agree that most people use 'int' when they want the fastest--
>> they just don't get it on most ISAs--due to I32LP64 model.

Use of "int" is idiomatic in C when you just want some integer without
any reason to make it extra-big or extra-small.  That has it's root in
history: B has only one (unnamed) type whose use for integers became
"int" in early C.  Early C also used int if no type was specified,
e.g.:

main(argc,argv)
  char * argv[];
{
  ...
}

The undeclared types of main() and argc defaulted to "int".

>As I understand it, it rarely makes a difference.  But there are 
>circumstances when a wrapping 32-bit type (either uint32_t, or int32_t 
>with wrapping semantics in the compiler) can be slower than bigger types 
>or UB on overflow - expressions like "xs[i++]".

One of the two cases where Wang et al. [wang+12] saw a slowdown from
defining as much behaviour as they could in SPEC Cint 2006 was on the
SPECint 200 benchmark 456.hmmer for the following loop:

|int k;
|int *ic, *is;
|...
|for (k = 1; k <= M; k++) {
|  ...
|  ic[k] += is[k];
|  ...
|}

Their explanation is:

|As allowed by the C standard, the compiler assumes that the signed
|addition k++ cannot overflow, and rewrites the loop using a 64-bit loop
|variable. Without the optimization, however, the compiler has to keep
|k as 32 bits and generate extra instructions to sign-extend the index
|k to 64 bits for array access.

This slowdown from using -fwrapv probably does not occur on RV64G nor
on ARM A64: RV64G has a sign-extending 32-bit addition instruction,
and ARM A64 has a sign-extending indexing addressing mode.  These
architectures pay with extra instructions or extra addressing modes
for the I32LP64 mistake.

@InProceedings{wang+12,
  author = 	 {Xi Wang and Haogang Chen and Alvin Cheung and Zhihao Jia and Nickolai Zeldovich and M. Frans Kaashoek},
  title = 	 {Undefined Behavior: What Happened to My Code?},
  booktitle =	 {Asia-Pacific Workshop on Systems (APSYS'12)},
  OPTpages = 	 {},
  year =	 {2012},
  url1 =	 {http://homes.cs.washington.edu/~akcheung/getFile.php?file=apsys12.pdf},
  url2 =	 {http://people.csail.mit.edu/nickolai/papers/wang-undef-2012-08-21.pdf},
  OPTannote = 	 {}
}

>If you really need an integer type to be as fast as you can, C has had 
>types like int_fast32_t for decades.

How do you print that?  It seems to me that using ptrdiff_t (%td) and
size_t (%zd) are a better choice, but even these length modifiers are
a recent (SUSv3, 2001; have they been standardized in C?) addition and
mean that the program will not work on old libcs.

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID: <2026Dec5.194727@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11cpqih$acf5$1@dont-email.me> <11eolqn$2peg5$1@dont-email.me> <2026Dec2.132411@mips.complang.tuwien.ac.at> <11epd07$362ph$1@dont-email.me> <1796263648-5857@newsgrouper.org> <11ergn7$6utu$1@dont-email.me> <2026Dec3.122036@mips.complang.tuwien.ac.at> <1796326998-5857@newsgrouper.org> <11etu0b$1aih9$1@dont-email.me> <TlCQS.44884$7vt4.19168@fx07.iad> <11eus8u$1q9ee$1@dont-email.me> <11euul5$1ritd$1@dont-email.me> <1796416716-5857@newsgrouper.org> <11f0ta4$2okjb$2@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: reassociation and undefined behaviour
Date: Sat, 05 Dec 2026 18:47:27 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 74
Message-ID: <2026Dec5.194727@mips.complang.tuwien.ac.at>
References: <11cpqih$acf5$1@dont-email.me> <11eolqn$2peg5$1@dont-email.me> <2026Dec2.132411@mips.complang.tuwien.ac.at> <11epd07$362ph$1@dont-email.me> <1796263648-5857@newsgrouper.org> <11ergn7$6utu$1@dont-email.me> <2026Dec3.122036@mips.complang.tuwien.ac.at> <1796326998-5857@newsgrouper.org> <11etu0b$1aih9$1@dont-email.me> <TlCQS.44884$7vt4.19168@fx07.iad> <11eus8u$1q9ee$1@dont-email.me> <11euul5$1ritd$1@dont-email.me> <1796416716-5857@newsgrouper.org> <11f0ta4$2okjb$2@dont-email.me>
Injection-Date: Sat, 05 Dec 2026 19:31:43 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3405574"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+pUP9txFNF/oCDcR4jk7kW"; posting-host="d916d7b60eb310d1fc33e841d1758884"
Cancel-Lock: sha1:s4ZOWJhpQ1KRtl3RqI2r+NTL6H4= sha256:uEcfGGw9ugyiOEruQpKWANdzK/vnAY7/K8fmFi6QOyA=
	sha1:a1YVU2YoyhTOKtWY7HHuOYPoFTE= sha256:0Ml1OGuK2hwaqVxBDBNO0ArnE51vgLlMheuOg0732a8=
X-newsreader: xrn 10.11