#
source:
trunk/third/gmp/mpz/kronuz.c
@
18191

Revision 18191, 3.1 KB checked in by ghudson, 22 years ago (diff) |
---|

Line | |
---|---|

1 | /* mpz_ui_kronecker -- ulong+mpz 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_ui_kronecker (unsigned long a, mpz_srcptr b) |

37 | { |

38 | mp_srcptr b_ptr = PTR(b); |

39 | mp_limb_t b_low = b_ptr[0]; |

40 | mp_size_t b_abs_size = ABSIZ (b); |

41 | mp_limb_t b_rem; |

42 | int twos; |

43 | int result_bit1 = 0; |

44 | |

45 | /* (a/-1)=1 when a>=0, so the sign of b is ignored */ |

46 | |

47 | if (b_abs_size == 0) |

48 | return JACOBI_U0 (a); /* (a/0) */ |

49 | |

50 | if (a > GMP_NUMB_MAX) |

51 | { |

52 | mp_limb_t alimbs[2]; |

53 | mpz_t az; |

54 | ALLOC(az) = numberof (alimbs); |

55 | PTR(az) = alimbs; |

56 | mpz_set_ui (az, a); |

57 | return mpz_kronecker (az, b); |

58 | } |

59 | |

60 | if (! (b_low & 1)) |

61 | { |

62 | /* (0/b)=0 for b!=+/-1; (even/even)=0 */ |

63 | if (! (a & 1)) |

64 | return 0; |

65 | |

66 | /* a odd, b even */ |

67 | |

68 | /* establish shifted b_low for use with reciprocity below */ |

69 | MPN_STRIP_LOW_ZEROS_NOT_ZERO (b_ptr, b_abs_size, b_low); |

70 | if (! (b_low & 1)) |

71 | { |

72 | if (b_low == GMP_NUMB_HIGHBIT) |

73 | { |

74 | if (b_abs_size == 1) /* (a/0x80000000) == (a/2)^(BPML-1) */ |

75 | return JACOBI_TWOS_U (GMP_NUMB_BITS-1, a); |

76 | |

77 | /* b_abs_size > 1 */ |

78 | b_low = b_ptr[1] << 1; |

79 | } |

80 | else |

81 | { |

82 | count_trailing_zeros (twos, b_low); |

83 | b_low >>= twos; |

84 | } |

85 | } |

86 | } |

87 | else |

88 | { |

89 | if (a == 0) /* (0/b)=1 for b=+/-1, 0 otherwise */ |

90 | return (b_abs_size == 1 && b_low == 1); |

91 | |

92 | if (! (a & 1)) |

93 | { |

94 | /* a even, b odd */ |

95 | count_trailing_zeros (twos, a); |

96 | a >>= twos; |

97 | /* (a*2^n/b) = (a/b) * (2/a)^n */ |

98 | result_bit1 = JACOBI_TWOS_U_BIT1 (twos, b_low); |

99 | } |

100 | } |

101 | |

102 | if (a == 1) |

103 | return JACOBI_BIT1_TO_PN (result_bit1); /* (1/b)=1 */ |

104 | |

105 | /* (a/b*2^n) = (b*2^n mod a / a) * RECIP(a,b) */ |

106 | JACOBI_MOD_OR_MODEXACT_1_ODD (result_bit1, b_rem, b_ptr, b_abs_size, a); |

107 | result_bit1 ^= JACOBI_RECIP_UU_BIT1 (a, b_low); |

108 | return mpn_jacobi_base (b_rem, (mp_limb_t) a, result_bit1); |

109 | } |

**Note:**See TracBrowser for help on using the repository browser.