Article View: comp.arch
Article #1096Re: reassociation and undefined behaviour
From: David Brown
Date: Sat, 05 Dec 2026 11:41
Date: Sat, 05 Dec 2026 11:41
46 lines
4250 bytes
4250 bytes
On 04/12/2026 21:38, MitchAlsup wrote: > David Brown <david.brown@hesbynett.no> posted: > >> On 04/12/2026 18:11, Thomas Koenig wrote: >>> EricP <ThatWouldBeTelling@thevillage.com> schrieb: >>>> On 2026-Dec-04 03:35, David Brown wrote: >>>>> On 03/12/2026 20:43, MitchAlsup wrote: >>>>> >>>>> So it is useful in the same way that two's complement wrapping for signed arithmetic is useful - it is absolutely absurd mathematically that you can add two positive numbers and get a negative one, there is almost certainly a bug in your program if this happens in general code, but it matches the hardware, is often harmless, and it is occasionally handy. >>>> >>>> Why is signed twos's complement wrapping mathematically absurd? >>> >>> If you add enough apples to a pile, the number of apples becomes >>> negative (to quote Dan Brown ;-) >>> >> >> I don't know who Dan Brown is (other than the author), but it is >> certainly the illustrative example I've used on a number of occasions. >> >> Of course if you want to have a modulo arithmetic system with signed >> integers of a particular size, two's complement wrapping integers are >> great (assuming they match the modulo you want!). But that's not what >> people usually want to model when they use "int" or similar types in >> most languages - they want a type that models mathematical integers as >> well as practically possible with maximally efficient implementations on >> their hardware. > > In most RISC ISA, 'long int' is faster than 'int' because when a unit > of arithmetic is performed and one cannot prove that the result fits > in an 'int' range then [SZ]EXTs have to be performed--or the result > cannot remain in a register. > > 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. 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++]". If you really need an integer type to be as fast as you can, C has had types like int_fast32_t for decades. They are rarely used because they are a bit cumbersome, but they are typically 64-bit on targets where that makes a difference.
Technical Details
Message-ID:
<11f0ta4$2okjb$2@dont-email.me>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11cpqih$acf5$1@dont-email.me> <86o6b6kaqf.fsf@linuxsc.com> <11ejr5e$nk8$1@reader2.panix.com> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> <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>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.arch Subject: Re: reassociation and undefined behaviour Date: Sat, 5 Dec 2026 12:41:56 +0100 Organization: A noiseless patient Spider Lines: 46 Message-ID: <11f0ta4$2okjb$2@dont-email.me> References: <11cpqih$acf5$1@dont-email.me> <86o6b6kaqf.fsf@linuxsc.com> <11ejr5e$nk8$1@reader2.panix.com> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> <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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 05 Dec 2026 11:41:57 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2904683"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18o1+4nWXIMPsCDwgqwvihvqDycjcQnHPY="; posting-host="e6744434c7b28607c9aa71bc7804445d" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:nV02aXE+/6pEa9mhYXB/mANXMkc= sha256:2kjF9CENzuME4oB4yrcLT1kqi4W0VxXEI6cedwahknU= sha1:KWEaxtCslgsY+wwDjAU1xrVYvts= sha256:uiMrBpeobGK0Xbs2tIWdazldC9yiXi8OGdMq3AYwSGo= In-Reply-To: <1796416716-5857@newsgrouper.org> Content-Language: en-GB