hash.c 14 KB

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