From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 46B5938582AD; Tue, 12 Mar 2024 11:31:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 46B5938582AD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710243075; bh=v31OqUIFV/1GUyS/v5k20Zqcre6bV5MYwSg8C/JMTC0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=hew4SayBjJLdLf8UBPc/vqg2ROnWjbxTx/jXXia0TpC9uv82MbqhSyHl1EXUuybpP uJmtqQwDHQCiG6akP78Gco7czjiC2YQx7TWmoQK1lkH4aXXQzs8n9qt9zmTnBhcOfb uoRpDqqWfRhuvmRn/iY8/Z/uAIA7EA9p84tT9J3E= From: "mkretz at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/77776] C++17 std::hypot implementation is poor Date: Tue, 12 Mar 2024 11:31:11 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 7.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: mkretz at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: emsr at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D77776 --- Comment #22 from Matthias Kretz (Vir) --- I took your hypot3_scale and reduced latency and throughput. I don't think = the sqrtmax/sqrtmin limits are correct (sqrtmax=C2=B2 * 3 -> infinity). TYPE Latency Speedup Throughput= =20=20=20=20 Speedup=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 [cycles/call] [per value] [cycles/call] = [per value]=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 float, 46.5 1 12.7= =20=20=20=20=20 1=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 float, hypot3_scale 35.5 1.31 27= =20=20=20=20=20 0.47=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 float, hypot3_mkretz 30.2 1.54 12= =20=20=20=20=20 1.06=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 ---------------------------------------------------------------------------= -----------=20 TYPE Latency Speedup Throughput= =20=20=20=20 Speedup=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 [cycles/call] [per value] [cycles/call] = [per value]=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 double, 59.6 1 60= =20=20=20=20=20 1=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 double, hypot3_scale 51.2 1.16 48.8= =20=20=20=20=20 1.23=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20 double, hypot3_mkretz 40.1 1.49 35= =20=20=20=20=20 1.71 template constexpr T=20 hypot(T x, T y, T z) {=20 using limits =3D std::numeric_limits; auto prev_power2 =3D [](const T value) constexpr noexcept -> T=20 {=20 return std::exp2(std::floor(std::log2(value))); }; constexpr T sqrtmax =3D std::sqrt(limits::max()); constexpr T sqrtmin =3D std::sqrt(limits::min()); constexpr T scale_up =3D prev_power2(sqrtmax); constexpr T scale_down =3D T(1) / scale_up; constexpr T zero =3D 0; if (not (std::isnormal(x) && std::isnormal(y) && std::isnormal(z))) [[unlikely]] {=20 if (std::isinf(x) | std::isinf(y) | std::isinf(z)) return limits::infinity(); else if (std::isnan(x) | std::isnan(y) | std::isnan(z)) return limits::quiet_NaN(); const bool xz =3D x =3D=3D zero; const bool yz =3D y =3D=3D zero; const bool zz =3D z =3D=3D zero; if (xz) {=20 if (yz) return zz ? zero : z; else if (zz) return y; }=20 else if (yz && zz) return x; }=20 x =3D std::abs(x); y =3D std::abs(y); z =3D std::abs(z); T a =3D std::max(std::max(x, y), z); T b =3D std::min(std::max(x, y), z); T c =3D std::min(x, y); if (a >=3D sqrtmin && a <=3D sqrtmax) [[likely]] return std::sqrt(__builtin_assoc_barrier(c * c + b * b) + a * a); const T scale =3D a >=3D sqrtmin ? scale_down : scale_up; a *=3D scale; b *=3D scale; c *=3D scale; return std::sqrt(__builtin_assoc_barrier(c * c + b * b) + a * a) / scal= e; }=