Branch and cache miss speedups
[fio.git] / lib / lfsr.c
index a83540494fc1964aebb7d89fad86a730030ab211..9771318ab00163b8b6a9f68dc076cd896c00fda6 100644 (file)
@@ -2,6 +2,7 @@
 #include <math.h>
 
 #include "lfsr.h"
+#include "../compiler/compiler.h"
 
 /*
  * LFSR taps retrieved from:
@@ -87,7 +88,6 @@ static inline void __lfsr_next(struct fio_lfsr *fl, unsigned int spin)
         * this switch.
         */
        switch (spin) {
-               case 16: __LFSR_NEXT(fl, fl->last_val);
                case 15: __LFSR_NEXT(fl, fl->last_val);
                case 14: __LFSR_NEXT(fl, fl->last_val);
                case 13: __LFSR_NEXT(fl, fl->last_val);
@@ -108,25 +108,34 @@ static inline void __lfsr_next(struct fio_lfsr *fl, unsigned int spin)
        }
 }
 
+/*
+ * lfsr_next does the following:
+ *
+ * a. Return if the number of max values has been exceeded.
+ * b. Check if we have a spin value that produces a repeating subsequence.
+ *    This is previously calculated in `prepare_spin` and cycle_length should
+ *    be > 0. If we do have such a spin:
+ *
+ *    i. Decrement the calculated cycle.
+ *    ii. If it reaches zero, add "+1" to the spin and reset the cycle_length
+ *        (we have it cached in the struct fio_lfsr)
+ *
+ *    In either case, continue with the calculation of the next value.
+ * c. Check if the calculated value exceeds the desirable range. In this case,
+ *    go back to b, else return.
+ */
 int lfsr_next(struct fio_lfsr *fl, uint64_t *off, uint64_t last)
 {
-       int repeat;
-       unsigned int spin;
-
-       repeat = fl->num_vals % fl->cycle_length;
-       if (repeat == 0)
-               spin = fl->spin + 1;
-       else
-               spin = fl->spin;
-
-       if (fl->num_vals > fl->max_val)
+       if (fl->num_vals++ > fl->max_val)
                return 1;
 
-       fl->num_vals++;
-
        do {
-               __lfsr_next(fl, spin);
-       } while (fl->last_val > fl->max_val);
+               if (fl->cycle_length && !--fl->cycle_length) {
+                       __lfsr_next(fl, fl->spin + 1);
+                       fl->cycle_length = fl->cached_cycle_length;
+               } else
+                       __lfsr_next(fl, fl->spin);
+       } while (fio_unlikely(fl->last_val > fl->max_val));
 
        *off = fl->last_val;
        return 0;
@@ -147,8 +156,13 @@ static uint8_t *find_lfsr(uint64_t size)
 {
        int i;
 
+       /*
+        * For an LFSR, there is always a prohibited state (all ones).
+        * Thus, if we need to find the proper LFSR for our size, we must take that
+        * into account.
+        */
        for (i = 3; i < 64; i++)
-               if ((1UL << i) > size) /* TODO: Explain why. */
+               if ((1UL << i) > size)
                        return taps[i];
 
        return NULL;
@@ -183,7 +197,7 @@ int prepare_spin(struct fio_lfsr *fl, unsigned int spin)
 
        x = max / (spin + 1);
        y = max % (spin + 1);
-       fl->cycle_length = max; /* This is the expected cycle */
+       fl->cycle_length = 0;   /* No cycle occurs, other than the expected */
        fl->spin = spin;
 
        for (i = 1; i <= spin; i++) {
@@ -192,6 +206,13 @@ int prepare_spin(struct fio_lfsr *fl, unsigned int spin)
                        break;
                }
        }
+       fl->cached_cycle_length = fl->cycle_length;
+
+       /*
+        * Increment cycle length for the first time only since the stored value
+        * will not be printed otherwise.
+        */
+       fl->cycle_length++;
 
        return 0;
 }