#include <stdio.h>
-#include <math.h>
#include "lfsr.h"
+#include "../compiler/compiler.h"
/*
* LFSR taps retrieved from:
* The memory overhead of the following tap table should be relatively small,
* no more than 400 bytes.
*/
-static uint8_t taps[64][FIO_MAX_TAPS] =
+static uint8_t lfsr_taps[64][FIO_MAX_TAPS] =
{
{0}, {0}, {0}, //LFSRs with less that 3-bits cannot exist
{3, 2}, //Tap position for 3-bit LFSR
#define __LFSR_NEXT(__fl, __v) \
__v = ((__v >> 1) | __fl->cached_bit) ^ \
- (((__v & 1UL) - 1UL) & __fl->xormask);
+ (((__v & 1ULL) - 1ULL) & __fl->xormask);
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);
+ /* fall through */
case 14: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 13: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 12: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 11: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 10: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 9: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 8: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 7: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 6: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 5: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 4: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 3: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 2: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 1: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
case 0: __LFSR_NEXT(fl, fl->last_val);
+ /* fall through */
default: break;
}
}
* 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 lfsr_next(struct fio_lfsr *fl, uint64_t *off)
{
- unsigned int spin = fl->spin;
-
if (fl->num_vals++ > fl->max_val)
return 1;
do {
- if (fl->cycle_length) {
- fl->cycle_length--;
- if (!fl->cycle_length) {
- __lfsr_next(fl, fl->spin + 1);
- fl->cycle_length = fl->cached_cycle_length;
- goto check;
- }
- }
- __lfsr_next(fl, spin);
-check: ;
- } 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;
uint64_t xormask = 0;
for(i = 0; i < FIO_MAX_TAPS && taps[i] != 0; i++)
- xormask |= 1UL << (taps[i] - 1);
+ xormask |= 1ULL << (taps[i] - 1);
return xormask;
}
{
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. */
- return taps[i];
+ if ((1ULL << i) > size)
+ return lfsr_taps[i];
return NULL;
}
* Thus, [1] is equivalent to (y * i) % (spin + 1) == 0;
* Also, the cycle's length will be (x * i) + (y * i) / (spin + 1)
*/
-int prepare_spin(struct fio_lfsr *fl, unsigned int spin)
+static int prepare_spin(struct fio_lfsr *fl, unsigned int spin)
{
uint64_t max = (fl->cached_bit << 1) - 1;
uint64_t x, y;
}
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;
}
-int lfsr_reset(struct fio_lfsr *fl, unsigned long seed)
+int lfsr_reset(struct fio_lfsr *fl, uint64_t seed)
{
uint64_t bitmask = (fl->cached_bit << 1) - 1;
return 0;
}
-int lfsr_init(struct fio_lfsr *fl, uint64_t nums, unsigned long seed,
- unsigned int spin)
+int lfsr_init(struct fio_lfsr *fl, uint64_t nums, uint64_t seed,
+ unsigned int spin)
{
- uint8_t *lfsr_taps;
+ uint8_t *taps;
- lfsr_taps = find_lfsr(nums);
- if (!lfsr_taps)
+ taps = find_lfsr(nums);
+ if (!taps)
return 1;
fl->max_val = nums - 1;
- fl->xormask = lfsr_create_xormask(lfsr_taps);
- fl->cached_bit = 1UL << (lfsr_taps[0] - 1);
+ fl->xormask = lfsr_create_xormask(taps);
+ fl->cached_bit = 1ULL << (taps[0] - 1);
if (prepare_spin(fl, spin))
return 1;