1 | /* mpz_kronecker_ui -- mpz+ulong Kronecker/Jacobi symbol. |
---|
2 | |
---|
3 | Copyright 1999, 2000, 2001, 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 the GNU Lesser General Public License as published by |
---|
9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
10 | option) any later version. |
---|
11 | |
---|
12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
15 | License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Lesser General Public License |
---|
18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include "gmp.h" |
---|
23 | #include "gmp-impl.h" |
---|
24 | #include "longlong.h" |
---|
25 | |
---|
26 | |
---|
27 | /* This implementation depends on BITS_PER_MP_LIMB being even, so that |
---|
28 | (a/2)^BITS_PER_MP_LIMB = 1 and so there's no need to pay attention to how |
---|
29 | many low zero limbs are stripped. */ |
---|
30 | #if BITS_PER_MP_LIMB % 2 != 0 |
---|
31 | Error, error, unsupported BITS_PER_MP_LIMB |
---|
32 | #endif |
---|
33 | |
---|
34 | |
---|
35 | int |
---|
36 | mpz_kronecker_ui (mpz_srcptr a, unsigned long b) |
---|
37 | { |
---|
38 | mp_srcptr a_ptr = PTR(a); |
---|
39 | mp_size_t a_size; |
---|
40 | mp_limb_t a_rem; |
---|
41 | int result_bit1; |
---|
42 | |
---|
43 | a_size = SIZ(a); |
---|
44 | if (a_size == 0) |
---|
45 | return JACOBI_0U (b); |
---|
46 | |
---|
47 | if (b > GMP_NUMB_MAX) |
---|
48 | { |
---|
49 | mp_limb_t blimbs[2]; |
---|
50 | mpz_t bz; |
---|
51 | ALLOC(bz) = numberof (blimbs); |
---|
52 | PTR(bz) = blimbs; |
---|
53 | mpz_set_ui (bz, b); |
---|
54 | return mpz_kronecker (a, bz); |
---|
55 | } |
---|
56 | |
---|
57 | if ((b & 1) != 0) |
---|
58 | { |
---|
59 | result_bit1 = JACOBI_ASGN_SU_BIT1 (a_size, b); |
---|
60 | } |
---|
61 | else |
---|
62 | { |
---|
63 | mp_limb_t a_low = a_ptr[0]; |
---|
64 | int twos; |
---|
65 | |
---|
66 | if (b == 0) |
---|
67 | return JACOBI_LS0 (a_low, a_size); /* (a/0) */ |
---|
68 | |
---|
69 | if (! (a_low & 1)) |
---|
70 | return 0; /* (even/even)=0 */ |
---|
71 | |
---|
72 | /* (a/2)=(2/a) for a odd */ |
---|
73 | count_trailing_zeros (twos, b); |
---|
74 | b >>= twos; |
---|
75 | result_bit1 = (JACOBI_TWOS_U_BIT1 (twos, a_low) |
---|
76 | ^ JACOBI_ASGN_SU_BIT1 (a_size, b)); |
---|
77 | } |
---|
78 | |
---|
79 | if (b == 1) |
---|
80 | return JACOBI_BIT1_TO_PN (result_bit1); /* (a/1)=1 for any a */ |
---|
81 | |
---|
82 | a_size = ABS(a_size); |
---|
83 | |
---|
84 | /* (a/b) = (a mod b / b) */ |
---|
85 | JACOBI_MOD_OR_MODEXACT_1_ODD (result_bit1, a_rem, a_ptr, a_size, b); |
---|
86 | return mpn_jacobi_base (a_rem, (mp_limb_t) b, result_bit1); |
---|
87 | } |
---|