From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 103753858D33 for ; Thu, 4 Jan 2024 16:34:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 103753858D33 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 103753858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704386088; cv=none; b=xyWEx+d3MhNLmn9aDzBJ2mVDqn6wMaGT0Ilmpq/NC52KMfEQMS5qLu9xCtje6smC9AxlYiuA+aVOWacSyESd5a02MQJpLFiBn2MckUMgC/uu6gj7DkXUyMmaPbOr4h0Y/oWaUz/a2yRs1KsxYGR+8tOSkfOE2FZ6DUtwmwlDwwA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704386088; c=relaxed/simple; bh=bvwaONZlOF5DKGIHzkwC9qFl9/kA3wJy2ZwXHUC6TMo=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=bpoEdOiPnlKkpkk42DknPtNV+vq2yXGl2s+C7khlgABuyMZmKfP9lj2O+g2dJ+zbB5JwWrITV4x+2xXd+8Yaf9qRNunJSVeyl66LaAEcaLUn3Vj6TsiCOKZnRpAM4d2qw9+V5yu0Ik3J+DPrfHoNE+tgGnWuWwVPYpuF2lf69eA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1d3f2985425so4151405ad.3 for ; Thu, 04 Jan 2024 08:34:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704386081; x=1704990881; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=eboDHMjTtk7wtBYjT6wSwrN0h29YNeB3uxpUop+BIcg=; b=zmqgAXjCA8SST1L7cYD161lt+SETm6t5VhI67/2PnNU8yc7vW/8uB4XShIAs05ma1a 5XQIhlNmZJpDoubyJSatBfLvrj57yVyJhWJnOZmkpHx656vrlGiYyAgSn4mx9WcwiozY 7bltUL229gUPsTBefLWtyfjkomEgvBZduYHAzqsEIGnCU31A3o7T2zh5mygKU3CC6Yl+ 06pO8hhUZdLyNXoFdJqjAAEbV/jvsAWiy6NvJ4SFj0/Zvs9o9i3DCfMIEQfiz5ZkJscQ v5rmU8At0KFhnqP+yCN5l6bQIkZmdQQH8kO9p+C1IdOvZEbvQLl/YM6YBp2+om59bP+R kmdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704386081; x=1704990881; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eboDHMjTtk7wtBYjT6wSwrN0h29YNeB3uxpUop+BIcg=; b=p/V7O6shBfpSXqivzZURBtZXlQTIIgdGSbQFklEHyiaMI3dYZhD4Ua4gEvd0jsns8x ALV64ORfNmMXyqKC7n/Dvkkx3NFAHr29DYXNEsnmW9BnGlrtncNgmlXGzjdaYYSnB65F cJNOcGEyp4NXPt1YEBA9Xmk7IoPZG9N6axphjg7BJeauQ23mWbj2xvQg1iWip8Z50gGv DrIPl7y+eIrZKxx6JtOh/rxLXP9ZjwtAzTncrFWvnvjQGDY4ZVIE4lwVVnjcFwtga+Tq FPZQap5ScWPObWUwASKRuGneVKkp0pokbpo+It/tF5B3vKO7Wd3aeCjPSU/2SLAS9Fjd t8vg== X-Gm-Message-State: AOJu0YyyDmHzyKYckBjHAZt2KfRWicVz4RnIEdlf1nHFU23D96Vxt9Sc 1eOS1b/ZHcFxz9mR2y1cA043kLxle8fYCQ== X-Google-Smtp-Source: AGHT+IEuMvQA2+/Y5EW0YY8rf/onhE2yDlKnEow8gTIIETiHu/W5s8r5GVm3purEgEzkkQsenBbZvg== X-Received: by 2002:a17:902:a3cd:b0:1d3:5369:459d with SMTP id q13-20020a170902a3cd00b001d35369459dmr613333plb.63.1704386080703; Thu, 04 Jan 2024 08:34:40 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c1:bd9:a05e:e1ed:d7fb:61bb? ([2804:1b3:a7c1:bd9:a05e:e1ed:d7fb:61bb]) by smtp.gmail.com with ESMTPSA id z3-20020a170902ee0300b001d3561680aasm25688394plb.82.2024.01.04.08.34.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Jan 2024 08:34:40 -0800 (PST) Message-ID: <5869249a-634d-47b2-a351-adc5f63ce087@linaro.org> Date: Thu, 4 Jan 2024 13:34:36 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCHv2] Implement C23 Content-Language: en-US To: Joseph Myers , Florian Weimer Cc: libc-alpha@sourceware.org, Jakub Jelinek References: <22b591d7-142a-2d27-367d-164a9b8e8bda@polyomino.org.uk> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <22b591d7-142a-2d27-367d-164a9b8e8bda@polyomino.org.uk> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_ASCII_DIVIDERS,KAM_SHORT,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 List-Id: On 02/01/24 12:12, Joseph Myers wrote: > diff --git a/stdlib/tst-stdbit-Wconversion.c b/stdlib/tst-stdbit-Wconversion.c > new file mode 100644 > index 0000000000..1a5fbf965c > --- /dev/null > +++ b/stdlib/tst-stdbit-Wconversion.c > @@ -0,0 +1,107 @@ > +/* Test type-generic macros with -Wconversion. > + Copyright (C) 2024 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 > + . */ > + > +#include > +#include > + > +unsigned char uc; > +unsigned short us; > +unsigned int ui; > +unsigned long int ul; > +unsigned long long int ull; > + > +static int > +do_test (void) > +{ > + /* The main point of this test is the compile-time test that there > + are no -Wconversion warnings from the type-generic macros, not > + the runtime execution of those macros. */ > + (void) stdc_leading_zeros (uc); > + (void) stdc_leading_zeros (us); > + (void) stdc_leading_zeros (ui); > + (void) stdc_leading_zeros (ul); > + (void) stdc_leading_zeros (ull); > + (void) stdc_leading_ones (uc); > + (void) stdc_leading_ones (us); > + (void) stdc_leading_ones (ui); > + (void) stdc_leading_ones (ul); > + (void) stdc_leading_ones (ull); > + (void) stdc_trailing_zeros (uc); > + (void) stdc_trailing_zeros (us); > + (void) stdc_trailing_zeros (ui); > + (void) stdc_trailing_zeros (ul); > + (void) stdc_trailing_zeros (ull); > + (void) stdc_trailing_ones (uc); > + (void) stdc_trailing_ones (us); > + (void) stdc_trailing_ones (ui); > + (void) stdc_trailing_ones (ul); > + (void) stdc_trailing_ones (ull); > + (void) stdc_first_leading_zero (uc); > + (void) stdc_first_leading_zero (us); > + (void) stdc_first_leading_zero (ui); > + (void) stdc_first_leading_zero (ul); > + (void) stdc_first_leading_zero (ull); > + (void) stdc_first_leading_one (uc); > + (void) stdc_first_leading_one (us); > + (void) stdc_first_leading_one (ui); > + (void) stdc_first_leading_one (ul); > + (void) stdc_first_leading_one (ull); > + (void) stdc_first_trailing_zero (uc); > + (void) stdc_first_trailing_zero (us); > + (void) stdc_first_trailing_zero (ui); > + (void) stdc_first_trailing_zero (ul); > + (void) stdc_first_trailing_zero (ull); > + (void) stdc_first_trailing_one (uc); > + (void) stdc_first_trailing_one (us); > + (void) stdc_first_trailing_one (ui); > + (void) stdc_first_trailing_one (ul); > + (void) stdc_first_trailing_one (ull); > + (void) stdc_count_zeros (uc); > + (void) stdc_count_zeros (us); > + (void) stdc_count_zeros (ui); > + (void) stdc_count_zeros (ul); > + (void) stdc_count_zeros (ull); > + (void) stdc_count_ones (uc); > + (void) stdc_count_ones (us); > + (void) stdc_count_ones (ui); > + (void) stdc_count_ones (ul); > + (void) stdc_count_ones (ull); > + (void) stdc_has_single_bit (uc); > + (void) stdc_has_single_bit (us); > + (void) stdc_has_single_bit (ui); > + (void) stdc_has_single_bit (ul); > + (void) stdc_has_single_bit (ull); > + (void) stdc_bit_width (uc); > + (void) stdc_bit_width (us); > + (void) stdc_bit_width (ui); > + (void) stdc_bit_width (ul); > + (void) stdc_bit_width (ull); > + (void) stdc_bit_floor (uc); > + (void) stdc_bit_floor (us); > + (void) stdc_bit_floor (ui); > + (void) stdc_bit_floor (ul); > + (void) stdc_bit_floor (ull); > + (void) stdc_bit_ceil (uc); > + (void) stdc_bit_ceil (us); > + (void) stdc_bit_ceil (ui); > + (void) stdc_bit_ceil (ul); > + (void) stdc_bit_ceil (ull); > + return 0; > +} > + > +#include It seems the current macros does not work flawlessly with old gcc versions or with clang. With gcc 6.5.0, 7.5.0, 8.5.0, and 9.5.0 I see: ../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); ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The gcc 10.4.1, 11.3.1, 12.2.1, and 13.2.1 does not thrown any warning issues. Similar issues also happen with clang 14 and also with master: ../stdlib/stdbit.h:701:40: error: implicit conversion loses integer precision: 'int' to 'uint16_t' (aka 'unsigned short') [-Werror,-Wimplicit-int-conversion] return __x == 0 ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1); ~~~~~~ ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../stdlib/stdbit.h:707:39: error: implicit conversion loses integer precision: 'int' to 'uint8_t' (aka 'unsigned char') [-Werror,-Wimplicit-int-conversion] return __x == 0 ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1); ~~~~~~ ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~ ../stdlib/stdbit.h:751:40: error: implicit conversion loses integer precision: 'int' to 'uint16_t' (aka 'unsigned short') [-Werror,-Wimplicit-int-conversion] return __x <= 1 ? 1 : ((uint16_t) 2) << (__bw16_inline (__x - 1) - 1); ~~~~~~ ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../stdlib/stdbit.h:757:39: error: implicit conversion loses integer precision: 'int' to 'uint8_t' (aka 'unsigned char') [-Werror,-Wimplicit-int-conversion] return __x <= 1 ? 1 : ((uint8_t) 2) << (__bw8_inline (__x - 1) - 1); ~~~~~~ ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tst-stdbit-Wconversion.c:45:31: error: implicit conversion loses integer precision: 'unsigned short' to 'uint8_t' (aka 'unsigned char') [-Werror,-Wimplicit-int-conversion] (void) stdc_trailing_zeros (us); ~~~~~~~~~~~~~~~~~~~~~^~~ ../stdlib/stdbit.h:164:30: note: expanded from macro 'stdc_trailing_zeros' : stdc_trailing_zeros_uc (x)) ~~~~~~~~~~~~~~~~~~~~~~~~^~ ../stdlib/stdbit.h:191:52: note: expanded from macro 'stdc_trailing_zeros_uc' # define stdc_trailing_zeros_uc(x) (__ctz8_inline (x)) ~~~~~~~~~~~~~ ^ tst-stdbit-Wconversion.c:46:31: error: implicit conversion loses integer precision: 'unsigned int' to 'uint16_t' (aka 'unsigned short') [-Werror,-Wimplicit-int-conversion] (void) stdc_trailing_zeros (ui); ~~~~~~~~~~~~~~~~~~~~~^~~ ../stdlib/stdbit.h:163:48: note: expanded from macro 'stdc_trailing_zeros' : sizeof (x) == 2 ? stdc_trailing_zeros_us (x) \ ~~~~~~~~~~~~~~~~~~~~~~~~^~ ../stdlib/stdbit.h:192:53: note: expanded from macro 'stdc_trailing_zeros_us' # define stdc_trailing_zeros_us(x) (__ctz16_inline (x)) ~~~~~~~~~~~~~~ ^ tst-stdbit-Wconversion.c:46:31: error: implicit conversion loses integer precision: 'unsigned int' to 'uint8_t' (aka 'unsigned char') [-Werror,-Wimplicit-int-conversion] (void) stdc_trailing_zeros (ui); ~~~~~~~~~~~~~~~~~~~~~^~~ ../stdlib/stdbit.h:164:30: note: expanded from macro 'stdc_trailing_zeros' : stdc_trailing_zeros_uc (x)) ~~~~~~~~~~~~~~~~~~~~~~~~^~ ../stdlib/stdbit.h:191:52: note: expanded from macro 'stdc_trailing_zeros_uc' # define stdc_trailing_zeros_uc(x) (__ctz8_inline (x)) ~~~~~~~~~~~~~ ^ tst-stdbit-Wconversion.c:47:31: error: implicit conversion loses integer precision: 'unsigned long' to 'uint16_t' (aka 'unsigned short') [-Werror,-Wimplicit-int-conversion] (void) stdc_trailing_zeros (ul); ~~~~~~~~~~~~~~~~~~~~~^~~ ../stdlib/stdbit.h:163:48: note: expanded from macro 'stdc_trailing_zeros' : sizeof (x) == 2 ? stdc_trailing_zeros_us (x) \ ~~~~~~~~~~~~~~~~~~~~~~~~^~ ../stdlib/stdbit.h:192:53: note: expanded from macro 'stdc_trailing_zeros_us' # define stdc_trailing_zeros_us(x) (__ctz16_inline (x)) ~~~~~~~~~~~~~~ ^ [...] It seems to boiler down to __builtin_clz not having a variant for 8 or 16 bits. I am not sure which would be best way to support it.