From patchwork Fri Sep 11 23:35:15 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Turner X-Patchwork-Id: 143508 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 77576484B9B4 for ; Fri, 11 Sep 2026 23:36:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 77576484B9B4 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=OmgwbXcj X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yx2-x11.google.com (mail-yx2-x11.google.com [IPv6:2607:f8b0:4864:41::11]) by sourceware.org (Postfix) with ESMTPS id 9631E48F60F0 for ; Fri, 11 Sep 2026 23:35:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9631E48F60F0 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9631E48F60F0 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:41::11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789169721; cv=none; b=eWOTDTJLKU0fXS25pTKNVxtEUxlK9AFYLFdLQjkaxWc3PC+3OAdQhcI4ub+00oXu9Nd+nOM4REOvirGGsP2EjUB+bBfb8NEyj952PEvm6YR7j/Q+KrVgPzyEL3ANoi0m7mtR8oKrjPhIZmk5geBpWzeoAvRNfmsU5Hha2+Itzvo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789169721; c=relaxed/simple; bh=7zgDRvH2v7mHx3NyRNN7KETazLVZMrpvKzEe+fw8O3Q=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=YdqNVOMg1ddveMFfBx09B9nBdZ4QI7Je38tMeWYa8gBvO5U3KD1Ga6wGmZY2GKIU4Jp6XV9BPd4kg3TJuKvByXPhekypFmadGS41gxItHsd73YKsW789XoiYxAcrkwA1ulCMJPx+ZTTssCfaO03DCqnZpjsJHJPL0PTFrcpOFNY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=OmgwbXcj DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9631E48F60F0 Received: by mail-yx2-x11.google.com with SMTP id 00721157ae682-85d43db0c29so873617b3.2 for ; Fri, 11 Sep 2026 16:35:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789169721; x=1789774521; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=mRqpfKzZZMX6mxie05IQyodL0K0XGfacGkLR4qV45wc=; b=OmgwbXcjOR88zEN03YZrhcIYZRLVgAMU8Ruw/hGEoMwQnvsWk55agZoT1zjRP1trWK 7NIqB1kIy6QyupnifKCsZnNOfnexghNaIF9GARPRRI/+ok9MbAN3AlDRVoNSqVRqIhoo NxXzfsJF7aHQZPN0kL0yfwud42uI2Zp4tRYQIlr3D49NbOReX0tSCz7aYwPrs1OQPNaY snRcjNDSGdqagdJZ6EKQ4+fDEClgHb6ZGTsn+squDKjSY/eob5yJi3JCZ4aBlMp6j2HV gHbUgCDLyHjNausukuu+JZb1+eU08rrRKC0CCHChaG8SOharuwd2lrBKd7yBXH6ipNI5 b9qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789169721; x=1789774521; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=mRqpfKzZZMX6mxie05IQyodL0K0XGfacGkLR4qV45wc=; b=ne530BSRZEFFxzTJJ3E4uMcLkL9YsLw0D22nhp6MbGw0LkFv7eNAaYh8WV68dz/qjS spKw2+OPA4TYww9UZkcjiFN/nVliqndygm/m18ucEwiT8oC9THVxz30Noji7DYONsaIp DTSROD+yiq0nugk2kIlHY+peYwetmCJkjquvhRPWcKivMp809QL+zJqJqdRc2Toi86rv /vKZyWqrQWDrXl4BgofH4UrOj7dvZaSqGd4XbCJgWeWV1EQs+RrUicNdIk2+wMLNnCBS lDKE2VWQfQbY9PNtK4tjazaFdObni+o/2SnWwnqDOSM3Ma+O2Ksiu35+Q0xglKx856ht 8b+g== X-Gm-Message-State: AFuF++nP6LdZlGWL01yNjHwMNHfZv+EKDiJhTkl2syGaWmkk4X624w9B WTcZiMYP205UOR1mq4uls89/6wLrB128xVeZp+U3S/7GfLm4oueq1gpq8dZrIA== X-Gm-Gg: AYBFou2uCc8ChrKIeE6dy24jNciZ1v8fyDdD/0ma8te8KHsYGvbMUoqxdqUegcoZrkg OYB8Sz03zgXMH7lrh4s1S78+8HsOuPzRdIozb2T6sTQL9gRM8iDsc8eUUFlfK91iuxJ1Hmfqa0i 5eRUrv8lRZfzd4PmWsPQz4F+BpbvakiJUMBUx/6ONAZEeze5nPAVMBCAA4Hus7vVSBqTVsPItjt S0LSsbGJu2lb6wH10KRu/7OZWfjKKUEi4V25aZK/gah1vysi8NGrN0OfG8HohId94lWmsOmdQMH /JugF3AwIHQ5C3nV6AmLEgT1B9kz1ib8T5JPKRem+uZERotE68Gfe6EeJ/mugnnk6p2uSwa2ZEV CX2PfHzfcaNhP4IWu279EGVFiMzRD3XGQBxdgGCz0xSDH5fU5rHDLmYgiugJwyh5Xc9U177lfcb QLgCvyO2XSWss2unN2GXtx23qH43hG00Ce5UQrahrSf6/j8koH5kzfO+ULH1z1MiaGh4RPfLM= X-Received: by 2002:a05:690c:281:b0:869:adba:625b with SMTP id 00721157ae682-887a87bf532mr2353447b3.25.1789169720489; Fri, 11 Sep 2026 16:35:20 -0700 (PDT) Received: from localhost ([2600:1702:7a90:6f9f:8bc4:8aec:108d:7a04]) by smtp.gmail.com with ESMTPSA id 00721157ae682-88486e2ecb6sm16794727b3.21.2026.09.11.16.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 16:35:19 -0700 (PDT) From: Matt Turner To: libc-alpha@sourceware.org Cc: mattst88@gmail.com Subject: [PATCH v3 1/3] math: move the classification macros to bits/fpclassify.h Date: Fri, 11 Sep 2026 19:35:15 -0400 Message-ID: <20260911233517.2113403-1-mattst88@gmail.com> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org fpclassify, isfinite, isnormal, isnan and isinf expand to floating-point comparisons wherever the compiler builtins do, and a machine may not be able to classify a value that way. Move them to a new bits/fpclassify.h so that such a machine can override them. signbit moves ahead of the moved macros to keep them contiguous. It is unaffected, as it is not defined in terms of a comparison. No functional change. --- bits/fpclassify.h | 85 +++++++++++++++++++++++++++++++++++++++++++++++ math/Makefile | 1 + math/math.h | 64 +---------------------------------- 3 files changed, 87 insertions(+), 63 deletions(-) create mode 100644 bits/fpclassify.h diff --git ./bits/fpclassify.h ./bits/fpclassify.h new file mode 100644 index 0000000000..939bbb8a81 --- /dev/null +++ ./bits/fpclassify.h @@ -0,0 +1,85 @@ +/* Define floating-point classification macros. + Copyright (C) 2026 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _MATH_H +# error "Never use directly; include instead." +#endif + +/* GCC bug 66462 means we cannot use the math builtins with -fsignaling-nan, + so disable builtins if this is enabled. When fixed in a newer GCC, + the __SUPPORT_SNAN__ check may be skipped for those versions. */ + +/* Return number of classification appropriate for X. */ +#if ((__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ + || __glibc_clang_prereq (2,8)) \ + && (!defined __OPTIMIZE_SIZE__ || defined __cplusplus) + /* The check for __cplusplus allows the use of the builtin, even + when optimization for size is on. This is provided for + libstdc++, only to let its configure test work when it is built + with -Os. No further use of this definition of fpclassify is + expected in C++ mode, since libstdc++ provides its own version + of fpclassify in cmath (which undefines fpclassify). */ +# define fpclassify(x) __builtin_fpclassify (FP_NAN, FP_INFINITE, \ + FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x) +#else +# define fpclassify(x) __MATH_TG ((x), __fpclassify, (x)) +#endif + +/* Return nonzero value if X is not +-Inf or NaN. */ +#if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ + || __glibc_clang_prereq (2,8) +# define isfinite(x) __builtin_isfinite (x) +#else +# define isfinite(x) __MATH_TG ((x), __finite, (x)) +#endif + +/* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */ +#if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ + || __glibc_clang_prereq (2,8) +# define isnormal(x) __builtin_isnormal (x) +#else +# define isnormal(x) (fpclassify (x) == FP_NORMAL) +#endif + +/* Return nonzero value if X is a NaN. We could use `fpclassify' but + we already have this functions `__isnan' and it is faster. */ +#if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ + || __glibc_clang_prereq (2,8) +# define isnan(x) __builtin_isnan (x) +#else +# define isnan(x) __MATH_TG ((x), __isnan, (x)) +#endif + +/* Return nonzero value if X is positive or negative infinity. */ +#if __HAVE_DISTINCT_FLOAT128 && !__GNUC_PREREQ (7,0) \ + && !defined __SUPPORT_SNAN__ && !defined __cplusplus \ + && !defined __clang__ + /* Since __builtin_isinf_sign is broken for float128 before GCC 7.0, + use the helper function, __isinff128, with older compilers. This is + only provided for C mode, because in C++ mode, GCC has no support + for __builtin_types_compatible_p (and when in C++ mode, this macro is + not used anyway, because libstdc++ headers undefine it). */ +# define isinf(x) \ + (__builtin_types_compatible_p (__typeof (x), _Float128) \ + ? __isinff128 (x) : __builtin_isinf_sign (x)) +#elif (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ + || __glibc_clang_prereq (3,7) +# define isinf(x) __builtin_isinf_sign (x) +#else +# define isinf(x) __MATH_TG ((x), __isinf, (x)) +#endif diff --git ./math/Makefile ./math/Makefile index 5b48e53300..d9b17a8277 100644 --- ./math/Makefile +++ ./math/Makefile @@ -30,6 +30,7 @@ headers := \ bits/flt-eval-method.h \ bits/fp-fast.h \ bits/fp-logb.h \ + bits/fpclassify.h \ bits/iscanonical.h \ bits/libm-simd-decl-stubs.h \ bits/long-double.h \ diff --git ./math/math.h ./math/math.h index 75830d7696..0926b418a5 100644 --- ./math/math.h +++ ./math/math.h @@ -1087,26 +1087,6 @@ enum FP_NORMAL }; -/* GCC bug 66462 means we cannot use the math builtins with -fsignaling-nan, - so disable builtins if this is enabled. When fixed in a newer GCC, - the __SUPPORT_SNAN__ check may be skipped for those versions. */ - -/* Return number of classification appropriate for X. */ -# if ((__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ - || __glibc_clang_prereq (2,8)) \ - && (!defined __OPTIMIZE_SIZE__ || defined __cplusplus) - /* The check for __cplusplus allows the use of the builtin, even - when optimization for size is on. This is provided for - libstdc++, only to let its configure test work when it is built - with -Os. No further use of this definition of fpclassify is - expected in C++ mode, since libstdc++ provides its own version - of fpclassify in cmath (which undefines fpclassify). */ -# define fpclassify(x) __builtin_fpclassify (FP_NAN, FP_INFINITE, \ - FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x) -# else -# define fpclassify(x) __MATH_TG ((x), __fpclassify, (x)) -# endif - /* Return nonzero value if sign of X is negative. */ # if __GNUC_PREREQ (6,0) || __glibc_clang_prereq (3,3) # define signbit(x) __builtin_signbit (x) @@ -1125,49 +1105,7 @@ enum # define signbit(x) __MATH_TG ((x), __signbit, (x)) # endif -/* Return nonzero value if X is not +-Inf or NaN. */ -# if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ - || __glibc_clang_prereq (2,8) -# define isfinite(x) __builtin_isfinite (x) -# else -# define isfinite(x) __MATH_TG ((x), __finite, (x)) -# endif - -/* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */ -# if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ - || __glibc_clang_prereq (2,8) -# define isnormal(x) __builtin_isnormal (x) -# else -# define isnormal(x) (fpclassify (x) == FP_NORMAL) -# endif - -/* Return nonzero value if X is a NaN. We could use `fpclassify' but - we already have this functions `__isnan' and it is faster. */ -# if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ - || __glibc_clang_prereq (2,8) -# define isnan(x) __builtin_isnan (x) -# else -# define isnan(x) __MATH_TG ((x), __isnan, (x)) -# endif - -/* Return nonzero value if X is positive or negative infinity. */ -# if __HAVE_DISTINCT_FLOAT128 && !__GNUC_PREREQ (7,0) \ - && !defined __SUPPORT_SNAN__ && !defined __cplusplus \ - && !defined __clang__ - /* Since __builtin_isinf_sign is broken for float128 before GCC 7.0, - use the helper function, __isinff128, with older compilers. This is - only provided for C mode, because in C++ mode, GCC has no support - for __builtin_types_compatible_p (and when in C++ mode, this macro is - not used anyway, because libstdc++ headers undefine it). */ -# define isinf(x) \ - (__builtin_types_compatible_p (__typeof (x), _Float128) \ - ? __isinff128 (x) : __builtin_isinf_sign (x)) -# elif (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \ - || __glibc_clang_prereq (3,7) -# define isinf(x) __builtin_isinf_sign (x) -# else -# define isinf(x) __MATH_TG ((x), __isinf, (x)) -# endif +# include /* Bitmasks for the math_errhandling macro. */ # define MATH_ERRNO 1 /* errno set by math functions. */