xref: /dflybsd-src/contrib/gmp/mpz/kronsz.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpz_si_kronecker -- long+mpz Kronecker/Jacobi symbol.
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
486d7f5d3SJohn Marino 
586d7f5d3SJohn Marino This file is part of the GNU MP Library.
686d7f5d3SJohn Marino 
786d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
886d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
986d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1086d7f5d3SJohn Marino option) any later version.
1186d7f5d3SJohn Marino 
1286d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1386d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1486d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1586d7f5d3SJohn Marino License for more details.
1686d7f5d3SJohn Marino 
1786d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
1886d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
1986d7f5d3SJohn Marino 
2086d7f5d3SJohn Marino #include "gmp.h"
2186d7f5d3SJohn Marino #include "gmp-impl.h"
2286d7f5d3SJohn Marino #include "longlong.h"
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino 
2586d7f5d3SJohn Marino int
mpz_si_kronecker(long a,mpz_srcptr b)2686d7f5d3SJohn Marino mpz_si_kronecker (long a, mpz_srcptr b)
2786d7f5d3SJohn Marino {
2886d7f5d3SJohn Marino   mp_srcptr  b_ptr;
2986d7f5d3SJohn Marino   mp_limb_t  b_low;
3086d7f5d3SJohn Marino   mp_size_t  b_size;
3186d7f5d3SJohn Marino   mp_size_t  b_abs_size;
3286d7f5d3SJohn Marino   mp_limb_t  a_limb, b_rem;
3386d7f5d3SJohn Marino   unsigned   twos;
3486d7f5d3SJohn Marino   int        result_bit1;
3586d7f5d3SJohn Marino 
3686d7f5d3SJohn Marino #if GMP_NUMB_BITS < BITS_PER_ULONG
3786d7f5d3SJohn Marino   if (a > GMP_NUMB_MAX || a < -GMP_NUMB_MAX)
3886d7f5d3SJohn Marino     {
3986d7f5d3SJohn Marino       mp_limb_t  alimbs[2];
4086d7f5d3SJohn Marino       mpz_t      az;
4186d7f5d3SJohn Marino       ALLOC(az) = numberof (alimbs);
4286d7f5d3SJohn Marino       PTR(az) = alimbs;
4386d7f5d3SJohn Marino       mpz_set_si (az, a);
4486d7f5d3SJohn Marino       return mpz_kronecker (az, b);
4586d7f5d3SJohn Marino     }
4686d7f5d3SJohn Marino #endif
4786d7f5d3SJohn Marino 
4886d7f5d3SJohn Marino   b_size = SIZ (b);
4986d7f5d3SJohn Marino   if (b_size == 0)
5086d7f5d3SJohn Marino     return JACOBI_S0 (a);  /* (a/0) */
5186d7f5d3SJohn Marino 
5286d7f5d3SJohn Marino   /* account for the effect of the sign of b, then ignore it */
5386d7f5d3SJohn Marino   result_bit1 = JACOBI_BSGN_SS_BIT1 (a, b_size);
5486d7f5d3SJohn Marino 
5586d7f5d3SJohn Marino   b_ptr = PTR(b);
5686d7f5d3SJohn Marino   b_low = b_ptr[0];
5786d7f5d3SJohn Marino   b_abs_size = ABS (b_size);
5886d7f5d3SJohn Marino 
5986d7f5d3SJohn Marino   if ((b_low & 1) != 0)
6086d7f5d3SJohn Marino     {
6186d7f5d3SJohn Marino       /* b odd */
6286d7f5d3SJohn Marino 
6386d7f5d3SJohn Marino       result_bit1 ^= JACOBI_ASGN_SU_BIT1 (a, b_low);
6486d7f5d3SJohn Marino       a_limb = (unsigned long) ABS(a);
6586d7f5d3SJohn Marino 
6686d7f5d3SJohn Marino       if ((a_limb & 1) == 0)
6786d7f5d3SJohn Marino         {
6886d7f5d3SJohn Marino           /* (0/b)=1 for b=+/-1, 0 otherwise */
6986d7f5d3SJohn Marino           if (a_limb == 0)
7086d7f5d3SJohn Marino             return (b_abs_size == 1 && b_low == 1);
7186d7f5d3SJohn Marino 
7286d7f5d3SJohn Marino           /* a even, b odd */
7386d7f5d3SJohn Marino           count_trailing_zeros (twos, a_limb);
7486d7f5d3SJohn Marino           a_limb >>= twos;
7586d7f5d3SJohn Marino           /* (a*2^n/b) = (a/b) * twos(n,a) */
7686d7f5d3SJohn Marino           result_bit1 ^= JACOBI_TWOS_U_BIT1 (twos, b_low);
7786d7f5d3SJohn Marino         }
7886d7f5d3SJohn Marino     }
7986d7f5d3SJohn Marino   else
8086d7f5d3SJohn Marino     {
8186d7f5d3SJohn Marino       /* (even/even)=0, and (0/b)=0 for b!=+/-1 */
8286d7f5d3SJohn Marino       if ((a & 1) == 0)
8386d7f5d3SJohn Marino         return 0;
8486d7f5d3SJohn Marino 
8586d7f5d3SJohn Marino       /* a odd, b even
8686d7f5d3SJohn Marino 
8786d7f5d3SJohn Marino          Establish shifted b_low with valid bit1 for ASGN and RECIP below.
8886d7f5d3SJohn Marino          Zero limbs stripped are accounted for, but zero bits on b_low are
8986d7f5d3SJohn Marino          not because they remain in {b_ptr,b_abs_size} for the
9086d7f5d3SJohn Marino          JACOBI_MOD_OR_MODEXACT_1_ODD. */
9186d7f5d3SJohn Marino 
9286d7f5d3SJohn Marino       JACOBI_STRIP_LOW_ZEROS (result_bit1, a, b_ptr, b_abs_size, b_low);
9386d7f5d3SJohn Marino       if ((b_low & 1) == 0)
9486d7f5d3SJohn Marino         {
9586d7f5d3SJohn Marino           if (UNLIKELY (b_low == GMP_NUMB_HIGHBIT))
9686d7f5d3SJohn Marino             {
9786d7f5d3SJohn Marino               /* need b_ptr[1] to get bit1 in b_low */
9886d7f5d3SJohn Marino               if (b_abs_size == 1)
9986d7f5d3SJohn Marino                 {
10086d7f5d3SJohn Marino                   /* (a/0x80000000) = (a/2)^(BPML-1) */
10186d7f5d3SJohn Marino                   if ((GMP_NUMB_BITS % 2) == 0)
10286d7f5d3SJohn Marino                     result_bit1 ^= JACOBI_TWO_U_BIT1 (a);
10386d7f5d3SJohn Marino                   return JACOBI_BIT1_TO_PN (result_bit1);
10486d7f5d3SJohn Marino                 }
10586d7f5d3SJohn Marino 
10686d7f5d3SJohn Marino               /* b_abs_size > 1 */
10786d7f5d3SJohn Marino               b_low = b_ptr[1] << 1;
10886d7f5d3SJohn Marino             }
10986d7f5d3SJohn Marino           else
11086d7f5d3SJohn Marino             {
11186d7f5d3SJohn Marino               count_trailing_zeros (twos, b_low);
11286d7f5d3SJohn Marino               b_low >>= twos;
11386d7f5d3SJohn Marino             }
11486d7f5d3SJohn Marino         }
11586d7f5d3SJohn Marino 
11686d7f5d3SJohn Marino       result_bit1 ^= JACOBI_ASGN_SU_BIT1 (a, b_low);
11786d7f5d3SJohn Marino       a_limb = (unsigned long) ABS(a);
11886d7f5d3SJohn Marino     }
11986d7f5d3SJohn Marino 
12086d7f5d3SJohn Marino   if (a_limb == 1)
12186d7f5d3SJohn Marino     return JACOBI_BIT1_TO_PN (result_bit1);  /* (1/b)=1 */
12286d7f5d3SJohn Marino 
12386d7f5d3SJohn Marino   /* (a/b*2^n) = (b*2^n mod a / a) * recip(a,b) */
12486d7f5d3SJohn Marino   JACOBI_MOD_OR_MODEXACT_1_ODD (result_bit1, b_rem, b_ptr, b_abs_size, a_limb);
12586d7f5d3SJohn Marino   result_bit1 ^= JACOBI_RECIP_UU_BIT1 (a_limb, b_low);
12686d7f5d3SJohn Marino   return mpn_jacobi_base (b_rem, a_limb, result_bit1);
12786d7f5d3SJohn Marino }
128