hash.c 14 KB

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