Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

All your files have been destroyed (sorry). Paul.


devel / comp.lang.c / Re: How to avoid an overflow during multiplication?

SubjectAuthor
* How to avoid an overflow during multiplication?Mateusz Viste
+* Re: How to avoid an overflow during multiplication?Stefan Ram
|`- Re: How to avoid an overflow during multiplication?Ben Bacarisse
+* Re: How to avoid an overflow during multiplication?Ben Bacarisse
|`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| +* Re: How to avoid an overflow during multiplication?Stefan Ram
| |+* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||`* Re: How to avoid an overflow during multiplication?Keith Thompson
| || `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  +* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  | `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |  `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   +* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   | +- Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   | `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |  `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |   `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |    `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |     `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |      `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |       `* Re: How to avoid an overflow during multiplication?Richard Damon
| ||  |   |        +* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |        |`* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |        | `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |        |  `- Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |        `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |         `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |          `* Re: How to avoid an overflow during multiplication?Bart
| ||  |   |           `* Re: How to avoid an overflow during multiplication?Öö Tiib
| ||  |   |            +- Re: How to avoid an overflow during multiplication?Bart
| ||  |   |            `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   +* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   | `- Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   +* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   | `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |  `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |   +* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |`* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   | `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |  `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   |   `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    +* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |`* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   |    | `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |  +* Re: How to avoid an overflow during multiplication?Keith Thompson
| ||  |   |   |    |  |`* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |  | +* Re: How to avoid an overflow during multiplication?Ben Bacarisse
| ||  |   |   |    |  | |`- Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |  | `* Re: How to avoid an overflow during multiplication?Keith Thompson
| ||  |   |   |    |  |  `- Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |  `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   |    `* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   |   |     `* Re: How to avoid an overflow during multiplication?Manfred
| ||  |   |   |      `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |       `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   `- Re: How to avoid an overflow during multiplication?Ben Bacarisse
| ||  |   +* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   | +- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   | `* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   |  `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   +- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |    `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |     `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |      `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       +* Re: How to avoid an overflow during multiplication?Keith Thompson
| ||  |       |`- Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       +* Re: How to avoid an overflow during multiplication?Vir Campestris
| ||  |       |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       | +* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |       | |+* Re: How to avoid an overflow during multiplication?Öö Tiib
| ||  |       | ||`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       | || +- Re: How to avoid an overflow during multiplication?Öö Tiib
| ||  |       | || `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |       | ||  `* Re: How to avoid an overflow during multiplication?Bart
| ||  |       | ||   +* Re: How to avoid an overflow during multiplication?Ben Bacarisse
| ||  |       | ||   |`- Re: How to avoid an overflow during multiplication?Bart
| ||  |       | ||   `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |       | |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       | | +* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | |+* Re: How to avoid an overflow during multiplication?Malcolm McLean
| ||  |       | | ||`* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | || `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       | | ||  `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | ||   `- Re: How to avoid an overflow during multiplication?Malcolm McLean
| ||  |       | | |`* Re: How to avoid an overflow during multiplication?Bart
| ||  |       | | | +* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | | |`* Re: How to avoid an overflow during multiplication?Bart
| ||  |       | | | | +* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | | | |+* Re: How to avoid an overflow during multiplication?Bart
| ||  |       | | | | ||`* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | | | || `- Re: How to avoid an overflow during multiplication?Bart
| ||  |       | | | | |`* Re: How to avoid an overflow during multiplication?Manfred
| ||  |       | | | | | +* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |       | | | | | |`- Re: How to avoid an overflow during multiplication?Manfred
| ||  |       | | | | | `- Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | | | `* Re: How to avoid an overflow during multiplication?james...@alumni.caltech.edu
| ||  |       | | | `* Re: How to avoid an overflow during multiplication?james...@alumni.caltech.edu
| ||  |       | | `* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |       | +- Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |       | `* Re: How to avoid an overflow during multiplication?Vir Campestris
| ||  |       +- Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |       `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  `- Re: How to avoid an overflow during multiplication?Keith Thompson
| |`- Re: How to avoid an overflow during multiplication?Guillaume
| `* Re: How to avoid an overflow during multiplication?David Brown
+* Re: How to avoid an overflow during multiplication?Bonita Montero
+* Re: How to avoid an overflow during multiplication?Michael S
+* Re: How to avoid an overflow during multiplication?Kaz Kylheku
+* Re: How to avoid an overflow during multiplication?Tim Rentsch
`* Re: How to avoid an overflow during multiplication?Stefan Ram

Pages:12345678
Re: How to avoid an overflow during multiplication?

<86a6ggzl16.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19734&group=comp.lang.c#19734

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 10:28:21 -0800
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <86a6ggzl16.fsf@linuxsc.com>
References: <sqmkg9$157v$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="06b28be905d0d032491ccb4d10c11358";
logging-data="9317"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18OARlqSGWz/7CX2CeF5IQuj4wKeI3J55Q="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:SMxroslwmRGOvCwkLZ0Qv7sx3pU=
sha1:vryVE9GqBR6FsI/lz+O4TisnKrA=
 by: Tim Rentsch - Fri, 31 Dec 2021 18:28 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:

> I have this little function:
>
> /* converts an amount of ticks into human time (micro-seconds)
> * timeunits: number of ticks to convert into actual time
> * tempo : the time length (in microseconds) of grouplen ticks
> */
> uint32_t ticks2time(uint32_t ticks, uint32_t grouplen, uint16_t tempo)
> {
> return ticks * tempo / grouplen;
> }
>
> In practical situations the end result of this computation is
> guaranteed to always fit inside an uint32_t, but the above formula
> overflows easily in the (ticks * tempo) part. [...]

I haven't followed all the extra information you have given, but
here are some ideas for you to try.

First let me restate the function, with minor lexical changes:

uint32_t
ticks2time( uint32_t ticks, uint32_t scale, uint16_t tempo ){
return ticks * tempo / scale;
}

Next you might not know the identity

a*b/c === a*(b/c) + a*(b%c)/c

Of course multiplication is commutative, so we can also write:

a*b/c === b*(a/c) + b*(a%c)/c

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;
}

If one of these works for you (checking both performance and the
lack of overflow) then great.

If the first one works in terms of avoiding overflow, but is too
slow, then if tempo doesn't change often, we can precompute the
quantities tempo/scale and tempo%scale when tempo changes:

uint16_t tempo_scale_q = tempo/scale;
uint16_t tempo_scale_r = tempo%scale;

void
set_tempo( uint16_t tempo, uint16_t scale ){
tempo_scale_q = tempo / scale;
tempo_scale_r = tempo % scale;
}

uint32_t
ticks2time_2( uint32_t ticks, uint32_t scale ){
return ticks*tempo_scale_q + ticks*tempo_scale_r/scale;
}

which probably will be faster than the original.

Incidentally, if 'scale' (aka grouplen) never changes, if you can
make it be a compile-time constant then the compiler may be able
to turn the division (and remainder) into more efficient
computations, giving better performance.

Please let us know if these techniques turn out to be helpful.
Good luck!

Re: How to avoid an overflow during multiplication?

<sqnihk$171d$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19735&group=comp.lang.c#19735

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!UgLt14+w9tVHe1BtIa3HDQ.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 19:35:28 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sqnihk$171d$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="39981"; posting-host="UgLt14+w9tVHe1BtIa3HDQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
Content-Language: fr
X-Notice: Filtered by postfilter v. 0.9.2
 by: Guillaume - Fri, 31 Dec 2021 18:35 UTC

Le 31/12/2021 à 14:41, Stefan Ram a écrit :
> Mateusz Viste <mateusz@xyz.invalid> wrote:
>> 2021-12-31 at 12:33 +0000, Ben Bacarisse wrote:
>>> There's a technical detail here which is that in C unsigned arithmetic
>>> can't overflow. Arithmetic overflow is reserved for serious undefined
>>> situation. Unsigned arithmetic just "wraps round" in a defined
>>> manner.
>> Can you provide the source please? I know that what you describe is the
>
> |A computation involving unsigned operands can never overflow,
> |because a result that cannot be represented by the resulting
> |unsigned integer type is reduced modulo the number that is
> |one greater than the largest value that can be represented by
> |the resulting type.

Yep. Which, by the way, is a pretty funny way of defining "never
overflowing".

Yes, in C it's very straightforward. Arithmetic operations on unsigned
are always modulo 2^N, with N the number of bits of the resulting type.

So indeed, they can't overflow. The result is just effectively truncated
if it *would* overflow.

Now make sure you understand the implications of doing your operations
modulo 2^N.

Re: How to avoid an overflow during multiplication?

<muldiv-20211231201955@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19736&group=comp.lang.c#19736

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: 31 Dec 2021 19:21:04 GMT
Organization: Stefan Ram
Lines: 60
Expires: 1 Apr 2022 11:59:58 GMT
Message-ID: <muldiv-20211231201955@ram.dialup.fu-berlin.de>
References: <sqmkg9$157v$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de GTHUSHTwWbyt1jSYwnC2FgA8TdQYb8evWa9s8c6VyYJUyM
X-Copyright: (C) Copyright 2021 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 31 Dec 2021 19:21 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:
>uint32_t ticks2time(uint32_t ticks, uint32_t grouplen, uint16_t tempo)
>{
> return ticks * tempo / grouplen;
>}

In the Web, one can sometimes see the expression

((a/c)*b)+(a%c)*b/c

for this.

I tried to implement this as "muldiv_experimental" using
"unsigned char" as a model for "uint32_t". So far, my test
program reports an error, but it might be an error in my
implementation or test code ... The following listing has
long lines with more than 72 characters.

main.c

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

#define TOP (UCHAR_MAX+1)

void the_exact_result_is_not_representable_with_unsigned_char_anyway_so_we_dont_care( void ){}

unsigned long muldiv_exact( const unsigned long a, const unsigned long b, const unsigned long c )
{ return a * b / c; }

unsigned char muldiv_experimental( const unsigned char a, const unsigned char b, const unsigned char c )
{ const unsigned char q =( unsigned char )( a / c );
const unsigned char p =( unsigned char )( q * b );
const unsigned char r =( unsigned char )( a % c );
const unsigned char m =( unsigned char )( r * b );
const unsigned char d =( unsigned char )( m / c );
const unsigned char s =( unsigned char )( p + d );
return s; }

int main( void )
{ for( unsigned long a = 0; a < TOP; ++a )
for( unsigned long b = 0; b < TOP; ++b )
for( unsigned long c = 1; c < TOP; ++c )
{ unsigned long exact_result = muldiv_exact( a,b,c );
unsigned char experimental_result = muldiv_experimental(( unsigned char )a,( unsigned char )b,( unsigned char )c );
if( exact_result >= TOP )
the_exact_result_is_not_representable_with_unsigned_char_anyway_so_we_dont_care();
else
if( exact_result != experimental_result )
{ puts( "Error found!" );
printf( "a=%lu b=%lu c=%lu: exact_result=%lu experimental_result=%u\n", a, b, c, exact_result, experimental_result );
exit( 99 ); }}}

transcript

Error found!
a=2 b=128 c=3: exact_result=85 experimental_result=0

Re: How to avoid an overflow during multiplication?

<877dbk36qu.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19737&group=comp.lang.c#19737

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 11:38:01 -0800
Organization: None to speak of
Lines: 28
Message-ID: <877dbk36qu.fsf@nosuchdomain.example.com>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="535866f7e1f9aa1f9c859c38fe4fa9a9";
logging-data="30610"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/XHiF1fJFqHIxohqXt5ibp"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:DBpeHPASlO2uzKyY1EpZVplwaik=
sha1:u/GW9JhN1BTn+ZdW3ZYP57RUpEk=
 by: Keith Thompson - Fri, 31 Dec 2021 19:38 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:
> 2021-12-31 at 13:41 GMT, Stefan Ram wrote:
>> |A computation involving unsigned operands can never overflow,
>> |because a result that cannot be represented by the resulting
>> |unsigned integer type is reduced modulo the number that is
>> |one greater than the largest value that can be represented by
>> |the resulting type.
>> n2310, 6.2.5p9
>
> Had missed that one, thanks!
>
> (In my ANSI C copy it is at 6.1.2.5, p23)

You mean ISO C. (C standards have been published by ISO, not ANSI,
since 1990.) Which edition are you using?

It's section 6.2.5 in C99 and later. If you're seeing that paragraph in
section 6.1.2.5, you're probably using the C90 standard (which doesn't
have paragraph numbers, BTW). I suggest grabbing a more recent draft.
N1570 is nearly equivalent to the C11 standard. (There are drafts that
are close to the C17 standard, but most of them are password protected.)

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to avoid an overflow during multiplication?

<sqnmsm$1ff8$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19738&group=comp.lang.c#19738

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 20:49:42 +0100
Organization: . . .
Message-ID: <sqnmsm$1ff8$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="48616"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 31 Dec 2021 19:49 UTC

2021-12-31 at 11:38 -0800, Keith Thompson wrote:
> > (In my ANSI C copy it is at 6.1.2.5, p23)
>
> You mean ISO C. (C standards have been published by ISO, not ANSI,
> since 1990.)

No, I do mean ANSI, although it was a conjoint work with ISO.

> Which edition are you using?

The cover says:

"American National Standard for Programming Languages - C
ANSI/ISO 9899-1990
(revision and redesignation of ANSI X3.159-1989)
Approved August 3, 1992"

> I suggest grabbing a more recent draft. N1570 is nearly equivalent to
> the C11 standard. (There are drafts that are close to the C17
> standard, but most of them are password protected.)

Meh. I'm *really* not interested in those post-1990 atrocities. At all.
Will stay with ANSI C, it worked very well for me so far. Sorry. :-)

Mateusz

Re: How to avoid an overflow during multiplication?

<sqnn6b$18gn$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19739&group=comp.lang.c#19739

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 20:54:51 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sqnn6b$18gn$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<ca774586-49c6-4f46-9dd1-f39df2231dd1n@googlegroups.com>
<sqn24c$14ld$1@gioia.aioe.org>
<d9928f83-2dd3-4943-8938-de5627d9f05fn@googlegroups.com>
<sqn6j3$1utr$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="41495"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Fri, 31 Dec 2021 19:54 UTC

On 12/31/2021 4:11 PM, Mateusz Viste wrote:
> 2021-12-31 at 06:12 -0800, Michael S wrote:
>> On Friday, December 31, 2021 at 3:55:35 PM UTC+2, Mateusz Viste wrote:
>>> 2021-12-31 at 05:37 -0800, Michael S wrote:
>>>> Couple of questions:
>>>> 1. You said that grouplen never changes. Does it imply that tempo
>>>> could change?
>>> Yes, tempo can change during a song play.
>>
>> Then it's tough.
>
> There are many constraints, yes.
>
>> Still, if we can divide it into two parts, something like
>> void SetTempo(uint32_t grouplen, uint16_t tempo) that calculates
>> A = tempo/grouplen
>> B = ((tempo%grouplen) * 128)/grouplen
>> and
>> uint32_t ticks2time(uint32_t ticks) that uses A and B
>> and if SetTempo() is called many times less often than ticks2time()
>> then situation could be improved.
>
> You mean this, right?
>
> uint32_t A = tempo / grouplen;
> uint32_t B = (tempo % grouplen) * 128 / grouplen;
> return ticks * A + (ticks * B) / 128;
>
> another way to express it would be this:
>
> return ticks * (tempo / grouplen) +
> ((ticks * (((tempo % grouplen) << 7) / grouplen)) >> 7);
>
> This is close to my solution with bit shifting, but if I get it right
> yours is more resilient to wraparounds as it only shifts a remainder
> and not the whole tempo. Thanks for the pointer!

Splitting quotient and remainder could be something indeed.
Others have mentioned that, however I'll post the following trivial
modification of one of your early sources:

$ cat tempo2.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main(int argc, char **argv)
{ if (argc != 4) {
printf("usage: %s ticks tempo grouplen\n", argv[0]);
} else {
uint32_t r1, r2, r3, r4, r5, r6;
uint32_t ticks = atoi(argv[1]);
uint32_t tempo = atoi(argv[2]);
uint32_t grouplen = atoi(argv[3]);

r1 = ticks * tempo / grouplen;
r2 = ticks * (tempo / grouplen);
r3 = (ticks * (((tempo << 3) / grouplen))) >> 3;
r4 = (uint64_t)ticks * (uint64_t)tempo / grouplen;

{
uint32_t quot1 = tempo/grouplen;
uint32_t rem1 = tempo % grouplen;

r5 = ticks*quot1 + (rem1 * ticks)/grouplen;

uint32_t quot2 = ticks/grouplen;
uint32_t rem2 = ticks % grouplen;

r6 = ticks*quot1 + rem1*quot2 + (rem1 * rem2)/grouplen;

}

printf("r1 = %u\nr2 = %u\nr3 = %u\nr4 = %u\nr5 = %u\nr6 = %u\n",
r1, r2, r3, r4, r5, r6);
}

return 0;
}

$ cc -std=c11 -Wall tempo2.c && ./a.out 9120 1090909 15370
r1 = 88429
r2 = 638400
r3 = 646380
r4 = 647305
r5 = 647305
r6 = 647305

Assuming that the final result fits in 32 bits, r5 is exact as long as:
grouplen*ticks < UINT32_MAX

r6 is exact as long as:
grouplen*grouplen < UINT32_MAX

The r5 expression could also be refactored so as to require
grouplen*tempo < UINT32_MAX, but your example is already out of this range.

The expressions are also simple enough to be coded directly in x86 asm,
for example you may know that the 'div' instruction yields quotient and
remainder in one go - however, many decent optimizing compilers may do
the same and more.

>
>> I still don't understand, but so be it.
>
> Another way to say it would be that the CPU is so slow that it
> struggles with its current tasks so it doesn't have much time to fiddle
> around with too much math (esp. 32-bit math). :)
>
> Mateusz
>

Re: How to avoid an overflow during multiplication?

<sqnoim$s74$1@reader1.panix.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19740&group=comp.lang.c#19740

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.panix3.panix.com!not-for-mail
From: pa...@see.signature.invalid (Pierre Asselin)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 20:18:30 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <sqnoim$s74$1@reader1.panix.com>
References: <sqmkg9$157v$1@gioia.aioe.org> <86a6ggzl16.fsf@linuxsc.com>
Injection-Date: Fri, 31 Dec 2021 20:18:30 -0000 (UTC)
Injection-Info: reader1.panix.com; posting-host="panix3.panix.com:166.84.1.3";
logging-data="28900"; mail-complaints-to="abuse@panix.com"
User-Agent: tin/2.6.0-20210823 ("Coleburn") (NetBSD/9.2 (amd64))
 by: Pierre Asselin - Fri, 31 Dec 2021 20:18 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> 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.

#include <stdlib.h>
tmp= div(tempo, scale)
return ticks*tmp.quot + (tics*tmp.rem)/scale

Ditto if you use (ticks/scale). div() is in C99. I don't know if
it meets your timing constraints but it could be faster than
computing division and remainder separately.

If (ticks*scale) is more likely to overflow than (tempo*scale),
use Tim's second solution. Otherwise use the first.

Re: How to avoid an overflow during multiplication?

<sqnojq$1ff8$2@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19741&group=comp.lang.c#19741

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 21:19:06 +0100
Organization: . . .
Message-ID: <sqnojq$1ff8$2@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<ca774586-49c6-4f46-9dd1-f39df2231dd1n@googlegroups.com>
<sqn24c$14ld$1@gioia.aioe.org>
<d9928f83-2dd3-4943-8938-de5627d9f05fn@googlegroups.com>
<sqn6j3$1utr$1@gioia.aioe.org>
<sqnn6b$18gn$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="48616"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 31 Dec 2021 20:19 UTC

2021-12-31 at 20:54 +0100, Manfred wrote:
> Splitting quotient and remainder could be something indeed.
> Others have mentioned that, however I'll post the following trivial
> modification of one of your early sources:

Hello Manfred, I've actually got to the (almost) same code on
my end. I agree that it is the nicest solution, and precomputing the
quotient & remainder is definitely an interesting optimization.

Thanks for taking the time to put it in code!

Mateusz

Re: How to avoid an overflow during multiplication?

<sqnp3v$1vle$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19742&group=comp.lang.c#19742

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 21:27:43 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sqnp3v$1vle$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org> <86a6ggzl16.fsf@linuxsc.com>
<sqnoim$s74$1@reader1.panix.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="65198"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Fri, 31 Dec 2021 20:27 UTC

On 12/31/2021 9:18 PM, Pierre Asselin wrote:
> Tim Rentsch <tr.17687@z991.linuxsc.com> 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.
>
> #include <stdlib.h>
> tmp= div(tempo, scale)
> return ticks*tmp.quot + (tics*tmp.rem)/scale
>
> Ditto if you use (ticks/scale). div() is in C99. I don't know if
> it meets your timing constraints but it could be faster than
> computing division and remainder separately.
>
> If (ticks*scale) is more likely to overflow than (tempo*scale),
> use Tim's second solution. Otherwise use the first.
>

I like C's div as well, but AFAIK it is for signed integers only, unless
I've missed the unsigned counterpart.

Re: How to avoid an overflow during multiplication?

<sqnpau$1ff8$3@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19743&group=comp.lang.c#19743

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 21:31:26 +0100
Organization: . . .
Message-ID: <sqnpau$1ff8$3@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<86a6ggzl16.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="48616"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 31 Dec 2021 20:31 UTC

2021-12-31 at 10:28 -0800, Tim Rentsch wrote:
> Next you might not know the identity
>
> a*b/c === a*(b/c) + a*(b%c)/c
>
> Of course multiplication is commutative, so we can also write:
>
> a*b/c === b*(a/c) + b*(a%c)/c

Hello Tim,

Thank you for the extensive explanations. What you suggest has already
been proposed by Michael S. earlier today (although in a slightly more
opaque version and extra scaling, that I later developed further).

I tested it, and it does work very well.

> uint32_t ticks2time_2( uint32_t ticks, uint32_t scale ) {
> return ticks*tempo_scale_q + ticks*tempo_scale_r/scale;
> }
>
> which probably will be faster than the original.

The pre-computation subject has been also mentioned already, but thank
you nonetheless for the nice and self-explanatory example code, that's
very kind of you.

As for being faster than the original - I really doubt it.
The original was this:

return ticks * (tempo / scale);

ie. one division followed by one multiplication. The new optimized
version is two multiplications, one division and one addition. It can
hardly be faster... But of course it has the benefit of a much higher
precision.

Mateusz

Re: How to avoid an overflow during multiplication?

<sqnppu$1ff8$4@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19744&group=comp.lang.c#19744

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 21:39:25 +0100
Organization: . . .
Message-ID: <sqnppu$1ff8$4@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<86a6ggzl16.fsf@linuxsc.com>
<sqnoim$s74$1@reader1.panix.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="48616"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 31 Dec 2021 20:39 UTC

2021-12-31 at 20:18 -0000, Pierre Asselin wrote:
> 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.
>
> #include <stdlib.h>
> tmp= div(tempo, scale)
> return ticks*tmp.quot + (tics*tmp.rem)/scale

That was my first thought as well, but div() is for int, while I work
on unsigned longs (because int is only 16 bits wide). I checked the
documentation of my compiler, and there is no div() equivalent for
unsigned longs.

Mateusz

Re: How to avoid an overflow during multiplication?

<7c025db6-fe06-4083-a71b-a56a390c31ddn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19746&group=comp.lang.c#19746

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:7f8b:: with SMTP id z11mr32054392qtj.513.1640992878837;
Fri, 31 Dec 2021 15:21:18 -0800 (PST)
X-Received: by 2002:a05:620a:44c1:: with SMTP id y1mr26869074qkp.187.1640992878616;
Fri, 31 Dec 2021 15:21:18 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 31 Dec 2021 15:21:18 -0800 (PST)
In-Reply-To: <sqmufp$7vc$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1003:b033:f651:1551:ed0b:799d:acdf;
posting-account=Ix1u_AoAAAAILVQeRkP2ENwli-Uv6vO8
NNTP-Posting-Host: 2600:1003:b033:f651:1551:ed0b:799d:acdf
References: <sqmkg9$157v$1@gioia.aioe.org> <sqmufp$7vc$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7c025db6-fe06-4083-a71b-a56a390c31ddn@googlegroups.com>
Subject: Re: How to avoid an overflow during multiplication?
From: jameskuy...@alumni.caltech.edu (james...@alumni.caltech.edu)
Injection-Date: Fri, 31 Dec 2021 23:21:18 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 20
 by: james...@alumni.calt - Fri, 31 Dec 2021 23:21 UTC

On Friday, December 31, 2021 at 7:53:24 AM UTC-5, Bonita Montero wrote:
> Am 31.12.2021 um 11:02 schrieb Mateusz Viste:
>
> > uint32_t ticks2time(uint32_t ticks, uint32_t grouplen, uint16_t tempo)
> > {
> > return ticks * tempo / grouplen;
> return (uint32_t)((uint64_t)ticks * (uint64_t)tempo / grouplen);
>
> On x86 f.e. the compiler will still use a 32 * 32 multiplication
> because it sees that the casted value's upper halves are zero.
> And after / grouplen you can safely cast the result to uint32_t.

Do you meant that it will use an instruction that multiplies two 32 bit
quantities and produces the correct 64-bit result? If so, that's nice to know,
but not very important - how they get the right result is generally less
important than getting the right result.
If you mean that they multiply two 32-bit quantities, producing a result
that has only 32 bits, even if the mathematically correct result of the
multiplication would require more than 32 bits to be represented correctly,
then that's very important to know, because that would render the
implementation non-conforming.

Re: How to avoid an overflow during multiplication?

<sqo47m$n9p$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19747&group=comp.lang.c#19747

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 18:37:25 -0500
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <sqo47m$n9p$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 31 Dec 2021 23:37:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="aa0f15e5fd1b31c2c44e738a8e369dbb";
logging-data="23865"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DnEtuJuDM3xI81UpON+O3OwRUw64GliM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:Q40fHN3Mg92bDfgTDWoWXoOsK6k=
In-Reply-To: <sqnmsm$1ff8$1@gioia.aioe.org>
Content-Language: en-US
 by: James Kuyper - Fri, 31 Dec 2021 23:37 UTC

On 12/31/21 2:49 PM, Mateusz Viste wrote:
> 2021-12-31 at 11:38 -0800, Keith Thompson wrote:
>>> (In my ANSI C copy it is at 6.1.2.5, p23)
>>
>> You mean ISO C. (C standards have been published by ISO, not ANSI,
>> since 1990.)
>
> No, I do mean ANSI, although it was a conjoint work with ISO.
>
>> Which edition are you using?
>
> The cover says:
>
> "American National Standard for Programming Languages - C
> ANSI/ISO 9899-1990
> (revision and redesignation of ANSI X3.159-1989)
> Approved August 3, 1992"

Calling it ANSI C is not very useful, because that designation is
ambiguous. Every version of the C standard has been adopted as a
standard by both ANSI and ISO, so referring to ANSI C doesn't make it
clear whether you're referring to C90, C99, C2011, or C2017. That would
be fine if you were referring to any or all four of those versions
collectively - but you do seem to be referring only to C90.

uint32_t and uint16_t, both of which are used in your code, were added
to the language in C99. So apparently you don't consider C99 a complete
atrocity.

Re: How to avoid an overflow during multiplication?

<87zgog1gwq.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19748&group=comp.lang.c#19748

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 15:41:25 -0800
Organization: None to speak of
Lines: 40
Message-ID: <87zgog1gwq.fsf@nosuchdomain.example.com>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="e63c1bf8e89647c74f643a1efe1616e1";
logging-data="22793"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18H8gVwFCqRr22fpJR6pBYP"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:RnImhOMr3u4mRcNpU50FxBApbHE=
sha1:hk0qARIP/CJJHL9MJzFQ2GZv6OU=
 by: Keith Thompson - Fri, 31 Dec 2021 23:41 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:
> 2021-12-31 at 11:38 -0800, Keith Thompson wrote:
>> > (In my ANSI C copy it is at 6.1.2.5, p23)
>>
>> You mean ISO C. (C standards have been published by ISO, not ANSI,
>> since 1990.)
>
> No, I do mean ANSI, although it was a conjoint work with ISO.
>
>> Which edition are you using?
>
> The cover says:
>
> "American National Standard for Programming Languages - C
> ANSI/ISO 9899-1990
> (revision and redesignation of ANSI X3.159-1989)
> Approved August 3, 1992"

The 1990 ISO C standard is nearly equivalent to the 1989 ANSI C
standard. A major difference is that the sections were renumbered. In
the 1989 edition, the core language was in section 3. In the 1990 ISO
standard, it's section 6.

>> I suggest grabbing a more recent draft. N1570 is nearly equivalent to
>> the C11 standard. (There are drafts that are close to the C17
>> standard, but most of them are password protected.)
>
> Meh. I'm *really* not interested in those post-1990 atrocities. At all.
> Will stay with ANSI C, it worked very well for me so far. Sorry. :-)

Strictly speaking, the term "ANSI C" is ambiguous. People often use it
to refer to the language defined by the 1989 ANSI C standard, but in
fact ANSI has officially adopted each new ISO C standard edition and
considers the previous editions to be obsolete. I suggest referring to
"C90" rather than "ANSI C".

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to avoid an overflow during multiplication?

<sqo611$vte$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19750&group=comp.lang.c#19750

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 19:08:00 -0500
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <sqo611$vte$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org>
<muldiv-20211231201955@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 1 Jan 2022 00:08:02 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2612d5815fefd4123c851f95030a84f7";
logging-data="32686"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+L8s9t511DiBs+RbqEmwfgEW11hYoGsCM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:YlJZ/MaEX+fTNdUFs2x5z2ePQU4=
In-Reply-To: <muldiv-20211231201955@ram.dialup.fu-berlin.de>
Content-Language: en-US
 by: James Kuyper - Sat, 1 Jan 2022 00:08 UTC

On 12/31/21 2:21 PM, Stefan Ram wrote:
> Mateusz Viste <mateusz@xyz.invalid> writes:
>> uint32_t ticks2time(uint32_t ticks, uint32_t grouplen, uint16_t tempo)
>> {
>> return ticks * tempo / grouplen;
>> }
>
> In the Web, one can sometimes see the expression
>
> ((a/c)*b)+(a%c)*b/c
>
> for this.
>
> I tried to implement this as "muldiv_experimental" using
> "unsigned char" as a model for "uint32_t". So far, my test
> program reports an error, but it might be an error in my
> implementation or test code ... The following listing has
> long lines with more than 72 characters.

That relies upon the identity that a == (a/c)*c + a%c. That identity
only applies to the types of the operands after the usual arithmetic
conversions (which include the integer promotions). Unless UCHAR_MAX >
INT_MAX (which is permitted, but unlikely) unsigned char will promote to
int, and all calculations will be carried out using int arithmetic, with
an int result. If any of your multiplications produce a result greater
than UCHAR_MAX, information will be lost when you convert the result
back to unsigned char, producing apparent violations of that identity.

For the case you mentioned, a=2, b=128, c=3, if you used int values for
the intermediate results you would have gotten:

p: 2/3 = 0
q: 0*128 = 0
r: 2%3 = 2
m: 2*128 = 256
d: 256/3 = 85
s: 0+85 = 85

However, since you converted the result back to unsigned char, m ends up
with a value of 0 rather than 256.

Re: How to avoid an overflow during multiplication?

<unsigned-char-20220101012417@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19752&group=comp.lang.c#19752

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: 1 Jan 2022 00:25:21 GMT
Organization: Stefan Ram
Lines: 15
Expires: 1 Apr 2022 11:59:58 GMT
Message-ID: <unsigned-char-20220101012417@ram.dialup.fu-berlin.de>
References: <sqmkg9$157v$1@gioia.aioe.org> <muldiv-20211231201955@ram.dialup.fu-berlin.de> <sqo611$vte$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de ZQXrkpYzcTUdi4/EDgl6YAIKOtS2YFR7AFpJjP/p//t5j0
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sat, 1 Jan 2022 00:25 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>However, since you converted the result back to unsigned char, m ends up
>with a value of 0 rather than 256.

I believe Mateusz wrote something along the lines of "Using
64 bits in any way is not really an option.". So, I assumed
that he wanted to use uint32_t throughout, and every of his
binary operations would process two 32 bit values and yield
one 32 bit value.

I tried to scale the whole thing down from 32 bits to 8 bits
to facilitate observation. That's why I painstakingly converted
every intermediate result back to "unsigned char".

Re: How to avoid an overflow during multiplication?

<sqo86f$a7j$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19754&group=comp.lang.c#19754

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 19:45:01 -0500
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <sqo86f$a7j$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org>
<muldiv-20211231201955@ram.dialup.fu-berlin.de> <sqo611$vte$1@dont-email.me>
<unsigned-char-20220101012417@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 1 Jan 2022 00:45:03 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="aa0f15e5fd1b31c2c44e738a8e369dbb";
logging-data="10483"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19sUjjiz79v830ZRPIN++SeBoYa/dvPnBE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:WM4l6IxjAoCijTdBNKJZJtg2DDo=
In-Reply-To: <unsigned-char-20220101012417@ram.dialup.fu-berlin.de>
Content-Language: en-US
 by: James Kuyper - Sat, 1 Jan 2022 00:45 UTC

On 12/31/21 7:25 PM, Stefan Ram wrote:
> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>> However, since you converted the result back to unsigned char, m ends up
>> with a value of 0 rather than 256.
>
> I believe Mateusz wrote something along the lines of "Using
> 64 bits in any way is not really an option.". So, I assumed
> that he wanted to use uint32_t throughout, and every of his
> binary operations would process two 32 bit values and yield
> one 32 bit value.
>
> I tried to scale the whole thing down from 32 bits to 8 bits
> to facilitate observation. That's why I painstakingly converted
> every intermediate result back to "unsigned char".

Which implies that you didn't think things through properly. Scaling it
down to unsigned char introduces a problem (the integer promotions) that
would come up with uint32_t only if INT_MAX > UINT32_MAX. I've
frequently used systems where 'int' was a 64-bit type, but from the OP's
description, that's unlikely to be the case on the platform he's
targeting. If you had used unsigned int rather than unsigned char, you
would guarantee that the integer promotions could not come into play.

Re: How to avoid an overflow during multiplication?

<864k6oz11s.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19756&group=comp.lang.c#19756

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 31 Dec 2021 17:39:59 -0800
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <864k6oz11s.fsf@linuxsc.com>
References: <sqmkg9$157v$1@gioia.aioe.org> <86a6ggzl16.fsf@linuxsc.com> <sqnpau$1ff8$3@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="8d4c26aa7c7194cacc690ff896698f96";
logging-data="25193"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hxPgUOsmxQkBeJwi55RvlpNl8Ut/nGzQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:DdMz21Kf3Vsk9HomCNTlReRsYV0=
sha1:VBtLAbIJ8xq54WEnBuFKpNezgoA=
 by: Tim Rentsch - Sat, 1 Jan 2022 01:39 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:

> 2021-12-31 at 10:28 -0800, Tim Rentsch wrote:
>
>> Next you might not know the identity
>>
>> a*b/c === a*(b/c) + a*(b%c)/c
>>
>> Of course multiplication is commutative, so we can also write:
>>
>> a*b/c === b*(a/c) + b*(a%c)/c
>
> Hello Tim,
>
> Thank you for the extensive explanations. What you suggest has already
> been proposed by Michael S. earlier today [...]

What can I say, great minds think alike. :)

> I tested it, and it does work very well.
>
>> uint32_t ticks2time_2( uint32_t ticks, uint32_t scale ) {
>> return ticks*tempo_scale_q + ticks*tempo_scale_r/scale;
>> }
>>
>> which probably will be faster than the original.
>
> The pre-computation subject has been also mentioned already, but thank
> you nonetheless for the nice and self-explanatory example code, that's
> very kind of you.

I try to give lucid explanations. I appreciate you appreciating
them.

> As for being faster than the original - I really doubt it.
> The original was this:
>
> return ticks * (tempo / scale);
>
> ie. one division followed by one multiplication. [...]

Oh, by original I meant my own earlier version:

uint32_t
ticks2time( uint32_t ticks, uint32_t scale, uint16_t tempo ){
return ticks*(tempo/scale) + ticks*(tempo%scale)/scale;
}

in which case I expect the precompute version will indeed be
faster.

One further idea.. after posting it occurred to me that there is
a technique that is guaranteed to give the right answer as long
as the result fits in 32 bits:

uint32_t
ticks2time( uint32_t ticks, uint16_t tempo, uint16_t scale ){
uint32_t th = ticks >> 16;
uint32_t th_tempo = th * tempo;
uint32_t tl = (uint16_t) ticks;
return
(th_tempo / scale << 16) +
((th_tempo % scale << 16) + tl*tempo) / scale;
}

This approach is of course more expensive to compute, but it
might be worth trying, as a sanity check if nothing else.

Re: How to avoid an overflow during multiplication?

<uint32_t-20220101030736@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19757&group=comp.lang.c#19757

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: 1 Jan 2022 02:08:46 GMT
Organization: Stefan Ram
Lines: 36
Expires: 1 Apr 2022 11:59:58 GMT
Message-ID: <uint32_t-20220101030736@ram.dialup.fu-berlin.de>
References: <sqmkg9$157v$1@gioia.aioe.org> <muldiv-20211231201955@ram.dialup.fu-berlin.de> <sqo611$vte$1@dont-email.me> <unsigned-char-20220101012417@ram.dialup.fu-berlin.de> <sqo86f$a7j$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de hAsXHXgPSuB4zNEQycBRKg+8nQGYHzX72fXGrrey8VjcZj
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sat, 1 Jan 2022 02:08 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes: Scaling it
>down to unsigned char introduces a problem (the integer promotions) that
> would come up with uint32_t only if INT_MAX > UINT32_MAX. I've

If you wanted to say that the runtime error message would
not occur had uint32_t been used instead of "unsigned char":
I get error messages with uint32_t, too. With

uint32_t muldiv_experimental
( uint32_t const a, uint32_t const b, uint32_t const c )
{ const uint32_t q =( uint32_t )( a / c );
const uint32_t p =( uint32_t )( q * b );
const uint32_t r =( uint32_t )( a % c );
const uint32_t m =( uint32_t )( r * b );
const uint64_t m_ =( uint64_t )r * b;
const uint32_t d =( uint32_t )( m / c );
const uint32_t s =( uint32_t )( p + d );
printf
( "q=%u, p=%u, r=%u, m=%u, m_=%llu, d=%u, s=%u\n",
q, p, r, m, m_, d, s );
return s; }

, the exact result for a call with the arguments 2705443,
415131524, and 1256275308 would be 894003, so it would
fit into an uint32_t, but muldiv_experimental returns 1.
And the implementation used does not have INT_MAX > UINT32_MAX.

The internal variables for the above argument values are:

q=0, p=0, r=2705443, m=2202617612, m_=1123114675685132, d=1, s=1

. Had m_ been used instead of m, maybe the result would be
correct, but, as I wrote, I believe Mateusz wrote something
like, "Using 64 bits in any way is not really an option.".

Re: How to avoid an overflow during multiplication?

<PS-20220101032942@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19758&group=comp.lang.c#19758

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: 1 Jan 2022 02:30:20 GMT
Organization: Stefan Ram
Lines: 17
Expires: 1 Apr 2022 11:59:58 GMT
Message-ID: <PS-20220101032942@ram.dialup.fu-berlin.de>
References: <sqmkg9$157v$1@gioia.aioe.org> <muldiv-20211231201955@ram.dialup.fu-berlin.de> <sqo611$vte$1@dont-email.me> <unsigned-char-20220101012417@ram.dialup.fu-berlin.de> <sqo86f$a7j$1@dont-email.me> <uint32_t-20220101030736@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de rImefLUlDcHKloyzf9ejgA4Y4BP5whAfMFIbAXGe5Kjco7
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sat, 1 Jan 2022 02:30 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>, the exact result for a call with the arguments 2705443,
>415131524, and 1256275308 would be 894003, so it would

PS: In the OP, "tempo" has only 16 bits instead of 32.
I did not take this reduced width into account, and used
too many test values. "tempo" corresponds to my "b".
But even when "b" fits into 16 bits, I still get errors:

q=0, p=0, r=2705443, m=4251374074, m_=17136275962, d=2, s=2
Error found!
a=2705443 b=6334 c=1736723169: exact_result=9 experimental_result=2

Re: How to avoid an overflow during multiplication?

<sqpimg$oo6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19763&group=comp.lang.c#19763

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Sat, 1 Jan 2022 13:50:23 +0100
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <sqpimg$oo6$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <sqmufp$7vc$1@dont-email.me>
<7c025db6-fe06-4083-a71b-a56a390c31ddn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 1 Jan 2022 12:50:24 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="710d0f9c710370121b9b8fb22d6c83e6";
logging-data="25350"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//J865eufj2jzH0dKfdpGZYKSt5s3y7Z4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:4AxUeEdVRqps3sFEL16J9lawydY=
In-Reply-To: <7c025db6-fe06-4083-a71b-a56a390c31ddn@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Sat, 1 Jan 2022 12:50 UTC

On 01/01/2022 00:21, james...@alumni.caltech.edu wrote:
> On Friday, December 31, 2021 at 7:53:24 AM UTC-5, Bonita Montero wrote:
>> Am 31.12.2021 um 11:02 schrieb Mateusz Viste:
>>
>>> uint32_t ticks2time(uint32_t ticks, uint32_t grouplen, uint16_t tempo)
>>> {
>>> return ticks * tempo / grouplen;
>> return (uint32_t)((uint64_t)ticks * (uint64_t)tempo / grouplen);
>>
>> On x86 f.e. the compiler will still use a 32 * 32 multiplication
>> because it sees that the casted value's upper halves are zero.
>> And after / grouplen you can safely cast the result to uint32_t.
>
> Do you meant that it will use an instruction that multiplies two 32 bit
> quantities and produces the correct 64-bit result? If so, that's nice to know,
> but not very important - how they get the right result is generally less
> important than getting the right result.

It is quite common for ISA's to have multiply instructions that take two
registers as inputs and give a double register output - so for a 32-bit
cpu, you'd get a 32x32 -> 64 bit multiply. And it is common for
compilers, even relatively weak ones, to use such instructions when you
write something like "(uint64_t) a * b", with "a" and "b" being of the
smaller size.

As you say, the important thing is to get the right answer - but it's
nice to know that your compiler is likely to get it in an efficient manner.

Re: How to avoid an overflow during multiplication?

<sqq0lb$1g77$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19765&group=comp.lang.c#19765

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Sat, 1 Jan 2022 17:48:43 +0100
Organization: . . .
Message-ID: <sqq0lb$1g77$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
<sqo47m$n9p$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="49383"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Sat, 1 Jan 2022 16:48 UTC

2021-12-31 at 18:37 -0500, James Kuyper wrote:
> Calling it ANSI C is not very useful, because that designation is
> ambiguous.

I was only stating about "my copy of ANSI C". Didn't mean to specify
any more than that, really.

> uint32_t and uint16_t, both of which are used in your code, were added
> to the language in C99. So apparently you don't consider C99 a
> complete atrocity.

My answer was obviously slightly provocative. In truth, I am no puritan
and I do pick sometime things out of C89, like uint32_t, snprintf() and
the like. I also appreciate __uint128_t very much, even though it is not
part of any standard. For practical purposes I find the gnu89 dialect
to suit me pretty well.

Mateusz

Re: How to avoid an overflow during multiplication?

<sqq2p9$1g77$2@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19767&group=comp.lang.c#19767

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Sat, 1 Jan 2022 18:24:56 +0100
Organization: . . .
Message-ID: <sqq2p9$1g77$2@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<muldiv-20211231201955@ram.dialup.fu-berlin.de>
<sqo611$vte$1@dont-email.me>
<unsigned-char-20220101012417@ram.dialup.fu-berlin.de>
<sqo86f$a7j$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="49383"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Sat, 1 Jan 2022 17:24 UTC

2021-12-31 at 19:45 -0500, James Kuyper wrote:
> it down to unsigned char introduces a problem (the integer
> promotions) that would come up with uint32_t only if INT_MAX >
> UINT32_MAX. I've frequently used systems where 'int' was a 64-bit
> type, but from the OP's description, that's unlikely to be the case
> on the platform he's targeting.

Indeed. The platform is a 40-years old system where ints are 16-bit.

Mateusz

Re: How to avoid an overflow during multiplication?

<7e62ed9d-4548-4472-9668-7afc3d45946bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19769&group=comp.lang.c#19769

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:6714:: with SMTP id e20mr34828838qtp.664.1641061863062;
Sat, 01 Jan 2022 10:31:03 -0800 (PST)
X-Received: by 2002:a05:6214:76a:: with SMTP id f10mr37495970qvz.80.1641061862929;
Sat, 01 Jan 2022 10:31:02 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 1 Jan 2022 10:31:02 -0800 (PST)
In-Reply-To: <sqmufp$7vc$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:f0ca:a00:bde2:2cc1;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:f0ca:a00:bde2:2cc1
References: <sqmkg9$157v$1@gioia.aioe.org> <sqmufp$7vc$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7e62ed9d-4548-4472-9668-7afc3d45946bn@googlegroups.com>
Subject: Re: How to avoid an overflow during multiplication?
From: already5...@yahoo.com (Michael S)
Injection-Date: Sat, 01 Jan 2022 18:31:03 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 12
 by: Michael S - Sat, 1 Jan 2022 18:31 UTC

On Friday, December 31, 2021 at 2:53:24 PM UTC+2, Bonita Montero wrote:
> Am 31.12.2021 um 11:02 schrieb Mateusz Viste:
>
> > uint32_t ticks2time(uint32_t ticks, uint32_t grouplen, uint16_t tempo)
> > {
> > return ticks * tempo / grouplen;
> return (uint32_t)((uint64_t)ticks * (uint64_t)tempo / grouplen);
>
> On x86 f.e. the compiler will still use a 32 * 32 multiplication
> because it sees that the casted value's upper halves are zero.
> And after / grouplen you can safely cast the result to uint32_t.

It seems like you are confusing x86 with x386, a.k.a. IA32.

Re: How to avoid an overflow during multiplication?

<sqq8hp$g6d$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19770&group=comp.lang.c#19770

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Sat, 1 Jan 2022 20:03:19 +0100
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <sqq8hp$g6d$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <sqmufp$7vc$1@dont-email.me>
<7e62ed9d-4548-4472-9668-7afc3d45946bn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 1 Jan 2022 19:03:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5e135780b11a8060e772ad557d0d2a36";
logging-data="16589"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Iu/J8tgNRDKAEOio2ClKTi8lKuPJlkqM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
Cancel-Lock: sha1:4K+WgH48nUIavAGLDQmQqhBr6QM=
In-Reply-To: <7e62ed9d-4548-4472-9668-7afc3d45946bn@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 1 Jan 2022 19:03 UTC

Am 01.01.2022 um 19:31 schrieb Michael S:

>> On x86 f.e. the compiler will still use a 32 * 32 multiplication
>> because it sees that the casted value's upper halves are zero.
>> And after / grouplen you can safely cast the result to uint32_t.

> It seems like you are confusing x86 with x386, a.k.a. IA32.

x86 is a common synonym for all x86 and descendant CPUs.

Pages:12345678
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor