1 /*
2 * Copyright (c) 2004-2007 The University of Tennessee and The University
3 * of Tennessee Research Foundation. All rights
4 * reserved.
5 * $COPYRIGHT$
6 *
7 * Additional copyrights may follow
8 *
9 * $HEADER$
10 */
11
12 /** @file
13 *
14 * Simple macros to quickly compute a hash value from a string.
15 *
16 */
17
18 #ifndef OPAL_HASH_STRING_H
19 #define OPAL_HASH_STRING_H
20
21 /**
22 * Compute the hash value and the string length simultaneously
23 *
24 * @param str (IN) The string which will be parsed (char*)
25 * @param hash (OUT) Where the hash value will be stored (uint32_t)
26 * @param length (OUT) The computed length of the string (uint32_t)
27 */
28 #define OPAL_HASH_STRLEN( str, hash, length ) \
29 do { \
30 register const char *_str = (str); \
31 register uint32_t _hash = 0; \
32 register uint32_t _len = 0; \
33 \
34 while( *_str ) { \
35 _len++; \
36 _hash += *_str++; \
37 _hash += (_hash << 10); \
38 _hash ^= (_hash >> 6); \
39 } \
40 \
41 _hash += (_hash << 3); \
42 _hash ^= (_hash >> 11); \
43 (hash) = (_hash + (_hash << 15)); \
44 (length) = _len; \
45 } while(0)
46
47 /**
48 * Compute the hash value
49 *
50 * @param str (IN) The string which will be parsed (char*)
51 * @param hash (OUT) Where the hash value will be stored (uint32_t)
52 */
53 #define OPAL_HASH_STR( str, hash ) \
54 do { \
55 register const char *_str = (str); \
56 register uint32_t _hash = 0; \
57 \
58 while( *_str ) { \
59 _hash += *_str++; \
60 _hash += (_hash << 10); \
61 _hash ^= (_hash >> 6); \
62 } \
63 \
64 _hash += (_hash << 3); \
65 _hash ^= (_hash >> 11); \
66 (hash) = (_hash + (_hash << 15)); \
67 } while(0)
68
69 #endif /* OPAL_HASH_STRING_H */