10 #include "../lib/lfsr.h"
11 #include "../gettime.h"
12 #include "../fio_time.h"
16 printf("Usage: lfsr-test 0x<numbers> [seed] [spin] [verify]\n");
17 printf("-------------------------------------------------------------\n");
18 printf("*numbers: how many random numbers to produce (in hex)\n"
19 "seed: initial value\n"
20 "spin: how many iterations before we produce a number\n"
21 "verify: check if LFSR has iterated correctly\n\n"
22 "Only <numbers> is required. The rest are evaluated to 0 or false\n"
23 "Elapsed/mean time and verification results are printed at the"
27 int main(int argc, char *argv[])
30 struct timespec start, end;
33 unsigned int spin = 0;
38 void *v = NULL, *v_start;
45 case 5: if (strncmp(argv[4], "verify", 7) == 0)
47 case 4: spin = atoi(argv[3]);
48 case 3: seed = atol(argv[2]);
49 case 2: numbers = strtol(argv[1], NULL, 16);
56 fl = malloc(sizeof(struct fio_lfsr));
62 r = lfsr_init(fl, numbers, seed, spin);
64 printf("Initialization failed.\n");
69 printf("LFSR specs\n");
70 printf("==========================\n");
71 printf("Size is %u\n", 64 - __builtin_clzl(fl->cached_bit));
72 printf("Max val is %lu\n", (unsigned long) fl->max_val);
73 printf("XOR-mask is 0x%lX\n", (unsigned long) fl->xormask);
74 printf("Seed is %lu\n", (unsigned long) fl->last_val);
75 printf("Spin is %u\n", fl->spin);
76 printf("Cycle length is %lu\n", (unsigned long) fl->cycle_length);
78 /* Create verification table */
80 v_size = numbers * sizeof(uint8_t);
83 printf("\nVerification table is %lf KiB\n", (double)(v_size) / 1024);
88 * Iterate over a tight loop until we have produced all the requested
89 * numbers. Verifying the results should introduce some small yet not
90 * negligible overhead.
92 fprintf(stderr, "\nTest initiated... ");
93 fio_gettime(&start, NULL);
94 while (!lfsr_next(fl, &i)) {
96 *(uint8_t *)(v + i) += 1;
98 fio_gettime(&end, NULL);
99 fprintf(stderr, "finished.\n");
102 /* Check if all expected numbers within range have been calculated */
105 fprintf(stderr, "Verifying results... ");
106 for (i = 0; i < numbers; i++) {
107 if (*(uint8_t *)(v + i) != 1) {
108 fprintf(stderr, "failed (%lu = %d).\n",
110 *(uint8_t *)(v + i));
116 fprintf(stderr, "OK!\n");
119 /* Calculate elapsed time and mean time per number */
120 total = utime_since(&start, &end);
121 mean = total / fl->num_vals;
123 printf("\nTime results ");
125 printf("(slower due to verification)");
126 printf("\n==============================\n");
127 printf("Elapsed: %lf s\n", total / pow(10,6));
128 printf("Mean: %lf us\n", mean);