fix up radeon span functions using latest r200 code from Brian,
[mesa.git] / src / mesa / drivers / dri / dri_client / xf86drmSL.c
1 /* xf86drmSL.c -- Skip list support
2 * Created: Mon May 10 09:28:13 1999 by faith@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
27 *
28 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmSL.c,v 1.3 2000/06/17 00:03:34 martin Exp $
29 *
30 * DESCRIPTION
31 *
32 * This file contains a straightforward skip list implementation.n
33 *
34 * FUTURE ENHANCEMENTS
35 *
36 * REFERENCES
37 *
38 * [Pugh90] William Pugh. Skip Lists: A Probabilistic Alternative to
39 * Balanced Trees. CACM 33(6), June 1990, pp. 668-676.
40 *
41 */
42
43 #define SL_MAIN 0
44
45 #if SL_MAIN
46 # include <stdio.h>
47 # include <stdlib.h>
48 # include <sys/time.h>
49 #else
50 # include "xf86drm.h"
51 # ifdef XFree86LOADER
52 # include "xf86.h"
53 # include "xf86_ansic.h"
54 # else
55 # include <stdio.h>
56 # include <stdlib.h>
57 # endif
58 #endif
59
60 #define SL_LIST_MAGIC 0xfacade00LU
61 #define SL_ENTRY_MAGIC 0x00fab1edLU
62 #define SL_FREED_MAGIC 0xdecea5edLU
63 #define SL_MAX_LEVEL 16
64 #define SL_DEBUG 0
65 #define SL_RANDOM_SEED 0xc01055a1LU
66
67 #if SL_MAIN
68 #define SL_ALLOC malloc
69 #define SL_FREE free
70 #define SL_RANDOM_DECL static int state = 0;
71 #define SL_RANDOM_INIT(seed) if (!state) { srandom(seed); ++state; }
72 #define SL_RANDOM random()
73 #else
74 #define SL_ALLOC drmMalloc
75 #define SL_FREE drmFree
76 #define SL_RANDOM_DECL static void *state = NULL
77 #define SL_RANDOM_INIT(seed) if (!state) state = drmRandomCreate(seed)
78 #define SL_RANDOM drmRandom(state)
79
80 #endif
81
82 typedef struct SLEntry {
83 unsigned long magic; /* SL_ENTRY_MAGIC */
84 unsigned long key;
85 void *value;
86 int levels;
87 struct SLEntry *forward[1]; /* variable sized array */
88 } SLEntry, *SLEntryPtr;
89
90 typedef struct SkipList {
91 unsigned long magic; /* SL_LIST_MAGIC */
92 int level;
93 int count;
94 SLEntryPtr head;
95 SLEntryPtr p0; /* Position for iteration */
96 } SkipList, *SkipListPtr;
97
98 #if SL_MAIN
99 extern void *drmSLCreate(void);
100 extern int drmSLDestroy(void *l);
101 extern int drmSLLookup(void *l, unsigned long key, void **value);
102 extern int drmSLInsert(void *l, unsigned long key, void *value);
103 extern int drmSLDelete(void *l, unsigned long key);
104 extern int drmSLNext(void *l, unsigned long *key, void **value);
105 extern int drmSLFirst(void *l, unsigned long *key, void **value);
106 extern void drmSLDump(void *l);
107 extern int drmSLLookupNeighbors(void *l, unsigned long key,
108 unsigned long *prev_key, void **prev_value,
109 unsigned long *next_key, void **next_value);
110 #endif
111
112 static SLEntryPtr SLCreateEntry(int max_level, unsigned long key, void *value)
113 {
114 SLEntryPtr entry;
115
116 if (max_level < 0 || max_level > SL_MAX_LEVEL) max_level = SL_MAX_LEVEL;
117
118 entry = SL_ALLOC(sizeof(*entry)
119 + (max_level + 1) * sizeof(entry->forward[0]));
120 if (!entry) return NULL;
121 entry->magic = SL_ENTRY_MAGIC;
122 entry->key = key;
123 entry->value = value;
124 entry->levels = max_level + 1;
125
126 return entry;
127 }
128
129 static int SLRandomLevel(void)
130 {
131 int level = 1;
132 SL_RANDOM_DECL;
133
134 SL_RANDOM_INIT(SL_RANDOM_SEED);
135
136 while ((SL_RANDOM & 0x01) && level < SL_MAX_LEVEL) ++level;
137 return level;
138 }
139
140 void *drmSLCreate(void)
141 {
142 SkipListPtr list;
143 int i;
144
145 list = SL_ALLOC(sizeof(*list));
146 if (!list) return NULL;
147 list->magic = SL_LIST_MAGIC;
148 list->level = 0;
149 list->head = SLCreateEntry(SL_MAX_LEVEL, 0, NULL);
150 list->count = 0;
151
152 for (i = 0; i <= SL_MAX_LEVEL; i++) list->head->forward[i] = NULL;
153
154 return list;
155 }
156
157 int drmSLDestroy(void *l)
158 {
159 SkipListPtr list = (SkipListPtr)l;
160 SLEntryPtr entry;
161 SLEntryPtr next;
162
163 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
164
165 for (entry = list->head; entry; entry = next) {
166 if (entry->magic != SL_ENTRY_MAGIC) return -1; /* Bad magic */
167 next = entry->forward[0];
168 entry->magic = SL_FREED_MAGIC;
169 SL_FREE(entry);
170 }
171
172 list->magic = SL_FREED_MAGIC;
173 SL_FREE(list);
174 return 0;
175 }
176
177 static SLEntryPtr SLLocate(void *l, unsigned long key, SLEntryPtr *update)
178 {
179 SkipListPtr list = (SkipListPtr)l;
180 SLEntryPtr entry;
181 int i;
182
183 if (list->magic != SL_LIST_MAGIC) return NULL;
184
185 for (i = list->level, entry = list->head; i >= 0; i--) {
186 while (entry->forward[i] && entry->forward[i]->key < key)
187 entry = entry->forward[i];
188 update[i] = entry;
189 }
190
191 return entry->forward[0];
192 }
193
194 int drmSLInsert(void *l, unsigned long key, void *value)
195 {
196 SkipListPtr list = (SkipListPtr)l;
197 SLEntryPtr entry;
198 SLEntryPtr update[SL_MAX_LEVEL + 1];
199 int level;
200 int i;
201
202 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
203
204 entry = SLLocate(list, key, update);
205
206 if (entry && entry->key == key) return 1; /* Already in list */
207
208
209 level = SLRandomLevel();
210 if (level > list->level) {
211 level = ++list->level;
212 update[level] = list->head;
213 }
214
215 entry = SLCreateEntry(level, key, value);
216
217 /* Fix up forward pointers */
218 for (i = 0; i <= level; i++) {
219 entry->forward[i] = update[i]->forward[i];
220 update[i]->forward[i] = entry;
221 }
222
223 ++list->count;
224 return 0; /* Added to table */
225 }
226
227 int drmSLDelete(void *l, unsigned long key)
228 {
229 SkipListPtr list = (SkipListPtr)l;
230 SLEntryPtr update[SL_MAX_LEVEL + 1];
231 SLEntryPtr entry;
232 int i;
233
234 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
235
236 entry = SLLocate(list, key, update);
237
238 if (!entry || entry->key != key) return 1; /* Not found */
239
240 /* Fix up forward pointers */
241 for (i = 0; i <= list->level; i++) {
242 if (update[i]->forward[i] == entry)
243 update[i]->forward[i] = entry->forward[i];
244 }
245
246 entry->magic = SL_FREED_MAGIC;
247 SL_FREE(entry);
248
249 while (list->level && !list->head->forward[list->level]) --list->level;
250 --list->count;
251 return 0;
252 }
253
254 int drmSLLookup(void *l, unsigned long key, void **value)
255 {
256 SkipListPtr list = (SkipListPtr)l;
257 SLEntryPtr update[SL_MAX_LEVEL + 1];
258 SLEntryPtr entry;
259
260 entry = SLLocate(list, key, update);
261
262 if (entry && entry->key == key) {
263 *value = entry;
264 return 0;
265 }
266 *value = NULL;
267 return -1;
268 }
269
270 int drmSLLookupNeighbors(void *l, unsigned long key,
271 unsigned long *prev_key, void **prev_value,
272 unsigned long *next_key, void **next_value)
273 {
274 SkipListPtr list = (SkipListPtr)l;
275 SLEntryPtr update[SL_MAX_LEVEL + 1];
276 SLEntryPtr entry;
277 int retcode = 0;
278
279 entry = SLLocate(list, key, update);
280
281 *prev_key = *next_key = key;
282 *prev_value = *next_value = NULL;
283
284 if (update[0]) {
285 *prev_key = update[0]->key;
286 *prev_value = update[0]->value;
287 ++retcode;
288 if (update[0]->forward[0]) {
289 *next_key = update[0]->forward[0]->key;
290 *next_value = update[0]->forward[0]->value;
291 ++retcode;
292 }
293 }
294 return retcode;
295 }
296
297 int drmSLNext(void *l, unsigned long *key, void **value)
298 {
299 SkipListPtr list = (SkipListPtr)l;
300 SLEntryPtr entry;
301
302 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
303
304 entry = list->p0;
305
306 if (entry) {
307 list->p0 = entry->forward[0];
308 *key = entry->key;
309 *value = entry->value;
310 return 1;
311 }
312 list->p0 = NULL;
313 return 0;
314 }
315
316 int drmSLFirst(void *l, unsigned long *key, void **value)
317 {
318 SkipListPtr list = (SkipListPtr)l;
319
320 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
321
322 list->p0 = list->head->forward[0];
323 return drmSLNext(list, key, value);
324 }
325
326 /* Dump internal data structures for debugging. */
327 void drmSLDump(void *l)
328 {
329 SkipListPtr list = (SkipListPtr)l;
330 SLEntryPtr entry;
331 int i;
332
333 if (list->magic != SL_LIST_MAGIC) {
334 printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
335 list->magic, SL_LIST_MAGIC);
336 return;
337 }
338
339 printf("Level = %d, count = %d\n", list->level, list->count);
340 for (entry = list->head; entry; entry = entry->forward[0]) {
341 if (entry->magic != SL_ENTRY_MAGIC) {
342 printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
343 list->magic, SL_ENTRY_MAGIC);
344 }
345 printf("\nEntry %p <0x%08lx, %p> has %2d levels\n",
346 entry, entry->key, entry->value, entry->levels);
347 for (i = 0; i < entry->levels; i++) {
348 if (entry->forward[i]) {
349 printf(" %2d: %p <0x%08lx, %p>\n",
350 i,
351 entry->forward[i],
352 entry->forward[i]->key,
353 entry->forward[i]->value);
354 } else {
355 printf(" %2d: %p\n", i, entry->forward[i]);
356 }
357 }
358 }
359 }
360
361 #if SL_MAIN
362 static void print(SkipListPtr list)
363 {
364 unsigned long key;
365 void *value;
366
367 if (drmSLFirst(list, &key, &value)) {
368 do {
369 printf("key = %5lu, value = %p\n", key, value);
370 } while (drmSLNext(list, &key, &value));
371 }
372 }
373
374 static double do_time(int size, int iter)
375 {
376 SkipListPtr list;
377 int i, j;
378 unsigned long keys[1000000];
379 unsigned long previous;
380 unsigned long key;
381 void *value;
382 struct timeval start, stop;
383 double usec;
384 SL_RANDOM_DECL;
385
386 SL_RANDOM_INIT(12345);
387
388 list = drmSLCreate();
389
390 for (i = 0; i < size; i++) {
391 keys[i] = SL_RANDOM;
392 drmSLInsert(list, keys[i], NULL);
393 }
394
395 previous = 0;
396 if (drmSLFirst(list, &key, &value)) {
397 do {
398 if (key <= previous) {
399 printf( "%lu !< %lu\n", previous, key);
400 }
401 previous = key;
402 } while (drmSLNext(list, &key, &value));
403 }
404
405 gettimeofday(&start, NULL);
406 for (j = 0; j < iter; j++) {
407 for (i = 0; i < size; i++) {
408 if (drmSLLookup(list, keys[i], &value))
409 printf("Error %lu %d\n", keys[i], i);
410 }
411 }
412 gettimeofday(&stop, NULL);
413
414 usec = (double)(stop.tv_sec * 1000000 + stop.tv_usec
415 - start.tv_sec * 1000000 - start.tv_usec) / (size * iter);
416
417 printf("%0.2f microseconds for list length %d\n", usec, size);
418
419 drmSLDestroy(list);
420
421 return usec;
422 }
423
424 static void print_neighbors(void *list, unsigned long key)
425 {
426 unsigned long prev_key = 0;
427 unsigned long next_key = 0;
428 void *prev_value;
429 void *next_value;
430 int retval;
431
432 retval = drmSLLookupNeighbors(list, key,
433 &prev_key, &prev_value,
434 &next_key, &next_value);
435 printf("Neighbors of %5lu: %d %5lu %5lu\n",
436 key, retval, prev_key, next_key);
437 }
438
439 int main(void)
440 {
441 SkipListPtr list;
442 double usec, usec2, usec3, usec4;
443
444 list = drmSLCreate();
445 printf( "list at %p\n", list);
446
447 print(list);
448 printf("\n==============================\n\n");
449
450 drmSLInsert(list, 123, NULL);
451 drmSLInsert(list, 213, NULL);
452 drmSLInsert(list, 50, NULL);
453 print(list);
454 printf("\n==============================\n\n");
455
456 print_neighbors(list, 0);
457 print_neighbors(list, 50);
458 print_neighbors(list, 51);
459 print_neighbors(list, 123);
460 print_neighbors(list, 200);
461 print_neighbors(list, 213);
462 print_neighbors(list, 256);
463 printf("\n==============================\n\n");
464
465 drmSLDelete(list, 50);
466 print(list);
467 printf("\n==============================\n\n");
468
469 drmSLDump(list);
470 drmSLDestroy(list);
471 printf("\n==============================\n\n");
472
473 usec = do_time(100, 10000);
474 usec2 = do_time(1000, 500);
475 printf("Table size increased by %0.2f, search time increased by %0.2f\n",
476 1000.0/100.0, usec2 / usec);
477
478 usec3 = do_time(10000, 50);
479 printf("Table size increased by %0.2f, search time increased by %0.2f\n",
480 10000.0/100.0, usec3 / usec);
481
482 usec4 = do_time(100000, 4);
483 printf("Table size increased by %0.2f, search time increased by %0.2f\n",
484 100000.0/100.0, usec4 / usec);
485
486 return 0;
487 }
488 #endif