Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

LOAD "LINUX",8,1 -- Topic on #LinuxGER


devel / comp.lang.c / on an analogy for verifying whether another digit fits (into an unsigned type)

SubjectAuthor
* on an analogy for verifying whether another digit fits (into an unsigned type)Meredith Montgomery
+* Re: on an analogy for verifying whether another digit fits (into anBart
|`* Re: on an analogy for verifying whether another digit fits (into an unsigned typMeredith Montgomery
| `* Re: on an analogy for verifying whether another digit fits (into an unsigned typBen Bacarisse
|  +* Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
|  |`* Re: on an analogy for verifying whether another digit fits (into an unsigned typBen Bacarisse
|  | `- Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
|  `* Re: on an analogy for verifying whether another digit fits (into an unsigned typMeredith Montgomery
|   `* Re: on an analogy for verifying whether another digit fits (into an unsigned typBen Bacarisse
|    `* Re: on an analogy for verifying whether another digit fits (into an unsigned typMeredith Montgomery
|     `- Re: on an analogy for verifying whether another digit fits (into an unsigned typBen Bacarisse
+* Re: on an analogy for verifying whether another digit fits (into anÖö Tiib
|`* Re: on an analogy for verifying whether another digit fits (into an unsigned typMeredith Montgomery
| `- Re: on an analogy for verifying whether another digit fits (into an unsigned typMeredith Montgomery
+* Re: on an analogy for verifying whether another digit fits (into anBonita Montero
|`* Re: on an analogy for verifying whether another digit fits (into anÖö Tiib
| `* Re: on an analogy for verifying whether another digit fits (into anBonita Montero
|  `* Re: on an analogy for verifying whether another digit fits (into anBonita Montero
|   `* Re: on an analogy for verifying whether another digit fits (into anBart
|    +* Re: on an analogy for verifying whether another digit fits (into anBonita Montero
|    |`* Re: on an analogy for verifying whether another digit fits (into anBonita Montero
|    | `- Re: on an analogy for verifying whether another digit fits (into anÖö Tiib
|    `* Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
|     `* Re: on an analogy for verifying whether another digit fits (into anBart
|      +* Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
|      |`* Re: on an analogy for verifying whether another digit fits (into anBart
|      | `* Re: on an analogy for verifying whether another digit fits (into an unsigned typBen Bacarisse
|      |  `* Re: on an analogy for verifying whether another digit fits (into anBart
|      |   `* Re: on an analogy for verifying whether another digit fits (into an unsigned typBen Bacarisse
|      |    `* Re: on an analogy for verifying whether another digit fits (into anBart
|      |     `- Re: on an analogy for verifying whether another digit fits (into anBart
|      `* Re: on an analogy for verifying whether another digit fits (into an unsigned typKeith Thompson
|       `* Re: on an analogy for verifying whether another digit fits (into anBart
|        +* Re: on an analogy for verifying whether another digit fits (into anLew Pitcher
|        |`- Re: on an analogy for verifying whether another digit fits (into anBart
|        +* Re: on an analogy for verifying whether another digit fits (into an unsigned typBen Bacarisse
|        |+* Re: on an analogy for verifying whether another digit fits (into anBart
|        ||+* Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
|        |||`* Re: on an analogy for verifying whether another digit fits (into an unsigned typKeith Thompson
|        ||| `- Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
|        ||+* Re: on an analogy for verifying whether another digit fits (into an unsigned typBen Bacarisse
|        |||`* Re: on an analogy for verifying whether another digit fits (into anBart
|        ||| `* Re: on an analogy for verifying whether another digit fits (into an unsigned typÖö Tiib
|        |||  `- Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
|        ||`- Re: on an analogy for verifying whether another digit fits (into an unsigned typKeith Thompson
|        |`- Re: on an analogy for verifying whether another digit fits (into anBart
|        +* Re: on an analogy for verifying whether another digit fits (into an unsigned typKeith Thompson
|        |`- Re: on an analogy for verifying whether another digit fits (into anBart
|        `* Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
|         `* Re: on an analogy for verifying whether another digit fits (into anBart
|          `* Re: on an analogy for verifying whether another digit fits (into an unsigned typKeith Thompson
|           `* Re: on an analogy for verifying whether another digit fits (into anBart
|            +- Re: on an analogy for verifying whether another digit fits (into an unsigned typKeith Thompson
|            `* Re: on an analogy for verifying whether another digit fits (into anManfred
|             `* Re: on an analogy for verifying whether another digit fits (into anBart
|              `- Re: on an analogy for verifying whether another digit fits (into anManfred
`* Re: on an analogy for verifying whether another digit fits (into an unsigned typTim Rentsch
 +* Re: on an analogy for verifying whether another digit fits (into anBonita Montero
 |`* Re: on an analogy for verifying whether another digit fits (into an unsigned typTim Rentsch
 | `* Re: on an analogy for verifying whether another digit fits (into anBonita Montero
 |  +* Re: on an analogy for verifying whether another digit fits (into an unsigned typScott Lurndal
 |  |`- Re: on an analogy for verifying whether another digit fits (into anBonita Montero
 |  `- Re: on an analogy for verifying whether another digit fits (into an unsigned typTim Rentsch
 `- Re: on an analogy for verifying whether another digit fits (into an unsigned typMeredith Montgomery

Pages:123
on an analogy for verifying whether another digit fits (into an unsigned type)

<86mtjws9cg.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!/eRJ076R8qgwl6rQB+gRxA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: on an analogy for verifying whether another digit fits (into an unsigned type)
Date: Sat, 15 Jan 2022 23:27:59 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86mtjws9cg.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="30002"; posting-host="/eRJ076R8qgwl6rQB+gRxA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:0myuaaTP6a3emx0W8kaypC7rBnI=
 by: Meredith Montgomery - Sun, 16 Jan 2022 02:27 UTC

I've been trying to think of an analogy for the verification

if( ((UINT64_MAX - c) / 10) >= r)
r = r * 10 + c;
else return -1; /* doesn't fit */

in the procedure below.

I thought of the following, which doesn't quite work. Consider fill up
a bucker of water where we must make sure not to overflow. We're given
one last glass of water to throw in the bucket and we need a strategy to
know whether that last glass would or would not overflow the bucket. We
have all the quantities involved --- the maximum volume in the bucket,
the volume of the glass of water and the volume currently in the bucket.
Say the maximum is M, r is the current volume and c is the volume of
water in the glass. The subtraction M - c represents the amount of
water that would be the exact amount to fill up the bucket completely
without a drop overflowing if we add c to the bucket. So, if the
current volume is greater than M - c, then we can't put c in the bucket.

That gives the general strategy, but it's not too good because there's
nothing in this analogy that matches the division by 10. I could of
course make a up world in which every glass of water you throw in a
bucket makes the volume in the bucket to be multiplied by a factor of 10
before the glass of water goes in. The purpose of an analogy is to make
something odd look natural, so this is a poor analogy.

Any cool ideas? Thank you.

(*) The procedure

uint64_t array_to_uint64(char *s, uint64_t *u)
{ uint64_t pos;
uint64_t r;
uint64_t c;

pos = 0; r = 0;

for ( ;; ) {
c = (uint64_t) (unsigned char) (s[pos] - '0');
if (c < 10) {
if( ((UINT64_MAX - c) / 10) >= r)
r = r * 10 + c;
else return -1; /* doesn't fit */
++pos; continue;
}
break;
}

*u = r;
return pos;
}

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ss1p1k$shu$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Sun, 16 Jan 2022 18:44:04 +0000
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <ss1p1k$shu$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 16 Jan 2022 18:44:04 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="aaa411883d63460c2462962c25e9f957";
logging-data="29246"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gzDk47/w0hJVIJK1+BIKEgZkXXPcgbWg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:zw1D3jgkztQbYSDLJjOPthB5yMs=
In-Reply-To: <86mtjws9cg.fsf@levado.to>
 by: Bart - Sun, 16 Jan 2022 18:44 UTC

On 16/01/2022 02:27, Meredith Montgomery wrote:
> uint64_t array_to_uint64(char *s, uint64_t *u)
> {
> uint64_t pos;
> uint64_t r;
> uint64_t c;
>
> pos = 0; r = 0;
>
> for ( ;; ) {
> c = (uint64_t) (unsigned char) (s[pos] - '0');
> if (c < 10) {
> if( ((UINT64_MAX - c) / 10) >= r)

Dividing by 10 each time is unnecessary (even if usually optimised to
shifts and multiplies).

You only need to make this check after you've already processed 19
characters, as it could overflow on the 20th, but not before.

I think when pos >= 18.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<f3730208-71a9-491a-b151-17b9918edfdcn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:7dc5:: with SMTP id c5mr6141454qte.173.1642364146640;
Sun, 16 Jan 2022 12:15:46 -0800 (PST)
X-Received: by 2002:ac8:7d4c:: with SMTP id h12mr14963219qtb.588.1642364146518;
Sun, 16 Jan 2022 12:15:46 -0800 (PST)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!weretis.net!feeder8.news.weretis.net!3.eu.feeder.erje.net!feeder.erje.net!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: Sun, 16 Jan 2022 12:15:46 -0800 (PST)
In-Reply-To: <86mtjws9cg.fsf@levado.to>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.164; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.164
References: <86mtjws9cg.fsf@levado.to>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f3730208-71a9-491a-b151-17b9918edfdcn@googlegroups.com>
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Sun, 16 Jan 2022 20:15:46 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 11
 by: Öö Tiib - Sun, 16 Jan 2022 20:15 UTC

On Sunday, 16 January 2022 at 04:28:22 UTC+2, Meredith Montgomery wrote:
> Any cool ideas? Thank you.

It can not be cool or something as decimal system is arbitrary and
nothing in real world (besides count of human fingers) suggests to
use it. I can try ...

If we want to take 10 times as lot of water as we currently have plus
some then it makes sense to first try if the current water (together with
1/10th of "plus some") fits into 10 times smaller bucket.

But it is unclear if that analogy helps to clarify anything.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<86k0eyplyy.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!g347Oy2unFD3X1GTkuZnxA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Date: Mon, 17 Jan 2022 09:48:05 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86k0eyplyy.fsf@levado.to>
References: <86mtjws9cg.fsf@levado.to> <ss1p1k$shu$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="47194"; posting-host="g347Oy2unFD3X1GTkuZnxA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:QJ/xdsoN0LrT5F3nEe+5JQUjvXo=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Mon, 17 Jan 2022 12:48 UTC

Bart <bc@freeuk.com> writes:

> On 16/01/2022 02:27, Meredith Montgomery wrote:
>> uint64_t array_to_uint64(char *s, uint64_t *u)
>> {
>> uint64_t pos;
>> uint64_t r;
>> uint64_t c;
>> pos = 0; r = 0;
>> for ( ;; ) {
>> c = (uint64_t) (unsigned char) (s[pos] - '0');
>> if (c < 10) {
>> if( ((UINT64_MAX - c) / 10) >= r)
>
> Dividing by 10 each time is unnecessary (even if usually optimised to
> shifts and multiplies).
>
> You only need to make this check after you've already processed 19
> characters, as it could overflow on the 20th, but not before.
>
> I think when pos >= 18.

I suppose you're right, but imagine putting such check there. It would
take even more paragraphs to explain it to myself some time later when
I'm trying to figure out what I wrote a while back.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<86a6fuplqo.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!g347Oy2unFD3X1GTkuZnxA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Date: Mon, 17 Jan 2022 09:53:03 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86a6fuplqo.fsf@levado.to>
References: <86mtjws9cg.fsf@levado.to>
<f3730208-71a9-491a-b151-17b9918edfdcn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="58458"; posting-host="g347Oy2unFD3X1GTkuZnxA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:ntinZeIJM3cooMKH30Vs5wOXtH8=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Mon, 17 Jan 2022 12:53 UTC

Öö Tiib <ootiib@hot.ee> writes:

> On Sunday, 16 January 2022 at 04:28:22 UTC+2, Meredith Montgomery wrote:
>> Any cool ideas? Thank you.
>
> It can not be cool or something as decimal system is arbitrary and
> nothing in real world (besides count of human fingers) suggests to
> use it. I can try ...
>
> If we want to take 10 times as lot of water as we currently have plus
> some then it makes sense to first try if the current water (together with
> 1/10th of "plus some") fits into 10 times smaller bucket.
>
> But it is unclear if that analogy helps to clarify anything.

Hey, I think that helps: we're at least reading out (more clearly) what
we're doing in the arithmetic expression. I'll go with that for now.
Thank you with so much.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<861r16pkus.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!g347Oy2unFD3X1GTkuZnxA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Date: Mon, 17 Jan 2022 10:12:11 -0300
Organization: Aioe.org NNTP Server
Message-ID: <861r16pkus.fsf@levado.to>
References: <86mtjws9cg.fsf@levado.to>
<f3730208-71a9-491a-b151-17b9918edfdcn@googlegroups.com>
<86a6fuplqo.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="12102"; posting-host="g347Oy2unFD3X1GTkuZnxA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:0+4EIZ3dubv8aq3BnqmyTHOU15o=
 by: Meredith Montgomery - Mon, 17 Jan 2022 13:12 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:

> Öö Tiib <ootiib@hot.ee> writes:
>
>> On Sunday, 16 January 2022 at 04:28:22 UTC+2, Meredith Montgomery wrote:
>>> Any cool ideas? Thank you.
>>
>> It can not be cool or something as decimal system is arbitrary and
>> nothing in real world (besides count of human fingers) suggests to
>> use it. I can try ...
>>
>> If we want to take 10 times as lot of water as we currently have plus
>> some then it makes sense to first try if the current water (together with
>> 1/10th of "plus some") fits into 10 times smaller bucket.
>>
>> But it is unclear if that analogy helps to clarify anything.
>
> Hey, I think that helps: we're at least reading out (more clearly) what
> we're doing in the arithmetic expression. I'll go with that for now.
> Thank you with so much.

Hm. A good analogy here is anything with a certain exponential growth
because base-10 numbers grow exponentially. So I guess a colony of
bacteria would do. I can say whenever I add, say, c amount food to the
colony, they multiply themselves by 10 and (remarkably) c new members
are born too. (Researchers are investigating why.) Also astounding is
the fact that once they get very near UINT64_MAX members, they just stop
growing no matter what --- baffling the biologists. The exact condition
is that if the new population size would exceed UINT64_MAX, the c amount
of food does nothing at all. (They stop eating at that point and
everything stays at it is.)

That's probably the best I can do.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<87a6fu5l2q.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Date: Mon, 17 Jan 2022 17:27:41 +0000
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <87a6fu5l2q.fsf@bsb.me.uk>
References: <86mtjws9cg.fsf@levado.to> <ss1p1k$shu$1@dont-email.me>
<86k0eyplyy.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="ab704c70d1bdbef5793a106f0a9c6472";
logging-data="3971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+UtDG2nLO8mNTUFJN+8vT3JFRiSpevrtk="
Cancel-Lock: sha1:rMEmbYKY8BK3gHEpEWYbphHX57g=
sha1:CFu4PwPx3OMhBtA0Asx2qoLD0HA=
X-BSB-Auth: 1.a42185997bed3c7d0ae5.20220117172741GMT.87a6fu5l2q.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 17 Jan 2022 17:27 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:

> Bart <bc@freeuk.com> writes:
>
>> On 16/01/2022 02:27, Meredith Montgomery wrote:
>>> uint64_t array_to_uint64(char *s, uint64_t *u)
>>> {
>>> uint64_t pos;
>>> uint64_t r;
>>> uint64_t c;
>>> pos = 0; r = 0;
>>> for ( ;; ) {
>>> c = (uint64_t) (unsigned char) (s[pos] - '0');
>>> if (c < 10) {
>>> if( ((UINT64_MAX - c) / 10) >= r)
>>
>> Dividing by 10 each time is unnecessary (even if usually optimised to
>> shifts and multiplies).
>>
>> You only need to make this check after you've already processed 19
>> characters, as it could overflow on the 20th, but not before.
>>
>> I think when pos >= 18.
>
> I suppose you're right, but imagine putting such check there. It would
> take even more paragraphs to explain it to myself some time later when
> I'm trying to figure out what I wrote a while back.

You could just check that the array contains a string of digits
lexicographically less than or equal to 18446744073709551615. If there
are fewer digits than this, or, at every position, the digit you have is
no greater than the corresponding digit of that number, you are ok.

--
Ben.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<OeiFJ.357177$IW4.181382@fx48.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.freedyn.de!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx48.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Newsgroups: comp.lang.c
References: <86mtjws9cg.fsf@levado.to> <ss1p1k$shu$1@dont-email.me> <86k0eyplyy.fsf@levado.to> <87a6fu5l2q.fsf@bsb.me.uk>
Lines: 65
Message-ID: <OeiFJ.357177$IW4.181382@fx48.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 17 Jan 2022 18:06:38 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 17 Jan 2022 18:06:38 GMT
X-Received-Bytes: 3641
 by: Scott Lurndal - Mon, 17 Jan 2022 18:06 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>Meredith Montgomery <mmontgomery@levado.to> writes:
>
>> Bart <bc@freeuk.com> writes:
>>
>>> On 16/01/2022 02:27, Meredith Montgomery wrote:
>>>> uint64_t array_to_uint64(char *s, uint64_t *u)
>>>> {
>>>> uint64_t pos;
>>>> uint64_t r;
>>>> uint64_t c;
>>>> pos = 0; r = 0;
>>>> for ( ;; ) {
>>>> c = (uint64_t) (unsigned char) (s[pos] - '0');
>>>> if (c < 10) {
>>>> if( ((UINT64_MAX - c) / 10) >= r)
>>>
>>> Dividing by 10 each time is unnecessary (even if usually optimised to
>>> shifts and multiplies).
>>>
>>> You only need to make this check after you've already processed 19
>>> characters, as it could overflow on the 20th, but not before.
>>>
>>> I think when pos >= 18.
>>
>> I suppose you're right, but imagine putting such check there. It would
>> take even more paragraphs to explain it to myself some time later when
>> I'm trying to figure out what I wrote a while back.
>
>You could just check that the array contains a string of digits
>lexicographically less than or equal to 18446744073709551615. If there
>are fewer digits than this, or, at every position, the digit you have is
>no greater than the corresponding digit of that number, you are ok.

That reminds me of an algorithm used on BCD mainframes to do addition
and subtraction on large (up to 100 digit) variable length operands.

The most significant digit was stored as the lowest addressed digit (nibble).

The algorithm handled addition and subtraction of two variable length
operands (with overflow detection) with a single pass starting from the
most significant digit of each operand.

"The processor uses an adder that accumulates two
fields from the most sigificant to the least significant
digit positions. Reverse addition ... has the advantage of
detecting an overflow condition prior to altering the
receiving field for the result"

"If the data fields are signed, sign manipulation takes place
prior to the addition since they are the most significant digits."

Fundamentally, the algorithm sign/zero extends the smaller operand
and adds a digit from each operand; if carry occurs and if all prior additions
(if any) had summed to nine, overflow is signaled and the operation completes.

To avoid writing to the receiving field before overflow is detected,
the processor had a nines-counter which counted the number of leading
digits each having the value 9. Once a digit no longer sums to 9 or has
a carry, the BCD digit '9' is flushed to the receiving field until the nines
counter is zero. The most recent digit sum is stored in a register
to accomodate carry (add one to the register, flush it, and move current
result to register).

1025475_B2500_B3500_RefMan_Oct69.pdf (flowchart p. 5-11)

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<87wniy3woi.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Date: Mon, 17 Jan 2022 20:59:57 +0000
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <87wniy3woi.fsf@bsb.me.uk>
References: <86mtjws9cg.fsf@levado.to> <ss1p1k$shu$1@dont-email.me>
<86k0eyplyy.fsf@levado.to> <87a6fu5l2q.fsf@bsb.me.uk>
<OeiFJ.357177$IW4.181382@fx48.iad>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="ab704c70d1bdbef5793a106f0a9c6472";
logging-data="32088"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6fmaExBM3jDv3r5iQpdQEmeZi9+BgyII="
Cancel-Lock: sha1:CI5sn3NpEB87r930MHhIQeZmo+g=
sha1:B1AIcx9rFRQqnXADXNg3/Vz03LM=
X-BSB-Auth: 1.730f2c009e1f4de8e5d7.20220117205957GMT.87wniy3woi.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 17 Jan 2022 20:59 UTC

scott@slp53.sl.home (Scott Lurndal) writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>Meredith Montgomery <mmontgomery@levado.to> writes:
>>
>>> Bart <bc@freeuk.com> writes:
>>>
>>>> On 16/01/2022 02:27, Meredith Montgomery wrote:
>>>>> uint64_t array_to_uint64(char *s, uint64_t *u)
>>>>> {
>>>>> uint64_t pos;
>>>>> uint64_t r;
>>>>> uint64_t c;
>>>>> pos = 0; r = 0;
>>>>> for ( ;; ) {
>>>>> c = (uint64_t) (unsigned char) (s[pos] - '0');
>>>>> if (c < 10) {
>>>>> if( ((UINT64_MAX - c) / 10) >= r)
>>>>
>>>> Dividing by 10 each time is unnecessary (even if usually optimised to
>>>> shifts and multiplies).
>>>>
>>>> You only need to make this check after you've already processed 19
>>>> characters, as it could overflow on the 20th, but not before.
>>>>
>>>> I think when pos >= 18.
>>>
>>> I suppose you're right, but imagine putting such check there. It would
>>> take even more paragraphs to explain it to myself some time later when
>>> I'm trying to figure out what I wrote a while back.
>>
>>You could just check that the array contains a string of digits
>>lexicographically less than or equal to 18446744073709551615. If there
>>are fewer digits than this, or, at every position, the digit you have is
>>no greater than the corresponding digit of that number, you are ok.
>
> That reminds me of an algorithm used on BCD mainframes to do addition
> and subtraction on large (up to 100 digit) variable length operands.
>
> The most significant digit was stored as the lowest addressed digit (nibble).
>
> The algorithm handled addition and subtraction of two variable length
> operands (with overflow detection) with a single pass starting from the
> most significant digit of each operand.
>
> "The processor uses an adder that accumulates two
> fields from the most sigificant to the least significant
> digit positions. Reverse addition ... has the advantage of
> detecting an overflow condition prior to altering the
> receiving field for the result"
>
> "If the data fields are signed, sign manipulation takes place
> prior to the addition since they are the most significant digits."
>
> Fundamentally, the algorithm sign/zero extends the smaller operand
> and adds a digit from each operand; if carry occurs and if all prior additions
> (if any) had summed to nine, overflow is signaled and the operation completes.
>
> To avoid writing to the receiving field before overflow is detected,
> the processor had a nines-counter which counted the number of leading
> digits each having the value 9. Once a digit no longer sums to 9 or has
> a carry, the BCD digit '9' is flushed to the receiving field until the nines
> counter is zero. The most recent digit sum is stored in a register
> to accomodate carry (add one to the register, flush it, and move current
> result to register).
>
> 1025475_B2500_B3500_RefMan_Oct69.pdf (flowchart p. 5-11)

Interesting. Thanks.

Unrelated, but I noticed this gem in the list of the system's
advantages:

d. Programming so simple it can be started by one programmer and
finished by another

--
Ben.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<SjoFJ.45462$Q11.11616@fx33.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx33.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Newsgroups: comp.lang.c
References: <86mtjws9cg.fsf@levado.to> <ss1p1k$shu$1@dont-email.me> <86k0eyplyy.fsf@levado.to> <87a6fu5l2q.fsf@bsb.me.uk> <OeiFJ.357177$IW4.181382@fx48.iad> <87wniy3woi.fsf@bsb.me.uk>
Lines: 95
Message-ID: <SjoFJ.45462$Q11.11616@fx33.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 18 Jan 2022 01:01:38 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 18 Jan 2022 01:01:38 GMT
X-Received-Bytes: 7590
X-Original-Bytes: 7539
 by: Scott Lurndal - Tue, 18 Jan 2022 01:01 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>scott@slp53.sl.home (Scott Lurndal) writes:
>
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

>>>
>>>You could just check that the array contains a string of digits
>>>lexicographically less than or equal to 18446744073709551615. If there
>>>are fewer digits than this, or, at every position, the digit you have is
>>>no greater than the corresponding digit of that number, you are ok.
>>
>> That reminds me of an algorithm used on BCD mainframes to do addition
>> and subtraction on large (up to 100 digit) variable length operands.
>>
>> The most significant digit was stored as the lowest addressed digit (nibble).
>>
>> The algorithm handled addition and subtraction of two variable length
>> operands (with overflow detection) with a single pass starting from the
>> most significant digit of each operand.
>>
>> "The processor uses an adder that accumulates two
>> fields from the most sigificant to the least significant
>> digit positions. Reverse addition ... has the advantage of
>> detecting an overflow condition prior to altering the
>> receiving field for the result"
>>
>> "If the data fields are signed, sign manipulation takes place
>> prior to the addition since they are the most significant digits."
>>
>> Fundamentally, the algorithm sign/zero extends the smaller operand
>> and adds a digit from each operand; if carry occurs and if all prior additions
>> (if any) had summed to nine, overflow is signaled and the operation completes.
>>
>> To avoid writing to the receiving field before overflow is detected,
>> the processor had a nines-counter which counted the number of leading
>> digits each having the value 9. Once a digit no longer sums to 9 or has
>> a carry, the BCD digit '9' is flushed to the receiving field until the nines
>> counter is zero. The most recent digit sum is stored in a register
>> to accomodate carry (add one to the register, flush it, and move current
>> result to register).
>>
>> 1025475_B2500_B3500_RefMan_Oct69.pdf (flowchart p. 5-11)
>
>Interesting. Thanks.
>
>Unrelated, but I noticed this gem in the list of the system's
>advantages:
>
> d. Programming so simple it can be started by one programmer and
> finished by another

Indeed, it was quite simple to program. Reading a memory
dump was a breeze:

RECD NO FILE: TRKTAP EOF = 394 7/27/2021 (TUESDAY) 17:05 PAGE 0001

1 40F87AF2F8 4000100064 0790000000 6430017400 4001000000 0010010426 94C2000064 0000060768 5600100007 7972000000
8 : 2 8 m B `
(00100) 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0003000064
...00100/001
2 E3D9D2E3C1 D7F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0
T R K T A P 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
(00100) F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0 F0F0F0F0F0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...00100/001
3 E3D76DD6E4 E340404040 4040D7D9D6 C6C9D30400 0040404040 4040E00149 6001600000 00000000C4 C9E2D24040 0040404040
T P _ O U T P R O F I L \ - - D I S K
(00100) 4040404040 4040404040 4040400000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000
...00100/001
4 D7D9C9D5E3 4040404040 4040D7D9E3 E3D9D20200 0040404040 4040E00600 8001600000 00000000C4 C9E2D24040 0040404040
P R I N T P R T T R K \ - D I S K
(00100) 4040404040 4040404040 4040400000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000
...00100/001
5 E3D9D2E3C1 D740404040 4040E3D9D2 E3C1D70400 0040404040 4040E00668 0001600000 00000000C4 C9E2D24040 0040404040
T R K T A P T R K T A P \ - D I S K
(00100) 4040404040 4040404040 4040400000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000
...00100/001
Decoding the instruction stream was trivial by eye:

0547000E LIX IX1 IX4 Save table address 003104 670740 100008
0548000E Validate(IX2,"+ERR-6:") /Validate Address
0548000M MVW IX2 -VRqst Set request ptr 003116 120002 000016 C00340
0548000M MVN +ERR-6 LL -VLab Set error label 003134 11A606 004446 C00348
0548000M VEN -Vparm Valid Do the validation 003152 350007 E00340 0D008356
0549000E
0550000E MVW RQ-CSD X2 STRCSD X7 Set descriptor 003172 120005 800004 4E000002
0551000E MPY 05 BADR SZ TBL-RN X4 IX1 Branch table offset 003192 05A502 000060 4D000004 100008
0552000E INC DOVRD1 IX1 Make code relative 003218 010707 100108 100008

note the MPY instruction:
Opcode = 05
AFBF = A502 (A Field and B field lengths, bcd; AF=A5 (5 digit unsigned literal), BF=02
A = 000060 (Literal 6 encoded in address field)
B = 4D000004 (Four digits from the base of Index Register 4)
C = 100008 (Signed 7 (AF + BF) digit result at address 8 in memory, which is also IX1)

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ss8g0e$8dv$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Wed, 19 Jan 2022 08:52:46 +0100
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <ss8g0e$8dv$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 19 Jan 2022 07:52:46 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5bb99297bc311f57a665ef11b375e3e5";
logging-data="8639"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XwJbhq/lClGxPueoO6FeiFHjSRYD7Sis="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:yNZcYKbpm3BAIKCAlTG1Rsc68ac=
In-Reply-To: <86mtjws9cg.fsf@levado.to>
Content-Language: de-DE
 by: Bonita Montero - Wed, 19 Jan 2022 07:52 UTC

How about that. I wrote it with g++ and it should fit with clang++:

#include <iostream>
#include <stdexcept>
#include <string>

using namespace std;

unsigned long long parseUll( char const *str )
{ using ull_t = unsigned long long;
ull_t value = 0;
for( unsigned char const *scn = (unsigned char const *)str; *scn; ++scn )
{
if( __builtin_umulll_overflow( value, 10, &value ) )
throw overflow_error( "parseUll overflow" );;
if( __builtin_uaddll_overflow( value, *scn - '0', &value ) )
throw overflow_error( "parseUll overflow" );;
}
return value;
}

int main()
{ for( ; ; )
try
{
string strValue;
cin >> strValue;
cout << parseUll( strValue.c_str() ) << endl;
}
catch( overflow_error & )
{
cout << "overflow" << endl;
}
}

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:2789:: with SMTP id g9mr21282703qkp.746.1642601290805;
Wed, 19 Jan 2022 06:08:10 -0800 (PST)
X-Received: by 2002:a37:a9c4:: with SMTP id s187mr21240644qke.52.1642601290650;
Wed, 19 Jan 2022 06:08:10 -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: Wed, 19 Jan 2022 06:08:10 -0800 (PST)
In-Reply-To: <ss8g0e$8dv$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=84.50.190.130; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 84.50.190.130
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Wed, 19 Jan 2022 14:08:10 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 5
 by: Öö Tiib - Wed, 19 Jan 2022 14:08 UTC

On Wednesday, 19 January 2022 at 09:52:58 UTC+2, Bonita Montero wrote:
> How about that. I wrote it with g++ and it should fit with clang++:
>

The analogy that OP asked for was likely meant from real word not from
other programming language.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ss9arn$rtm$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Wed, 19 Jan 2022 16:31:03 +0100
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <ss9arn$rtm$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 19 Jan 2022 15:31:03 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5bb99297bc311f57a665ef11b375e3e5";
logging-data="28598"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QBFH6Bjfc+5C0LXUlj+jx/EIlbmARYWk="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:j1xPqDTA7levjEPaPWPfRh934FM=
In-Reply-To: <6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Wed, 19 Jan 2022 15:31 UTC

Am 19.01.2022 um 15:08 schrieb Öö Tiib:
> On Wednesday, 19 January 2022 at 09:52:58 UTC+2, Bonita Montero wrote:
>> How about that. I wrote it with g++ and it should fit with clang++:
>>
>
> The analogy that OP asked for was likely meant from real word not from
> other programming language.

It's just about the principle; the code can be easily ported to C.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ss9g7q$5ud$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Wed, 19 Jan 2022 18:02:50 +0100
Organization: A noiseless patient Spider
Lines: 96
Message-ID: <ss9g7q$5ud$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 19 Jan 2022 17:02:50 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5bb99297bc311f57a665ef11b375e3e5";
logging-data="6093"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/75QK/120Y5cQBhdffUy0EarDcYs3SRgM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:Ikk5bweD50BR5mWOMXJ/78ISZWE=
In-Reply-To: <ss9arn$rtm$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 19 Jan 2022 17:02 UTC

Am 19.01.2022 um 16:31 schrieb Bonita Montero:

> Am 19.01.2022 um 15:08 schrieb Öö Tiib:

>> On Wednesday, 19 January 2022 at 09:52:58 UTC+2, Bonita Montero wrote:

>>> How about that. I wrote it with g++ and it should fit with clang++:
>>>

>> The analogy that OP asked for was likely meant from real word not from
>> other programming language.

> It's just about the principle; the code can be easily ported to C.

Here's a slightly better implementation with improvements for MSVC
with a benchmark.

#include <iostream>
#include <stdexcept>
#include <string>
#include <string>
#include <vector>
#include <random>
#include <sstream>
#include <chrono>
#include <immintrin.h>

using namespace std;
using namespace chrono;

#if defined(_MSC_VER)
__declspec(noinline)
#elif defined(__GNUC__)
__attribute__((noinline))
#endif
unsigned long long parseUll( char const *str )
{ unsigned long long value = 0;
for( ; *str; ++str )
{
#if (!defined(__llvm__) && defined(__GNUC__) && !defined(_MSC_VER)) ||
defined(PARSE_ULL_SIMPLE)
if( value * 10 / 10 != value )
goto overflow;
value *= 10;
unsigned char digit = *str - '0';
if( value + digit < value )
goto overflow;
value += digit;
#elif defined(__llvm__) || defined(__GNUC__)
if( __builtin_umulll_overflow( value, 10, &value ) )
goto overflow;
if( __builtin_uaddll_overflow( value, (unsigned char)*str - '0', &value) )
goto overflow;
#elif defined(_MSC_VER)
unsigned long long hi;
value = _mulx_u64( value, 10, &hi );
if( hi )
goto overflow;
// _addcarry_u64 specified but missing (MSVC 2022)
if( value + ((unsigned char)*str - '0') < value )
goto overflow;
value += (unsigned char)*str - '0';
#endif
}
return value;
overflow:
throw overflow_error( "parseUll() overflow" );
}

unsigned long long volatile vSum;

int main()
{ constexpr size_t N = 1000;
vector<string> rNums;
rNums.reserve( N );
mt19937_64 mt;
uniform_int_distribution<unsigned long long> uidValues( 0, -1 );
ostringstream oss;
for( size_t i = 0; i != N; ++i )
{
oss.str( "" );
oss << uidValues( mt );
rNums.emplace_back( oss.str() );
}
unsigned long long sum = 0;
auto start = high_resolution_clock::now();
for( size_t i = 0; i != 1000; ++i )
for( string &str : rNums )
sum += parseUll( str.c_str() );
::vSum = sum;
double ns = (int64_t)duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count() / (1000.0 * N);
cout << ns << endl;
}

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ss9l6b$ctv$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Wed, 19 Jan 2022 18:27:24 +0000
Organization: A noiseless patient Spider
Lines: 147
Message-ID: <ss9l6b$ctv$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 19 Jan 2022 18:27:23 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3064a75f4151c0f19f4b19640018b091";
logging-data="13247"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19gLz44kVNJ3PQBxW6RPPqOzwFScI/CSS8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:W2EEJvKehq2QQpiiYECQ8UGxaqo=
In-Reply-To: <ss9g7q$5ud$1@dont-email.me>
 by: Bart - Wed, 19 Jan 2022 18:27 UTC

On 19/01/2022 17:02, Bonita Montero wrote:
> Am 19.01.2022 um 16:31 schrieb Bonita Montero:
>
>> Am 19.01.2022 um 15:08 schrieb Öö Tiib:
>
>>> On Wednesday, 19 January 2022 at 09:52:58 UTC+2, Bonita Montero wrote:
>
>>>> How about that. I wrote it with g++ and it should fit with clang++:
>>>>
>
>>> The analogy that OP asked for was likely meant from real word not from
>>> other programming language.
>
>> It's just about the principle; the code can be easily ported to C.
>
> Here's a slightly better implementation with improvements for MSVC
> with a benchmark.
>
> #include <iostream>
> #include <stdexcept>
> #include <string>
> #include <string>
> #include <vector>
> #include <random>
> #include <sstream>
> #include <chrono>
> #include <immintrin.h>
>
> using namespace std;
> using namespace chrono;
>
> #if defined(_MSC_VER)
> __declspec(noinline)
> #elif defined(__GNUC__)
> __attribute__((noinline))
> #endif
> unsigned long long parseUll( char const *str )
> {
>     unsigned long long value = 0;
>     for( ; *str; ++str )
>     {
> #if (!defined(__llvm__) && defined(__GNUC__) && !defined(_MSC_VER)) ||
> defined(PARSE_ULL_SIMPLE)
>         if( value * 10 / 10 != value )
>             goto overflow;
>         value *= 10;
>         unsigned char digit = *str - '0';
>         if( value + digit < value )
>             goto overflow;
>         value += digit;
> #elif defined(__llvm__) || defined(__GNUC__)
>         if( __builtin_umulll_overflow( value, 10, &value ) )
>             goto overflow;
>         if( __builtin_uaddll_overflow( value, (unsigned char)*str -
> '0', &value) )
>             goto overflow;
> #elif defined(_MSC_VER)
>         unsigned long long hi;
>         value = _mulx_u64( value, 10, &hi );
>         if( hi )
>             goto overflow;
>         // _addcarry_u64 specified but missing (MSVC 2022)
>         if( value + ((unsigned char)*str - '0') < value )
>             goto overflow;
>         value += (unsigned char)*str - '0';
> #endif
>     }
>     return value;
> overflow:
>     throw overflow_error( "parseUll() overflow" );
> }
>
> unsigned long long volatile vSum;
>
> int main()
> {
>     constexpr size_t N = 1000;
>     vector<string> rNums;
>     rNums.reserve( N );
>     mt19937_64 mt;
>     uniform_int_distribution<unsigned long long> uidValues( 0, -1 );
>     ostringstream oss;
>     for( size_t i = 0; i != N; ++i )
>     {
>         oss.str( "" );
>         oss << uidValues( mt );
>         rNums.emplace_back( oss.str() );
>     }
>     unsigned long long sum = 0;
>     auto start = high_resolution_clock::now();
>     for( size_t i = 0; i != 1000; ++i )
>         for( string &str : rNums )
>             sum += parseUll( str.c_str() );
>     ::vSum = sum;
>     double ns = (int64_t)duration_cast<nanoseconds>(
> high_resolution_clock::now() - start ).count() / (1000.0 * N);
>     cout << ns << endl;
> }

Complicated. I used the simpler **C** code below. It's runtime was 10%
slower than the C++ (that is, elapsed time of the 10,000 outer loop for
both).

(Building the C++ took 3.3 seconds; building the C even with a slow gcc
took 0.32 seconds. Faster C compilers do it instantly.)

My version calls strlen on the string (which is also assumed here to
contain the number without signs, leading zeros, and to end on the last
digit).

In practice this information will often already be known. If I modify
parseull() to take a length (here emulated with a table of precalculated
values), then the C version is 25% faster than your C++.

Maybe your C++ version can also benefit from knowing the length, but I
can't see how from the way it's written.

------------------------------------------------------------

u64 parseull(char* s) {
int length=strlen(s);

if (length>20 || (length==20 && strcmp(s,
"18446744073709551615")>0)) {
puts("Overflow"); exit(1);
}

u64 a=*s -'0';
while (--length) {a=a*10+*++s-'0';};
return a;
}

int main(void) {
u64 a;
volatile u64 sum=0;

for (int j=0; j<10000; ++j) {
for (int i=0; i<1000; ++i) {
sum+=parseull(numbers[i]);
}
}

printf("%llu\n",sum);
}

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ss9m74$kc4$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Wed, 19 Jan 2022 19:44:52 +0100
Organization: A noiseless patient Spider
Lines: 151
Message-ID: <ss9m74$kc4$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me>
<ss9l6b$ctv$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 19 Jan 2022 18:44:52 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5bb99297bc311f57a665ef11b375e3e5";
logging-data="20868"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18s1NGOGqv1PANuURmsuUuji0kAR37TLN4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:PIdK2mIKSdq7CEfN7sQb8qVi75o=
In-Reply-To: <ss9l6b$ctv$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 19 Jan 2022 18:44 UTC

Am 19.01.2022 um 19:27 schrieb Bart:
> On 19/01/2022 17:02, Bonita Montero wrote:
>> Am 19.01.2022 um 16:31 schrieb Bonita Montero:
>>
>>> Am 19.01.2022 um 15:08 schrieb Öö Tiib:
>>
>>>> On Wednesday, 19 January 2022 at 09:52:58 UTC+2, Bonita Montero wrote:
>>
>>>>> How about that. I wrote it with g++ and it should fit with clang++:
>>>>>
>>
>>>> The analogy that OP asked for was likely meant from real word not from
>>>> other programming language.
>>
>>> It's just about the principle; the code can be easily ported to C.
>>
>> Here's a slightly better implementation with improvements for MSVC
>> with a benchmark.
>>
>> #include <iostream>
>> #include <stdexcept>
>> #include <string>
>> #include <string>
>> #include <vector>
>> #include <random>
>> #include <sstream>
>> #include <chrono>
>> #include <immintrin.h>
>>
>> using namespace std;
>> using namespace chrono;
>>
>> #if defined(_MSC_VER)
>> __declspec(noinline)
>> #elif defined(__GNUC__)
>> __attribute__((noinline))
>> #endif
>> unsigned long long parseUll( char const *str )
>> {
>>      unsigned long long value = 0;
>>      for( ; *str; ++str )
>>      {
>> #if (!defined(__llvm__) && defined(__GNUC__) && !defined(_MSC_VER)) ||
>> defined(PARSE_ULL_SIMPLE)
>>          if( value * 10 / 10 != value )
>>              goto overflow;
>>          value *= 10;
>>          unsigned char digit = *str - '0';
>>          if( value + digit < value )
>>              goto overflow;
>>          value += digit;
>> #elif defined(__llvm__) || defined(__GNUC__)
>>          if( __builtin_umulll_overflow( value, 10, &value ) )
>>              goto overflow;
>>          if( __builtin_uaddll_overflow( value, (unsigned char)*str -
>> '0', &value) )
>>              goto overflow;
>> #elif defined(_MSC_VER)
>>          unsigned long long hi;
>>          value = _mulx_u64( value, 10, &hi );
>>          if( hi )
>>              goto overflow;
>>          // _addcarry_u64 specified but missing (MSVC 2022)
>>          if( value + ((unsigned char)*str - '0') < value )
>>              goto overflow;
>>          value += (unsigned char)*str - '0';
>> #endif
>>      }
>>      return value;
>> overflow:
>>      throw overflow_error( "parseUll() overflow" );
>> }
>>
>> unsigned long long volatile vSum;
>>
>> int main()
>> {
>>      constexpr size_t N = 1000;
>>      vector<string> rNums;
>>      rNums.reserve( N );
>>      mt19937_64 mt;
>>      uniform_int_distribution<unsigned long long> uidValues( 0, -1 );
>>      ostringstream oss;
>>      for( size_t i = 0; i != N; ++i )
>>      {
>>          oss.str( "" );
>>          oss << uidValues( mt );
>>          rNums.emplace_back( oss.str() );
>>      }
>>      unsigned long long sum = 0;
>>      auto start = high_resolution_clock::now();
>>      for( size_t i = 0; i != 1000; ++i )
>>          for( string &str : rNums )
>>              sum += parseUll( str.c_str() );
>>      ::vSum = sum;
>>      double ns = (int64_t)duration_cast<nanoseconds>(
>> high_resolution_clock::now() - start ).count() / (1000.0 * N);
>>      cout << ns << endl;
>> }
>
> Complicated. I used the simpler **C** code below. It's runtime was 10%
> slower than the C++ (that is, elapsed time of the 10,000 outer loop for
> both).
>
> (Building the C++ took 3.3 seconds; building the C even with a slow gcc
> took 0.32 seconds. Faster C compilers do it instantly.)
>
> My version calls strlen on the string (which is also assumed here to
> contain the number without signs, leading zeros, and to end on the last
> digit).
>
> In practice this information will often already be known. If I modify
> parseull() to take a length (here emulated with a table of precalculated
> values), then the C version is 25% faster than your C++.
>
> Maybe your C++ version can also benefit from knowing the length, but I
> can't see how from the way it's written.
>
>
> ------------------------------------------------------------
>
>     u64 parseull(char* s) {
>         int length=strlen(s);
>
>         if (length>20 || (length==20 && strcmp(s,
> "18446744073709551615")>0)) {
>             puts("Overflow"); exit(1);
>         }
>
>         u64 a=*s -'0';
>         while (--length) {a=a*10+*++s-'0';};
>         return a;
>     }
>
>     int main(void) {
>         u64 a;
>         volatile u64 sum=0;
>
>         for (int j=0; j<10000; ++j) {
>             for (int i=0; i<1000; ++i) {
>                 sum+=parseull(numbers[i]);
>             }
>         }
>
>         printf("%llu\n",sum);
>     }
>
>

That's slower than the variants with intrinsics.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<C0ZFJ.134459$Gco3.62864@fx01.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!news.freedyn.de!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx01.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Newsgroups: comp.lang.c
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me> <6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com> <ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me> <ss9l6b$ctv$1@dont-email.me>
Lines: 9
Message-ID: <C0ZFJ.134459$Gco3.62864@fx01.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 19 Jan 2022 18:46:58 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 19 Jan 2022 18:46:58 GMT
X-Received-Bytes: 1152
 by: Scott Lurndal - Wed, 19 Jan 2022 18:46 UTC

Bart <bc@freeuk.com> writes:
>On 19/01/2022 17:02, Bonita Montero wrote:

>
>Complicated. I used the simpler **C** code below. It's runtime was 10%
>slower than the C++ (that is, elapsed time of the 10,000 outer loop for
>both).

I just use strtoll. Why reinvent the wheel?

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ss9u3b$jdr$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Wed, 19 Jan 2022 20:59:23 +0000
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <ss9u3b$jdr$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me>
<ss9l6b$ctv$1@dont-email.me> <C0ZFJ.134459$Gco3.62864@fx01.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 19 Jan 2022 20:59:23 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3064a75f4151c0f19f4b19640018b091";
logging-data="19899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FDRgyo2di3imbLnExI/SLasAN8ukv/dQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:Asf2TXvj93weSns8jHzIhgjt4vE=
In-Reply-To: <C0ZFJ.134459$Gco3.62864@fx01.iad>
 by: Bart - Wed, 19 Jan 2022 20:59 UTC

On 19/01/2022 18:46, Scott Lurndal wrote:
> Bart <bc@freeuk.com> writes:
>> On 19/01/2022 17:02, Bonita Montero wrote:
>
>>
>> Complicated. I used the simpler **C** code below. It's runtime was 10%
>> slower than the C++ (that is, elapsed time of the 10,000 outer loop for
>> both).
>
> I just use strtoll. Why reinvent the wheel?

It needs to be stroull() for this purpose, which is more elusive (gcc
has it on Windows, but the two other compilers I have don't),

As to why it's worth reinventing, if I use strtoull instead, then my
benchmarks runs in 2.8 seconds instead of 0.28 seconds; it's much slower!

Besides which, when I need to do this stuff, the requirements are more
diverse (eg dealing with numeric separators, or determining whether the
literal fits in i64, u64, i128, u128, or represents a big number).

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<X8%FJ.28$rU.26@fx34.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx34.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Newsgroups: comp.lang.c
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me> <6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com> <ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me> <ss9l6b$ctv$1@dont-email.me> <C0ZFJ.134459$Gco3.62864@fx01.iad> <ss9u3b$jdr$1@dont-email.me>
Lines: 22
Message-ID: <X8%FJ.28$rU.26@fx34.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 19 Jan 2022 21:12:23 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 19 Jan 2022 21:12:23 GMT
X-Received-Bytes: 1735
 by: Scott Lurndal - Wed, 19 Jan 2022 21:12 UTC

Bart <bc@freeuk.com> writes:
>On 19/01/2022 18:46, Scott Lurndal wrote:
>> Bart <bc@freeuk.com> writes:
>>> On 19/01/2022 17:02, Bonita Montero wrote:
>>
>>>
>>> Complicated. I used the simpler **C** code below. It's runtime was 10%
>>> slower than the C++ (that is, elapsed time of the 10,000 outer loop for
>>> both).
>>
>> I just use strtoll. Why reinvent the wheel?
>
>It needs to be stroull() for this purpose, which is more elusive (gcc
>has it on Windows, but the two other compilers I have don't),
>
>As to why it's worth reinventing, if I use strtoull instead, then my
>benchmarks runs in 2.8 seconds instead of 0.28 seconds; it's much slower!

Does your benchmark measure anything useful?

Very, very, very few application will have strtoull as a dominant
performance driver.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ssa342$rih$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Wed, 19 Jan 2022 22:25:05 +0000
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <ssa342$rih$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me>
<ss9l6b$ctv$1@dont-email.me> <C0ZFJ.134459$Gco3.62864@fx01.iad>
<ss9u3b$jdr$1@dont-email.me> <X8%FJ.28$rU.26@fx34.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 19 Jan 2022 22:25:06 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3064a75f4151c0f19f4b19640018b091";
logging-data="28241"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+iMjAsYRiuaubMxMDPUZc47kVNAHUOj4o="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:IUvzF92np+3LTfJY+oO4lgo4DdY=
In-Reply-To: <X8%FJ.28$rU.26@fx34.iad>
 by: Bart - Wed, 19 Jan 2022 22:25 UTC

On 19/01/2022 21:12, Scott Lurndal wrote:
> Bart <bc@freeuk.com> writes:
>> On 19/01/2022 18:46, Scott Lurndal wrote:
>>> Bart <bc@freeuk.com> writes:
>>>> On 19/01/2022 17:02, Bonita Montero wrote:
>>>
>>>>
>>>> Complicated. I used the simpler **C** code below. It's runtime was 10%
>>>> slower than the C++ (that is, elapsed time of the 10,000 outer loop for
>>>> both).
>>>
>>> I just use strtoll. Why reinvent the wheel?
>>
>> It needs to be stroull() for this purpose, which is more elusive (gcc
>> has it on Windows, but the two other compilers I have don't),
>>
>> As to why it's worth reinventing, if I use strtoull instead, then my
>> benchmarks runs in 2.8 seconds instead of 0.28 seconds; it's much slower!
>
> Does your benchmark measure anything useful?

Only text to binary conversion of numbers, but then nobody ever does
that do that? Apart from processing textual formats such XML, HTML,
source code of virtually every language, configuration files, database
files, CDF/CSV and other data files.

>
> Very, very, very few application will have strtoull as a dominant
> performance driver.

Plenty of slow software about. Ignoring things like this adds up.

But in the case of strtoull, there are other issues with it:

* Not all compilers may recognise it

* The performance depends on the library used

Using your own routine for this simple task eliminates those variables.

The timing I quoted was the worst:

* gcc/strtoull on Windows: 2.8 seconds (for 1M conversions)

* gcc/strtoull on WSL: 0.6 seconds

* gcc/_strtoui64 on Windows (in msvcrt): 0.7 seconds

* My parseull routine on Windows: 0.28/0.35 seconds (with/without length)

The intention was anyway to match that complex C++ code, which was also
the wrong language, and also took 10 times as long to compile.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<87tudz2his.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Date: Thu, 20 Jan 2022 03:49:31 +0000
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <87tudz2his.fsf@bsb.me.uk>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me>
<ss9l6b$ctv$1@dont-email.me> <C0ZFJ.134459$Gco3.62864@fx01.iad>
<ss9u3b$jdr$1@dont-email.me> <X8%FJ.28$rU.26@fx34.iad>
<ssa342$rih$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="dfcd1fb40a8bbfe2bdbfd141a475be77";
logging-data="8948"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NBQC5nMhsTg4W72i6ihr/L3i1VE/52zc="
Cancel-Lock: sha1:ZKpuRbNDOQbDLfNn8bX9wWgW9O4=
sha1:LcrAMUsvZfb3Dhct8VpmgIA8D38=
X-BSB-Auth: 1.30b11164d61e616f8775.20220120034931GMT.87tudz2his.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 20 Jan 2022 03:49 UTC

Bart <bc@freeuk.com> writes:

> But in the case of strtoull, there are other issues with it:
>
> * Not all compilers may recognise it

It's almost always library issue, not a compiler issue. Any
implementation (compiler + library) that does not have it is a poor one
since it's been standard for a long time now.

> * The performance depends on the library used
>
> Using your own routine for this simple task eliminates those variables.
>
> The timing I quoted was the worst:
>
> * gcc/strtoull on Windows: 2.8 seconds (for 1M conversions)
>
> * gcc/strtoull on WSL: 0.6 seconds
>
> * gcc/_strtoui64 on Windows (in msvcrt): 0.7 seconds

You keep citing figures with little useful information. What length
numbers? What are these various (apparently slow) implementation of
strtoull that you have managed to find? Calling them gcc/strtoull is
not helpful.

For 10^6 numbers with, on average, 18.3 digits, the strtoull on a recent
Ubuntu install (glibc 2.34) does the conversions in 0.042 seconds
(i5-8265U CPU). Is your machine really more than 10 times slower than
my laptop?

> * My parseull routine on Windows: 0.28/0.35 seconds (with/without
> length)

This one probably doesn't do the same job, so it's not a direct
comparison.

--
Ben.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ssb1pb$nk6$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Thu, 20 Jan 2022 08:08:26 +0100
Organization: A noiseless patient Spider
Lines: 158
Message-ID: <ssb1pb$nk6$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me>
<ss9l6b$ctv$1@dont-email.me> <ss9m74$kc4$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 20 Jan 2022 07:08:27 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="039a5368f066a038184275fcc8ccf950";
logging-data="24198"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ev9IjyK4K8VGUoIdCFEgRSniOmpxHmfg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:6CpmBr6QEBsjQ1JmmFMlHB5lP5M=
In-Reply-To: <ss9m74$kc4$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 20 Jan 2022 07:08 UTC

Am 19.01.2022 um 19:44 schrieb Bonita Montero:

> That's slower than the variants with intrinsics.

Here's a little benchmark:

#include <iostream>
#include <stdexcept>
#include <string>
#include <string>
#include <vector>
#include <random>
#include <sstream>
#include <chrono>
#include <cstring>
#include <immintrin.h>

using namespace std;
using namespace chrono;

unsigned long long parseUllIntrinsic( char const *str );
unsigned long long parseUllStd( char const *str );
unsigned long long parseUllBart( char const *str );

unsigned long long volatile vSum;

int main()
{ constexpr size_t N = 1000;
vector<string> rNums;
rNums.reserve( N );
mt19937_64 mt;
uniform_int_distribution<unsigned long long> uidValues( 0, -1 );
ostringstream oss;
for( size_t i = 0; i != N; ++i )
{
oss.str( "" );
oss << uidValues( mt );
rNums.emplace_back( oss.str() );
}
auto bench = [&]( unsigned long long (*parseUllFn)( char const *) ) ->
double
{
unsigned long long sum = 0;
auto start = high_resolution_clock::now();
for( size_t i = 0; i != 10'000; ++i )
for( string &str : rNums )
sum += parseUllFn( str.c_str() );
::vSum = sum;
return (int64_t)duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count() / (10'000.0 * N);
};
cout << "intrinsic: " << bench( parseUllIntrinsic ) << endl;
cout << "std: " << bench( parseUllStd ) << endl;
cout << "Bart: " << bench( parseUllBart ) << endl;
}

#if defined(_MSC_VER)
__declspec(noinline)
#elif defined(__GNUC__)
__attribute__((noinline))
#endif
unsigned long long parseUllIntrinsic( char const *str )
{ if( !*str )
return 0;
unsigned long long value = (unsigned char)*str++ - '0';
for( ; *str; ++str )
{
#if defined(__llvm__) || defined(__GNUC__)
if( __builtin_umulll_overflow( value, 10, &value ) )
goto overflow;
if( __builtin_uaddll_overflow( value, (unsigned char)*str - '0', &value) )
goto overflow;
#elif defined(_MSC_VER)
unsigned long long hi;
value = _mulx_u64( value, 10, &hi );
if( hi )
goto overflow;
// _addcarry_u64 specified but missing (MSVC 2022)
if( value + ((unsigned char)*str - '0') < value )
goto overflow;
value += (unsigned char)*str - '0';
#else
#error no intinsic version
#endif
}
return value;
overflow:
throw overflow_error( "parseUll() overflow" );
}

#if defined(_MSC_VER)
__declspec(noinline)
#elif defined(__GNUC__)
__attribute__((noinline))
#endif
unsigned long long parseUllStd( char const *str )
{ unsigned long long value;
if( !*str )
return 0;
value = (unsigned char)*str++ - '0';
for( ; *str; ++str )
{
if( value * 10 / 10 != value )
goto overflow;
value *= 10;
unsigned char digit = *str - '0';
if( value + digit < value )
goto overflow;
value += digit;
}
return value;
overflow:
throw overflow_error( "parseUll() overflow" );
}

#if defined(_MSC_VER)
__declspec(noinline)
#elif defined(__GNUC__)
__attribute__((noinline))
#endif
unsigned long long parseUllBart( char const *str )
{ size_t len = strlen( str );
unsigned long long value;
if( len > 20 || len == 20 && strcmp( str, "18446744073709551615" ) > 0 )
goto overflow;
if( !*str )
return 0;
value = (unsigned char)*str++ - '0';
while( *str )
value *= 10,
value += (unsigned char)*str++ - '0';
return value;
overflow:
throw overflow_error( "parseUll() overflow" );
}

Here are the MSVC-results:

intrinsic: 20.4304
std: 20.3481
Bart: 22.2739

The gcc/O2-results:

intrinsic: 20.4639
std: 20.7395
Bart: 23.3007

The clang++/O2-results:

intrinsic: 21.1037
std: 21.9567
Bart: 22.7341

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<ssbc4i$9a0$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
Date: Thu, 20 Jan 2022 10:05:06 +0000
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <ssbc4i$9a0$1@dont-email.me>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me>
<ss9l6b$ctv$1@dont-email.me> <C0ZFJ.134459$Gco3.62864@fx01.iad>
<ss9u3b$jdr$1@dont-email.me> <X8%FJ.28$rU.26@fx34.iad>
<ssa342$rih$1@dont-email.me> <87tudz2his.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 20 Jan 2022 10:05:06 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d0672c75265e6b57e90f5faaf813e36f";
logging-data="9536"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fH11AYixzAMFw4YoVfWV8F8G1J+H6bqg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:36N6AixE3BPgAJZcsT72SEimh5k=
In-Reply-To: <87tudz2his.fsf@bsb.me.uk>
 by: Bart - Thu, 20 Jan 2022 10:05 UTC

On 20/01/2022 03:49, Ben Bacarisse wrote:
> Bart <bc@freeuk.com> writes:
>
>> But in the case of strtoull, there are other issues with it:
>>
>> * Not all compilers may recognise it
>
> It's almost always library issue, not a compiler issue. Any
> implementation (compiler + library) that does not have it is a poor one
> since it's been standard for a long time now.
>
>> * The performance depends on the library used
>>
>> Using your own routine for this simple task eliminates those variables.
>>
>> The timing I quoted was the worst:
>>
>> * gcc/strtoull on Windows: 2.8 seconds (for 1M conversions)
>>
>> * gcc/strtoull on WSL: 0.6 seconds
>>
>> * gcc/_strtoui64 on Windows (in msvcrt): 0.7 seconds
>
> You keep citing figures with little useful information. What length
> numbers? What are these various (apparently slow) implementation of
> strtoull that you have managed to find? Calling them gcc/strtoull is
> not helpful.
>

The full program is here:

https://github.com/sal55/langs/blob/master/parseull.c

You need to uncomment the routine you want in the inner loop. The
numbers are the ones generated with BM's program. Timings are elapsed
time from running the program, measured externally.

> For 10^6 numbers with, on average, 18.3 digits, the strtoull on a recent
> Ubuntu install (glibc 2.34) does the conversions in 0.042 seconds
> (i5-8265U CPU). Is your machine really more than 10 times slower than
> my laptop?

Sorry, that was my mistake: it's 10^7 conversions (originally 10^6 in
the C++ code but that was too quick to easily measure).

>> * My parseull routine on Windows: 0.28/0.35 seconds (with/without
>> length)
>
> This one probably doesn't do the same job, so it's not a direct
> comparison.

It was supposed to do the same job as BM's program: take a string
containing only digits and convert them while checking for overflows.

Although mine won't allow leading zeros if the overflow check is to
work; that would slow it down slightly to eliminate them.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<87ilue2vda.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: on an analogy for verifying whether another digit fits (into an unsigned type)
Date: Thu, 20 Jan 2022 17:02:41 +0000
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <87ilue2vda.fsf@bsb.me.uk>
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com>
<ss9arn$rtm$1@dont-email.me> <ss9g7q$5ud$1@dont-email.me>
<ss9l6b$ctv$1@dont-email.me> <C0ZFJ.134459$Gco3.62864@fx01.iad>
<ss9u3b$jdr$1@dont-email.me> <X8%FJ.28$rU.26@fx34.iad>
<ssa342$rih$1@dont-email.me> <87tudz2his.fsf@bsb.me.uk>
<ssbc4i$9a0$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="dfcd1fb40a8bbfe2bdbfd141a475be77";
logging-data="21780"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198ohiIjNuCTaxcdfObw/a3Jner9pCFKtU="
Cancel-Lock: sha1:pKZBq5gJJgY7wEYKVH3ETV9ilIQ=
sha1:0aoxmrmz4Jg8Tcsab5Nah74ie50=
X-BSB-Auth: 1.41800b11cd81f0b73db4.20220120170241GMT.87ilue2vda.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 20 Jan 2022 17:02 UTC

Bart <bc@freeuk.com> writes:

> On 20/01/2022 03:49, Ben Bacarisse wrote:
>> Bart <bc@freeuk.com> writes:

>>> * My parseull routine on Windows: 0.28/0.35 seconds (with/without
>>> length)
>>
>> This one probably doesn't do the same job, so it's not a direct
>> comparison.
>
> It was supposed to do the same job as BM's program: take a string
> containing only digits and convert them while checking for overflows.

Right. But it does not do the same job as the other functions you gave
timings for /in the post I replied to/.

--
Ben.

Re: on an analogy for verifying whether another digit fits (into an unsigned type)

<f824df51-7eeb-478b-b064-227b08bd4c5dn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:208:: with SMTP id b8mr21101qtx.657.1642700867944;
Thu, 20 Jan 2022 09:47:47 -0800 (PST)
X-Received: by 2002:ac8:678c:: with SMTP id b12mr101510qtp.20.1642700867815;
Thu, 20 Jan 2022 09:47:47 -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: Thu, 20 Jan 2022 09:47:47 -0800 (PST)
In-Reply-To: <ssb1pb$nk6$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.164; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.164
References: <86mtjws9cg.fsf@levado.to> <ss8g0e$8dv$1@dont-email.me>
<6df246f4-204e-4fd1-ad91-bb4fce96f100n@googlegroups.com> <ss9arn$rtm$1@dont-email.me>
<ss9g7q$5ud$1@dont-email.me> <ss9l6b$ctv$1@dont-email.me> <ss9m74$kc4$1@dont-email.me>
<ssb1pb$nk6$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f824df51-7eeb-478b-b064-227b08bd4c5dn@googlegroups.com>
Subject: Re: on an analogy for verifying whether another digit fits (into an
unsigned type)
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Thu, 20 Jan 2022 17:47:47 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 16
 by: Öö Tiib - Thu, 20 Jan 2022 17:47 UTC

On Thursday, 20 January 2022 at 09:08:39 UTC+2, Bonita Montero wrote:
> Am 19.01.2022 um 19:44 schrieb Bonita Montero:
>
> > That's slower than the variants with intrinsics.
> Here's a little benchmark:

All those test functions do something strange compared to what OP's posted
code did. OP posted relatively reasonable function like that:

uint64_t array_to_uint64(char *s, uint64_t *u) {./*...*/}

For example to string "15A" that function returned 2 and put 15 into value
pointed by u. That made sense.
Your code however returns 167. That feels nonsense, where it was specified?
It does not matter at what speed a code gives such nonsense answers. At
least among my customers.

Pages:123
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor