Coverage Report

Created: 2024-06-28 06:19

/src/gmp-6.2.1/mpz/divis_ui.c
Line
Count
Source (jump to first uncovered line)
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-impl.h"
32
#include "longlong.h"
33
34
35
int
36
mpz_divisible_ui_p (mpz_srcptr a, unsigned long d)
37
65
{
38
65
  mp_size_t  asize;
39
65
  mp_ptr     ap;
40
65
  unsigned   twos;
41
42
65
  asize = SIZ(a);
43
65
  if (UNLIKELY (d == 0))
44
0
    return (asize == 0);
45
46
65
  if (asize == 0)  /* 0 divisible by any d */
47
1
    return 1;
48
49
  /* For nails don't try to be clever if d is bigger than a limb, just fake
50
     up an mpz_t and go to the main mpz_divisible_p.  */
51
64
  if (d > GMP_NUMB_MAX)
52
0
    {
53
0
      mp_limb_t  dlimbs[2];
54
0
      mpz_t      dz;
55
0
      ALLOC(dz) = 2;
56
0
      PTR(dz) = dlimbs;
57
0
      mpz_set_ui (dz, d);
58
0
      return mpz_divisible_p (a, dz);
59
0
    }
60
61
64
  ap = PTR(a);
62
64
  asize = ABS(asize);  /* ignore sign of a */
63
64
64
  if (ABOVE_THRESHOLD (asize, BMOD_1_TO_MOD_1_THRESHOLD))
65
16
    return mpn_mod_1 (ap, asize, (mp_limb_t) d) == 0;
66
67
48
  if (! (d & 1))
68
20
    {
69
      /* Strip low zero bits to get odd d required by modexact.  If d==e*2^n
70
   and a is divisible by 2^n and by e, then it's divisible by d. */
71
72
20
      if ((ap[0] & LOW_ZEROS_MASK (d)) != 0)
73
11
  return 0;
74
75
9
      count_trailing_zeros (twos, (mp_limb_t) d);
76
9
      d >>= twos;
77
9
    }
78
79
37
  return mpn_modexact_1_odd (ap, asize, (mp_limb_t) d) == 0;
80
48
}