github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/divis_ui.c (about)

     1  /* mpz_divisible_ui_p -- mpz by ulong divisibility test.
     2  
     3  Copyright 2000-2002 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library.
     6  
     7  The GNU MP Library is free software; you can redistribute it and/or modify
     8  it under the terms of either:
     9  
    10    * the GNU Lesser General Public License as published by the Free
    11      Software Foundation; either version 3 of the License, or (at your
    12      option) any later version.
    13  
    14  or
    15  
    16    * the GNU General Public License as published by the Free Software
    17      Foundation; either version 2 of the License, or (at your option) any
    18      later version.
    19  
    20  or both in parallel, as here.
    21  
    22  The GNU MP Library is distributed in the hope that it will be useful, but
    23  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    24  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    25  for more details.
    26  
    27  You should have received copies of the GNU General Public License and the
    28  GNU Lesser General Public License along with the GNU MP Library.  If not,
    29  see https://www.gnu.org/licenses/.  */
    30  
    31  #include "gmp.h"
    32  #include "gmp-impl.h"
    33  #include "longlong.h"
    34  
    35  
    36  int
    37  mpz_divisible_ui_p (mpz_srcptr a, unsigned long d)
    38  {
    39    mp_size_t  asize;
    40    mp_ptr     ap;
    41    unsigned   twos;
    42  
    43    asize = SIZ(a);
    44    if (UNLIKELY (d == 0))
    45      return (asize == 0);
    46  
    47    if (asize == 0)  /* 0 divisible by any d */
    48      return 1;
    49  
    50    /* For nails don't try to be clever if d is bigger than a limb, just fake
    51       up an mpz_t and go to the main mpz_divisible_p.  */
    52    if (d > GMP_NUMB_MAX)
    53      {
    54        mp_limb_t  dlimbs[2];
    55        mpz_t      dz;
    56        ALLOC(dz) = 2;
    57        PTR(dz) = dlimbs;
    58        mpz_set_ui (dz, d);
    59        return mpz_divisible_p (a, dz);
    60      }
    61  
    62    ap = PTR(a);
    63    asize = ABS(asize);  /* ignore sign of a */
    64  
    65    if (ABOVE_THRESHOLD (asize, BMOD_1_TO_MOD_1_THRESHOLD))
    66      return mpn_mod_1 (ap, asize, (mp_limb_t) d) == 0;
    67  
    68    if (! (d & 1))
    69      {
    70        /* Strip low zero bits to get odd d required by modexact.  If d==e*2^n
    71  	 and a is divisible by 2^n and by e, then it's divisible by d. */
    72  
    73        if ((ap[0] & LOW_ZEROS_MASK (d)) != 0)
    74  	return 0;
    75  
    76        count_trailing_zeros (twos, (mp_limb_t) d);
    77        d >>= twos;
    78      }
    79  
    80    return mpn_modexact_1_odd (ap, asize, (mp_limb_t) d) == 0;
    81  }