count_leading_zeros on 64-bit ppc

Torbjorn Granlund tg-this-will-bounce-but-I-am-subscribed-to-the-list-honest at
Sun Jun 3 23:21:57 CEST 2007

David Harvey <dmharvey at> writes:

  I think there may be a bug in count_leading_zeros in longlong.h on 64- 
  bit PPC platforms. This is GMP 4.2.1.
  This is a bit worrying since I looked in longlong.h, and it looks  
  like assembly is used directly:
  #define count_leading_zeros(count, x) \
     __asm__ ("cntlzd %0,%1" : "=r" (count) : "r" (x))
  So if I'm reading this right, it might be a bug in the chip itself.
  Here is an example program that illustrates the bug:
  #define UWtype unsigned long
  #define UHWtype unsigned long
  #define UDWtype unsigned long
  #define W_TYPE_SIZE 64
  #include <stdio.h>
  #include <gmp.h>
  #include "longlong.h"
  int main()
      unsigned long x = 0x0000000001379102;
      unsigned long c;
      count_leading_zeros(c, x);
      printf("%d\n", c);
      return 0;
You're not using the macro properly.
Your example works wrong, perhaps because it gets a 32-bit gmp.h,
or something else.

But in a proper 64-bit gmp context, it works right.

I'll let you debug the exact cause of the failure.  :-)  


More information about the gmp-bugs mailing list