1 /* mpn_jacobi_base -- limb/limb Jacobi symbol with restricted arguments.
3 THIS INTERFACE IS PRELIMINARY AND MIGHT DISAPPEAR OR BE SUBJECT TO
4 INCOMPATIBLE CHANGES IN A FUTURE RELEASE OF GMP.
6 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
8 This file is part of the GNU MP Library.
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
15 The GNU MP Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 /* Use the simple loop by default. The generic count_trailing_zeros is not
29 very fast, and the extra trickery of method 3 has proven to be less use
30 than might have been though. */
31 #ifndef JACOBI_BASE_METHOD
32 #define JACOBI_BASE_METHOD 2
36 /* Use count_trailing_zeros. */
37 #if JACOBI_BASE_METHOD == 1
38 #define PROCESS_TWOS_ANY \
41 count_trailing_zeros (twos, a); \
42 result_bit1 ^= JACOBI_TWOS_U_BIT1 (twos, b); \
45 #define PROCESS_TWOS_EVEN PROCESS_TWOS_ANY
48 /* Use a simple loop. A disadvantage of this is that there's a branch on a
49 50/50 chance of a 0 or 1 low bit. */
50 #if JACOBI_BASE_METHOD == 2
51 #define PROCESS_TWOS_EVEN \
54 two = JACOBI_TWO_U_BIT1 (b); \
61 while ((a & 1) == 0); \
63 #define PROCESS_TWOS_ANY \
68 /* Process one bit arithmetically, then a simple loop. This cuts the loop
69 condition down to a 25/75 chance, which should branch predict better.
70 The CPU will need a reasonable variable left shift. */
71 #if JACOBI_BASE_METHOD == 3
72 #define PROCESS_TWOS_EVEN \
74 int two, mask, shift; \
76 two = JACOBI_TWO_U_BIT1 (b); \
82 result_bit1 ^= two ^ (two & mask); \
84 while ((a & 1) == 0) \
91 #define PROCESS_TWOS_ANY \
93 int two, mask, shift; \
95 two = JACOBI_TWO_U_BIT1 (b); \
100 result_bit1 ^= (two & mask); \
102 while ((a & 1) == 0) \
105 result_bit1 ^= two; \
112 /* Calculate the value of the Jacobi symbol (a/b) of two mp_limb_t's, but
113 with a restricted range of inputs accepted, namely b>1, b odd, and a<=b.
115 The initial result_bit1 is taken as a parameter for the convenience of
116 mpz_kronecker_ui() et al. The sign changes both here and in those
117 routines accumulate nicely in bit 1, see the JACOBI macros.
119 The return value here is the normal +1, 0, or -1. Note that +1 and -1
120 have bit 1 in the "BIT1" sense, which could be useful if the caller is
121 accumulating it into some extended calculation.
123 Duplicating the loop body to avoid the MP_LIMB_T_SWAP(a,b) would be
124 possible, but a couple of tests suggest it's not a significant speedup,
125 and may even be a slowdown, so what's here is good enough for now.
127 Future: The code doesn't demand a<=b actually, so maybe this could be
128 relaxed. All the places this is used currently call with a<=b though. */
131 mpn_jacobi_base (mp_limb_t a, mp_limb_t b, int result_bit1)
133 ASSERT (b & 1); /* b odd */
146 result_bit1 ^= JACOBI_RECIP_UU_BIT1 (a, b);
147 MP_LIMB_T_SWAP (a, b);
151 /* working on (a/b), a,b odd, a>=b */
167 return JACOBI_BIT1_TO_PN (result_bit1);