GMP 6.1.2 t-count_zeros failure on ARM with assertions

Niels Möller nisse at lysator.liu.se
Tue Dec 26 10:51:28 UTC 2017


Vincent Lefevre <vincent at vinc17.net> writes:

> #ifdef COUNT_LEADING_ZEROS_0
>   check_clz (COUNT_LEADING_ZEROS_0, CNST_LIMB(0));
> #endif
>
> If GMP does a test with CNST_LIMB(0), I wonder why it has an
> assertion (n) != 0. This is not consistent!

The docs say:

   5) count_leading_zeros(count, x) counts the number of zero-bits from the
   msb to the first non-zero bit in the UWtype X.  This is the number of
   steps X needs to be shifted left to set the msb.  Undefined for X == 0,
   unless the symbol COUNT_LEADING_ZEROS_0 is defined to some value.

Which definition of count_leading_zeros is used in your build? It should
be fixed to set COUNT_LEADING_ZEROS_0 to match the implementation.

Hmm, I guess the problem is

  #define count_leading_zeros_gcc_clz(count,x)	\
    do {						\
      ASSERT ((x) != 0);				\
      (count) = __builtin_clzll (x);		\
    } while (0)

in combination with 

  #define count_leading_zeros(count, x) count_leading_zeros_gcc_clz(count, x)
  #define count_trailing_zeros(count, x) count_trailing_zeros_gcc_ctz(count, x)
  #define COUNT_LEADING_ZEROS_0 32

in the arm32 and arm64 parts of longlong.h.

The gcc docs seem to say that the result of __builtin_clz is undefined
for zero input. So I'd suggest the below patch.

And as far as I find (based on greping for COUNT_LEADING_ZEROS_0), we
don't make use of count_leading_zeros with zero input, even on platforms
where it is allowed. So maybe we can delete this preprocessor constant
altogether and say that count_leading_zeros mustn't be used with zero
input?

Regards,
/Niels

diff -r 20cf1131dc94 longlong.h
--- a/longlong.h	Thu Aug 31 01:00:02 2017 +0200
+++ b/longlong.h	Tue Dec 26 10:59:24 2017 +0100
@@ -535,7 +535,6 @@ extern UWtype __MPN(udiv_qrnnd) (UWtype 
 #endif /* defined(__ARM_ARCH_2__) ... */
 #define count_leading_zeros(count, x)  count_leading_zeros_gcc_clz(count, x)
 #define count_trailing_zeros(count, x)  count_trailing_zeros_gcc_ctz(count, x)
-#define COUNT_LEADING_ZEROS_0 32
 #endif /* __arm__ */
 
 #if defined (__aarch64__) && W_TYPE_SIZE == 64
@@ -586,7 +585,6 @@ extern UWtype __MPN(udiv_qrnnd) (UWtype 
 #endif
 #define count_leading_zeros(count, x)  count_leading_zeros_gcc_clz(count, x)
 #define count_trailing_zeros(count, x)  count_trailing_zeros_gcc_ctz(count, x)
-#define COUNT_LEADING_ZEROS_0 64
 #endif /* __aarch64__ */
 
 #if defined (__clipper__) && W_TYPE_SIZE == 32

-- 
Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.


More information about the gmp-bugs mailing list