1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following conditions are met:
12
13* Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the
15 following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the
19 following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23 contributors may be used to endorse or promote products
24 derived from this software without specific prior
25 written permission of the assimp team.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41
42#ifndef AI_HASH_H_INCLUDED
43#define AI_HASH_H_INCLUDED
44
45#include <stdint.h>
46#include <string.h>
47
48// ------------------------------------------------------------------------------------------------
49// Hashing function taken from
50// http://www.azillionmonkeys.com/qed/hash.html
51// (incremental version)
52//
53// This code is Copyright 2004-2008 by Paul Hsieh. It is used here in the belief that
54// Assimp's license is considered compatible with Pauls's derivative license as specified
55// on his web page.
56//
57// (stdint.h should have been been included here)
58// ------------------------------------------------------------------------------------------------
59#undef get16bits
60#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
61 || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
62#define get16bits(d) (*((const uint16_t *) (d)))
63#endif
64
65#if !defined (get16bits)
66#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
67 +(uint32_t)(((const uint8_t *)(d))[0]) )
68#endif
69
70// ------------------------------------------------------------------------------------------------
71inline uint32_t SuperFastHash (const char * data, uint32_t len = 0, uint32_t hash = 0) {
72uint32_t tmp;
73int rem;
74
75 if (!data) return 0;
76 if (!len)len = (uint32_t)::strlen(data);
77
78 rem = len & 3;
79 len >>= 2;
80
81 /* Main loop */
82 for (;len > 0; len--) {
83 hash += get16bits (data);
84 tmp = (get16bits (data+2) << 11) ^ hash;
85 hash = (hash << 16) ^ tmp;
86 data += 2*sizeof (uint16_t);
87 hash += hash >> 11;
88 }
89
90 /* Handle end cases */
91 switch (rem) {
92 case 3: hash += get16bits (data);
93 hash ^= hash << 16;
94 hash ^= data[sizeof (uint16_t)] << 18;
95 hash += hash >> 11;
96 break;
97 case 2: hash += get16bits (data);
98 hash ^= hash << 11;
99 hash += hash >> 17;
100 break;
101 case 1: hash += *data;
102 hash ^= hash << 10;
103 hash += hash >> 1;
104 }
105
106 /* Force "avalanching" of final 127 bits */
107 hash ^= hash << 3;
108 hash += hash >> 5;
109 hash ^= hash << 4;
110 hash += hash >> 17;
111 hash ^= hash << 25;
112 hash += hash >> 6;
113
114 return hash;
115}
116
117#endif // !! AI_HASH_H_INCLUDED
118