Mercurial > hg > Game > Cerium
annotate TaskManager/kernel/memory/MemHash.cc @ 874:188e8bc16aca draft
new hash function
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 04 Jul 2010 19:17:10 +0900 |
parents | c50f39fbb6ca |
children | 2919078d069f |
rev | line source |
---|---|
184 | 1 #include <stdio.h> |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
2 #include <string.h> |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
3 #include <stdlib.h> |
383
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
4 #include "MemHash.h" |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
5 |
874 | 6 unsigned int |
7 MemHash::hash(memaddr data0) | |
8 { | |
9 unsigned long data = (unsigned long)data0; | |
10 #if ABIBIT>32 | |
11 register int i_PeRlHaSh = 8; | |
12 #else | |
13 register int i_PeRlHaSh = 4; | |
14 #endif | |
15 register uint32 hash_PeRlHaSh = 0; | |
16 while (i_PeRlHaSh--) { | |
17 hash_PeRlHaSh += data & 0xff; | |
18 hash_PeRlHaSh += (hash_PeRlHaSh << 10); | |
19 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); | |
20 data >>= 8; | |
21 } | |
22 hash_PeRlHaSh += (hash_PeRlHaSh << 3); | |
23 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); | |
24 return (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); | |
25 } | |
26 | |
27 #if 0 | |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
28 static unsigned short PRIME[8] = { |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
29 0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd, |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
30 }; |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
31 |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
32 int |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
388
diff
changeset
|
33 MemHash::hash(memaddr data0) |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
34 { |
873
c50f39fbb6ca
fix hash problem ( unsigned int-> long overflow )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
35 unsigned long data = (unsigned long)data0; |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
36 int value = 0; |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
37 int n = 0; |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
388
diff
changeset
|
38 long key; |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
39 |
380 | 40 for (uint32 i = 0; i < sizeof(memaddr) * 2; i ++) { |
352 | 41 key = data & 0xf; |
380 | 42 value += key * PRIME[n++ & 7]; |
352 | 43 data >>= 4; |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
44 } |
873
c50f39fbb6ca
fix hash problem ( unsigned int-> long overflow )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
45 printf("hash value %0x => %0x\n",data0, value); |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
46 |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
47 return value % hashSize; |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
48 } |
874 | 49 #endif |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
50 |
388
3d1e86396d16
MemHash (OS X version)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
387
diff
changeset
|
51 MemHash::MemHash() |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
52 { |
383
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
53 table = (MemorySegmentPtr*)malloc(tableSize); |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
54 clear(); |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
55 } |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
56 |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
57 int |
383
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
58 MemHash::put(memaddr key, MemorySegmentPtr data) |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
59 { |
380 | 60 int hashval = hash(key); |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
61 |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
62 for (int i = 0; i < hashSize/2; i++) { |
352 | 63 int index = (hashval + i*i)%hashSize; |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
64 |
352 | 65 if (table[index] == 0) { // 空の table に入れる |
66 table[index] = data; | |
67 return index; | |
68 } | |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
69 } |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
70 |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
71 return -1; |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
72 } |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
73 |
383
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
74 MemorySegmentPtr |
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
75 MemHash::get(memaddr key) |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
76 { |
380 | 77 int hashval = hash(key); |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
78 |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
79 for (int i = 0; i < hashSize/2; i++) { |
352 | 80 int index = (hashval + i*i)%hashSize; |
81 | |
82 if (table[index] != NULL && | |
380 | 83 table[index]->address == key) { |
873
c50f39fbb6ca
fix hash problem ( unsigned int-> long overflow )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
84 printf("get hash value %0x\n",index); |
352 | 85 return table[index]; |
86 } | |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
87 } |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
88 |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
89 return NULL; |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
90 } |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
91 |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
92 void |
383
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
93 MemHash::remove(memaddr key) |
173 | 94 { |
380 | 95 int hashval = hash(key); |
173 | 96 |
97 for (int i = 0; i < hashSize/2; i++) { | |
352 | 98 int index = (hashval + i*i)%hashSize; |
99 | |
100 if (table[index] != NULL && | |
380 | 101 table[index]->address == key) { |
352 | 102 table[index] = NULL; |
103 } | |
173 | 104 } |
105 } | |
106 | |
107 void | |
383
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
108 MemHash::clear(void) |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
109 { |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
110 bzero(table, tableSize); |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff
changeset
|
111 } |
383
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
112 |
b3fb0013e6b2
fix header, MemHash in kernel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
113 /* end */ |