]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
x86_64: Optimize ffsll function code size.
authorSunil K Pandey <skpgkp2@gmail.com>
Wed, 26 Jul 2023 15:34:05 +0000 (08:34 -0700)
committerSunil K Pandey <skpgkp2@gmail.com>
Thu, 8 Feb 2024 18:34:19 +0000 (10:34 -0800)
Ffsll function randomly regress by ~20%, depending on how code gets
aligned in memory.  Ffsll function code size is 17 bytes.  Since default
function alignment is 16 bytes, it can load on 16, 32, 48 or 64 bytes
aligned memory.  When ffsll function load at 16, 32 or 64 bytes aligned
memory, entire code fits in single 64 bytes cache line.  When ffsll
function load at 48 bytes aligned memory, it splits in two cache line,
hence random regression.

Ffsll function size reduction from 17 bytes to 12 bytes ensures that it
will always fit in single 64 bytes cache line.

This patch fixes ffsll function random performance regression.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
(cherry picked from commit 9d94997b5f9445afd4f2bccc5fa60ff7c4361ec1)

sysdeps/x86_64/ffsll.c

index 206deb68102f73c16d65810710a1d829d6c4f5d3..5cfc62817d36fb6b8a4354789411f01471f501ae 100644 (file)
@@ -27,13 +27,13 @@ int
 ffsll (long long int x)
 {
   long long int cnt;
-  long long int tmp;
 
-  asm ("bsfq %2,%0\n"          /* Count low bits in X and store in %1.  */
-       "cmoveq %1,%0\n"                /* If number was zero, use -1 as result.  */
-       : "=&r" (cnt), "=r" (tmp) : "rm" (x), "1" (-1));
+  asm ("mov $-1,%k0\n" /* Initialize cnt to -1.  */
+       "bsf %1,%0\n"   /* Count low bits in x and store in cnt.  */
+       "inc %k0\n"     /* Increment cnt by 1.  */
+       : "=&r" (cnt) : "r" (x));
 
-  return cnt + 1;
+  return cnt;
 }
 
 #ifndef __ILP32__