Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

"Indecision is the basis of flexibility" -- button at a Science Fiction convention.


devel / comp.std.c / Re: casts and contraction of floating expressions

SubjectAuthor
* casts and contraction of floating expressionsVincent Lefevre
`- casts and contraction of floating expressionsTim Rentsch

1
casts and contraction of floating expressions

<20231122162220$3bc5@qaa.vinc17.org>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=1446&group=comp.std.c#1446

  copy link   Newsgroups: comp.std.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vincent-news@vinc17.net (Vincent Lefevre)
Newsgroups: comp.std.c
Subject: casts and contraction of floating expressions
Date: Wed, 22 Nov 2023 16:35:57 -0000 (UTC)
Organization: a training zoo
Lines: 59
Message-ID: <20231122162220$3bc5@qaa.vinc17.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 22 Nov 2023 16:35:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="95f5c5b1303a9872b3f2e7ccc7517d12";
logging-data="1485955"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Kr8K7qCJ6HFnrGBya7EC9"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-13-amd64 (x86_64))
Cancel-Lock: sha1:DEROzRNysSh0iUSd72F2u4IvXcQ=
 by: Vincent Lefevre - Wed, 22 Nov 2023 16:35 UTC

The C standard says in 6.5:

A floating expression may be contracted, that is, evaluated as
though it were a single operation, thereby omitting rounding errors
implied by the source code and the expression evaluation method.

with the following note:

The intermediate operations in the contracted expression are
evaluated as if to infinite range and precision, while the final
operation is rounded to the format determined by the expression
evaluation method. [...]

But it says nothing about casts, which are explicit conversion
operations that normally remove any intermediate extended precision,
as if they were assimilated to a real rounding operation (rather
than something that would just give an additional rounding error),
just like the rint() function.

So my question is:

In the following code

volatile double a = 0x1.00000001p+0;
volatile double b = -1.0;

printf ("%a\n", (double) (a * a) + b);
printf ("%a\n", (float) (a * a) + b);

may the (double) (a * a) + b and (float) (a * a) + b FP expressions
be contracted with the use of a FMA, i.e. as if fma(a,a,b) were used?

Based on my above remark, I would say "no".

But GCC (at least until a 14.0.0 20231017 snapshot), Clang (at least
until version 17) and Intel's oneAPI compiler 2021.3.0.20210619 all
contract (double) (a * a) + b with the use of a FMA; thus they output
0x1.000000008p-31 instead of 0x1p-31.

However, these compilers do not contract (float) (a * a) + b:
they output 0x1p-31. It is rather surprising that they treat it
differently.

Notes:
* With GCC, at least the -O2 optimization level is needed for
contraction of FP expressions, and options like -std=c17 must not
be used, because GCC disables the contraction of FP expressions
if such an option is present, due to the fact that GCC doesn't
support the STDC FP_CONTRACT pragma.
* A compiler option like -march=native may be needed in case the
default architecture is assumed not to have a hardware FMA.
* Intel's oneAPI compiler 2021.3.0.20210619 is actually buggy as
it contracts FP expressions even with -ffp-contract=off and the
STDC FP_CONTRACT pragma set to OFF.

--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

Re: casts and contraction of floating expressions

<86jzq3vwo0.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=1447&group=comp.std.c#1447

  copy link   Newsgroups: comp.std.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.std.c
Subject: Re: casts and contraction of floating expressions
Date: Mon, 27 Nov 2023 11:11:27 -0800
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <86jzq3vwo0.fsf@linuxsc.com>
References: <20231122162220$3bc5@qaa.vinc17.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="7d98286dbd687c0424bff5d7e57fa81b";
logging-data="4090333"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fcjGL5sWWKv/4LtmTS6ZP2AALQWDgmOc="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:UjdHoTlamT89xscDMgYnqY82db8=
sha1:YC4y+qUleQbFs/0TNhEnYG7Ci9E=
 by: Tim Rentsch - Mon, 27 Nov 2023 19:11 UTC

Vincent Lefevre <vincent-news@vinc17.net> writes:

> The C standard says in 6.5:
>
> A floating expression may be contracted, that is, evaluated as
> though it were a single operation, thereby omitting rounding errors
> implied by the source code and the expression evaluation method.
>
> with the following note:
>
> The intermediate operations in the contracted expression are
> evaluated as if to infinite range and precision, while the final
> operation is rounded to the format determined by the expression
> evaluation method. [...]
>
> But it says nothing about casts, which are explicit conversion
> operations that normally remove any intermediate extended precision,
> as if they were assimilated to a real rounding operation (rather
> than something that would just give an additional rounding error),
> just like the rint() function.
>
> So my question is:
>
> In the following code
>
> volatile double a = 0x1.00000001p+0;
> volatile double b = -1.0;
>
> printf ("%a\n", (double) (a * a) + b);
> printf ("%a\n", (float) (a * a) + b);
>
> may the (double) (a * a) + b and (float) (a * a) + b FP expressions
> be contracted with the use of a FMA, i.e. as if fma(a,a,b) were used?
>
> Based on my above remark, I would say "no".
>
> But GCC (at least until a 14.0.0 20231017 snapshot), Clang (at least
> until version 17) and Intel's oneAPI compiler 2021.3.0.20210619 all
> contract (double) (a * a) + b with the use of a FMA; thus they output
> 0x1.000000008p-31 instead of 0x1p-31.
>
> However, these compilers do not contract (float) (a * a) + b:
> they output 0x1p-31. It is rather surprising that they treat it
> differently.

I can see how an argument might be made that contraction is
allowed for the (double) cast line, and not allowed for the
line with the (float) cast. A good test to do would be the
same exression but with a (long double) cast in place of the
(double) or (float) cast. I think an argument could be made
either way that contraction is allowed in that situation.

I think I'm mostly on your side, the cast not preventing the
contraction is unexpected and at least at first blush at
odds with what else the standard says. I completely agree
that the point needs clarifying.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor