gf_t* get_gf(int w, int argc, char **argv, int starting)
{
  gf_t *gf = (gf_t*)malloc(sizeof(gf_t));
  if (create_gf_from_argv(gf, w, argc, argv, starting) == 0) {
    free(gf);
    gf = NULL;
  }
  return gf;
}
示例#2
0
文件: gf_unit.c 项目: imp/gf-complete
int main(int argc, char **argv)
{
  signal(SIGSEGV, SigHandler);

  int w, i, verbose, single, region, tested, top;
  int s_start, d_start, bytes, xor, alignment_test;
  gf_t   gf, gf_def;
  time_t t0;
  gf_internal_t *h;
  gf_general_t *a, *b, *c, *d, *ai, *bi;
  uint8_t a8, b8, c8, *mult4, *div4, *mult8, *div8;
  uint16_t a16, b16, c16, d16, *log16, *alog16;
  char as[50], bs[50], cs[50], ds[50], ais[50], bis[50];
  uint32_t mask;
  char *ra, *rb, *rc, *rd, *target;
  int align;


  if (argc < 4) usage(NULL);

  if (sscanf(argv[1], "%d", &w) == 0){
    usage("Bad w\n");
  }

  if (sscanf(argv[3], "%ld", &t0) == 0) usage("Bad seed\n");
  if (t0 == -1) t0 = time(0);
  MOA_Seed(t0);

  if (w > 32 && w != 64 && w != 128) usage("Bad w");
  
  if (create_gf_from_argv(&gf, w, argc, argv, 4) == 0) usage(BM);
  printf("Size (bytes): %d\n", gf_size(&gf));

  for (i = 0; i < strlen(argv[2]); i++) {
    if (strchr("ASRV", argv[2][i]) == NULL) usage("Bad test\n");
  }

  h = (gf_internal_t *) gf.scratch;
  a = (gf_general_t *) malloc(sizeof(gf_general_t));
  b = (gf_general_t *) malloc(sizeof(gf_general_t));
  c = (gf_general_t *) malloc(sizeof(gf_general_t));
  d = (gf_general_t *) malloc(sizeof(gf_general_t));
  ai = (gf_general_t *) malloc(sizeof(gf_general_t));
  bi = (gf_general_t *) malloc(sizeof(gf_general_t));

  //15 bytes extra to make sure it's 16byte aligned
  ra = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  rb = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  rc = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  rd = (char *) malloc(sizeof(char)*REGION_SIZE+15);

  //this still assumes 8 byte aligned pointer from malloc
  //(which is usual on 32-bit machines)
  ra += (uint64_t)ra & 0xf;
  rb += (uint64_t)rb & 0xf;
  rc += (uint64_t)rc & 0xf;
  rd += (uint64_t)rd & 0xf;

  if (w <= 32) {
    mask = 0;
    for (i = 0; i < w; i++) mask |= (1 << i);
  }

  verbose = (strchr(argv[2], 'V') != NULL);
  single = (strchr(argv[2], 'S') != NULL || strchr(argv[2], 'A') != NULL);
  region = (strchr(argv[2], 'R') != NULL || strchr(argv[2], 'A') != NULL);

  if (!gf_init_hard(&gf_def, w, GF_MULT_DEFAULT, GF_REGION_DEFAULT, GF_DIVIDE_DEFAULT,
      (h->mult_type != GF_MULT_COMPOSITE) ? h->prim_poly : 0, 0, 0, NULL, NULL))
    problem("No default for this value of w");
  if (w == 4) {
    mult4 = gf_w4_get_mult_table(&gf);
    div4 = gf_w4_get_div_table(&gf);
  }

  if (w == 8) {
    mult8 = gf_w8_get_mult_table(&gf);
    div8 = gf_w8_get_div_table(&gf);
  }

  if (w == 16) {
    log16 = gf_w16_get_log_table(&gf);
    alog16 = gf_w16_get_mult_alog_table(&gf);
  }

  if (verbose) printf("Seed: %ld\n", t0);

  if (single) {
    
    if (gf.multiply.w32 == NULL) problem("No multiplication operation defined.");
    if (verbose) { printf("Testing single multiplications/divisions.\n"); fflush(stdout); }
    if (w <= 10) {
      top = (1 << w)*(1 << w);
    } else {
      top = 1024*1024;
    }
    for (i = 0; i < top; i++) {
      if (w <= 10) {
        a->w32 = i % (1 << w);
        b->w32 = (i >> w);

      //Allen: the following conditions were being run 10 times each. That didn't seem like nearly enough to
      //me for these special cases, so I converted to doing this mod stuff to easily make the number of times
      //run both larger and proportional to the total size of the run.
      } else {
        switch (i % 32)
        {
          case 0: 
            gf_general_set_zero(a, w);
            gf_general_set_random(b, w, 1);
            break;
          case 1:
            gf_general_set_random(a, w, 1);
            gf_general_set_zero(b, w);
            break;
          case 2:
            gf_general_set_one(a, w);
            gf_general_set_random(b, w, 1);
            break;
          case 3:
            gf_general_set_random(a, w, 1);
            gf_general_set_one(b, w);
            break;
          default:
            gf_general_set_random(a, w, 1);
            gf_general_set_random(b, w, 1);
        }
      }

      //Allen: the following special cases for w=64 are based on the code below for w=128.
      //These w=64 cases are based on Dr. Plank's suggestion because some of the methods for w=64
      //involve splitting it in two. I think they're less likely to give errors than the 128-bit case
      //though, because the 128 bit case is always split in two.
      //As with w=128, I'm arbitrarily deciding to do this sort of thing with a quarter of the cases
      if (w == 64) {
        switch (i % 32)
        {
          case 0: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; break;
          case 1: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; break;
          case 2: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
          case 3: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
          case 4: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
          case 5: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
          case 6: if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
          case 7: if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
        }
      }

      //Allen: for w=128, we have important special cases where one half or the other of the number is all
      //zeros. The probability of hitting such a number randomly is 1^-64, so if we don't force these cases
      //we'll probably never hit them. This could be implemented more efficiently by changing the set-random
      //function for w=128, but I think this is easier to follow.
      //I'm arbitrarily deciding to do this sort of thing with a quarter of the cases
      if (w == 128) {
        switch (i % 32)
        {
          case 0: if (!gf_general_is_one(a, w)) a->w128[0] = 0; break;
          case 1: if (!gf_general_is_one(a, w)) a->w128[1] = 0; break;
          case 2: if (!gf_general_is_one(a, w)) a->w128[0] = 0; if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
          case 3: if (!gf_general_is_one(a, w)) a->w128[0] = 0; if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
          case 4: if (!gf_general_is_one(a, w)) a->w128[1] = 0; if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
          case 5: if (!gf_general_is_one(a, w)) a->w128[1] = 0; if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
          case 6: if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
          case 7: if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
        }
      }

      tested = 0;
      gf_general_multiply(&gf, a, b, c);
      
      /* If w is 4, 8 or 16, then there are inline multiplication/division methods.  
         Test them here. */

      if (w == 4 && mult4 != NULL) {
        a8 = a->w32;
        b8 = b->w32;
        c8 = GF_W4_INLINE_MULTDIV(mult4, a8, b8);
        if (c8 != c->w32) {
          printf("Error in inline multiplication. %d * %d.  Inline = %d.  Default = %d.\n",
             a8, b8, c8, c->w32);
          exit(1);
        }
      }

      if (w == 8 && mult8 != NULL) {
        a8 = a->w32;
        b8 = b->w32;
        c8 = GF_W8_INLINE_MULTDIV(mult8, a8, b8);
        if (c8 != c->w32) {
          printf("Error in inline multiplication. %d * %d.  Inline = %d.  Default = %d.\n",
             a8, b8, c8, c->w32);
          exit(1);
        }
      }

      if (w == 16 && log16 != NULL) {
        a16 = a->w32;
        b16 = b->w32;
        c16 = GF_W16_INLINE_MULT(log16, alog16, a16, b16);
        if (c16 != c->w32) {
          printf("Error in inline multiplication. %d * %d.  Inline = %d.  Default = %d.\n",
             a16, b16, c16, c->w32);
          printf("%d %d\n", log16[a16], log16[b16]);
          top = log16[a16] + log16[b16];
          printf("%d %d\n", top, alog16[top]);
          exit(1);
        }
      }

      /* If this is not composite, then first test against the default: */

      if (h->mult_type != GF_MULT_COMPOSITE) {
        tested = 1;
        gf_general_multiply(&gf_def, a, b, d);

        if (!gf_general_are_equal(c, d, w)) {
          gf_general_val_to_s(a, w, as, 1);
          gf_general_val_to_s(b, w, bs, 1);
          gf_general_val_to_s(c, w, cs, 1);
          gf_general_val_to_s(d, w, ds, 1);
          printf("Error in single multiplication (all numbers in hex):\n\n");
          printf("  gf.multiply(gf, %s, %s) = %s\n", as, bs, cs);
          printf("  The default gf multiplier returned %s\n", ds);
          exit(1);
        }
      }

      /* Now, we also need to double-check by other means, in case the default is wanky, 
         and when we're performing composite operations. Start with 0 and 1, where we know
         what the result should be. */

      if (gf_general_is_zero(a, w) || gf_general_is_zero(b, w) || 
          gf_general_is_one(a, w)  || gf_general_is_one(b, w)) {
        tested = 1;
        if (((gf_general_is_zero(a, w) || gf_general_is_zero(b, w)) && !gf_general_is_zero(c, w)) ||
            (gf_general_is_one(a, w) && !gf_general_are_equal(b, c, w)) ||
            (gf_general_is_one(b, w) && !gf_general_are_equal(a, c, w))) {
          gf_general_val_to_s(a, w, as, 1);
          gf_general_val_to_s(b, w, bs, 1);
          gf_general_val_to_s(c, w, cs, 1);
          printf("Error in single multiplication (all numbers in hex):\n\n");
          printf("  gf.multiply(gf, %s, %s) = %s, which is clearly wrong.\n", as, bs, cs);
;
          exit(1);
        }
      }

      /* Dumb check to make sure that it's not returning numbers that are too big: */

      if (w < 32 && (c->w32 & mask) != c->w32) {
        gf_general_val_to_s(a, w, as, 1);
        gf_general_val_to_s(b, w, bs, 1);
        gf_general_val_to_s(c, w, cs, 1);
        printf("Error in single multiplication (all numbers in hex):\n\n");
        printf("  gf.multiply.w32(gf, %s, %s) = %s, which is too big.\n", as, bs, cs);
        exit(1);
      }

      /* Finally, let's check to see that multiplication and division work together */

      if (!gf_general_is_zero(a, w)) {
        gf_general_divide(&gf, c, a, d);
        if (!gf_general_are_equal(b, d, w)) {
          gf_general_val_to_s(a, w, as, 1);
          gf_general_val_to_s(b, w, bs, 1);
          gf_general_val_to_s(c, w, cs, 1);
          gf_general_val_to_s(d, w, ds, 1);
          printf("Error in single multiplication/division (all numbers in hex):\n\n");
          printf("  gf.multiply(gf, %s, %s) = %s, but gf.divide(gf, %s, %s) = %s\n", as, bs, cs, cs, as, ds);
          exit(1);
        }
      }

    }
示例#3
0
文件: gf_methods.c 项目: AleksMx/qfs
int main(int argc, char *argv[])
{
  int m, r, d, w, i, sa, j, k, reset, ok;
  int nregions;
  int nmults;
  char **regions;
  char **mults;
  int exhaustive = 0;
  int divide = 0;
  int cauchy = 0;
  int listing;
  char *gf_argv[50], *x;
  gf_t gf;
  char ls[10];
  char * w_str;

  if (argc != 4) usage(NULL);
  w = atoi(argv[1]);
  ok = (w >= 1 && w <= 32);
  if (w == 64) ok = 1;
  if (w == 128) ok = 1;
  if (!ok) usage("Bad w");
  
  if (argv[2][0] != '-' || argv[3][0] != '-' || strlen(argv[2]) == 1 || strlen(argv[3]) != 2) {
    usage(NULL);
  }
  for (i = 1; argv[2][i] != '\0'; i++) {
    switch(argv[2][i]) {
      case 'B': exhaustive = 0; break;
      case 'A': exhaustive = 1; break;
      case 'D': divide = 1; break;
      case 'C': cauchy = 1; break;
      default: usage("Bad -BADC");
    }
  }

  if (strchr("LUMDRB", argv[3][1]) == NULL) { usage("Bad -LUMDRB"); }
  listing = argv[3][1];

  if (listing == 'U') {
    w_str = "../test/gf_unit %d A -1";
  } else if (listing == 'L') {
    w_str = "w=%d:";
  } else {
    w_str = strdup("sh time_tool.sh X %d");
    x = strchr(w_str, 'X');
    *x = listing;
  }

  gf_argv[0] = "-";
  if (create_gf_from_argv(&gf, w, 1, gf_argv, 0) > 0) {
    printf(w_str, w);
    printf(" - \n");
    gf_free(&gf, 1);
  } else if (_gf_errno == GF_E_DEFAULT) {
    fprintf(stderr, "Unlabeled failed method: w=%d: -\n", 2);
    exit(1);
  }

  nregions = (exhaustive) ? NREGIONS : BNREGIONS;
  if (!cauchy) nregions--;
  regions = (exhaustive) ? REGIONS : BREGIONS;
  mults = (exhaustive) ? MULTS : BMULTS;
  nmults = (exhaustive) ? NMULTS : BNMULTS;


  for (m = 0; m < nmults; m++) {
    sa = 0;
    gf_argv[sa++] = "-m";
    if (strcmp(mults[m], "GROUP44") == 0) {
      gf_argv[sa++] = "GROUP";
      gf_argv[sa++] = "4";
      gf_argv[sa++] = "4";
    } else if (strcmp(mults[m], "GROUP48") == 0) {
      gf_argv[sa++] = "GROUP";
      gf_argv[sa++] = "4";
      gf_argv[sa++] = "8";
    } else if (strcmp(mults[m], "SPLIT2") == 0) {
      gf_argv[sa++] = "SPLIT";
      sprintf(ls, "%d", w);
      gf_argv[sa++] = ls;
      gf_argv[sa++] = "2";
    } else if (strcmp(mults[m], "SPLIT4") == 0) {
      gf_argv[sa++] = "SPLIT";
      sprintf(ls, "%d", w);
      gf_argv[sa++] = ls;
      gf_argv[sa++] = "4";
    } else if (strcmp(mults[m], "SPLIT8") == 0) {
      gf_argv[sa++] = "SPLIT";
      sprintf(ls, "%d", w);
      gf_argv[sa++] = ls;
      gf_argv[sa++] = "8";
    } else if (strcmp(mults[m], "SPLIT16") == 0) {
      gf_argv[sa++] = "SPLIT";
      sprintf(ls, "%d", w);
      gf_argv[sa++] = ls;
      gf_argv[sa++] = "16";
    } else if (strcmp(mults[m], "SPLIT88") == 0) {
      gf_argv[sa++] = "SPLIT";
      gf_argv[sa++] = "8";
      gf_argv[sa++] = "8";
    } else if (strcmp(mults[m], "COMPOSITE") == 0) {
      gf_argv[sa++] = "COMPOSITE";
      gf_argv[sa++] = "2";
      gf_argv[sa++] = "-";
    } else {
      gf_argv[sa++] = mults[m];
    }
    reset = sa;


    for (r = 0; r < (1 << nregions); r++) {
      sa = reset;
      for (k = 0; k < nregions; k++) {
        if (r & (1 << k)) {
          gf_argv[sa++] = "-r";
          gf_argv[sa++] = regions[k];
        }
      }
      gf_argv[sa++] = "-";

      /* printf("Hmmmm. %s", gf_argv[0]);
      for (j = 0; j < sa; j++) printf(" %s", gf_argv[j]);
      printf("\n");  */
  
      if (create_gf_from_argv(&gf, w, sa, gf_argv, 0) > 0) {
        printf(w_str, w);
        for (j = 0; j < sa; j++) printf(" %s", gf_argv[j]);
        printf("\n");
        gf_free(&gf, 1);
      } else if (_gf_errno == GF_E_DEFAULT) {
        fprintf(stderr, "Unlabeled failed method: w=%d:", w);
        for (j = 0; j < sa; j++) fprintf(stderr, " %s", gf_argv[j]);
        fprintf(stderr, "\n");
        exit(1);
      }
      sa--;
      if (divide) {
        for (d = 0; d < NDIVS; d++) {
          gf_argv[sa++] = "-d";
          gf_argv[sa++] = divides[d];
          /*          printf("w=%d:", w);
                      for (j = 0; j < sa; j++) printf(" %s", gf_argv[j]);
                      printf("\n"); */
          gf_argv[sa++] = "-";
          if (create_gf_from_argv(&gf, w, sa, gf_argv, 0) > 0) {
            printf(w_str, w);
            for (j = 0; j < sa; j++) printf(" %s", gf_argv[j]);
            printf("\n");
            gf_free(&gf, 1);
          } else if (_gf_errno == GF_E_DEFAULT) {
            fprintf(stderr, "Unlabeled failed method: w=%d:", w);
            for (j = 0; j < sa; j++) fprintf(stderr, " %s", gf_argv[j]);
            fprintf(stderr, "\n");
            exit(1);
          } 
          sa-=3;
        }
      }
    }
  }
  return 0;
}
示例#4
0
int main(int argc, char **argv)
{
  int w, it, i, size, iterations, xor;
  char tests[100];
  char test;
  char *single_tests = "MDI";
  char *region_tests = "G012";
  char *tstrings[256];
  void *tmethods[256];
  gf_t      gf;
  double timer, elapsed, ds, di, dnum;
  int num;
  time_t t0;
  uint8_t *ra, *rb;
  gf_general_t a;

  
  if (argc < 6) usage(NULL);
  
  if (sscanf(argv[1], "%d", &w) == 0){
    usage("Bad w[-pp]\n");
  }

  
  if (sscanf(argv[3], "%ld", &t0) == 0) usage("Bad seed\n");
  if (sscanf(argv[4], "%d", &size) == 0) usage("Bad size\n");
  if (sscanf(argv[5], "%d", &iterations) == 0) usage("Bad iterations\n");
  if (t0 == -1) t0 = time(0);
  MOA_Seed(t0);

  ds = size;
  di = iterations;

  if ((w > 32 && w != 64 && w != 128) || w < 0) usage("Bad w");
  if ((size * 8) % w != 0) usage ("Bad size -- must be a multiple of w*8\n");
  
  if (!create_gf_from_argv(&gf, w, argc, argv, 6)) usage(BM);

  strcpy(tests, "");
  for (i = 0; argv[2][i] != '\0'; i++) {
    switch(argv[2][i]) {
      case 'A': strcat(tests, single_tests); 
                strcat(tests, region_tests); 
                break;
      case 'S': strcat(tests, single_tests); break;
      case 'R': strcat(tests, region_tests); break;
      case 'G': strcat(tests, "G"); break;
      case '0': strcat(tests, "0"); break;
      case '1': strcat(tests, "1"); break;
      case '2': strcat(tests, "2"); break;
      case 'M': strcat(tests, "M"); break;
      case 'D': strcat(tests, "D"); break;
      case 'I': strcat(tests, "I"); break;
      default: usage("Bad tests");
    }
  }

  tstrings['M'] = "Multiply";
  tstrings['D'] = "Divide";
  tstrings['I'] = "Inverse";
  tstrings['G'] = "Region-Random";
  tstrings['0'] = "Region-By-Zero";
  tstrings['1'] = "Region-By-One";
  tstrings['2'] = "Region-By-Two";

  tmethods['M'] = (void *) gf.multiply.w32;
  tmethods['D'] = (void *) gf.divide.w32;
  tmethods['I'] = (void *) gf.inverse.w32;
  tmethods['G'] = (void *) gf.multiply_region.w32;
  tmethods['0'] = (void *) gf.multiply_region.w32;
  tmethods['1'] = (void *) gf.multiply_region.w32;
  tmethods['2'] = (void *) gf.multiply_region.w32;

  printf("Seed: %ld\n", t0);

  ra = (uint8_t *) malloc(size);
  rb = (uint8_t *) malloc(size);

  if (ra == NULL || rb == NULL) { perror("malloc"); exit(1); }

  for (i = 0; i < 3; i++) {
    test = single_tests[i];
    if (strchr(tests, test) != NULL) {
      if (tmethods[(int)test] == NULL) {
        printf("No %s method.\n", tstrings[(int)test]);
      } else {
        elapsed = 0;
        dnum = 0;
        for (it = 0; it < iterations; it++) {
          gf_general_set_up_single_timing_test(w, ra, rb, size);
          timer_start(&timer);
          num = gf_general_do_single_timing_test(&gf, ra, rb, size, test);
          dnum += num;
          elapsed += timer_split(&timer);
        }
        printf("%14s:           %10.6lf s   Mops: %10.3lf    %10.3lf Mega-ops/s\n", 
               tstrings[(int)test], elapsed, 
               dnum/1024.0/1024.0, dnum/1024.0/1024.0/elapsed);
      }
    }
  }

  for (i = 0; i < 4; i++) {
    test = region_tests[i];
    if (strchr(tests, test) != NULL) {
      if (tmethods[(int)test] == NULL) {
        printf("No %s method.\n", tstrings[(int)test]);
      } else {
        if (test == '0') gf_general_set_zero(&a, w);
        if (test == '1') gf_general_set_one(&a, w);
        if (test == '2') gf_general_set_two(&a, w);

        for (xor = 0; xor < 2; xor++) {
          elapsed = 0;
          for (it = 0; it < iterations; it++) {
            if (test == 'G') gf_general_set_random(&a, w, 1);
            gf_general_set_up_single_timing_test(8, ra, rb, size);
            timer_start(&timer);
            gf_general_do_region_multiply(&gf, &a, ra, rb, size, xor);
            elapsed += timer_split(&timer);
          }
          printf("%14s: XOR: %d    %10.6lf s     MB: %10.3lf    %10.3lf MB/s\n", 
               tstrings[(int)test], xor, elapsed, 
               ds*di/1024.0/1024.0, ds*di/1024.0/1024.0/elapsed);
        }
      }
    }
  }
  return 0;
}