hash.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_HASH_H
  3. #define _ASM_HASH_H
  4. /*
  5. * Fortunately, most people who want to run Linux on Microblaze enable
  6. * both multiplier and barrel shifter, but omitting them is technically
  7. * a supported configuration.
  8. *
  9. * With just a barrel shifter, we can implement an efficient constant
  10. * multiply using shifts and adds. GCC can find a 9-step solution, but
  11. * this 6-step solution was found by Yevgen Voronenko's implementation
  12. * of the Hcub algorithm at http://spiral.ece.cmu.edu/mcm/gen.html.
  13. *
  14. * That software is really not designed for a single multiplier this large,
  15. * but if you run it enough times with different seeds, it'll find several
  16. * 6-shift, 6-add sequences for computing x * 0x61C88647. They are all
  17. * c = (x << 19) + x;
  18. * a = (x << 9) + c;
  19. * b = (x << 23) + a;
  20. * return (a<<11) + (b<<6) + (c<<3) - b;
  21. * with variations on the order of the final add.
  22. *
  23. * Without even a shifter, it's hopless; any hash function will suck.
  24. */
  25. #if CONFIG_XILINX_MICROBLAZE0_USE_HW_MUL == 0
  26. #define HAVE_ARCH__HASH_32 1
  27. /* Multiply by GOLDEN_RATIO_32 = 0x61C88647 */
  28. static inline u32 __attribute_const__ __hash_32(u32 a)
  29. {
  30. #if CONFIG_XILINX_MICROBLAZE0_USE_BARREL
  31. unsigned int b, c;
  32. /* Phase 1: Compute three intermediate values */
  33. b = a << 23;
  34. c = (a << 19) + a;
  35. a = (a << 9) + c;
  36. b += a;
  37. /* Phase 2: Compute (a << 11) + (b << 6) + (c << 3) - b */
  38. a <<= 5;
  39. a += b; /* (a << 5) + b */
  40. a <<= 3;
  41. a += c; /* (a << 8) + (b << 3) + c */
  42. a <<= 3;
  43. return a - b; /* (a << 11) + (b << 6) + (c << 3) - b */
  44. #else
  45. /*
  46. * "This is really going to hurt."
  47. *
  48. * Without a barrel shifter, left shifts are implemented as
  49. * repeated additions, and the best we can do is an optimal
  50. * addition-subtraction chain. This one is not known to be
  51. * optimal, but at 37 steps, it's decent for a 31-bit multiplier.
  52. *
  53. * Question: given its size (37*4 = 148 bytes per instance),
  54. * and slowness, is this worth having inline?
  55. */
  56. unsigned int b, c, d;
  57. b = a << 4; /* 4 */
  58. c = b << 1; /* 1 5 */
  59. b += a; /* 1 6 */
  60. c += b; /* 1 7 */
  61. c <<= 3; /* 3 10 */
  62. c -= a; /* 1 11 */
  63. d = c << 7; /* 7 18 */
  64. d += b; /* 1 19 */
  65. d <<= 8; /* 8 27 */
  66. d += a; /* 1 28 */
  67. d <<= 1; /* 1 29 */
  68. d += b; /* 1 30 */
  69. d <<= 6; /* 6 36 */
  70. return d + c; /* 1 37 total instructions*/
  71. #endif
  72. }
  73. #endif /* !CONFIG_XILINX_MICROBLAZE0_USE_HW_MUL */
  74. #endif /* _ASM_HASH_H */