|
| 1 | +(* Copyright (C) 2018 Authors of BuckleScript |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +let (<< ) = Nativeint.shift_left |
| 28 | +let (>>>) = Nativeint.shift_right_logical |
| 29 | +let (|~) = Nativeint.logor |
| 30 | +let (^) = Nativeint.logxor |
| 31 | + |
| 32 | +external ( *~ ) : nativeint -> nativeint -> nativeint = "caml_int32_mul" |
| 33 | +external ( +~ ) : nativeint -> nativeint -> nativeint = "caml_int32_add" |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +let rotl32 (x : nativeint) n = |
| 38 | + (x << n) |~ (x >>> (32 - n)) |
| 39 | + |
| 40 | + |
| 41 | +let caml_hash_mix_int h d = |
| 42 | + let d = ref d in |
| 43 | + d := !d *~ 0xcc9e2d51n ; |
| 44 | + d := rotl32 !d 15 ; |
| 45 | + d := !d *~ 0x1b873593n ; |
| 46 | + let h = ref (h ^ !d) in |
| 47 | + h := rotl32 !h 13 ; |
| 48 | + !h +~ (!h << 2) +~ 0xe6546b64n |
| 49 | + |
| 50 | +let caml_hash_final_mix h = |
| 51 | + let h = ref (h ^ (h >>> 16)) in |
| 52 | + h := !h *~ 0x85ebca6bn ; |
| 53 | + h := !h ^ (!h >>> 13); |
| 54 | + h := !h *~ 0xc2b2ae35n ; |
| 55 | + !h ^ (!h >>> 16) |
| 56 | + (* Nativeint.logand (!h ^ (!h >>> 16)) 0x3FFFFFFFn *) |
| 57 | + |
| 58 | +let caml_hash_mix_string h s = |
| 59 | + let len =Bs_string.length s in |
| 60 | + let block = len / 4 - 1 in |
| 61 | + let hash = ref h in |
| 62 | + for i = 0 to block do |
| 63 | + let j = 4 * i in |
| 64 | + let w = |
| 65 | + Char.code s.[j] lor |
| 66 | + (Char.code s.[j+1] lsl 8) lor |
| 67 | + (Char.code s.[j+2] lsl 16) lor |
| 68 | + (Char.code s.[j+3] lsl 24) |
| 69 | + in |
| 70 | + hash := caml_hash_mix_int !hash (Nativeint.of_int w) |
| 71 | + done ; |
| 72 | + let modulo = len land 0b11 in |
| 73 | + if modulo <> 0 then |
| 74 | + begin |
| 75 | + let w = |
| 76 | + if modulo = 3 then |
| 77 | + (Char.code s.[len - 1] lsl 16) lor |
| 78 | + (Char.code s.[len - 2] lsl 8) lor |
| 79 | + (Char.code s.[len - 3]) |
| 80 | + else if modulo = 2 then |
| 81 | + (Char.code s.[len -1] lsl 8) lor |
| 82 | + Char.code s.[len -2] |
| 83 | + else Char.code s.[len - 1] |
| 84 | + in |
| 85 | + hash := caml_hash_mix_int !hash (Nativeint.of_int w) |
| 86 | + end; |
| 87 | + hash := !hash ^ (Nativeint.of_int len) ; |
| 88 | + !hash |
| 89 | + |
| 90 | + |
0 commit comments