mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Subject: Re: FE Exception triggered by comparison
Date: Mon, 25 Feb 2019 16:51:09 +0100	[thread overview]
Message-ID: <20190225155109.GB28106@voyager> (raw)
In-Reply-To: <alpine.LRH.2.02.1902250826020.8962@key0.esi.com.au>

On Mon, Feb 25, 2019 at 08:50:52AM +1100, Damian McGuckin wrote:
> The context of this is that I want a coding style which means I can compare
> numbers without affecting the exception state. And it must have zero
> overhead, and not affect the readability of the code, i.e. no function
> calls.
> 

Yet another problem brought to you by premature optimization. What you
want to know is if a certain number is NaN. There's a perfectly
acceptable idiom for that:

if (isnan(x)) {
}

This (the exception stuff) is the reason why the special cases are
always front-loaded in musl. So they can be sorted out without raising
spurious exceptions.

The only reason the above may not be acceptable to you is that it looks
like a function call. But it isn't. isnan() is a macro that doesn't
always revert to a function call.

> I want to know that
> 
> 	x != x
> 
> will always detect any NaN with affecting the exception status and that
> comparisons like
> 
> 	if (x == x) { ... }
> 	else if (x > y) { .... }
> 	else if (x < y) { .... }
> 	else { x or y are NaN }
> 
> will similarly also not modify the exception status, and in the last section
> let me process how I interpret the NaNs.
> 

Well, sorry, but these instructions do not work that way. Any operation
on a NaN, including comparison, even comparison for equality, will raise
IE, at least if it is an sNaN.

Honestly, though, I wonder what the big deal is. In most cases, if you
see a NaN, someone probably just made an invalid operation, or chose to
start another invalid operation with your function. In which case
raising IE is not spurious at all.

> Otherwise, avoidance of creating spurious exceptions forces you to start
> working at the IEEE 754 encoding level.

Or you could just use the interface provided by the C standard:

if (!isnormal(x) || !isnormal(y)) {
    guard clause here;
    return;
}
/* definitely normal x and y from this point on. */

Hopefully you don't need to deal with subnormals...

> This is counterproductive to clear
> and concise programming. Such a style is riddled through a lot of the maths
> routines in MUSL and the libraries that came before it.

No, that's just an optimization. Musl strives to minimize the numeric
error while also delivering good performance, and that means that often
a lot of boundary conditions can be detected at the same time with a
single comparison (e.g. you can detect "x < 0 || x > 2**limit ||
isinf(x)" by detecting if the exponent and sign bit together exceed a
certain limit.)

But nothing is stopping you from writing these conditions out entirely.

Ciao,
Markus


  parent reply	other threads:[~2019-02-25 15:51 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-24 13:28 Damian McGuckin
2019-02-24 17:12 ` Markus Wichmann
2019-02-24 19:25 ` Szabolcs Nagy
2019-02-24 20:04   ` Jens Gustedt
2019-02-24 21:50     ` Damian McGuckin
2019-02-25  5:21       ` Damian McGuckin
2019-02-25 15:51       ` Markus Wichmann [this message]
2019-02-26  3:55         ` Damian McGuckin
2019-02-27 14:14           ` Alexander Monakov
2019-02-27 15:38             ` Damian McGuckin
2019-02-27 16:00               ` Alexander Monakov
2019-02-27 16:09                 ` Damian McGuckin
2019-02-27 16:14                   ` Markus Wichmann
2019-02-27 16:20                     ` Damian McGuckin
2019-02-28  1:07                     ` Damian McGuckin
2019-02-28  1:27                       ` Rich Felker
2019-02-28  2:28                         ` Damian McGuckin
2019-02-27 16:32                   ` Alexander Monakov
2019-02-27 16:42                     ` Rich Felker
2019-02-27 17:08                       ` Szabolcs Nagy
2019-02-27 17:14                       ` Alexander Monakov
2019-02-27 17:26                         ` Rich Felker
2019-02-27 19:36                           ` Szabolcs Nagy
2019-02-27 19:48                           ` Alexander Monakov
2019-02-27 20:16                             ` Szabolcs Nagy
2019-02-27 20:35                               ` Rich Felker
2019-02-27 21:03                                 ` Szabolcs Nagy

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=20190225155109.GB28106@voyager \
    --to=nullplan@gmx.net \
    --cc=musl@lists.openwall.com \
    /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).