1 /* go-type-identity.c -- hash and equality identity functions.
2
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
6
7 #include <stddef.h>
8
9 #include "runtime.h"
10 #include "go-type.h"
11
12 /* An identity hash function for a type. This is used for types where
13 we can simply use the type value itself as a hash code. This is
14 true of, e.g., integers and pointers. */
15
16 uintptr_t
__go_type_hash_identity(const void * key,uintptr_t key_size)17 __go_type_hash_identity (const void *key, uintptr_t key_size)
18 {
19 uintptr_t ret;
20 uintptr_t i;
21 const unsigned char *p;
22
23 if (key_size <= 8)
24 {
25 union
26 {
27 uint64 v;
28 unsigned char a[8];
29 } u;
30 u.v = 0;
31 #ifdef WORDS_BIGENDIAN
32 __builtin_memcpy (&u.a[8 - key_size], key, key_size);
33 #else
34 __builtin_memcpy (&u.a[0], key, key_size);
35 #endif
36 if (sizeof (uintptr_t) >= 8)
37 return (uintptr_t) u.v;
38 else
39 return (uintptr_t) ((u.v >> 32) ^ (u.v & 0xffffffff));
40 }
41
42 ret = 5381;
43 for (i = 0, p = (const unsigned char *) key; i < key_size; i++, p++)
44 ret = ret * 33 + *p;
45 return ret;
46 }
47
48 const FuncVal __go_type_hash_identity_descriptor =
49 { (void *) __go_type_hash_identity };
50
51 /* An identity equality function for a type. This is used for types
52 where we can check for equality by checking that the values have
53 the same bits. */
54
55 _Bool
__go_type_equal_identity(const void * k1,const void * k2,uintptr_t key_size)56 __go_type_equal_identity (const void *k1, const void *k2, uintptr_t key_size)
57 {
58 return __builtin_memcmp (k1, k2, key_size) == 0;
59 }
60
61 const FuncVal __go_type_equal_identity_descriptor =
62 { (void *) __go_type_equal_identity };
63