g++ version 4.3.6 and older, when encoutering a constant literal
  0xffffffffffffffff
gives an error
  error: integer constant is too large for ‘long’ type

For example, on Fedora 1 with g++ 3.3.2, we get this error:

g++ -DHAVE_CONFIG_H -DEXEEXT=\"\" -DEXEEXT=\"\" -I. -I..  
-DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I. -I.. -I./.. -I../gllib 
-I./../gllib -Wall  -Wno-error -g -O2 -MT test-stdbit-h-c++.o -MD -MP -MF 
$depbase.Tpo -c -o test-stdbit-h-c++.o test-stdbit-h-c++.cc &&\
mv -f $depbase.Tpo $depbase.Po
In file included from test-stdbit-h-c++.cc:22:
../gllib/stdbit.h: In function `int __gl_stdbit_popcount_wide(long long 
   unsigned int)':
../gllib/stdbit.h:299: warning: comparison between signed and unsigned integer 
   expressions
../gllib/stdbit.h:312: error: integer constant is too large for "long" type
../gllib/stdbit.h:332: warning: comparison between signed and unsigned integer 
   expressions
make[4]: *** [test-stdbit-h-c++.o] Error 1

This patch fixes it.


2024-09-16  Bruno Haible  <br...@clisp.org>

        stdc_count_ones: Fix compilation error with g++ < 4.4.
        * lib/stdbit.in.h (__gl_stdbit_popcount_wide): Suffix 64-bit integer
        constant with LL.

diff --git a/lib/stdbit.in.h b/lib/stdbit.in.h
index 9f9e60a5d3..20b9f4f466 100644
--- a/lib/stdbit.in.h
+++ b/lib/stdbit.in.h
@@ -308,7 +308,7 @@ __gl_stdbit_popcount_wide (unsigned long long int n)
         x333333 = max / (1 << 2 | 1),  /* 0x333333... */
         x0f0f0f = max / (1 << 4 | 1),  /* 0x0f0f0f... */
         x010101 = max / ((1 << 8) - 1),        /* 0x010101... */
-        x000_7f = max / 0xffffffffffffffff * 0x7f; /* 0x000000000000007f... */
+        x000_7f = max / 0xffffffffffffffffLL * 0x7f; /* 0x000000000000007f... 
*/
       n -= (n >> 1) & x555555;
       n = (n & x333333) + ((n >> 2) & x333333);
       n = (n + (n >> 4)) & x0f0f0f;




Reply via email to