reciprocal_div.c 492 B

12345678910111213141516171819202122232425262728
  1. #include <linux/kernel.h>
  2. #include <asm/div64.h>
  3. #include <linux/reciprocal_div.h>
  4. #include <linux/export.h>
  5. /*
  6. * For a description of the algorithm please have a look at
  7. * include/linux/reciprocal_div.h
  8. */
  9. struct reciprocal_value reciprocal_value(u32 d)
  10. {
  11. struct reciprocal_value R;
  12. u64 m;
  13. int l;
  14. l = fls(d - 1);
  15. m = ((1ULL << 32) * ((1ULL << l) - d));
  16. do_div(m, d);
  17. ++m;
  18. R.m = (u32)m;
  19. R.sh1 = min(l, 1);
  20. R.sh2 = max(l - 1, 0);
  21. return R;
  22. }
  23. EXPORT_SYMBOL(reciprocal_value);