public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] get rid of POPCOUNTCST* macros in libgcc2.c
@ 2022-04-16 15:01 Stefan Kanthak
  0 siblings, 0 replies; only message in thread
From: Stefan Kanthak @ 2022-04-16 15:01 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 697 bytes --]

Hi @ll,

the "magic" constants 0x55...55, 0x33...33, 0x0f...0f and 0x01...01 used
in the popcountsi2() and popcountdi2() functions defined in libgcc2.c are
currently generated iterative via the 4 macros POPCOUNTCST, POPCOUNTCST8,
POPCOUNTCST4 and POPCOUNTCST2 from 2 nibbles over 4 and 8 nibbles to
finally 16 nibbles.

The patch removes these macros and builds the constants from expressions
  ~(U*Wtype) 0 / 3u  = 0x55...55,    ~(U*Wtype) 0 / 5u   = 0x33...33,
  ~(U*Wtype) 0 / 17u = 0x0f...0f and ~(U*Wtype) 0 / 255u = 0x01...01
of the appropriate type (and size).

Additionally the patch lower-cases the single lonesome lingering ULL; all
other suffixes are already lower case.

regards
Stefan

[-- Attachment #2: libgcc2.diff --]
[-- Type: application/octet-stream, Size: 2965 bytes --]

--- -/gcc/libgcc/libgcc2.c
+++ +/gcc/libgcc/libgcc2.c
@@ -801,21 +801,6 @@
 };
 #endif
 \f
-#if defined(L_popcountsi2) || defined(L_popcountdi2)
-#define POPCOUNTCST2(x) (((UWtype) x << __CHAR_BIT__) | x)
-#define POPCOUNTCST4(x) (((UWtype) x << (2 * __CHAR_BIT__)) | x)
-#define POPCOUNTCST8(x) (((UWtype) x << (4 * __CHAR_BIT__)) | x)
-#if W_TYPE_SIZE == __CHAR_BIT__
-#define POPCOUNTCST(x) x
-#elif W_TYPE_SIZE == 2 * __CHAR_BIT__
-#define POPCOUNTCST(x) POPCOUNTCST2 (x)
-#elif W_TYPE_SIZE == 4 * __CHAR_BIT__
-#define POPCOUNTCST(x) POPCOUNTCST4 (POPCOUNTCST2 (x))
-#elif W_TYPE_SIZE == 8 * __CHAR_BIT__
-#define POPCOUNTCST(x) POPCOUNTCST8 (POPCOUNTCST4 (POPCOUNTCST2 (x)))
-#endif
-#endif
-\f
 #ifdef L_popcountsi2
 #undef int
 int
@@ -824,11 +809,11 @@
   /* Force table lookup on targets like AVR and RL78 which only
      pretend they have LIBGCC2_UNITS_PER_WORD 4, but actually
      have 1, and other small word targets.  */
-#if __SIZEOF_INT__ > 2 && defined (POPCOUNTCST) && __CHAR_BIT__ == 8
-  x = x - ((x >> 1) & POPCOUNTCST (0x55));
-  x = (x & POPCOUNTCST (0x33)) + ((x >> 2) & POPCOUNTCST (0x33));
-  x = (x + (x >> 4)) & POPCOUNTCST (0x0F);
-  return (x * POPCOUNTCST (0x01)) >> (W_TYPE_SIZE - __CHAR_BIT__);
+#if __SIZEOF_INT__ > 2 && __CHAR_BIT__ == 8
+  x = x - ((x >> 1) & (~(UWtype) 0 / 3u));
+  x = (x & (~(UWtype) 0 / 5u)) + ((x >> 2) & (~(UWtype) 0 / 5u));
+  x = (x + (x >> 4)) & (~(UWtype) 0 / 17u);
+  return (x * (~(UWtype) 0 / 255u)) >> (W_TYPE_SIZE - __CHAR_BIT__);
 #else
   int i, ret = 0;
 
@@ -848,17 +833,17 @@
   /* Force table lookup on targets like AVR and RL78 which only
      pretend they have LIBGCC2_UNITS_PER_WORD 4, but actually
      have 1, and other small word targets.  */
-#if __SIZEOF_INT__ > 2 && defined (POPCOUNTCST) && __CHAR_BIT__ == 8
+#if __SIZEOF_INT__ > 2 && __CHAR_BIT__ == 8
   const DWunion uu = {.ll = x};
   UWtype x1 = uu.s.low, x2 = uu.s.high;
-  x1 = x1 - ((x1 >> 1) & POPCOUNTCST (0x55));
-  x2 = x2 - ((x2 >> 1) & POPCOUNTCST (0x55));
-  x1 = (x1 & POPCOUNTCST (0x33)) + ((x1 >> 2) & POPCOUNTCST (0x33));
-  x2 = (x2 & POPCOUNTCST (0x33)) + ((x2 >> 2) & POPCOUNTCST (0x33));
-  x1 = (x1 + (x1 >> 4)) & POPCOUNTCST (0x0F);
-  x2 = (x2 + (x2 >> 4)) & POPCOUNTCST (0x0F);
+  x1 = x1 - ((x1 >> 1) & (~(UDWtype) 0 / 3u));
+  x2 = x2 - ((x2 >> 1) & (~(UDWtype) 0 / 3u));
+  x1 = (x1 & (~(UDWtype) 0 / 5u)) + ((x1 >> 2) & (~(UDWtype) 0 / 5u));
+  x2 = (x2 & (~(UDWtype) 0 / 5u)) + ((x2 >> 2) & (~(UDWtype) 0 / 5u));
+  x1 = (x1 + (x1 >> 4)) & (~(UDWtype) 0 / 17u);
+  x2 = (x2 + (x2 >> 4)) & (~(UDWtype) 0 / 17u);
   x1 += x2;
-  return (x1 * POPCOUNTCST (0x01)) >> (W_TYPE_SIZE - __CHAR_BIT__);
+  return (x1 * (~(UDWtype) 0 / 255u)) >> (W_TYPE_SIZE - __CHAR_BIT__);
 #else
   int i, ret = 0;
 
@@ -953,7 +938,7 @@
       if (r >= y)
 	{
 	  r = r - y;
-	  q =  (1ULL << k);
+	  q =  (1ull << k);
 	}
 
       if (k > 0)

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-16 15:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-16 15:01 [PATCH] get rid of POPCOUNTCST* macros in libgcc2.c Stefan Kanthak

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).