mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Damian McGuckin <damianm@esi.com.au>
To: musl@lists.openwall.com
Subject: Re: [musl] Considering x86-64 fenv.s to C
Date: Sat, 18 Jan 2020 15:45:27 +1100 (AEDT)	[thread overview]
Message-ID: <alpine.LRH.2.02.2001181011130.11425@key0.esi.com.au> (raw)
In-Reply-To: <20200117145350.GR30412@brightrain.aerifal.cx>

On Fri, 17 Jan 2020, Rich Felker wrote:

>> arm (bare)
>>
>> *	Empty
>
> I think you missed that there's fenv-hf.S for armhf. The default ABI
> does not have fpu/fenv.

Oops. Thanks for that.

>> *	The fldenv instruction to update the status registers has a serious
>> 	overhead which cannot be avoided in 'feraiseexcept'. No attempt is
>> 	made to optimize any unnecessary usage (as occurs in feclearexcept).
>
> One thing we could do in C is write feraiseexcept portably to raise
> the exceptions via performing appropriate floating point operations,
> rather than directly modifying status word. This would probably be
> faster on most if not all archs.

To confirm your use of 'probably' on (say) Ivy Bridge

Most places in MUSL use FORCE_EVAL for just that reason.  Just looking at 
the example within 'trunc', to force INEXACT there is an ADDSD and a MOVSD 
with memory accesses in both so 1 cycle each so 2 in total. Done inline, 
the STMXCSR and LDMXCSR are 3 cycles each so 6 in total. But in another 
example to force UNDERFLOW within 'nextafter' using FORCE_EVAL needs 4 
cycles total against the same 6. So a bit closer

Most of the time I prefer FORCE_EVAL but 'feraiseexcept' should probably 
be treated as a fall back as use the current method. My 2c is that we 
should retain 'modifying the register' concept of the 'feraiseexcept' 
routine.

.... Your call.

On a related note:

For the non-zero bits in the first 'setexceptflag' argument, doesn't this 
routine do the same thing as 'feraiseexcept' for those exceptions which 
correspond to those bits.

> Note that the high level C could avoid any action when the flags to be
> cleared are already clear.

Agreed. Nobody does this currently to keep the routine size small.

>> *	What is the best way to query '__hwcap' from inline __asm__ statement,
>
>> From *inline* asm you don't have to do it at all. You just write the
> branch in C. This is one of the reasons to prefer C.
>
>> 	specifically to verify if SSE instructions have to be supported

I have never done this in my life? On BSD there is a system call but that 
is a bit heavy handed.

> It's not to verify that they have to be supported, rather to verify
> that they *can* be used. If the bit is not set in hwcap, the
> instructions are not there and will fault if executed.

I understand this.

> C specifies it as:
>
>    "The feraiseexcept function returns zero if the excepts argument
>    is zero or if all the specified exceptions were successfully
>    raised. Otherwise, it returns a nonzero value."

Pretty vague. This is not why the M68K routines return (-1).

No routine currently checks that the exceptions were successfully raised. 
They assume that a write to the status register works. If we are going to 
check each instruction such as storing into a register works, we have a 
lot of work to do.

> It's not 100% clear to me that this is supposed to apply to invalid
> arguments rather than just some kind of failure to raise valid
> arguments, but I'd err on the side of assuming it does apply if we're
> overhauling this code in C. It's a minor issue though.

Most routines simply mask away anything invalid and then proceed.

>> powerpc
>>
>> *	All assembler
>>
>> *	I think that this architecture has more exception bits than IEEE 754
>> 	specifies. It has lots of specific cases of FE_INVALID. This needs
>> 	to be considered when dealing with FE_INVALID.
>
> I'm trying to remember -- does the hardware lack a unified FE_INVALID
> bit, so that you have to emulate it by aggregating the individual
> ones? I think that could be hidden inside a primitive that
> loads/stores the status word.

Not as far as I understand but others may wish to correct me. When they 
raise FE_INVALID, they concurrently raise a 'reason'. So if we are going 
to manually raise FE_INVALID, the mask MUST include a 'reason'. There is a 
catch-all 'miscellaneous' reason, what is call Software-Defined Condition, 
that is currently used. Other reasons are SNan, Infinity-Infinite, 
Infinity/Infinity, Zero/Zero, Infinite*Zero, Invalid Compare, Invalid 
Integer Convert and Invalid Square Root.

Reasons are a nice ideas but these 'explanations' have others chips have 
never picked up on the concept (which has been around a while).  Sadly, a 
lot of great ideas have never been commercial successes and humanity is 
the poorer for it. I digress. Sorry.

>> powerpc64

>> 	double to a union and then extract the data as a long.
>>
>> 		return (union {double f; long i;}) {get_fpscr_f()}.i;
>>
>> 	Is this style of coding universally accepted within MUSL? From my
>> 	reading of other routines, it is normally done as
>>
>> 		union {double f; long i;} f = { get_fpscr_f() };
>>
>> 		return f.i;
>>
>> 	Just curious.
>
> Yes, the compound literal form is preferred since it avoids a
> gratuitous named variable.

I would humbly suggest initially using the longer form, and then once the 
architecture of the routine is complete, we revert to the compound literal 
form where the code is simple enough to make it possible.

>> sh (SuperH??)
>>
>> *	In assembler
>>
>> *	I know zero about this assembler
>>
>> *	There is some pecularity about updating the environment. I have no
>> 	idea what is going on here. Anybody clear to elaborate?
>
> The comment about preserving precision bit is just incorrect as long
> as this is an external function. The function call ABI has precision
> set to double on function entry. If it's inline asm we might have to
> think about ensuring it's safe. I'm not sure how to constrain the
> precision on __asm__ statement entry but I assume there must be a way
> or you couldn't write inline asm using floating point operands.

Interesting. Way beyond my knowledge. Interesting chip.

>> x86_64
>>
>> *	In assembler
>>
>> *	Why does 'feclearexcept' disrespect the flags by clearing ALL x86 bits?
>
> As you said above, updating x87 status register is expensive because
> the only way to write it is to read-modify-write the whole fenv. But
> since we know on x86_64 we have sse registers, we can just move all
> the flags to the sse register, then use fnclex to clear the x87 one
> inexpensively, and the effective set of raised flags remains the same.

Thanks for the explanation. Neat.

> I think we could do this on i386 too with a couple tricks:
>
> 1. Do the same thing if sse is available (hwcap check).
>
> 2. If sse is not available, clear all flags then re-raise the desired
> set via arithmetic operations.

Simple. I like it. But more code.

Also, playing devil's advocate for a minute ....

Are we, or should we, be aiming to have

 	fetestexcept(int excepts)

and (even also)

 	feraiseexcept(int excepts)

being expanded inline so their use does not compromise optimization?  How 
feasible it this? How much will compilers get confused by the fact that 
there are side-effects of floating point operations that affect floating 
point status registers.

Regards - Damian

Pacific Engineering Systems International, 277-279 Broadway, Glebe NSW 2037
Ph:+61-2-8571-0847 .. Fx:+61-2-9692-9623 | unsolicited email not wanted here
Views & opinions here are mine and not those of any past or present employer

  reply	other threads:[~2020-01-18  4:45 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-16  4:30 Damian McGuckin
2020-01-16 15:11 ` Markus Wichmann
2020-01-16 16:14 ` Rich Felker
2020-01-16 18:56   ` Damian McGuckin
2020-01-16 19:33     ` Rich Felker
2020-01-16 21:31       ` Damian McGuckin
2020-01-17  3:36       ` Damian McGuckin
2020-01-17  3:48         ` Damian McGuckin
2020-01-17  3:53         ` David Edelsohn
2020-01-17 14:13           ` Rich Felker
2020-01-17 14:19             ` David Edelsohn
2020-01-17 14:53         ` Rich Felker
2020-01-18  4:45           ` Damian McGuckin [this message]
2020-01-18  5:29             ` Rich Felker
2020-01-19  8:50               ` Damian McGuckin
2020-01-19  9:07           ` Damian McGuckin
2020-01-19 10:42             ` Szabolcs Nagy
2020-01-19 12:25               ` Damian McGuckin
2020-01-20  5:32           ` Damian McGuckin
2020-01-20 17:38             ` Rich Felker
2020-01-20 21:11               ` [musl] Triggering Overflow (or Underflow) without triggering Inexact on i386 Damian McGuckin
2020-01-20 22:32                 ` Szabolcs Nagy
2020-01-21  3:53           ` [musl] Considering x86-64 fenv.s to C Damian McGuckin
2020-01-21  4:22             ` Rich Felker
2020-01-21  4:46               ` Damian McGuckin
2020-01-21  7:26                 ` Damian McGuckin
2020-01-17 16:41         ` Markus Wichmann
2020-01-18  1:15           ` Szabolcs Nagy
2020-01-18  5:03             ` Damian McGuckin
2020-01-18  5:37               ` Rich Felker
2020-01-18  9:40                 ` Szabolcs Nagy
2020-01-24  0:42                   ` Damian McGuckin
2020-01-24  1:11                     ` Rich Felker
2020-01-24  4:13                       ` Damian McGuckin
2020-01-24  4:55                         ` Rich Felker
2020-01-24  6:08                           ` Damian McGuckin
2020-01-24 13:44                             ` Rich Felker
2020-01-24 14:45                               ` Damian McGuckin
2020-01-24 23:59                               ` Damian McGuckin
2020-01-25  0:11                                 ` Rich Felker
2020-01-26  3:28                                   ` Damian McGuckin
2020-01-26  3:28                                   ` Damian McGuckin
2020-01-26  3:30                                   ` Damian McGuckin
2020-01-26  3:32                                   ` Damian McGuckin
2020-01-26  5:25                                   ` Damian McGuckin
2020-01-27  3:32                                   ` Damian McGuckin
2020-01-27  5:39                                   ` Damian McGuckin
2020-02-02  0:47                                   ` Damian McGuckin
2020-02-03  0:54                                   ` Damian McGuckin
2020-02-03  2:09                                     ` Rich Felker
2020-02-03  2:12                                       ` Damian McGuckin
2020-02-22 20:17                                         ` Rich Felker
2020-02-22 20:53                                           ` Damian McGuckin
2020-02-23  5:41                                           ` Damian McGuckin
2020-02-23  6:25                                             ` Rich Felker
2020-02-23  8:35                                               ` Damian McGuckin
2020-02-07 21:25                                   ` Damian McGuckin
2020-02-07 21:38                                     ` Rich Felker
2020-02-07 23:53                                       ` Damian McGuckin
2020-02-09  5:04                                       ` Damian McGuckin
2020-01-24  7:10                           ` Damian McGuckin
2020-01-18  5:04             ` Markus Wichmann
2020-02-03 14:16 [musl] PPC64(LE) support in musl requires ALTIVEC Romain Naour
2020-02-03 14:50 ` Rich Felker
2020-02-05  1:32   ` [musl] Considering x86-64 fenv.s to C Damian McGuckin

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=alpine.LRH.2.02.2001181011130.11425@key0.esi.com.au \
    --to=damianm@esi.com.au \
    --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).