Blame gen-jacobitab.c

Packit 5c3484
/* gen-jacobi.c
Packit 5c3484
Packit 5c3484
   Contributed to the GNU project by Niels Möller.
Packit 5c3484
Packit 5c3484
Copyright 2010 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
/* Generate the lookup table needed for fast left-to-right computation
Packit 5c3484
   of the Jacobi symbol. */
Packit 5c3484
Packit 5c3484
#include <assert.h>
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
Packit 5c3484
static const struct
Packit 5c3484
{
Packit 5c3484
  unsigned char a;
Packit 5c3484
  unsigned char b;
Packit 5c3484
} decode_table[13] = {
Packit 5c3484
  /*  0 */ { 0, 1 },
Packit 5c3484
  /*  1 */ { 0, 3 },
Packit 5c3484
  /*  2 */ { 1, 1 },
Packit 5c3484
  /*  3 */ { 1, 3 },
Packit 5c3484
  /*  4 */ { 2, 1 },
Packit 5c3484
  /*  5 */ { 2, 3 },
Packit 5c3484
  /*  6 */ { 3, 1 },
Packit 5c3484
  /*  7 */ { 3, 3 }, /* d = 1 */
Packit 5c3484
  /*  8 */ { 1, 0 },
Packit 5c3484
  /*  9 */ { 1, 2 },
Packit 5c3484
  /* 10 */ { 3, 0 },
Packit 5c3484
  /* 11 */ { 3, 2 },
Packit 5c3484
  /* 12 */ { 3, 3 }, /* d = 0 */
Packit 5c3484
Packit 5c3484
};
Packit 5c3484
#define JACOBI_A(bits) (decode_table[(bits)>>1].a)
Packit 5c3484
#define JACOBI_B(bits) (decode_table[(bits)>>1].b)
Packit 5c3484
Packit 5c3484
#define JACOBI_E(bits) ((bits) & 1)
Packit 5c3484
#define JACOBI_D(bits) (((bits)>>1) == 7) /* Gives 0 for don't care states. */
Packit 5c3484
Packit 5c3484
static unsigned
Packit 5c3484
encode (unsigned a, unsigned b, unsigned d)
Packit 5c3484
{
Packit 5c3484
  unsigned i;
Packit 5c3484
Packit 5c3484
  assert (d < 2);
Packit 5c3484
  assert (a < 4);
Packit 5c3484
  assert (b < 4);
Packit 5c3484
  assert ( (a | b ) & 1);
Packit 5c3484
Packit 5c3484
  if (a == 3 && b == 3)
Packit 5c3484
    return d ? 7 : 12;
Packit 5c3484
Packit 5c3484
  for (i = 0; i < 12; i++)
Packit 5c3484
    if (decode_table[i].a == a
Packit 5c3484
	&& decode_table[i].b == b)
Packit 5c3484
      return i;
Packit 5c3484
Packit 5c3484
  abort ();
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
main (int argc, char **argv)
Packit 5c3484
{
Packit 5c3484
  unsigned bits;
Packit 5c3484
Packit 5c3484
  for (bits = 0; bits < 208; bits++)
Packit 5c3484
    {
Packit 5c3484
      unsigned e, a, b, d_old, d, q;
Packit 5c3484
Packit 5c3484
      if (bits && !(bits & 0xf))
Packit 5c3484
	printf("\n");
Packit 5c3484
Packit 5c3484
      q = bits & 3;
Packit 5c3484
      d = (bits >> 2) & 1;
Packit 5c3484
Packit 5c3484
      e = JACOBI_E (bits >> 3);
Packit 5c3484
      a = JACOBI_A (bits >> 3);
Packit 5c3484
      b = JACOBI_B (bits >> 3);
Packit 5c3484
      d_old = JACOBI_D (bits >> 3);
Packit 5c3484
Packit 5c3484
      if (d != d_old && a == 3 && b == 3)
Packit 5c3484
	e ^= 1;
Packit 5c3484
Packit 5c3484
      if (d == 1)
Packit 5c3484
	{
Packit 5c3484
	  if (b == 2)
Packit 5c3484
	    e ^= (q & (a >> 1)) ^ (q >> 1);
Packit 5c3484
	  a = (a - q * b) & 3;
Packit 5c3484
	}
Packit 5c3484
      else
Packit 5c3484
	{
Packit 5c3484
	  if (a == 2)
Packit 5c3484
	    e ^= (q & (b >> 1)) ^ (q >> 1);
Packit 5c3484
	  b = (b - q * a) & 3;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
      printf("%2d,", (encode (a, b, d) << 1) | e);
Packit 5c3484
    }
Packit 5c3484
  printf("\n");
Packit 5c3484
Packit 5c3484
  return 0;
Packit 5c3484
}