(root)/
gmp-6.3.0/
mpz/
kronuz.c
       1  /* mpz_ui_kronecker -- ulong+mpz Kronecker/Jacobi symbol.
       2  
       3  Copyright 1999-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_ui_kronecker (unsigned long a, mpz_srcptr b)
      37  {
      38    mp_srcptr  b_ptr;
      39    mp_limb_t  b_low;
      40    int        b_abs_size;
      41    mp_limb_t  b_rem;
      42    int        twos;
      43    int        result_bit1;
      44  
      45    /* (a/-1)=1 when a>=0, so the sign of b is ignored */
      46    b_abs_size = ABSIZ (b);
      47  
      48    if (b_abs_size == 0)
      49      return JACOBI_U0 (a);  /* (a/0) */
      50  
      51    if (a > GMP_NUMB_MAX)
      52      {
      53        mp_limb_t  alimbs[2];
      54        mpz_t      az;
      55        ALLOC(az) = numberof (alimbs);
      56        PTR(az) = alimbs;
      57        mpz_set_ui (az, a);
      58        return mpz_kronecker (az, b);
      59      }
      60  
      61    b_ptr = PTR(b);
      62    b_low = b_ptr[0];
      63    result_bit1 = 0;
      64  
      65    if (! (b_low & 1))
      66      {
      67        /* (0/b)=0 for b!=+/-1; and (even/even)=0 */
      68        if (! (a & 1))
      69  	return 0;
      70  
      71        /* a odd, b even
      72  
      73  	 Establish shifted b_low with valid bit1 for the RECIP below.  Zero
      74  	 limbs stripped are accounted for, but zero bits on b_low are not
      75  	 because they remain in {b_ptr,b_abs_size} for
      76  	 JACOBI_MOD_OR_MODEXACT_1_ODD. */
      77  
      78        JACOBI_STRIP_LOW_ZEROS (result_bit1, a, b_ptr, b_abs_size, b_low);
      79        if (! (b_low & 1))
      80  	{
      81  	  if (UNLIKELY (b_low == GMP_NUMB_HIGHBIT))
      82  	    {
      83  	      /* need b_ptr[1] to get bit1 in b_low */
      84  	      if (b_abs_size == 1)
      85  		{
      86  		  /* (a/0x80...00) == (a/2)^(NUMB-1) */
      87  		  if ((GMP_NUMB_BITS % 2) == 0)
      88  		    {
      89  		      /* JACOBI_STRIP_LOW_ZEROS does nothing to result_bit1
      90  			 when GMP_NUMB_BITS is even, so it's still 0. */
      91  		      ASSERT (result_bit1 == 0);
      92  		      result_bit1 = JACOBI_TWO_U_BIT1 (a);
      93  		    }
      94  		  return JACOBI_BIT1_TO_PN (result_bit1);
      95  		}
      96  
      97  	      /* b_abs_size > 1 */
      98  	      b_low = b_ptr[1] << 1;
      99  	    }
     100  	  else
     101  	    {
     102  	      count_trailing_zeros (twos, b_low);
     103  	      b_low >>= twos;
     104  	    }
     105  	}
     106      }
     107    else
     108      {
     109        if (a == 0)        /* (0/b)=1 for b=+/-1, 0 otherwise */
     110  	return (b_abs_size == 1 && b_low == 1);
     111  
     112        if (! (a & 1))
     113  	{
     114  	  /* a even, b odd */
     115  	  count_trailing_zeros (twos, a);
     116  	  a >>= twos;
     117  	  /* (a*2^n/b) = (a/b) * (2/a)^n */
     118  	  result_bit1 = JACOBI_TWOS_U_BIT1 (twos, b_low);
     119  	}
     120      }
     121  
     122    if (a == 1)
     123      return JACOBI_BIT1_TO_PN (result_bit1);  /* (1/b)=1 */
     124  
     125    /* (a/b*2^n) = (b*2^n mod a / a) * RECIP(a,b) */
     126    JACOBI_MOD_OR_MODEXACT_1_ODD (result_bit1, b_rem, b_ptr, b_abs_size, a);
     127    result_bit1 ^= JACOBI_RECIP_UU_BIT1 (a, b_low);
     128    return mpn_jacobi_base (b_rem, (mp_limb_t) a, result_bit1);
     129  }