beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / gen-jacobitab.c
blob537994b3b27d1996e992fcdd2e732dc3920602e6
1 /* gen-jacobi.c
3 Contributed to the GNU project by Niels Möller.
5 Copyright 2010 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free Software
19 Foundation; either version 2 of the License, or (at your option) any
20 later version.
22 or both in parallel, as here.
24 The GNU MP Library is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 for more details.
29 You should have received copies of the GNU General Public License and the
30 GNU Lesser General Public License along with the GNU MP Library. If not,
31 see https://www.gnu.org/licenses/. */
33 /* Generate the lookup table needed for fast left-to-right computation
34 of the Jacobi symbol. */
36 #include <assert.h>
37 #include <stdio.h>
38 #include <stdlib.h>
40 static const struct
42 unsigned char a;
43 unsigned char b;
44 } decode_table[13] = {
45 /* 0 */ { 0, 1 },
46 /* 1 */ { 0, 3 },
47 /* 2 */ { 1, 1 },
48 /* 3 */ { 1, 3 },
49 /* 4 */ { 2, 1 },
50 /* 5 */ { 2, 3 },
51 /* 6 */ { 3, 1 },
52 /* 7 */ { 3, 3 }, /* d = 1 */
53 /* 8 */ { 1, 0 },
54 /* 9 */ { 1, 2 },
55 /* 10 */ { 3, 0 },
56 /* 11 */ { 3, 2 },
57 /* 12 */ { 3, 3 }, /* d = 0 */
60 #define JACOBI_A(bits) (decode_table[(bits)>>1].a)
61 #define JACOBI_B(bits) (decode_table[(bits)>>1].b)
63 #define JACOBI_E(bits) ((bits) & 1)
64 #define JACOBI_D(bits) (((bits)>>1) == 7) /* Gives 0 for don't care states. */
66 static unsigned
67 encode (unsigned a, unsigned b, unsigned d)
69 unsigned i;
71 assert (d < 2);
72 assert (a < 4);
73 assert (b < 4);
74 assert ( (a | b ) & 1);
76 if (a == 3 && b == 3)
77 return d ? 7 : 12;
79 for (i = 0; i < 12; i++)
80 if (decode_table[i].a == a
81 && decode_table[i].b == b)
82 return i;
84 abort ();
87 int
88 main (int argc, char **argv)
90 unsigned bits;
92 for (bits = 0; bits < 208; bits++)
94 unsigned e, a, b, d_old, d, q;
96 if (bits && !(bits & 0xf))
97 printf("\n");
99 q = bits & 3;
100 d = (bits >> 2) & 1;
102 e = JACOBI_E (bits >> 3);
103 a = JACOBI_A (bits >> 3);
104 b = JACOBI_B (bits >> 3);
105 d_old = JACOBI_D (bits >> 3);
107 if (d != d_old && a == 3 && b == 3)
108 e ^= 1;
110 if (d == 1)
112 if (b == 2)
113 e ^= (q & (a >> 1)) ^ (q >> 1);
114 a = (a - q * b) & 3;
116 else
118 if (a == 2)
119 e ^= (q & (b >> 1)) ^ (q >> 1);
120 b = (b - q * a) & 3;
123 printf("%2d,", (encode (a, b, d) << 1) | e);
125 printf("\n");
127 return 0;