From: "Stefan Kanthak" <stefan.kanthak@nexgo.de>
To: "Rich Felker" <dalias@libc.org>
Cc: "Szabolcs Nagy" <nsz@port70.net>, <musl@lists.openwall.com>
Subject: Re: [musl] [PATCH] Properly simplified nextafter()
Date: Wed, 11 Aug 2021 18:50:28 +0200 [thread overview]
Message-ID: <A626BC84BF3C4C3B88E7F696383397A7@H270> (raw)
In-Reply-To: <20210811160938.GB13220@brightrain.aerifal.cx>
Rich Felker <dalias@libc.org> wrote:
[...]
> static __inline unsigned __FLOAT_BITS(float __f)
> {
> union {float __f; unsigned __i;} __u;
> __u.__f = __f;
> return __u.__i;
> }
>
> #define isnan(x) ( \
> sizeof(x) == sizeof(float) ? (__FLOAT_BITS(x) & 0x7fffffff) > 0x7f800000 : \
> sizeof(x) == sizeof(double) ? (__DOUBLE_BITS(x) & -1ULL>>1) > 0x7ffULL<<52 : \
> __fpclassifyl(x) == FP_NAN)
>
> So, nope.
GCC typically uses its __builtin_isnan() for isnan(), which doesn't
use integer instructions or reloads:
$ cat isnan.c
int foo(double x) {
return isnan(x);
}
int bar(double x) {
return __builtin_isnan(x);
}
$ gcc -S -O3 -o- isnan.c
...
xorl %eax, %eax
ucomisd %xmm0, %xmm0
setp %al
ret
...
> Unless it's doing some extremely high level rewriting of
> this inspection of the representation.
It performs the high-level substitution of isnan with __builtin_isnan
[...]
>> GCC generates here at least 12 instructions more, also longer ones,
>> including 2 movabs to load 0x8000000000000000 and 0x7FFFFFFFFFFFFFFF,
>> so the code is more than 50% fatter, mixes integer SSE and FP SSE
>> instructions which incur 2 cycles penalty on many Intel CPUs, with
>> WAY TOO MANY not so predictable (un)conditional branches.
>
> We don't use asm to optimize out 2 cycles.
This is just ONE of the many deficiencies of the code GCC generates.
> If the compiler is choosing a bad way to perform these loads the compiler
> should be fixed. But I don't think it matters in any measurable way in real usage.
On several families of Intel Core-i processors this 1 cycle penalty occurs
EVERY time an SSE register is accessed by a FP instruction AFTER an integer
instruction and vice versa!
BAD:
pxor xmm1, xmm1
cmpsd xmm0, xmm1
good:
xorpd xmm1, xmm1
cmpsd xmm0, xmm1
Stefan
next prev parent reply other threads:[~2021-08-11 17:04 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-10 6:23 Stefan Kanthak
2021-08-10 21:34 ` Szabolcs Nagy
2021-08-10 22:53 ` Stefan Kanthak
2021-08-11 2:40 ` Rich Felker
2021-08-11 15:44 ` Stefan Kanthak
2021-08-11 16:09 ` Rich Felker
2021-08-11 16:50 ` Stefan Kanthak [this message]
2021-08-11 17:57 ` Rich Felker
2021-08-11 22:16 ` Szabolcs Nagy
2021-08-11 22:43 ` Stefan Kanthak
2021-08-12 0:59 ` Rich Felker
2021-08-11 8:23 ` Szabolcs Nagy
2021-08-13 12:04 ` [musl] [PATCH #2] " Stefan Kanthak
2021-08-13 15:59 ` Rich Felker
2021-08-13 18:30 ` Stefan Kanthak
2021-08-14 4:07 ` Damian McGuckin
2021-08-14 22:45 ` Szabolcs Nagy
2021-08-14 23:46 ` Szabolcs Nagy
2021-08-15 7:04 ` Stefan Kanthak
2021-08-15 7:46 ` Ariadne Conill
2021-08-15 13:59 ` Rich Felker
2021-08-15 14:57 ` Ariadne Conill
2021-08-15 8:24 ` Damian McGuckin
2021-08-15 14:03 ` Rich Felker
2021-08-15 15:10 ` Damian McGuckin
2021-08-15 14:56 ` Szabolcs Nagy
2021-08-15 15:19 ` Stefan Kanthak
2021-08-15 15:48 ` Rich Felker
2021-08-15 16:29 ` Stefan Kanthak
2021-08-15 16:49 ` Rich Felker
2021-08-15 20:52 ` Stefan Kanthak
2021-08-15 21:48 ` Rich Felker
2021-08-15 15:52 ` Ariadne Conill
2021-08-15 16:09 ` Rich Felker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=A626BC84BF3C4C3B88E7F696383397A7@H270 \
--to=stefan.kanthak@nexgo.de \
--cc=dalias@libc.org \
--cc=musl@lists.openwall.com \
--cc=nsz@port70.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).