public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Adhemerval Zanella <azanella@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] stdlib: Fix stdbit.h with -Wconversion for clang
Date: Fri,  5 Jan 2024 17:53:08 +0000 (GMT)	[thread overview]
Message-ID: <20240105175308.671793857400@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=48ef5aeb1b75991e1f92c9756e73ddbbe96b1bee

commit 48ef5aeb1b75991e1f92c9756e73ddbbe96b1bee
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Thu Jan 4 14:59:28 2024 -0300

    stdlib: Fix stdbit.h with -Wconversion for clang
    
    With clang 14 and also with main the tst-stdbit-Wconversion
    issues the warnings:
    
      ../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.  Fix it by explicit casting to the expected types.  Although
    not strickly required for older gcc, using the same __pacify macro
    simpify the required code.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu.

Diff:
---
 stdlib/stdbit.h | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/stdlib/stdbit.h b/stdlib/stdbit.h
index 61165dd725..f334eb174d 100644
--- a/stdlib/stdbit.h
+++ b/stdlib/stdbit.h
@@ -42,7 +42,8 @@
 __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.  */
+   only to pacify older GCC (e.g., GCC 10 -Wconversion) or non-GCC (e.g
+   clang -Wimplicit-int-conversion).  */
 #if __GNUC_PREREQ (11, 0)
 # define __pacify_uint8(n)  (n)
 # define __pacify_uint16(n) (n)
@@ -170,8 +171,8 @@ extern unsigned int stdc_trailing_zeros_ull (unsigned long long int __x)
 #define stdc_trailing_zeros(x)				\
   (sizeof (x) == 8 ? stdc_trailing_zeros_ull (x)	\
    : sizeof (x) == 4 ? stdc_trailing_zeros_ui (x)	\
-   : sizeof (x) == 2 ? stdc_trailing_zeros_us (x)	\
-   : stdc_trailing_zeros_uc (x))
+   : sizeof (x) == 2 ? stdc_trailing_zeros_us (__pacify_uint16 (x))	\
+   : stdc_trailing_zeros_uc (__pacify_uint8 (x)))
 
 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
 static __always_inline unsigned int
@@ -274,8 +275,8 @@ extern unsigned int stdc_first_leading_zero_ull (unsigned long long int __x)
 #define stdc_first_leading_zero(x)			\
   (sizeof (x) == 8 ? stdc_first_leading_zero_ull (x)	\
    : sizeof (x) == 4 ? stdc_first_leading_zero_ui (x)	\
-   : sizeof (x) == 2 ? stdc_first_leading_zero_us (x)	\
-   : stdc_first_leading_zero_uc (x))
+   : sizeof (x) == 2 ? stdc_first_leading_zero_us (__pacify_uint16 (x))	\
+   : stdc_first_leading_zero_uc (__pacify_uint8 (x)))
 
 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
 static __always_inline unsigned int
@@ -328,8 +329,8 @@ extern unsigned int stdc_first_leading_one_ull (unsigned long long int __x)
 #define stdc_first_leading_one(x)			\
   (sizeof (x) == 8 ? stdc_first_leading_one_ull (x)	\
    : sizeof (x) == 4 ? stdc_first_leading_one_ui (x)	\
-   : sizeof (x) == 2 ? stdc_first_leading_one_us (x)	\
-   : stdc_first_leading_one_uc (x))
+   : sizeof (x) == 2 ? stdc_first_leading_one_us (__pacify_uint16 (x))	\
+   : stdc_first_leading_one_uc (__pacify_uint8 (x)))
 
 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
 static __always_inline unsigned int
@@ -382,8 +383,8 @@ extern unsigned int stdc_first_trailing_zero_ull (unsigned long long int __x)
 #define stdc_first_trailing_zero(x)			\
   (sizeof (x) == 8 ? stdc_first_trailing_zero_ull (x)	\
    : sizeof (x) == 4 ? stdc_first_trailing_zero_ui (x)	\
-   : sizeof (x) == 2 ? stdc_first_trailing_zero_us (x)	\
-   : stdc_first_trailing_zero_uc (x))
+   : sizeof (x) == 2 ? stdc_first_trailing_zero_us (__pacify_uint16 (x)) \
+   : stdc_first_trailing_zero_uc (__pacify_uint8 (x)))
 
 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
 static __always_inline unsigned int
@@ -436,8 +437,8 @@ extern unsigned int stdc_first_trailing_one_ull (unsigned long long int __x)
 #define stdc_first_trailing_one(x)			\
   (sizeof (x) == 8 ? stdc_first_trailing_one_ull (x)	\
    : sizeof (x) == 4 ? stdc_first_trailing_one_ui (x)	\
-   : sizeof (x) == 2 ? stdc_first_trailing_one_us (x)	\
-   : stdc_first_trailing_one_uc (x))
+   : sizeof (x) == 2 ? stdc_first_trailing_one_us (__pacify_uint16 (x))	\
+   : stdc_first_trailing_one_uc (__pacify_uint8 (x)))
 
 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
 static __always_inline unsigned int

                 reply	other threads:[~2024-01-05 17:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240105175308.671793857400@sourceware.org \
    --to=azanella@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).