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

Subversion Repositories c0or1k

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

powered by: WebSVN 2.1.0

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