Bug 88474 - Inline built-in hypot for -ffast-math
Summary: Inline built-in hypot for -ffast-math
Status: REOPENED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: ---
Assignee: Uroš Bizjak
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2018-12-12 21:41 UTC by Joseph S. Myers
Modified: 2022-05-27 08:23 UTC (History)
1 user (show)

See Also:
Host:
Target: powerpc64le-linux
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-12-13 00:00:00


Attachments
Proposed patch (876 bytes, patch)
2018-12-13 11:02 UTC, Uroš Bizjak
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph S. Myers 2018-12-12 21:41:38 UTC
GCC should support inline code generation for hypot functions, under appropriate fast-math conditions.

glibc's bits/mathinline.h, for 32-bit non-SSE fast-math x86 only, has:

/* The argument range of the inline version of hypotl is slightly reduced.  */
__inline_mathcodeNP2 (hypot, __x, __y,
		      return __libc_sqrtl (__x * __x + __y * __y))

We're moving away from such inlines in glibc, preferring to leave it to the compiler to inline standard functions under appropriate conditions.  Although this is filed as a target issue, inlining hypot functions (given -funsafe-math-optimizations, as it may change results, and -ffinite-math-only, as it won't be correct for Inf, NaN arguments) is actually reasonably generic.  There are separate cases for when it's possible to do the * + sqrt operations on a wider (hardware) type, so avoiding reducing the argument range, and when a wider hardware type is unavailable or inappropriate to use, in which case the argument range would be reduced by inlining (although that glibc inline still does it for long double).
Comment 1 Uroš Bizjak 2018-12-13 11:02:43 UTC
Created attachment 45224 [details]
Proposed patch

Patch that implements hypotf and hypot using x87 XFmode arithmetic.
Comment 2 uros 2018-12-14 17:05:20 UTC
Author: uros
Date: Fri Dec 14 17:04:48 2018
New Revision: 267137

URL: https://gcc.gnu.org/viewcvs?rev=267137&root=gcc&view=rev
Log:
	PR target/88474
	* internal-fn.def (HYPOT): New.
	* optabs.def (hypot_optab): New.
	* config/i386/i386.md (hypot<mode>3): New expander.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/i386/i386.md
    trunk/gcc/internal-fn.def
    trunk/gcc/optabs.def
Comment 3 Uroš Bizjak 2019-03-25 10:59:17 UTC
Implemented for gcc-9.
Comment 4 Segher Boessenkool 2019-04-22 15:01:11 UTC
It isn't implemented for any other targets yet.  When I use __builtin_hypot
with -ffast-math (I tried on powerpc64-linux) I get a call to __hypot_finite,
instead of just three machine instructions, like e.g.

        fmul 2,2,2
        fmadd 1,1,1,2
        fsqrt 1,1

which is what you get for

double hypot(double x, double y) { return __builtin_sqrt(x*x + y*y); }

Reopened.  (Or do you want this PR to be just for x87?  If so, why?)
Comment 5 Jakub Jelinek 2019-05-03 09:17:56 UTC
GCC 9.1 has been released.
Comment 6 Jakub Jelinek 2019-08-12 08:57:19 UTC
GCC 9.2 has been released.
Comment 7 Jakub Jelinek 2020-03-12 11:58:54 UTC
GCC 9.3.0 has been released, adjusting target milestone.
Comment 8 Richard Biener 2021-06-01 08:12:35 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.