xref: /php-src/ext/dom/lexbor/lexbor/core/utils.c (revision bffab33a)
1 /*
2  * Copyright (C) 2018 Alexander Borisov
3  *
4  * Author: Alexander Borisov <borisov@lexbor.com>
5  */
6 
7 #include "lexbor/core/utils.h"
8 
9 
10 size_t
lexbor_utils_power(size_t t,size_t k)11 lexbor_utils_power(size_t t, size_t k)
12 {
13     size_t res = 1;
14 
15     while (k) {
16         if (k & 1) {
17             res *= t;
18         }
19 
20         t *= t;
21         k >>= 1;
22     }
23 
24     return res;
25 }
26 
27 size_t
lexbor_utils_hash_hash(const lxb_char_t * key,size_t key_size)28 lexbor_utils_hash_hash(const lxb_char_t *key, size_t key_size)
29 {
30     size_t hash, i;
31 
32     for (hash = i = 0; i < key_size; i++) {
33         hash += key[i];
34         hash += (hash << 10);
35         hash ^= (hash >> 6);
36     }
37 
38     hash += (hash << 3);
39     hash ^= (hash >> 11);
40     hash += (hash << 15);
41 
42     return hash;
43 }
44