only bring in as many sdl things as are strictly necessary
[tangerine.git] / arch / common / hidd.radeon / bitmap.c
bloba65d6315920f242e4893df565a96de75476da433
1 /*
2 Copyright � 2003-2007, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
7 #include "bitmap.h"
8 #include <exec/types.h>
9 #include <proto/exec.h>
10 #include <aros/debug.h>
11 #include <exec/memory.h>
12 #include <exec/memheaderext.h>
14 LONG bfffo(ULONG val, UBYTE bitoffset)
16 val &= (0xffffffff >> bitoffset);
17 if (val)
18 return __builtin_clz(val);
19 else
20 return 32;
23 LONG bfflo(ULONG val, UBYTE bitoffset)
25 val &= (0xffffffff << (31-bitoffset));
26 if (val)
27 return 31-__builtin_ctz(val);
28 else
29 return -1;
32 LONG bfffz(ULONG val, UBYTE bitoffset)
34 return bfffo(~val, bitoffset);
37 LONG bfflz(ULONG val, UBYTE bitoffset)
39 return bfflo(~val, bitoffset);
42 ULONG bfset(ULONG data, UBYTE bitoffset, UBYTE bits)
44 ULONG mask = ~((1 << (32 - bits)) - 1);
45 mask >>= bitoffset;
46 return data | mask;
49 ULONG bfclr(ULONG data, UBYTE bitoffset, UBYTE bits)
51 ULONG mask = ~((1 << (32 - bits)) - 1);
52 mask >>= bitoffset;
53 return data & ~mask;
56 ULONG bfcnto(ULONG v)
58 ULONG const w = v - ((v >> 1) & 0x55555555); // temp
59 ULONG const x = (w & 0x33333333) + ((w >> 2) & 0x33333333); // temp
60 ULONG const c = ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
62 return c;
65 ULONG bfcntz(ULONG v)
67 return bfcnto(~v);
70 LONG bmffo(ULONG *bitmap, ULONG longs, LONG bitoffset)
72 ULONG *scan = bitmap;
73 ULONG err = 32*longs;
74 int longoffset, bit;
76 longoffset = bitoffset >> 5;
77 longs -= longoffset;
78 scan += longoffset;
80 bitoffset = bitoffset & 0x1F;
82 if (bitoffset != 0) {
83 if ((bit = bfffo(*scan, bitoffset)) < 32) {
84 return (bit + ((scan - bitmap) << 5));
86 scan++;
87 longs--;
90 while (longs-- > 0) {
91 if (*scan++ != 0) {
92 return (bfffo(*--scan,0) + ((scan - bitmap) << 5));
96 return (err);
99 LONG bmffz(ULONG *bitmap, ULONG longs, LONG bitoffset)
101 ULONG *scan = bitmap;
102 ULONG err = 32*longs;
103 int longoffset, bit;
105 longoffset = bitoffset >> 5;
106 longs -= longoffset;
107 scan += longoffset;
109 bitoffset = bitoffset & 0x1F;
111 if (bitoffset != 0) {
112 if ((bit = bfffz(*scan, bitoffset)) < 32) {
113 return (bit + ((scan - bitmap) << 5));
115 scan++;
116 longs--;
119 while (longs-- > 0) {
120 if (*scan++ != 0xFFFFFFFF) {
121 return (bfffz(*--scan,0) + ((scan - bitmap) << 5));
125 return (err);
128 LONG bmclr(ULONG *bitmap, ULONG longs, LONG bitoffset, LONG bits)
130 ULONG *scan = bitmap;
131 int longoffset;
132 int orgbits = bits;
134 longoffset = bitoffset >> 5;
135 longs -= longoffset;
136 scan += longoffset;
138 bitoffset = bitoffset & 0x1F;
140 if (bitoffset != 0) {
141 if (bits < 32) {
142 *scan = bfclr(*scan, bitoffset, bits);
143 } else {
144 *scan = bfclr(*scan, bitoffset, 32);
146 scan++;
147 longs--;
148 bits -= 32 - bitoffset;
151 while (bits > 0 && longs-- > 0) {
152 if (bits > 31) {
153 *scan++ = 0;
154 } else {
155 *scan = bfclr(*scan, 0, bits);
157 bits -= 32;
160 if (bits <= 0) {
161 return (orgbits);
163 return (orgbits - bits);
166 LONG bmset(ULONG *bitmap, ULONG longs, LONG bitoffset, LONG bits)
168 ULONG *scan = bitmap;
169 int longoffset;
170 int orgbits = bits;
172 longoffset = bitoffset >> 5;
173 longs -= longoffset;
174 scan += longoffset;
176 bitoffset = bitoffset & 0x1F;
178 if (bitoffset != 0) {
179 if (bits < 32) {
180 *scan = (bfset((*scan), bitoffset, bits));
181 } else {
182 *scan = (bfset((*scan), bitoffset, 32));
184 scan++;
185 longs--;
186 bits -= 32 - bitoffset;
189 while (bits > 0 && longs-- > 0) {
190 if (bits > 31) {
191 *scan++ = 0xFFFFFFFF;
192 } else {
193 *scan = (bfset((*scan), 0, bits));
195 bits -= 32;
198 if (bits <= 0) {
199 return (orgbits);
201 return (orgbits - bits);
204 int bmtstz(ULONG *bitmap, ULONG bitoffset, LONG bits)
206 LONG longoffset = bitoffset >> 5;
207 ULONG *scan = bitmap;
208 ULONG mask;
210 scan += longoffset;
211 bitoffset &= 0x1f;
213 if (bitoffset != 0)
215 if ((bits + bitoffset) < 32)
217 mask = (0xffffffff >> bitoffset) & (0xffffffff << (32 - (bits+bitoffset)));
218 bits=0;
220 else
222 mask = (0xffffffff >> bitoffset);
223 bits -= (32-bitoffset);
225 if ((mask & (*scan++)) != 0)
226 return 0;
229 while (bits > 0)
231 if (bits >= 32)
233 mask=0xffffffff;
234 bits -= 32;
236 else
238 mask = 0xffffffff << (32-bits);
239 bits = 0;
241 if ((mask & (*scan++)) != 0)
242 return 0;
245 return 1;
248 ULONG bmcnto(ULONG *bitmap, LONG bitoffset, LONG bits)
250 LONG longoffset = bitoffset >> 5;
251 ULONG *scan = bitmap;
252 ULONG count = 0;
253 ULONG mask;
255 scan += longoffset;
256 bitoffset &= 0x1f;
258 if (bitoffset != 0)
260 if ((bits + bitoffset) < 32)
262 mask = (0xffffffff >> bitoffset) & (0xffffffff << (32 - (bits+bitoffset)));
263 bits=0;
265 else
267 mask = (0xffffffff >> bitoffset);
268 bits -= (32-bitoffset);
270 count += bfcnto(*scan++ & mask);
273 while (bits > 0)
275 if (bits >= 32)
277 mask=0xffffffff;
278 bits -= 32;
280 else
282 mask = 0xffffffff << (32-bits);
283 bits = 0;
285 count += bfcnto(*scan++ & mask);
288 return count;
291 ULONG bmcntz(ULONG *bitmap, LONG bitoffset, LONG bits)
293 LONG longoffset = bitoffset >> 5;
294 ULONG *scan = bitmap;
295 ULONG count = 0;
296 ULONG mask;
298 scan += longoffset;
299 bitoffset &= 0x1f;
301 if (bitoffset != 0)
303 if ((bits + bitoffset) < 32)
305 mask = ~((0xffffffff >> bitoffset) & (0xffffffff << (32 - (bits+bitoffset))));
306 bits=0;
308 else
310 mask = ~(0xffffffff >> bitoffset);
311 bits -= (32-bitoffset);
314 count += bfcntz(*scan++ | mask);
317 while (bits > 0)
319 if (bits >= 32)
321 mask=0;
322 bits -= 32;
324 else
326 mask = ~(0xffffffff << (32-bits));
327 bits = 0;
330 count += bfcntz(*scan++ | mask);
333 return count;
336 void *mh_Alloc(struct MemHeaderExt *mhe, ULONG size, ULONG *flags)
338 return NULL;
341 void mh_Free(struct MemHeaderExt *mhe, APTR mem, ULONG size)
345 void *mh_AllocAbs(struct MemHeaderExt *mhe, APTR mem, ULONG size)
347 return NULL;
350 void *mh_ReAlloc(struct MemHeaderExt *mhe, APTR old, ULONG size)
352 return NULL;
355 ULONG mh_Avail(struct MemHeaderExt *mhe, ULONG flags)
357 struct ati_staticdata *sd = mhe->mhe_UserData;
358 ULONG size = 0;
360 Forbid();
362 if (flags & MEMF_TOTAL)
363 size = sd->Card.FbUsableSize;
364 else if (flags & MEMF_LARGEST)
366 ULONG ptr, newptr;
368 ptr = bmffz(sd->CardMemBmp, sd->CardMemSize, 0);
370 while (ptr < (sd->CardMemSize << 5))
372 ULONG tmpptr = bmffo(sd->CardMemBmp, sd->CardMemSize, ptr);
374 if ((tmpptr - ptr) > size)
375 size = tmpptr - ptr;
377 ptr = bmffz(sd->CardMemBmp, sd->CardMemSize, tmpptr);
380 size <<= 10;
382 else
383 size = bmcntz(sd->CardMemBmp, 0, sd->Card.FbUsableSize >> 10) << 10;
385 Permit();
387 return size;
390 void BitmapInit(struct ati_staticdata *sd)
393 * If Radeon chip has some video memory, create a bitmap representing all allocations.
394 * Divide whole memory into 1KB chunks
396 if (sd->Card.FbUsableSize)
398 sd->CardMemBmp = AllocPooled(sd->memPool, sd->Card.FbUsableSize >> 13);
400 sd->managedMem.mhe_MemHeader.mh_Node.ln_Type = NT_MEMORY;
401 sd->managedMem.mhe_MemHeader.mh_Node.ln_Name = "Radeon VRAM";
402 sd->managedMem.mhe_MemHeader.mh_Node.ln_Pri = -128;
403 sd->managedMem.mhe_MemHeader.mh_Attributes = MEMF_CHIP | MEMF_MANAGED | MEMF_PUBLIC;
405 sd->managedMem.mhe_UserData = sd;
407 sd->managedMem.mhe_Alloc = mh_Alloc;
408 sd->managedMem.mhe_Free = mh_Free;
409 sd->managedMem.mhe_AllocAbs = mh_AllocAbs;
410 sd->managedMem.mhe_ReAlloc = mh_ReAlloc;
411 sd->managedMem.mhe_Avail = mh_Avail;
413 Disable();
414 AddTail(&SysBase->MemList, &sd->managedMem);
415 Enable();
418 /* Number of ULONG's in bitmap */
419 sd->CardMemSize = sd->Card.FbUsableSize >> 15;
421 bug("[ATIBMP] Bitmap at %p, size %d bytes (%d bits)\n", sd->CardMemBmp, sd->CardMemSize << 2, sd->Card.FbUsableSize >> 10);
424 void BitmapFree(struct ati_staticdata *sd, ULONG ptr, ULONG size)
426 if (ptr + size < sd->Card.FbUsableSize)
428 bmclr(sd->CardMemBmp, sd->CardMemSize, ptr >> 10, (size + 1023) >> 10);
432 ULONG BitmapAlloc(struct ati_staticdata *sd, ULONG size)
434 ULONG ptr;
435 size = (size + 1023) >> 10;
437 ptr = bmffz(sd->CardMemBmp, sd->CardMemSize, 0);
439 D(bug("[ATIBMP] BitmapAlloc(%d)\n", size));
441 while (ptr <= (sd->CardMemSize << 5) + size)
443 D(bug("[ATIBMP] ptr=%08x\n", ptr));
445 if (bmtstz(sd->CardMemBmp, ptr, size))
447 bmset(sd->CardMemBmp, sd->CardMemSize, ptr, size);
448 break;
451 ptr = bmffo(sd->CardMemBmp, sd->CardMemSize, ptr);
452 ptr = bmffz(sd->CardMemBmp, sd->CardMemSize, ptr);
455 if (ptr > (sd->CardMemSize << 5) - size)
456 ptr = 0xffffffff;
457 else
458 ptr <<= 10;
460 return ptr;