OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [libl4/] [src/] [lib/] [bit.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Bit manipulation functions.
3
 *
4
 * Copyright (C) 2007 Bahadir Balban
5
 */
6
#include <l4lib/lib/bit.h>
7
#include <stdio.h>
8
#include <l4/macros.h>
9
#include INC_GLUE(memory.h)
10
 
11
/* Emulation of ARM's CLZ (count leading zeroes) instruction */
12
unsigned int __clz(unsigned int bitvector)
13
{
14
        unsigned int x = 0;
15
        while((!(bitvector & ((unsigned)1 << 31))) && (x < 32)) {
16
                bitvector <<= 1;
17
                x++;
18
        }
19
        return x;
20
}
21
 
22
int find_and_set_first_free_bit(u32 *word, unsigned int limit)
23
{
24
        int success = 0;
25
        int i;
26
 
27
        for(i = 0; i < limit; i++) {
28
                /* Find first unset bit */
29
                if (!(word[BITWISE_GETWORD(i)] & BITWISE_GETBIT(i))) {
30
                        /* Set it */
31
                        word[BITWISE_GETWORD(i)] |= BITWISE_GETBIT(i);
32
                        success = 1;
33
                        break;
34
                }
35
        }
36
        /* Return bit just set */
37
        if (success)
38
                return i;
39
        else
40
                return -1;
41
}
42
 
43
int find_and_set_first_free_contig_bits(u32 *word,  unsigned int limit,
44
                                        int nbits)
45
{
46
        int i = 0, first = 0, last = 0, found = 0;
47
 
48
        /* Can't allocate more than the limit */
49
        if (nbits > limit)
50
                return -1;
51
 
52
        /* This is a state machine that checks n contiguous free bits. */
53
        while (i + nbits <= limit) {
54
                first = i;
55
                last  = i;
56
                while (!(word[BITWISE_GETWORD(last)] & BITWISE_GETBIT(last))) {
57
                        last++;
58
                        i++;
59
                        if (last == first + nbits) {
60
                                found = 1;
61
                                break;
62
                        }
63
                }
64
                if (found)
65
                        break;
66
                i++;
67
        }
68
 
69
        /* If found, set the bits */
70
        if (found) {
71
                for (int x = first; x < first + nbits; x++)
72
                        word[BITWISE_GETWORD(x)] |= BITWISE_GETBIT(x);
73
                return first;
74
        } else
75
                return -1;
76
}
77
 
78
int check_and_clear_bit(u32 *word, int bit)
79
{
80
        /* Check that bit was set */
81
        if (word[BITWISE_GETWORD(bit)] & BITWISE_GETBIT(bit)) {
82
                word[BITWISE_GETWORD(bit)] &= ~BITWISE_GETBIT(bit);
83
                return 0;
84
        } else {
85
                printf("Trying to clear already clear bit\n");
86
                return -1;
87
        }
88
}
89
 
90
int check_and_set_bit(u32 *word, int bit)
91
{
92
        /* Check that bit was clear */
93
        if (!(word[BITWISE_GETWORD(bit)] & BITWISE_GETBIT(bit))) {
94
                word[BITWISE_GETWORD(bit)] |= BITWISE_GETBIT(bit);
95
                return 0;
96
        } else {
97
                //printf("Trying to set already set bit\n");
98
                return -1;
99
        }
100
}
101
 
102
int check_and_clear_contig_bits(u32 *word, int first, int nbits)
103
{
104
        for (int i = first; i < first + nbits; i++)
105
                if (check_and_clear_bit(word, i) < 0)
106
                        return -1;
107
        return 0;
108
}
109
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.