mvp_math.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
  3. *
  4. * Copyright (C) 2010-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; see the file COPYING. If not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #line 5
  20. /**
  21. * @file
  22. *
  23. * @brief Math library.
  24. */
  25. #ifndef _MVP_MATH_H_
  26. #define _MVP_MATH_H_
  27. #define INCLUDE_ALLOW_VMX
  28. #define INCLUDE_ALLOW_PV
  29. #define INCLUDE_ALLOW_MODULE
  30. #define INCLUDE_ALLOW_MONITOR
  31. #define INCLUDE_ALLOW_GPL
  32. #define INCLUDE_ALLOW_HOSTUSER
  33. #include "include_check.h"
  34. #include "mvp_compiler_gcc.h"
  35. /**
  36. * @brief Compute floor log2 of a given 32-bit unsigned integer.
  37. *
  38. * @param n 32-bit unsigned integer, n > 0.
  39. *
  40. * @return floor(log2(n)).
  41. */
  42. #define LOG2(n) \
  43. ( \
  44. __builtin_constant_p(n) ? ( \
  45. (n) & (1UL << 31) ? 31 : \
  46. (n) & (1UL << 30) ? 30 : \
  47. (n) & (1UL << 29) ? 29 : \
  48. (n) & (1UL << 28) ? 28 : \
  49. (n) & (1UL << 27) ? 27 : \
  50. (n) & (1UL << 26) ? 26 : \
  51. (n) & (1UL << 25) ? 25 : \
  52. (n) & (1UL << 24) ? 24 : \
  53. (n) & (1UL << 23) ? 23 : \
  54. (n) & (1UL << 22) ? 22 : \
  55. (n) & (1UL << 21) ? 21 : \
  56. (n) & (1UL << 20) ? 20 : \
  57. (n) & (1UL << 19) ? 19 : \
  58. (n) & (1UL << 18) ? 18 : \
  59. (n) & (1UL << 17) ? 17 : \
  60. (n) & (1UL << 16) ? 16 : \
  61. (n) & (1UL << 15) ? 15 : \
  62. (n) & (1UL << 14) ? 14 : \
  63. (n) & (1UL << 13) ? 13 : \
  64. (n) & (1UL << 12) ? 12 : \
  65. (n) & (1UL << 11) ? 11 : \
  66. (n) & (1UL << 10) ? 10 : \
  67. (n) & (1UL << 9) ? 9 : \
  68. (n) & (1UL << 8) ? 8 : \
  69. (n) & (1UL << 7) ? 7 : \
  70. (n) & (1UL << 6) ? 6 : \
  71. (n) & (1UL << 5) ? 5 : \
  72. (n) & (1UL << 4) ? 4 : \
  73. (n) & (1UL << 3) ? 3 : \
  74. (n) & (1UL << 2) ? 2 : \
  75. (n) & (1UL << 1) ? 1 : \
  76. (n) & (1UL << 0) ? 0 : \
  77. 0xffffffff \
  78. ) : (uint32)(CLZ(1) - CLZ(n)) \
  79. )
  80. /**
  81. * @brief Multiplicative hash function for 32-bit key and p-bit range. See p229
  82. * Introduction to Algorithms, Cormen, Leiserson and Rivest, 1996.
  83. *
  84. * @param key 32-bit key.
  85. * @param p range order, <= 32.
  86. *
  87. * @return hash value in range [0..2^p)
  88. */
  89. static inline uint32
  90. Math_MultiplicativeHash(uint32 key,
  91. uint32 p)
  92. {
  93. return (key * 2654435769UL) >> (32 - p);
  94. }
  95. /**
  96. * @brief Compute ceiling log2 of a given 32-bit unsigned integer.
  97. *
  98. * @param n 32-bit unsigned integer, n > 0.
  99. *
  100. * @return ceiling(log2(n)).
  101. */
  102. static inline uint32 CLOG2(uint32 n)
  103. {
  104. return LOG2(n) + ((n & -n) != n);
  105. }
  106. /**
  107. * @brief djb2 String hashing function by Dan Bernstein, see
  108. * http://www.cse.yorku.ca/~oz/hash.html
  109. * @param str String to hash
  110. * @return 32-bit hash value
  111. */
  112. static inline
  113. uint32 Math_Djb2Hash(uint8 *str)
  114. {
  115. uint32 hash = 5381;
  116. int32 c;
  117. while ((c = *str++))
  118. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  119. return hash;
  120. }
  121. #endif /* ifndef _MVP_MATH_H_ */