From patchwork Sat Nov 2 07:10:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 2005392 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AtIaz4hR; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XgTRD6M13z1xwc for ; Sat, 2 Nov 2024 18:11:01 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5529A3858283 for ; Sat, 2 Nov 2024 07:10:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id C32CA3858D37 for ; Sat, 2 Nov 2024 07:10:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C32CA3858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C32CA3858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730531439; cv=none; b=h9FqxgVuYqsuch9q/5DkzHOEeta64+5OZhbodzYTmNzeP9fi3Jb3ZMZmgS0tF+9zf2OFuV8+zCR8gJogFRCDJZUv3sHOvoX2xiPAblHjnI4NGphpFkIzDGP9g15CLP0zyCZEwgNntHtYHjiL3kl3mcVADq4ZLwQduwggzRDsqDY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730531439; c=relaxed/simple; bh=wIqAsFuLIX4nPZrZE3hvtBmUpJkfky0RBfdtOQxCI/w=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=MbGvrA3k3YtZSuW92cPVx8baR4e6tDVLLYnaG1mBBXNXZD6bKcKQmAO1Y5jUWqGfNlFPqpaZKWPbAMAk0jgYtrsMdi5If6RYuBnnq+u3K7KYfE6Hp7wm8VJ799y4TupTl7RBjErTdIeNSD0X6/TJFWMnK38rFF04IOsOGqzrsXk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730531437; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=5RR8upSHjP9VPAN2LYmpkY+5KHnZoOgYZoJu9yqXipY=; b=AtIaz4hR7CjuTpwPmKzbJSMCSTWQC8uIUzv1r40ghLI1hcOiNURSHt0KPwJ+rZmzv59G2l dJp6M6LUjSXtOYk+3zHiFezaVKkHkVbtTiBPH0vWVKWTInRVF3jZjO0q00dOaJDqEynN8O qjjHmJn1yCAMKpxJfy5NjodL78v7880= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-657-NTmM-4bfPquRw-HEFhAtvA-1; Sat, 02 Nov 2024 03:10:34 -0400 X-MC-Unique: NTmM-4bfPquRw-HEFhAtvA-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CD8CD195608A; Sat, 2 Nov 2024 07:10:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.16]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 286551956089; Sat, 2 Nov 2024 07:10:31 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 4A27ASMJ2389715 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 2 Nov 2024 08:10:29 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4A27ASrW2389713; Sat, 2 Nov 2024 08:10:28 +0100 Date: Sat, 2 Nov 2024 08:10:28 +0100 From: Jakub Jelinek To: libstdc++@gcc.gnu.org Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Fix up std::{,b}float16_t std::{ilogb,l{,l}r{ound,int}} [PR117406] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! These overloads incorrectly cast the result of the float __builtin_* to _Float or __gnu_cxx::__bfloat16_t. For std::ilogb that changes behavior for the INT_MAX return because that isn't representable in either of the floating point formats, for the others it is I think just a very inefficient hop from int/long/long long to std::{,b}float16_t and back. I mean for the round/rint cases, either the argument is small and then the return value should be representable in the floating point format too, or it is too large that the argument is already integral and then it should just return the argument with the round trips. Too large value is unspecified unlike ilogb. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-11-02 Jakub Jelinek PR libstdc++/117406 * include/c_global/cmath (std::ilogb(_Float16), std::llrint(_Float16), std::llround(_Float16), std::lrint(_Float16), std::lround(_Float16)): Don't cast __builtin_* return to _Float16. (std::ilogb(__gnu_cxx::__bfloat16_t), std::llrint(__gnu_cxx::__bfloat16_t), std::llround(__gnu_cxx::__bfloat16_t), std::lrint(__gnu_cxx::__bfloat16_t), std::lround(__gnu_cxx::__bfloat16_t)): Don't cast __builtin_* return to __gnu_cxx::__bfloat16_t. * testsuite/26_numerics/headers/cmath/117406.cc: New test. Jakub --- libstdc++-v3/include/c_global/cmath.jj 2024-10-29 11:13:47.741803660 +0100 +++ libstdc++-v3/include/c_global/cmath 2024-11-01 21:11:40.367678439 +0100 @@ -2838,7 +2838,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr int ilogb(_Float16 __x) - { return _Float16(__builtin_ilogbf(__x)); } + { return __builtin_ilogbf(__x); } constexpr _Float16 lgamma(_Float16 __x) @@ -2846,11 +2846,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr long long llrint(_Float16 __x) - { return _Float16(__builtin_llrintf(__x)); } + { return __builtin_llrintf(__x); } constexpr long long llround(_Float16 __x) - { return _Float16(__builtin_llroundf(__x)); } + { return __builtin_llroundf(__x); } constexpr _Float16 log1p(_Float16 __x) @@ -2867,11 +2867,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr long lrint(_Float16 __x) - { return _Float16(__builtin_lrintf(__x)); } + { return __builtin_lrintf(__x); } constexpr long lround(_Float16 __x) - { return _Float16(__builtin_lroundf(__x)); } + { return __builtin_lroundf(__x); } constexpr _Float16 nearbyint(_Float16 __x) @@ -3560,7 +3560,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr int ilogb(__gnu_cxx::__bfloat16_t __x) - { return __gnu_cxx::__bfloat16_t(__builtin_ilogbf(__x)); } + { return __builtin_ilogbf(__x); } constexpr __gnu_cxx::__bfloat16_t lgamma(__gnu_cxx::__bfloat16_t __x) @@ -3568,11 +3568,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr long long llrint(__gnu_cxx::__bfloat16_t __x) - { return __gnu_cxx::__bfloat16_t(__builtin_llrintf(__x)); } + { return __builtin_llrintf(__x); } constexpr long long llround(__gnu_cxx::__bfloat16_t __x) - { return __gnu_cxx::__bfloat16_t(__builtin_llroundf(__x)); } + { return __builtin_llroundf(__x); } constexpr __gnu_cxx::__bfloat16_t log1p(__gnu_cxx::__bfloat16_t __x) @@ -3589,11 +3589,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr long lrint(__gnu_cxx::__bfloat16_t __x) - { return __gnu_cxx::__bfloat16_t(__builtin_lrintf(__x)); } + { return __builtin_lrintf(__x); } constexpr long lround(__gnu_cxx::__bfloat16_t __x) - { return __gnu_cxx::__bfloat16_t(__builtin_lroundf(__x)); } + { return __builtin_lroundf(__x); } constexpr __gnu_cxx::__bfloat16_t nearbyint(__gnu_cxx::__bfloat16_t __x) --- libstdc++-v3/testsuite/26_numerics/headers/cmath/117406.cc.jj 2024-11-02 07:55:07.555121231 +0100 +++ libstdc++-v3/testsuite/26_numerics/headers/cmath/117406.cc 2024-11-02 08:00:14.150679537 +0100 @@ -0,0 +1,59 @@ +// Copyright (C) 2024 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++23 } } +// { dg-require-cmath "" } + +#include +#include +#include +#include + +template +void +test () +{ + using lim = std::numeric_limits; + int t0 = std::ilogb(T(4.0)); + VERIFY( t0 == 2 ); + int t1 = std::ilogb(lim::infinity()); + VERIFY( t1 == INT_MAX ); + int t2 = std::ilogb(-lim::infinity()); + VERIFY( t2 == INT_MAX ); +} + +int +main () +{ +#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) + test (); +#endif +#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) + test (); +#endif +#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64) + test (); +#endif +#if defined(__STDCPP_FLOAT128_T__) \ + && (defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) \ + || defined(_GLIBCXX_HAVE_FLOAT128_MATH)) + test (); +#endif +#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) + test (); +#endif +}