Exemplo n.º 1
0
/*
 * Calculate:
 * (x*2^4 + word[3,0]*h) *
 * 2^4 + word[7,4]*h) *
 * ...
 * 2^4 + word[63,60]*h
 */
static struct gf128
gfmultword(uint64_t word, struct gf128 x, struct gf128table *tbl)
{
	struct gf128 row;
	unsigned bits;
	unsigned redbits;
	int i;

	for (i = 0; i < 64; i += 4) {
		bits = word % 16;

		/* fetch row */
		row = readrow(tbl, bits);

		/* x * 2^4 */
		redbits = x.v[1] % 16;
		x.v[1] = (x.v[1] >> 4) | (x.v[0] % 16) << 60;
		x.v[0] >>= 4;
		x.v[0] ^= (uint64_t)reduction[redbits] << (64 - 16);

		word >>= 4;

		x = gf128_add(x, row);
	}

	return x;
}
Exemplo n.º 2
0
/*
 * Calculate
 * (x*2^4 + worda[3,0]*h^4+wordb[3,0]*h^3+...+wordd[3,0]*h) *
 * ...
 * 2^4 + worda[63,60]*h^4+ ... + wordd[63,60]*h
 *
 * Passing/returning struct is .5% faster than passing in via pointer on
 * amd64.
 */
static struct gf128
gfmultword4(uint64_t worda, uint64_t wordb, uint64_t wordc, uint64_t wordd,
    struct gf128 x, struct gf128table4 *tbl)
{
	struct gf128 rowa, rowb, rowc, rowd;
	unsigned bitsa, bitsb, bitsc, bitsd;
	unsigned redbits;
	int i;

	/*
	 * XXX - nibble reverse words to save a shift? probably not as
	 * nibble reverse would take 20 ops (5 * 4) verse 16
	 */

	for (i = 0; i < 64; i += 4) {
		bitsa = worda % 16;
		bitsb = wordb % 16;
		bitsc = wordc % 16;
		bitsd = wordd % 16;

		/* fetch row */
		rowa = readrow(&tbl->tbls[3], bitsa);
		rowb = readrow(&tbl->tbls[2], bitsb);
		rowc = readrow(&tbl->tbls[1], bitsc);
		rowd = readrow(&tbl->tbls[0], bitsd);

		/* x * 2^4 */
		redbits = x.v[1] % 16;
		x.v[1] = (x.v[1] >> 4) | (x.v[0] % 16) << 60;
		x.v[0] >>= 4;
		x.v[0] ^= (uint64_t)reduction[redbits] << (64 - 16);

		worda >>= 4;
		wordb >>= 4;
		wordc >>= 4;
		wordd >>= 4;

		x = gf128_add(x, gf128_add(rowa, gf128_add(rowb,
		    gf128_add(rowc, rowd))));
	}

	return x;
}
void InitGrid(char *InputFile)
{
   /* Output:
    * GenFieldGrid, ParticleGrid initialization
    * Maxiters, TimeBit
   */
      
   int nv, iv;
   double dv;
   char filerow[80]; 
   FILE *inpunit;
      
   fprintf(stdout,"Initializing grids ...\n");
   
   inpunit = fopen(InputFile,"r");
   if ( ! inpunit ) {
      fprintf(stderr,"!!!! Error read access to file %s\n",InputFile);
      exit(-1);
   }

   // Now read measured values; they are read in the followin order:
   // GenFieldGrid.EX, GenFieldGrid.EY, 
   // GenFieldGrid.Xs, GenFieldGrid.Xe, GenFieldGrid.Ys, GenFieldGrid.Ye
   // ParticleGrid.Xs, ParticleGrid.Xe, ParticleGrid.Ys, ParticleGrid.Ye
   
   nv = 0; iv = 0; dv = 0.0;
   while ( 1 )
      {
      	  if ( readrow(filerow, 80, inpunit) < 1 ) {
      	  	  fprintf(stderr,"Error reading input file\n");
      	  	  exit(-1);
      	  }
      	  if ( filerow[0] == '#' ) continue;
      	  if ( nv <= 0 ) {
			if ( sscanf(filerow,"%d",&iv) < 1 ) {
              fprintf(stderr,"Error reading EX from string\n");
      	  	  exit(-1);
			}
			GenFieldGrid.EX = iv; nv = 1;
			continue;
		  }
		  if ( nv == 1 ) {
			if ( sscanf(filerow,"%d",&iv) < 1 ) {
              fprintf(stderr,"Error reading EY from string\n");
      	  	  exit(-1);
			}
			GenFieldGrid.EY = iv; nv++;
			continue;

		  }
		  if ( nv == 2 ) {
			if ( sscanf(filerow,"%lf",&dv) < 1 ) {
              fprintf(stderr,"Error reading GenFieldGrid.Xs from string\n");
      	  	  exit(-1);
			}
			GenFieldGrid.Xs = dv; nv++;
						continue;

		  }
		  if ( nv == 3 ) {
			if ( sscanf(filerow,"%lf",&dv) < 1 ) {
              fprintf(stderr,"Error reading GenFieldGrid.Xe from string\n");
      	  	  exit(-1);
			}
			GenFieldGrid.Xe = dv; nv++;
						continue;

		  }
		  if ( nv == 4 ) {
			if ( sscanf(filerow,"%lf",&dv) < 1 ) {
              fprintf(stderr,"Error reading GenFieldGrid.Ys from string\n");
      	  	  exit(-1);
			}
			GenFieldGrid.Ys = dv; nv++;
						continue;

		  }
		  if ( nv == 5 ) {
			if ( sscanf(filerow,"%lf",&dv) < 1 ) {
              fprintf(stderr,"Error reading GenFieldGrid.Ye from string\n");
      	  	  exit(-1);
			}
			GenFieldGrid.Ye = dv; nv++;
						continue;

		  }
      	  if ( nv <= 6 ) {
			if ( sscanf(filerow,"%d",&iv) < 1 ) {
              fprintf(stderr,"Error reading ParticleGrid.EX from string\n");
      	  	  exit(-1);
			}
			ParticleGrid.EX = iv; nv++;
			continue;
		  }
		  if ( nv == 7 ) {
			if ( sscanf(filerow,"%d",&iv) < 1 ) {
              fprintf(stderr,"Error reading ParticleGrid.EY from string\n");
      	  	  exit(-1);
			}
			ParticleGrid.EY = iv; nv++;
			continue;

		  }
		  if ( nv == 8 ) {
			if ( sscanf(filerow,"%lf",&dv) < 1 ) {
              fprintf(stderr,"Error reading ParticleGrid.Xs from string\n");
      	  	  exit(-1);
			}
			ParticleGrid.Xs = dv; nv++;
						continue;

		  }
		  if ( nv == 9 ) {
			if ( sscanf(filerow,"%lf",&dv) < 1 ) {
              fprintf(stderr,"Error reading ParticleGrid.Xe from string\n");
      	  	  exit(-1);
			}
			ParticleGrid.Xe = dv; nv++;
						continue;

		  }
		  if ( nv == 10 ) {
			if ( sscanf(filerow,"%lf",&dv) < 1 ) {
              fprintf(stderr,"Error reading ParticleGrid.Ys from string\n");
      	  	  exit(-1);
			}
			ParticleGrid.Ys = dv; nv++;
						continue;

		  }
		  if ( nv == 11 ) {
			if ( sscanf(filerow,"%lf",&dv) < 1 ) {
              fprintf(stderr,"Error reading ParticleGrid.Ye from string\n");
      	  	  exit(-1);
			}
			ParticleGrid.Ye = dv;
			break;
		  }
      }
	  
      /*
        Now read MaxIters
      */
      MaxIters = 0;
      while ( 1 )
      { 
          if ( readrow(filerow, 80, inpunit) < 1 ) {
      	  	  fprintf(stderr,"Error reading MaxIters from input file\n");
      	  	  exit(-1);
      	  }
      	  if ( filerow[0] == '#' || rowlen(filerow) < 1 ) continue;
          if ( sscanf(filerow,"%d",&MaxIters) < 1 ) {
              fprintf(stderr,"Error reading MaxIters from string\n");
      	  	  exit(-1);
      	  }
      	  printf("MaxIters = %d\n",MaxIters);
      	  break;      	  
      }
      
      /*
        Now read MaxSteps
      */
      MaxSteps = 0;
      while ( 1 )
      { 
          if ( readrow(filerow, 80, inpunit) < 1 ) {
      	  	  fprintf(stderr,"Error reading MaxSteps from input file\n");
      	  	  exit(-1);
      	  }
      	  if ( filerow[0] == '#' || rowlen(filerow) < 1 ) continue;
          if ( sscanf(filerow,"%d",&MaxSteps) < 1 ) {
              fprintf(stderr,"Error reading MaxSteps from string\n");
      	  	  exit(-1);
      	  }
      	  printf("MaxSteps = %d\n",MaxSteps);
      	  break;      	  
      }


      /*
      ! Now read TimeBit
      */
      TimeBit = 0;
      while ( 1 )
      { 
          if ( readrow(filerow, 80, inpunit) < 1 ) {
      	  	  fprintf(stderr,"Error reading TimeBit from input file\n");
      	  	  exit(-1);
      	  }
          if ( filerow[0] == '#' || rowlen(filerow) < 1 ) continue;
          if ( sscanf(filerow,"%lf",&TimeBit) < 1 ) {
              fprintf(stderr,"Error reading TimeBit from string\n");
      	  	  exit(-1);
      	  }
		printf("TimeBit = %lf\n",TimeBit);

      	  break;      	  
      }
      
      fclose(inpunit);

      // Grid allocations
      iv = GenFieldGrid.EX * GenFieldGrid.EY;
      GenFieldGrid.Values = malloc(iv*sizeof(1));
	  if ( GenFieldGrid.Values == NULL ) {
              fprintf(stderr,"Error allocating GenFieldGrid.Values \n");
      	  	  exit(-1);
  	  }
	  iv = ParticleGrid.EX * ParticleGrid.EY;
  	  ParticleGrid.Values = malloc(iv*sizeof(1));
	  if ( ParticleGrid.Values == NULL ) {
              fprintf(stderr,"Error allocating ParticleGrid.Values \n");
      	  	  exit(-1);
  	  }
        fprintf(stdout,"GenFieldGrid ");
		print_i2dGrid(GenFieldGrid);
        fprintf(stdout,"ParticleGrid ");
		print_i2dGrid(ParticleGrid);

      return;
} // end InitGrid