reciprocal_div.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bug.h>
  3. #include <linux/kernel.h>
  4. #include <asm/div64.h>
  5. #include <linux/reciprocal_div.h>
  6. #include <linux/export.h>
  7. /*
  8. * For a description of the algorithm please have a look at
  9. * include/linux/reciprocal_div.h
  10. */
  11. struct reciprocal_value reciprocal_value(u32 d)
  12. {
  13. struct reciprocal_value R;
  14. u64 m;
  15. int l;
  16. l = fls(d - 1);
  17. m = ((1ULL << 32) * ((1ULL << l) - d));
  18. do_div(m, d);
  19. ++m;
  20. R.m = (u32)m;
  21. R.sh1 = min(l, 1);
  22. R.sh2 = max(l - 1, 0);
  23. return R;
  24. }
  25. EXPORT_SYMBOL(reciprocal_value);
  26. struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec)
  27. {
  28. struct reciprocal_value_adv R;
  29. u32 l, post_shift;
  30. u64 mhigh, mlow;
  31. /* ceil(log2(d)) */
  32. l = fls(d - 1);
  33. /* NOTE: mlow/mhigh could overflow u64 when l == 32. This case needs to
  34. * be handled before calling "reciprocal_value_adv", please see the
  35. * comment at include/linux/reciprocal_div.h.
  36. */
  37. WARN(l == 32,
  38. "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor",
  39. d, __func__);
  40. post_shift = l;
  41. mlow = 1ULL << (32 + l);
  42. do_div(mlow, d);
  43. mhigh = (1ULL << (32 + l)) + (1ULL << (32 + l - prec));
  44. do_div(mhigh, d);
  45. for (; post_shift > 0; post_shift--) {
  46. u64 lo = mlow >> 1, hi = mhigh >> 1;
  47. if (lo >= hi)
  48. break;
  49. mlow = lo;
  50. mhigh = hi;
  51. }
  52. R.m = (u32)mhigh;
  53. R.sh = post_shift;
  54. R.exp = l;
  55. R.is_wide_m = mhigh > U32_MAX;
  56. return R;
  57. }
  58. EXPORT_SYMBOL(reciprocal_value_adv);