1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5
6
7#pragma once
8
9#include "type_traits.h"
10#include <stdio.h>
11
12namespace jitstd
13{
14template<typename Type>
15class hash
16{
17public:
18 size_t operator()(const Type& val) const
19 {
20 div_t qrem = ::div((int)(size_t) val, 127773);
21 qrem.rem = 16807 * qrem.rem - 2836 * qrem.quot;
22 if (qrem.rem < 0)
23 {
24 qrem.rem += 2147483647;
25 }
26 return ((size_t) qrem.rem);
27 }
28};
29
30template<>
31class hash<int>
32{
33public:
34 size_t operator()(const int& val) const
35 {
36 return val;
37 }
38};
39
40template<>
41class hash<unsigned __int64>
42{
43private:
44 typedef unsigned __int64 Type;
45
46public:
47 size_t operator()(const Type& val) const
48 {
49 return (hash<int>()((int)(val & 0xffffffffUL)) ^ hash<int>()((int)(val >> 32)));
50 }
51};
52
53template<>
54class hash<__int64>
55{
56private:
57 typedef __int64 Type;
58
59public:
60 size_t operator()(const Type& val) const
61 {
62 return (hash<unsigned __int64>()((unsigned __int64) val));
63 }
64};
65
66template<typename Type>
67class hash<Type*>
68{
69private:
70 typedef typename conditional<sizeof (Type*) <= sizeof (int), int, __int64>::type TInteger;
71public:
72 size_t operator()(const Type* val) const
73 {
74 return (hash<TInteger>()((TInteger) val));
75 }
76};
77
78template<>
79class hash<float>
80{
81private:
82 typedef float Type;
83public:
84 size_t operator()(const Type& val) const
85 {
86 unsigned long bits = *(unsigned long*) &val;
87 return (hash<unsigned long>()(bits == 0x80000000 ? 0 : bits));
88 }
89};
90
91template<>
92class hash<double>
93{
94public:
95 typedef double Type;
96 size_t operator()(const Type& val) const
97 {
98 unsigned __int64 bits = *(unsigned __int64*)&val;
99 return (hash<unsigned __int64>()((bits & (((unsigned __int64) -1) >> 1)) == 0 ? 0 : bits));
100 }
101};
102
103}
104