reciprocal_div.h 846 B

1234567891011121314151617181920212223242526272829303132
  1. #ifndef _LINUX_RECIPROCAL_DIV_H
  2. #define _LINUX_RECIPROCAL_DIV_H
  3. #include <linux/types.h>
  4. /*
  5. * This file describes reciprocical division.
  6. *
  7. * This optimizes the (A/B) problem, when A and B are two u32
  8. * and B is a known value (but not known at compile time)
  9. *
  10. * The math principle used is :
  11. * Let RECIPROCAL_VALUE(B) be (((1LL << 32) + (B - 1))/ B)
  12. * Then A / B = (u32)(((u64)(A) * (R)) >> 32)
  13. *
  14. * This replaces a divide by a multiply (and a shift), and
  15. * is generally less expensive in CPU cycles.
  16. */
  17. /*
  18. * Computes the reciprocal value (R) for the value B of the divisor.
  19. * Should not be called before each reciprocal_divide(),
  20. * or else the performance is slower than a normal divide.
  21. */
  22. extern u32 reciprocal_value(u32 B);
  23. static inline u32 reciprocal_divide(u32 A, u32 R)
  24. {
  25. return (u32)(((u64)A * R) >> 32);
  26. }
  27. #endif