hash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* Copyright 1995-1997,2000-2001,2003-2004,2006,2008-2015,2017-2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #ifdef HAVE_WCHAR_H
  19. #include <wchar.h>
  20. #endif
  21. #include <math.h>
  22. #include <string.h>
  23. #include <unistr.h>
  24. #include "chars.h"
  25. #include "foreign.h"
  26. #include "gsubr.h"
  27. #include "numbers.h"
  28. #include "pairs.h"
  29. #include "ports.h"
  30. #include "strings.h"
  31. #include "struct.h"
  32. #include "symbols.h"
  33. #include "syntax.h"
  34. #include "vectors.h"
  35. #include "hash.h"
  36. #ifndef floor
  37. extern double floor();
  38. #endif
  39. /* This hash function is originally from
  40. http://burtleburtle.net/bob/c/lookup3.c by Bob Jenkins, May 2006,
  41. Public Domain. No warranty. */
  42. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  43. #define mix(a,b,c) \
  44. { \
  45. a -= c; a ^= rot(c, 4); c += b; \
  46. b -= a; b ^= rot(a, 6); a += c; \
  47. c -= b; c ^= rot(b, 8); b += a; \
  48. a -= c; a ^= rot(c,16); c += b; \
  49. b -= a; b ^= rot(a,19); a += c; \
  50. c -= b; c ^= rot(b, 4); b += a; \
  51. }
  52. #define final(a,b,c) \
  53. { \
  54. c ^= b; c -= rot(b,14); \
  55. a ^= c; a -= rot(c,11); \
  56. b ^= a; b -= rot(a,25); \
  57. c ^= b; c -= rot(b,16); \
  58. a ^= c; a -= rot(c,4); \
  59. b ^= a; b -= rot(a,14); \
  60. c ^= b; c -= rot(b,24); \
  61. }
  62. #define JENKINS_LOOKUP3_HASHWORD2(k, length, ret) \
  63. do { \
  64. uint32_t a, b, c; \
  65. \
  66. /* Set up the internal state. */ \
  67. a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + 47; \
  68. \
  69. /* Handle most of the key. */ \
  70. while (length > 3) \
  71. { \
  72. a += k[0]; \
  73. b += k[1]; \
  74. c += k[2]; \
  75. mix (a, b, c); \
  76. length -= 3; \
  77. k += 3; \
  78. } \
  79. \
  80. /* Handle the last 3 elements. */ \
  81. switch(length) /* All the case statements fall through. */ \
  82. { \
  83. case 3 : c += k[2]; \
  84. case 2 : b += k[1]; \
  85. case 1 : a += k[0]; \
  86. final (a, b, c); \
  87. case 0: /* case 0: nothing left to add */ \
  88. break; \
  89. } \
  90. \
  91. if (sizeof (ret) == 8) \
  92. ret = (((unsigned long) c) << 32) | b; \
  93. else \
  94. ret = c; \
  95. } while (0)
  96. static unsigned long
  97. narrow_string_hash (const uint8_t *str, size_t len)
  98. {
  99. unsigned long ret;
  100. JENKINS_LOOKUP3_HASHWORD2 (str, len, ret);
  101. ret >>= 2; /* Ensure that it fits in a fixnum. */
  102. return ret;
  103. }
  104. static unsigned long
  105. wide_string_hash (const scm_t_wchar *str, size_t len)
  106. {
  107. unsigned long ret;
  108. JENKINS_LOOKUP3_HASHWORD2 (str, len, ret);
  109. ret >>= 2; /* Ensure that it fits in a fixnum. */
  110. return ret;
  111. }
  112. unsigned long
  113. scm_i_string_hash (SCM str)
  114. {
  115. size_t len = scm_i_string_length (str);
  116. if (scm_i_is_narrow_string (str))
  117. return narrow_string_hash ((const uint8_t *) scm_i_string_chars (str),
  118. len);
  119. else
  120. return wide_string_hash (scm_i_string_wide_chars (str), len);
  121. }
  122. unsigned long
  123. scm_i_locale_string_hash (const char *str, size_t len)
  124. {
  125. return scm_i_string_hash (scm_from_locale_stringn (str, len));
  126. }
  127. unsigned long
  128. scm_i_latin1_string_hash (const char *str, size_t len)
  129. {
  130. if (len == (size_t) -1)
  131. len = strlen (str);
  132. return narrow_string_hash ((const uint8_t *) str, len);
  133. }
  134. /* A tricky optimization, but probably worth it. */
  135. unsigned long
  136. scm_i_utf8_string_hash (const char *str, size_t len)
  137. {
  138. const uint8_t *end, *ustr = (const uint8_t *) str;
  139. unsigned long ret;
  140. /* The length of the string in characters. This name corresponds to
  141. Jenkins' original name. */
  142. size_t length;
  143. uint32_t a, b, c, u32;
  144. if (len == (size_t) -1)
  145. len = strlen (str);
  146. end = ustr + len;
  147. if (u8_check (ustr, len) != NULL)
  148. /* Invalid UTF-8; punt. */
  149. return scm_i_string_hash (scm_from_utf8_stringn (str, len));
  150. length = u8_strnlen (ustr, len);
  151. /* Set up the internal state. */
  152. a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + 47;
  153. /* Handle most of the key. */
  154. while (length > 3)
  155. {
  156. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  157. a += u32;
  158. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  159. b += u32;
  160. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  161. c += u32;
  162. mix (a, b, c);
  163. length -= 3;
  164. }
  165. /* Handle the last 3 elements's. */
  166. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  167. a += u32;
  168. if (--length)
  169. {
  170. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  171. b += u32;
  172. if (--length)
  173. {
  174. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  175. c += u32;
  176. }
  177. }
  178. final (a, b, c);
  179. if (sizeof (unsigned long) == 8)
  180. ret = (((unsigned long) c) << 32) | b;
  181. else
  182. ret = c;
  183. ret >>= 2; /* Ensure that it fits in a fixnum. */
  184. return ret;
  185. }
  186. static unsigned long scm_raw_ihashq (scm_t_bits key);
  187. static unsigned long scm_raw_ihash (SCM obj, size_t depth);
  188. /* Return the hash of struct OBJ. Traverse OBJ's fields to compute the
  189. result, unless DEPTH is zero. Assumes that OBJ is a struct. */
  190. static unsigned long
  191. scm_i_struct_hash (SCM obj, size_t depth)
  192. {
  193. size_t struct_size, field_num;
  194. unsigned long hash;
  195. struct_size = SCM_STRUCT_SIZE (obj);
  196. hash = scm_raw_ihashq (SCM_UNPACK (SCM_STRUCT_VTABLE (obj)));
  197. if (depth > 0)
  198. {
  199. for (field_num = 0; field_num < struct_size; field_num++)
  200. if (SCM_STRUCT_FIELD_IS_UNBOXED (obj, field_num))
  201. hash ^= scm_raw_ihashq (SCM_STRUCT_DATA_REF (obj, field_num));
  202. else
  203. hash ^= scm_raw_ihash (SCM_STRUCT_SLOT_REF (obj, field_num),
  204. depth / 2);
  205. }
  206. return hash;
  207. }
  208. /* Thomas Wang's integer hasher, from
  209. http://www.cris.com/~Ttwang/tech/inthash.htm. */
  210. static unsigned long
  211. scm_raw_ihashq (scm_t_bits key)
  212. {
  213. if (sizeof (key) < 8)
  214. {
  215. key = (key ^ 61) ^ (key >> 16);
  216. key = key + (key << 3);
  217. key = key ^ (key >> 4);
  218. key = key * 0x27d4eb2d;
  219. key = key ^ (key >> 15);
  220. }
  221. else
  222. {
  223. key = (~key) + (key << 21); // key = (key << 21) - key - 1;
  224. key = key ^ (key >> 24);
  225. key = (key + (key << 3)) + (key << 8); // key * 265
  226. key = key ^ (key >> 14);
  227. key = (key + (key << 2)) + (key << 4); // key * 21
  228. key = key ^ (key >> 28);
  229. key = key + (key << 31);
  230. }
  231. key >>= 2; /* Ensure that it fits in a fixnum. */
  232. return key;
  233. }
  234. /* `depth' is used to limit recursion. */
  235. static unsigned long
  236. scm_raw_ihash (SCM obj, size_t depth)
  237. {
  238. if (SCM_IMP (obj))
  239. return scm_raw_ihashq (SCM_UNPACK (obj));
  240. switch (SCM_TYP7(obj))
  241. {
  242. /* FIXME: do better for structs, variables, ... Also the hashes
  243. are currently associative, which ain't the right thing. */
  244. case scm_tc7_smob:
  245. return scm_raw_ihashq (SCM_TYP16 (obj));
  246. case scm_tc7_number:
  247. if (scm_is_integer (obj))
  248. {
  249. SCM n = SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM);
  250. if (scm_is_inexact (obj))
  251. obj = scm_inexact_to_exact (obj);
  252. return scm_raw_ihashq (scm_to_ulong (scm_modulo (obj, n)));
  253. }
  254. else
  255. return scm_i_string_hash (scm_number_to_string (obj, scm_from_int (10)));
  256. case scm_tc7_string:
  257. return scm_i_string_hash (obj);
  258. case scm_tc7_symbol:
  259. return scm_i_symbol_hash (obj);
  260. case scm_tc7_pointer:
  261. return scm_raw_ihashq ((uintptr_t) SCM_POINTER_VALUE (obj));
  262. case scm_tc7_wvect:
  263. case scm_tc7_vector:
  264. {
  265. size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj);
  266. size_t i = depth / 2;
  267. unsigned long h = scm_raw_ihashq (SCM_CELL_WORD_0 (obj));
  268. if (len)
  269. while (i--)
  270. h ^= scm_raw_ihash (scm_c_vector_ref (obj, h % len), i);
  271. return h;
  272. }
  273. case scm_tc7_syntax:
  274. {
  275. unsigned long h;
  276. h = scm_raw_ihash (scm_syntax_expression (obj), depth);
  277. h ^= scm_raw_ihash (scm_syntax_wrap (obj), depth);
  278. h ^= scm_raw_ihash (scm_syntax_module (obj), depth);
  279. return h;
  280. }
  281. case scm_tcs_cons_imcar:
  282. case scm_tcs_cons_nimcar:
  283. if (depth)
  284. return (scm_raw_ihash (SCM_CAR (obj), depth / 2)
  285. ^ scm_raw_ihash (SCM_CDR (obj), depth / 2));
  286. else
  287. return scm_raw_ihashq (scm_tc3_cons);
  288. case scm_tcs_struct:
  289. return scm_i_struct_hash (obj, depth);
  290. default:
  291. return scm_raw_ihashq (SCM_CELL_WORD_0 (obj));
  292. }
  293. }
  294. unsigned long
  295. scm_ihashq (SCM obj, unsigned long n)
  296. {
  297. return scm_raw_ihashq (SCM_UNPACK (obj)) % n;
  298. }
  299. SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
  300. (SCM key, SCM size),
  301. "Determine a hash value for @var{key} that is suitable for\n"
  302. "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
  303. "used as the equality predicate. The function returns an\n"
  304. "integer in the range 0 to @var{size} - 1. Note that\n"
  305. "@code{hashq} may use internal addresses. Thus two calls to\n"
  306. "hashq where the keys are @code{eq?} are not guaranteed to\n"
  307. "deliver the same value if the key object gets garbage collected\n"
  308. "in between. This can happen, for example with symbols:\n"
  309. "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
  310. "different values, since @code{foo} will be garbage collected.")
  311. #define FUNC_NAME s_scm_hashq
  312. {
  313. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  314. return scm_from_ulong (scm_ihashq (key, sz));
  315. }
  316. #undef FUNC_NAME
  317. unsigned long
  318. scm_ihashv (SCM obj, unsigned long n)
  319. {
  320. if (SCM_NUMP(obj))
  321. return scm_raw_ihash (obj, 10) % n;
  322. else
  323. return scm_raw_ihashq (SCM_UNPACK (obj)) % n;
  324. }
  325. SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
  326. (SCM key, SCM size),
  327. "Determine a hash value for @var{key} that is suitable for\n"
  328. "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
  329. "used as the equality predicate. The function returns an\n"
  330. "integer in the range 0 to @var{size} - 1. Note that\n"
  331. "@code{(hashv key)} may use internal addresses. Thus two calls\n"
  332. "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
  333. "deliver the same value if the key object gets garbage collected\n"
  334. "in between. This can happen, for example with symbols:\n"
  335. "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
  336. "different values, since @code{foo} will be garbage collected.")
  337. #define FUNC_NAME s_scm_hashv
  338. {
  339. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  340. return scm_from_ulong (scm_ihashv (key, sz));
  341. }
  342. #undef FUNC_NAME
  343. unsigned long
  344. scm_ihash (SCM obj, unsigned long n)
  345. {
  346. return (unsigned long) scm_raw_ihash (obj, 10) % n;
  347. }
  348. SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
  349. (SCM key, SCM size),
  350. "Determine a hash value for @var{key} that is suitable for\n"
  351. "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
  352. "is used as the equality predicate. The function returns an\n"
  353. "integer in the range 0 to @var{size} - 1.")
  354. #define FUNC_NAME s_scm_hash
  355. {
  356. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  357. return scm_from_ulong (scm_ihash (key, sz));
  358. }
  359. #undef FUNC_NAME
  360. void
  361. scm_init_hash ()
  362. {
  363. #include "hash.x"
  364. }