2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / go-type-string.c
blob719ecb0e7ea7e8cdf00b673d9d597a254b5f1359
1 /* go-type-string.c -- hash and equality string functions.
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. */
7 #include <stddef.h>
9 #include "go-string.h"
10 #include "go-type.h"
12 /* A string hash function for a map. */
14 uintptr_t
15 __go_type_hash_string (const void *vkey,
16 uintptr_t key_size __attribute__ ((unused)))
18 uintptr_t ret;
19 const struct __go_string *key;
20 int len;
21 int i;
22 const unsigned char *p;
24 ret = 5381;
25 key = (const struct __go_string *) vkey;
26 len = key->__length;
27 for (i = 0, p = key->__data; i < len; i++, p++)
28 ret = ret * 33 + *p;
29 return ret;
32 /* A string equality function for a map. */
34 _Bool
35 __go_type_equal_string (const void *vk1, const void *vk2,
36 uintptr_t key_size __attribute__ ((unused)))
38 const struct __go_string *k1;
39 const struct __go_string *k2;
41 k1 = (const struct __go_string *) vk1;
42 k2 = (const struct __go_string *) vk2;
43 return (k1->__length == k2->__length
44 && __builtin_memcmp (k1->__data, k2->__data, k1->__length) == 0);