From patchwork Thu Jan 4 19:47:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1882600 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=XHdQsU+7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.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 4T5cbC44V0z1yQ5 for ; Fri, 5 Jan 2024 06:48:39 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 923FB386183F for ; Thu, 4 Jan 2024 19:48:37 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id E34FF386182F for ; Thu, 4 Jan 2024 19:47:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E34FF386182F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E34FF386182F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::636 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704397664; cv=none; b=F+QwK2ZcyTYlZpl57N/3GOca7ckpFitYzGTlmZBXFruwZ+zVkAB2330JKOVZ8jFFG41W+Vpg3eXrImCHtGdFa1bUbJh+yckOBjbVIGOPSxWtMQsUHmpvotCFMcKRUJiWL2AYUkkrEKgI24pF+IWTGC8Q22+S+V3A3NN5DX2LGYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704397664; c=relaxed/simple; bh=RVYs7rmwPjPlHi6Pi3WiTpEyjnWwBza/adBMJSLUVMA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=dmKkVUKJuhzevaoP3cDUTf1p/bkoiMydHtZsX+hpO7p1QA3Zpmoox1CEwg1uEhqrlTV7lKA7dIrvUwxZ2yYmCohpTNOb9lJEO8DpClUOoD0r8xnVLpy6ZMOh7gRNEYr2OawtpRfa1xUehAjbPdMUn5AjQSsMUDBZL+ZYoVhuvNs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1d427518d52so5872785ad.0 for ; Thu, 04 Jan 2024 11:47:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704397658; x=1705002458; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WBgC4HIExLUhoWDbS4Q25u3oB98vuNk9TA71wKsrAkM=; b=XHdQsU+7NOjbkm1XxMgRsnFtxVYuoR3/YmdEtrVoCW5lKgKVtcHIMUrTxwvFKPIps8 5SFdCZL9/oV7Qow6PZ6MPRlwd3wbnHF46TZXqxe7RdieDoq2v60l7Qm5JEsf3ebooeZ6 HkKwZm1dn8yyuD5coiDjx+9oU6kRLGMNJgFiKg/hk1W5UV3aBVenhstZ9kMs/c2hvgZv 80yGV8wN2UagMP+b9j//OFNr/VvbuInr2P1lWc/Y+rFJlRSBmEIturWzbh4Y3U4FYp67 haqZUhc3s5np6XRrBElPBxtAeQWSVO3CLHQzpuDHa1TCTdpCP+ueAXMvRIbacwwqgzVp RxUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704397658; x=1705002458; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WBgC4HIExLUhoWDbS4Q25u3oB98vuNk9TA71wKsrAkM=; b=f9wlgSirMLH5gRnuw0NIjF49CnWAhnxTp6ubNS+7IGkq1OteNBu/HG9cuzSXTX3gda jRq5BGcmMsVo1/S2mxKQ94zBzPNPYx3HqTv9R5XAIiKgR3Nhg9lV0QwIo5s5DEfuitN9 WpJzCN6Z5/NwNd17I2vcjTD8dHcQAzdyiRfAS7bK94SOOIs+H9vnSJXr54Bk6oDeRLQR 0xgUAR1MA4r62OXeKfqree7q4biDM0cyucmIezY315OSzrb2BcFXGI45fN4f7OLqt/0f KjOVugtY8eI8PCIpc5viMJ1irh6JRSPfZColagn7LPD04Xau59VC37OIOlQEBNc+t4eC rEnw== X-Gm-Message-State: AOJu0Yz/ZlRwSbtzM0dctIUHTiH8wey9cV3n79kbX0Vqqjsnap9rUxQu KEiEnIw8kPr+OkCEoUybsZsygEkpmwnx5xyiNIz6Addb1d0= X-Google-Smtp-Source: AGHT+IEz0W8l5zivReRl44Xcbr45ZeV5xfIBcq+sP8I3o22MnczBdh7gamoBbrt9Ix6pN8OolmtM7g== X-Received: by 2002:a17:903:4113:b0:1d3:bb4b:dc04 with SMTP id r19-20020a170903411300b001d3bb4bdc04mr994401pld.17.1704397658267; Thu, 04 Jan 2024 11:47:38 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c1:bd9:e135:8c7a:c648:e012]) by smtp.gmail.com with ESMTPSA id n15-20020a170902968f00b001d45fd7d444sm18868227plp.175.2024.01.04.11.47.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 11:47:37 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Joseph Myers , Florian Weimer , Jakub Jelinek Subject: [PATCH 1/2] stdlib: Fix stdbit.h with -Wconversion for older gcc Date: Thu, 4 Jan 2024 16:47:30 -0300 Message-Id: <20240104194731.2357574-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240104194731.2357574-1-adhemerval.zanella@linaro.org> References: <20240104194731.2357574-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.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+incoming=patchwork.ozlabs.org@sourceware.org With gcc 6.5.0, 7.5.0, 8.5.0, and 9.5.0 the tst-stdbit-Wconversion issues the warnings: ../stdlib/stdbit.h: In function ‘__clo16_inline’: ../stdlib/stdbit.h:128:26: error: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion] return __clz16_inline (~__x); ^ ../stdlib/stdbit.h: In function ‘__clo8_inline’: ../stdlib/stdbit.h:134:25: error: conversion to ‘uint8_t {aka unsigned char}’ from ‘int’ may alter its value [-Werror=conversion] return __clz8_inline (~__x); ^ ../stdlib/stdbit.h: In function ‘__cto16_inline’: ../stdlib/stdbit.h:232:26: error: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion] return __ctz16_inline (~__x); ^ ../stdlib/stdbit.h: In function ‘__cto8_inline’: ../stdlib/stdbit.h:238:25: error: conversion to ‘uint8_t {aka unsigned char}’ from ‘int’ may alter its value [-Werror=conversion] return __ctz8_inline (~__x); ^ ../stdlib/stdbit.h: In function ‘__bf16_inline’: ../stdlib/stdbit.h:701:23: error: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion] return __x == 0 ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1); ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../stdlib/stdbit.h: In function ‘__bf8_inline’: ../stdlib/stdbit.h:707:23: error: conversion to ‘uint8_t {aka unsigned char}’ from ‘int’ may alter its value [-Werror=conversion] return __x == 0 ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1); ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../stdlib/stdbit.h: In function ‘__bc16_inline’: ../stdlib/stdbit.h:751:59: error: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion] return __x <= 1 ? 1 : ((uint16_t) 2) << (__bw16_inline (__x - 1) - 1); ^~~ ../stdlib/stdbit.h:751:23: error: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion] return __x <= 1 ? 1 : ((uint16_t) 2) << (__bw16_inline (__x - 1) - 1); ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../stdlib/stdbit.h: In function ‘__bc8_inline’: ../stdlib/stdbit.h:757:57: error: conversion to ‘uint8_t {aka unsigned char}’ from ‘int’ may alter its value [-Werror=conversion] return __x <= 1 ? 1 : ((uint8_t) 2) << (__bw8_inline (__x - 1) - 1); ^~~ ../stdlib/stdbit.h:757:23: error: conversion to ‘uint8_t {aka unsigned char}’ from ‘int’ may alter its value [-Werror=conversion] return __x <= 1 ? 1 : ((uint8_t) 2) << (__bw8_inline (__x - 1) - 1); It seems to boiler down to __builtin_clz not having a variant for 8 or 16 bits. Fix it by explicit casting to the expected types. Checked on x86_64-linux-gnu and i686-linux-gnu with gcc 9.5.0. --- stdlib/stdbit.h | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/stdlib/stdbit.h b/stdlib/stdbit.h index 773b2ab72d..2945550385 100644 --- a/stdlib/stdbit.h +++ b/stdlib/stdbit.h @@ -41,6 +41,16 @@ __BEGIN_DECLS +/* Use __pacify_uint16 (N) instead of (uint16_t) (N) when the cast is helpful + only to pacify older GCC (e.g., GCC 10 -Wconversion) or non-GCC. */ +#if __GNUC_PREREQ (11, 0) +# define __pacify_uint8(n) (n) +# define __pacify_uint16(n) (n) +#else +# define __pacify_uint8(n) ((uint8_t) (n)) +# define __pacify_uint16(n) ((uint16_t) (n)) +#endif + /* Count leading zeros. */ extern unsigned int stdc_leading_zeros_uc (unsigned char __x) __THROW __attribute_const__; @@ -125,13 +135,13 @@ __clo32_inline (uint32_t __x) static __always_inline unsigned int __clo16_inline (uint16_t __x) { - return __clz16_inline (~__x); + return __clz16_inline (__pacify_uint16 (~__x)); } static __always_inline unsigned int __clo8_inline (uint8_t __x) { - return __clz8_inline (~__x); + return __clz8_inline (__pacify_uint8 (~__x)); } # define stdc_leading_ones_uc(x) (__clo8_inline (x)) @@ -229,13 +239,13 @@ __cto32_inline (uint32_t __x) static __always_inline unsigned int __cto16_inline (uint16_t __x) { - return __ctz16_inline (~__x); + return __ctz16_inline (__pacify_uint16 (~__x)); } static __always_inline unsigned int __cto8_inline (uint8_t __x) { - return __ctz8_inline (~__x); + return __ctz8_inline (__pacify_uint8 (~__x)); } # define stdc_trailing_ones_uc(x) (__cto8_inline (x)) @@ -698,13 +708,15 @@ __bf32_inline (uint32_t __x) static __always_inline uint16_t __bf16_inline (uint16_t __x) { - return __x == 0 ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1); + return __pacify_uint16 (__x == 0 + ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1)); } static __always_inline uint8_t __bf8_inline (uint8_t __x) { - return __x == 0 ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1); + return __pacify_uint8 (__x == 0 + ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1)); } # define stdc_bit_floor_uc(x) ((unsigned char) __bf8_inline (x)) @@ -748,13 +760,19 @@ __bc32_inline (uint32_t __x) static __always_inline uint16_t __bc16_inline (uint16_t __x) { - return __x <= 1 ? 1 : ((uint16_t) 2) << (__bw16_inline (__x - 1) - 1); + return (uint16_t) (__x <= 1 + ? 1 + : ((uint16_t) 2) + << (__bw16_inline ((uint16_t) (__x - 1)) - 1)); } static __always_inline uint8_t __bc8_inline (uint8_t __x) { - return __x <= 1 ? 1 : ((uint8_t) 2) << (__bw8_inline (__x - 1) - 1); + return (uint8_t) (__x <= 1 + ? 1 + : ((uint8_t) 2) + << (__bw8_inline ((uint8_t) (__x - 1)) - 1)); } # define stdc_bit_ceil_uc(x) ((unsigned char) __bc8_inline (x))