https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83122

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu.org

--- Comment #3 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
Another example: https://godbolt.org/z/rbnrjncor

#include <stdint.h>

typedef __uint128_t pcg128_t;

static inline uint64_t pcg_rotr_64(uint64_t value, unsigned int rot) {
  return (value >> rot) | (value << ((-rot) & 63));
}

uint64_t pcg_output_xsl_rr_128_64(pcg128_t state) {
  return pcg_rotr_64(((uint64_t)(state >> 64u)) ^ (uint64_t)state,
                     state >> 122u);
}

produces:

<source>: In function 'pcg_output_xsl_rr_128_64':
<source>:11:28: warning: conversion from 'pcg128_t' {aka '__int128 unsigned'}
to 'unsigned int' may change value [-Wconversion]
   11 |                      state >> 122u);
      |                      ~~~~~~^~~~~~~
Compiler returned: 0


This happens in real-world code:
https://github.com/numpy/numpy/blob/main/numpy/random/src/pcg64/pcg64.h

Adding a cast is dangerous because it may silence a real bug as in (unsigned
int) state >> 12u.

It should be possible to detect that the expression is a shift, the shift
amount  is a constant and that it is enough to not alter the value.

Clang produces no warning for the above but it does warn for state >> 12u, so
it is doing something better than GCC.

Reply via email to