beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / divis_ui.c
blob41ff5f0b84efb20a713eba2af40b8ba52a5f689d
1 /* mpz_divisible_ui_p -- mpz by ulong divisibility test.
3 Copyright 2000-2002 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
33 #include "longlong.h"
36 int
37 mpz_divisible_ui_p (mpz_srcptr a, unsigned long d)
39 mp_size_t asize;
40 mp_ptr ap;
41 unsigned twos;
43 asize = SIZ(a);
44 if (UNLIKELY (d == 0))
45 return (asize == 0);
47 if (asize == 0) /* 0 divisible by any d */
48 return 1;
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)
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);
62 ap = PTR(a);
63 asize = ABS(asize); /* ignore sign of a */
65 if (ABOVE_THRESHOLD (asize, BMOD_1_TO_MOD_1_THRESHOLD))
66 return mpn_mod_1 (ap, asize, (mp_limb_t) d) == 0;
68 if (! (d & 1))
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. */
73 if ((ap[0] & LOW_ZEROS_MASK (d)) != 0)
74 return 0;
76 count_trailing_zeros (twos, (mp_limb_t) d);
77 d >>= twos;
80 return mpn_modexact_1_odd (ap, asize, (mp_limb_t) d) == 0;