You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
4.7 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /******************************************************************
  2. Random numbers and hashing
  3. (c) 1994, 1995 Innobase Oy
  4. Created 5/30/1994 Heikki Tuuri
  5. *******************************************************************/
  6. #define UT_HASH_RANDOM_MASK 1463735687
  7. #define UT_HASH_RANDOM_MASK2 1653893711
  8. #define UT_RND1 151117737
  9. #define UT_RND2 119785373
  10. #define UT_RND3 85689495
  11. #define UT_RND4 76595339
  12. #define UT_SUM_RND2 98781234
  13. #define UT_SUM_RND3 126792457
  14. #define UT_SUM_RND4 63498502
  15. #define UT_XOR_RND1 187678878
  16. #define UT_XOR_RND2 143537923
  17. extern ulint ut_rnd_ulint_counter;
  18. /************************************************************
  19. This is used to set the random number seed. */
  20. UNIV_INLINE
  21. void
  22. ut_rnd_set_seed(
  23. /*============*/
  24. ulint seed) /* in: seed */
  25. {
  26. ut_rnd_ulint_counter = seed;
  27. }
  28. /************************************************************
  29. The following function generates a series of 'random' ulint integers. */
  30. UNIV_INLINE
  31. ulint
  32. ut_rnd_gen_next_ulint(
  33. /*==================*/
  34. /* out: the next 'random' number */
  35. ulint rnd) /* in: the previous random number value */
  36. {
  37. ulint n_bits;
  38. n_bits = 8 * sizeof(ulint);
  39. rnd = UT_RND2 * rnd + UT_SUM_RND3;
  40. rnd = UT_XOR_RND1 ^ rnd;
  41. rnd = (rnd << 20) + (rnd >> (n_bits - 20));
  42. rnd = UT_RND3 * rnd + UT_SUM_RND4;
  43. rnd = UT_XOR_RND2 ^ rnd;
  44. rnd = (rnd << 20) + (rnd >> (n_bits - 20));
  45. rnd = UT_RND1 * rnd + UT_SUM_RND2;
  46. return(rnd);
  47. }
  48. /************************************************************
  49. The following function generates 'random' ulint integers which
  50. enumerate the value space of ulint integers in a pseudo random
  51. fashion. Note that the same integer is repeated always after
  52. 2 to power 32 calls to the generator (if ulint is 32-bit). */
  53. UNIV_INLINE
  54. ulint
  55. ut_rnd_gen_ulint(void)
  56. /*==================*/
  57. /* out: the 'random' number */
  58. {
  59. ulint rnd;
  60. ulint n_bits;
  61. n_bits = 8 * sizeof(ulint);
  62. ut_rnd_ulint_counter = UT_RND1 * ut_rnd_ulint_counter + UT_RND2;
  63. rnd = ut_rnd_gen_next_ulint(ut_rnd_ulint_counter);
  64. return(rnd);
  65. }
  66. /************************************************************
  67. Generates a random integer from a given interval. */
  68. UNIV_INLINE
  69. ulint
  70. ut_rnd_interval(
  71. /*============*/
  72. /* out: the 'random' number */
  73. ulint low, /* in: low limit; can generate also this value */
  74. ulint high) /* in: high limit; can generate also this value */
  75. {
  76. ulint rnd;
  77. ut_ad(high >= low);
  78. if (low == high) {
  79. return(low);
  80. }
  81. rnd = ut_rnd_gen_ulint();
  82. return(low + (rnd % (high - low + 1)));
  83. }
  84. /*************************************************************
  85. Generates a random iboolean value. */
  86. UNIV_INLINE
  87. ibool
  88. ut_rnd_gen_ibool(void)
  89. /*=================*/
  90. /* out: the random value */
  91. {
  92. ulint x;
  93. x = ut_rnd_gen_ulint();
  94. if (((x >> 20) + (x >> 15)) & 1) {
  95. return(TRUE);
  96. }
  97. return(FALSE);
  98. }
  99. /***********************************************************
  100. The following function generates a hash value for a ulint integer
  101. to a hash table of size table_size, which should be a prime
  102. or some random number for the hash table to work reliably. */
  103. UNIV_INLINE
  104. ulint
  105. ut_hash_ulint(
  106. /*==========*/
  107. /* out: hash value */
  108. ulint key, /* in: value to be hashed */
  109. ulint table_size) /* in: hash table size */
  110. {
  111. key = key ^ UT_HASH_RANDOM_MASK2;
  112. return(key % table_size);
  113. }
  114. /*****************************************************************
  115. Folds a pair of ulints. */
  116. UNIV_INLINE
  117. ulint
  118. ut_fold_ulint_pair(
  119. /*===============*/
  120. /* out: folded value */
  121. ulint n1, /* in: ulint */
  122. ulint n2) /* in: ulint */
  123. {
  124. return(((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1)
  125. ^ UT_HASH_RANDOM_MASK) + n2);
  126. }
  127. /*****************************************************************
  128. Folds a dulint. */
  129. UNIV_INLINE
  130. ulint
  131. ut_fold_dulint(
  132. /*===========*/
  133. /* out: folded value */
  134. dulint d) /* in: dulint */
  135. {
  136. return(ut_fold_ulint_pair(ut_dulint_get_low(d),
  137. ut_dulint_get_high(d)));
  138. }
  139. /*****************************************************************
  140. Folds a character string ending in the null character. */
  141. UNIV_INLINE
  142. ulint
  143. ut_fold_string(
  144. /*===========*/
  145. /* out: folded value */
  146. const char* str) /* in: null-terminated string */
  147. {
  148. #ifdef UNIV_DEBUG
  149. ulint i = 0;
  150. #endif
  151. ulint fold = 0;
  152. ut_ad(str);
  153. while (*str != '\0') {
  154. #ifdef UNIV_DEBUG
  155. i++;
  156. ut_a(i < 100);
  157. #endif
  158. fold = ut_fold_ulint_pair(fold, (ulint)(*str));
  159. str++;
  160. }
  161. return(fold);
  162. }
  163. /*****************************************************************
  164. Folds a binary string. */
  165. UNIV_INLINE
  166. ulint
  167. ut_fold_binary(
  168. /*===========*/
  169. /* out: folded value */
  170. const byte* str, /* in: string of bytes */
  171. ulint len) /* in: length */
  172. {
  173. const byte* str_end = str + len;
  174. ulint fold = 0;
  175. ut_ad(str || !len);
  176. while (str < str_end) {
  177. fold = ut_fold_ulint_pair(fold, (ulint)(*str));
  178. str++;
  179. }
  180. return(fold);
  181. }