Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Thu, 20 Jan 2022 19:41:10 -0800
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <867datrc15.fsf@linuxsc.com>
References: <86a6ggzl16.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="9bbd2bd2338bc444f9d0c408d9cb7c1c"; logging-data="6527"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CNfGAuDZNLUS8NLWDA55tN1XqNgR18nE="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:ZajIRfx2brsi9IXC7V1Cm58E0PI= sha1:rm8KPdAGpY9kQtljChUuSEqhpSs=
Xref: csiph.com comp.lang.c:164501
pa@see.signature.invalid (Pierre Asselin) writes:
> Tim Rentsch wrote:
>
>> [ ... ]
>> These identities suggest two alternate formulations:
>>
>> uint32_t
>> ticks2time( uint32_t ticks, uint32_t scale, uint16_t tempo ){
>> return ticks*(tempo/scale) + ticks*(tempo%scale)/scale;
>> }
>>
>> uint32_t
>> ticks2time( uint32_t ticks, uint32_t scale, uint16_t tempo ){
>> return tempo*(ticks/scale) + tempo*(ticks%scale)/scale;
>> }
>
> I was thinking along those lines myself, except, in Tim's first
> formula, using the div() function to compute (tempo/scale) and
> (tempo%scale) together. [...]
These days I expect compilers to be smart enough to see that both
the division and the remainder are being computed, and generate
single instruction that produces both values. And if there is no
such instruction then div() probably doesn't help much.