6 #include "../lib/lfsr.h"
7 #include "../gettime.h"
8 #include "../fio_time.h"
10 static void usage(void)
12 printf("Usage: lfsr-test 0x<numbers> [seed] [spin] [verify]\n");
13 printf("-------------------------------------------------------------\n");
14 printf("*numbers: how many random numbers to produce (in hex)\n"
15 "seed: initial value\n"
16 "spin: how many iterations before we produce a number\n"
17 "verify: check if LFSR has iterated correctly\n\n"
18 "Only <numbers> is required. The rest are evaluated to 0 or false\n"
19 "Elapsed/mean time and verification results are printed at the"
23 int main(int argc, char *argv[])
26 struct timespec start, end;
29 unsigned int spin = 0;
34 void *v = NULL, *v_start;
41 case 5: if (strncmp(argv[4], "verify", 7) == 0)
44 case 4: spin = atoi(argv[3]);
46 case 3: seed = atol(argv[2]);
48 case 2: numbers = strtol(argv[1], NULL, 16);
55 fl = malloc(sizeof(struct fio_lfsr));
61 r = lfsr_init(fl, numbers, seed, spin);
63 printf("Initialization failed.\n");
68 printf("LFSR specs\n");
69 printf("==========================\n");
70 printf("Size is %u\n", 64 - __builtin_clzl(fl->cached_bit));
71 printf("Max val is %lu\n", (unsigned long) fl->max_val);
72 printf("XOR-mask is 0x%lX\n", (unsigned long) fl->xormask);
73 printf("Seed is %lu\n", (unsigned long) fl->last_val);
74 printf("Spin is %u\n", fl->spin);
75 printf("Cycle length is %lu\n", (unsigned long) fl->cycle_length);
77 /* Create verification table */
79 v_size = numbers * sizeof(uint8_t);
82 printf("\nVerification table is %lf KiB\n", (double)(v_size) / 1024);
87 * Iterate over a tight loop until we have produced all the requested
88 * numbers. Verifying the results should introduce some small yet not
89 * negligible overhead.
91 fprintf(stderr, "\nTest initiated... ");
92 fio_gettime(&start, NULL);
93 while (!lfsr_next(fl, &i)) {
95 *(uint8_t *)(v + i) += 1;
97 fio_gettime(&end, NULL);
98 fprintf(stderr, "finished.\n");
101 /* Check if all expected numbers within range have been calculated */
104 fprintf(stderr, "Verifying results... ");
105 for (i = 0; i < numbers; i++) {
106 if (*(uint8_t *)(v + i) != 1) {
107 fprintf(stderr, "failed (%lu = %d).\n",
109 *(uint8_t *)(v + i));
115 fprintf(stderr, "OK!\n");
118 /* Calculate elapsed time and mean time per number */
119 total = utime_since(&start, &end);
120 mean = total / fl->num_vals;
122 printf("\nTime results ");
124 printf("(slower due to verification)");
125 printf("\n==============================\n");
126 printf("Elapsed: %lf s\n", total / pow(10,6));
127 printf("Mean: %lf us\n", mean);