beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / kronzs.c
blob695cd0f283e4dde62795764039ebe5d6154d53b2
1 /* mpz_kronecker_si -- mpz+long Kronecker/Jacobi symbol.
3 Copyright 1999-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 /* After the absolute value of b is established it's treated as an unsigned
37 long, because 0x80..00 doesn't fit in a signed long. */
39 int
40 mpz_kronecker_si (mpz_srcptr a, long b)
42 mp_srcptr a_ptr;
43 mp_size_t a_size;
44 mp_limb_t a_rem, b_limb;
45 int result_bit1;
47 a_size = SIZ(a);
48 if (a_size == 0)
49 return JACOBI_0S (b);
51 #if GMP_NUMB_BITS < BITS_PER_ULONG
52 if (b > GMP_NUMB_MAX || b < -GMP_NUMB_MAX)
54 mp_limb_t blimbs[2];
55 mpz_t bz;
56 ALLOC(bz) = numberof (blimbs);
57 PTR(bz) = blimbs;
58 mpz_set_si (bz, b);
59 return mpz_kronecker (a, bz);
61 #endif
63 result_bit1 = JACOBI_BSGN_SS_BIT1 (a_size, b);
64 b_limb = ABS_CAST (unsigned long, b);
65 a_ptr = PTR(a);
67 if ((b_limb & 1) == 0)
69 mp_limb_t a_low = a_ptr[0];
70 int twos;
72 if (b_limb == 0)
73 return JACOBI_LS0 (a_low, a_size); /* (a/0) */
75 if (! (a_low & 1))
76 return 0; /* (even/even)=0 */
78 /* (a/2)=(2/a) for a odd */
79 count_trailing_zeros (twos, b_limb);
80 b_limb >>= twos;
81 result_bit1 ^= JACOBI_TWOS_U_BIT1 (twos, a_low);
84 if (b_limb == 1)
85 return JACOBI_BIT1_TO_PN (result_bit1); /* (a/1)=1 for any a */
87 result_bit1 ^= JACOBI_ASGN_SU_BIT1 (a_size, b_limb);
88 a_size = ABS(a_size);
90 /* (a/b) = (a mod b / b) */
91 JACOBI_MOD_OR_MODEXACT_1_ODD (result_bit1, a_rem, a_ptr, a_size, b_limb);
92 return mpn_jacobi_base (a_rem, b_limb, result_bit1);