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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
184
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents: 173
diff changeset
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
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
6 unsigned int
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
7 MemHash::hash(memaddr data0)
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
8 {
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
9 unsigned long data = (unsigned long)data0;
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
10 #if ABIBIT>32
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
11 register int i_PeRlHaSh = 8;
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
12 #else
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
13 register int i_PeRlHaSh = 4;
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
14 #endif
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
15 register uint32 hash_PeRlHaSh = 0;
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
16 while (i_PeRlHaSh--) {
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
17 hash_PeRlHaSh += data & 0xff;
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
18 hash_PeRlHaSh += (hash_PeRlHaSh << 10);
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
19 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6);
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
20 data >>= 8;
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
21 }
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
22 hash_PeRlHaSh += (hash_PeRlHaSh << 3);
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
23 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11);
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
24 return (hash_PeRlHaSh + (hash_PeRlHaSh << 15));
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
25 }
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
26
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
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
fcd53a8b777c ps3 fix
e065701@localhost.localdomain
parents: 352
diff changeset
40 for (uint32 i = 0; i < sizeof(memaddr) * 2; i ++) {
352
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
41 key = data & 0xf;
380
fcd53a8b777c ps3 fix
e065701@localhost.localdomain
parents: 352
diff changeset
42 value += key * PRIME[n++ & 7];
352
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
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
188e8bc16aca new hash function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 873
diff changeset
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
fcd53a8b777c ps3 fix
e065701@localhost.localdomain
parents: 352
diff changeset
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
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
63 int index = (hashval + i*i)%hashSize;
167
508beb59e0eb DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
diff changeset
64
352
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
65 if (table[index] == 0) { // 空の table に入れる
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
66 table[index] = data;
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
67 return index;
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
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
fcd53a8b777c ps3 fix
e065701@localhost.localdomain
parents: 352
diff changeset
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
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
80 int index = (hashval + i*i)%hashSize;
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
81
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
82 if (table[index] != NULL &&
380
fcd53a8b777c ps3 fix
e065701@localhost.localdomain
parents: 352
diff changeset
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
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
85 return table[index];
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
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
1e62bd02f820 add scale
gongo@localhost.localdomain
parents: 167
diff changeset
94 {
380
fcd53a8b777c ps3 fix
e065701@localhost.localdomain
parents: 352
diff changeset
95 int hashval = hash(key);
173
1e62bd02f820 add scale
gongo@localhost.localdomain
parents: 167
diff changeset
96
1e62bd02f820 add scale
gongo@localhost.localdomain
parents: 167
diff changeset
97 for (int i = 0; i < hashSize/2; i++) {
352
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
98 int index = (hashval + i*i)%hashSize;
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
99
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
100 if (table[index] != NULL &&
380
fcd53a8b777c ps3 fix
e065701@localhost.localdomain
parents: 352
diff changeset
101 table[index]->address == key) {
352
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
102 table[index] = NULL;
9738837c225c add global_alloc.h
admin@mb22-no-macbook-2.local
parents: 210
diff changeset
103 }
173
1e62bd02f820 add scale
gongo@localhost.localdomain
parents: 167
diff changeset
104 }
1e62bd02f820 add scale
gongo@localhost.localdomain
parents: 167
diff changeset
105 }
1e62bd02f820 add scale
gongo@localhost.localdomain
parents: 167
diff changeset
106
1e62bd02f820 add scale
gongo@localhost.localdomain
parents: 167
diff changeset
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 */