2 Copyright © 2003-2007, The AROS Development Team. All rights reserved.
8 #include <exec/types.h>
9 #include <proto/exec.h>
10 #include <aros/debug.h>
12 LONG
bfffo(ULONG val
, UBYTE bitoffset
)
14 val
&= (0xffffffff >> bitoffset
);
16 return __builtin_clz(val
);
21 LONG
bfflo(ULONG val
, UBYTE bitoffset
)
23 val
&= (0xffffffff << (31-bitoffset
));
25 return 31-__builtin_ctz(val
);
30 LONG
bfffz(ULONG val
, UBYTE bitoffset
)
32 return bfffo(~val
, bitoffset
);
35 LONG
bfflz(ULONG val
, UBYTE bitoffset
)
37 return bfflo(~val
, bitoffset
);
40 ULONG
bfset(ULONG data
, UBYTE bitoffset
, UBYTE bits
)
42 ULONG mask
= ~((1 << (32 - bits
)) - 1);
47 ULONG
bfclr(ULONG data
, UBYTE bitoffset
, UBYTE bits
)
49 ULONG mask
= ~((1 << (32 - bits
)) - 1);
54 LONG
bmffo(ULONG
*bitmap
, ULONG longs
, LONG bitoffset
)
60 longoffset
= bitoffset
>> 5;
64 bitoffset
= bitoffset
& 0x1F;
67 if ((bit
= bfffo(*scan
, bitoffset
)) < 32) {
68 return (bit
+ ((scan
- bitmap
) << 5));
76 return (bfffo(*--scan
,0) + ((scan
- bitmap
) << 5));
83 LONG
bmffz(ULONG
*bitmap
, ULONG longs
, LONG bitoffset
)
89 longoffset
= bitoffset
>> 5;
93 bitoffset
= bitoffset
& 0x1F;
96 if ((bit
= bfffz(*scan
, bitoffset
)) < 32) {
97 return (bit
+ ((scan
- bitmap
) << 5));
103 while (longs
-- > 0) {
104 if (*scan
++ != 0xFFFFFFFF) {
105 return (bfffz(*--scan
,0) + ((scan
- bitmap
) << 5));
112 LONG
bmclr(ULONG
*bitmap
, ULONG longs
, LONG bitoffset
, LONG bits
)
114 ULONG
*scan
= bitmap
;
118 longoffset
= bitoffset
>> 5;
122 bitoffset
= bitoffset
& 0x1F;
124 if (bitoffset
!= 0) {
126 *scan
= bfclr(*scan
, bitoffset
, bits
);
128 *scan
= bfclr(*scan
, bitoffset
, 32);
132 bits
-= 32 - bitoffset
;
135 while (bits
> 0 && longs
-- > 0) {
139 *scan
= bfclr(*scan
, 0, bits
);
147 return (orgbits
- bits
);
150 LONG
bmset(ULONG
*bitmap
, ULONG longs
, LONG bitoffset
, LONG bits
)
152 ULONG
*scan
= bitmap
;
156 longoffset
= bitoffset
>> 5;
160 bitoffset
= bitoffset
& 0x1F;
162 if (bitoffset
!= 0) {
164 *scan
= (bfset((*scan
), bitoffset
, bits
));
166 *scan
= (bfset((*scan
), bitoffset
, 32));
170 bits
-= 32 - bitoffset
;
173 while (bits
> 0 && longs
-- > 0) {
175 *scan
++ = 0xFFFFFFFF;
177 *scan
= (bfset((*scan
), 0, bits
));
185 return (orgbits
- bits
);
188 int bmtstz(ULONG
*bitmap
, ULONG bitoffset
, LONG bits
)
190 LONG longoffset
= bitoffset
>> 5;
191 ULONG
*scan
= bitmap
;
199 if ((bits
+ bitoffset
) < 32)
201 mask
= (0xffffffff >> bitoffset
) & (0xffffffff << (32 - (bits
+bitoffset
)));
206 mask
= (0xffffffff >> bitoffset
);
207 bits
-= (32-bitoffset
);
209 if ((mask
& (*scan
++)) != 0)
222 mask
= 0xffffffff << (32-bits
);
225 if ((mask
& (*scan
++)) != 0)
232 void BitmapInit(struct ati_staticdata
*sd
)
235 * If Radeon chip has some video memory, create a bitmap representing all allocations.
236 * Divide whole memory into 1KB chunks
238 if (sd
->Card
.FbUsableSize
)
239 sd
->CardMemBmp
= AllocPooled(sd
->memPool
, sd
->Card
.FbUsableSize
>> 13);
241 /* Number of ULONG's in bitmap */
242 sd
->CardMemSize
= sd
->Card
.FbUsableSize
>> 15;
244 bug("[ATIBMP] Bitmap at %p, size %d bytes (%d bits)\n", sd
->CardMemBmp
, sd
->CardMemSize
<< 2, sd
->Card
.FbUsableSize
>> 10);
247 void BitmapFree(struct ati_staticdata
*sd
, ULONG ptr
, ULONG size
)
249 if (ptr
+ size
< sd
->Card
.FbUsableSize
)
251 bmclr(sd
->CardMemBmp
, sd
->CardMemSize
, ptr
>> 10, (size
+ 1023) >> 10);
255 ULONG
BitmapAlloc(struct ati_staticdata
*sd
, ULONG size
)
258 size
= (size
+ 1023) >> 10;
260 ptr
= bmffz(sd
->CardMemBmp
, sd
->CardMemSize
, 0);
262 D(bug("[ATIBMP] BitmapAlloc(%d)\n", size
));
264 while (ptr
<= (sd
->CardMemSize
<< 5) + size
)
266 D(bug("[ATIBMP] ptr=%08x\n", ptr
));
268 if (bmtstz(sd
->CardMemBmp
, ptr
, size
))
270 bmset(sd
->CardMemBmp
, sd
->CardMemSize
, ptr
, size
);
274 ptr
= bmffo(sd
->CardMemBmp
, sd
->CardMemSize
, ptr
);
275 ptr
= bmffz(sd
->CardMemBmp
, sd
->CardMemSize
, ptr
);
278 if (ptr
> (sd
->CardMemSize
<< 5) - size
)