2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libgo / runtime / go-type-float.c
blob4ae73470de9ed68cb05afabdbac83f3cc54c0e8f
1 /* go-type-float.c -- hash and equality float functions.
3 Copyright 2012 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 <math.h>
8 #include <stdint.h>
9 #include "runtime.h"
10 #include "go-type.h"
12 /* Hash function for float types. */
14 uintptr_t
15 __go_type_hash_float (const void *vkey, uintptr_t key_size)
17 if (key_size == 4)
19 const float *fp;
20 float f;
21 uint32_t si;
23 fp = (const float *) vkey;
24 f = *fp;
26 if (isinf (f) || f == 0)
27 return 0;
29 /* NaN != NaN, so the hash code of a NaN is irrelevant. Make it
30 random so that not all NaNs wind up in the same place. */
31 if (isnan (f))
32 return runtime_fastrand1 ();
34 memcpy (&si, vkey, 4);
35 return (uintptr_t) si;
37 else if (key_size == 8)
39 const double *dp;
40 double d;
41 uint64_t di;
43 dp = (const double *) vkey;
44 d = *dp;
46 if (isinf (d) || d == 0)
47 return 0;
49 if (isnan (d))
50 return runtime_fastrand1 ();
52 memcpy (&di, vkey, 8);
53 return (uintptr_t) di;
55 else
56 runtime_throw ("__go_type_hash_float: invalid float size");
59 /* Equality function for float types. */
61 _Bool
62 __go_type_equal_float (const void *vk1, const void *vk2, uintptr_t key_size)
64 if (key_size == 4)
66 const float *fp1;
67 const float *fp2;
69 fp1 = (const float *) vk1;
70 fp2 = (const float *) vk2;
72 return *fp1 == *fp2;
74 else if (key_size == 8)
76 const double *dp1;
77 const double *dp2;
79 dp1 = (const double *) vk1;
80 dp2 = (const double *) vk2;
82 return *dp1 == *dp2;
84 else
85 runtime_throw ("__go_type_equal_float: invalid float size");