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

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [mm0/] [tests/] [idpool_test/] [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 "bit.h"
7
#include <stdio.h>
8
 
9
/* Emulation of ARM's CLZ (count leading zeroes) instruction */
10
unsigned int __clz(unsigned int bitvector)
11
{
12
        unsigned int x = 0;
13
        while((!(bitvector & ((unsigned)1 << 31))) && (x < 32)) {
14
                bitvector <<= 1;
15
                x++;
16
        }
17
        return x;
18
}
19
 
20
int find_and_set_first_free_bit(u32 *word, unsigned int limit)
21
{
22
        int success = 0;
23
        int i;
24
 
25
        for(i = 0; i < limit; i++) {
26
                /* Find first unset bit */
27
                if (!(word[BITWISE_GETWORD(i)] & BITWISE_GETBIT(i))) {
28
                        /* Set it */
29
                        word[BITWISE_GETWORD(i)] |= BITWISE_GETBIT(i);
30
                        success = 1;
31
                        break;
32
                }
33
        }
34
 
35
        /* Return bit just set */
36
        if (success)
37
                return i;
38
        else
39
                return -1;
40
}
41
 
42
int find_and_set_first_free_contig_bits(u32 *word, unsigned int limit,
43
                                        int nbits)
44
{
45
        int i = 0, first = 0, last = 0, found = 0;
46
 
47
        /* Can't allocate more than the limit */
48
        if (nbits > limit)
49
                return -1;
50
 
51
        /* This is a state machine that checks n contiguous free bits. */
52
        while (i < limit) {
53
                first = i;
54
                last  = i;
55
                while (!(word[BITWISE_GETWORD(last)] & BITWISE_GETBIT(last))) {
56
                        last++;
57
                        i++;
58
                        if (last == first + nbits) {
59
                                found = 1;
60
                                break;
61
                        }
62
                        if (i == limit)
63
                                break;
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_clear_contig_bits(u32 *word, int first, int nbits)
92
{
93
        for (int i = first; i < first + nbits; i++)
94
                if (check_and_clear_bit(word, i) < 0)
95
                        return -1;
96
        return 0;
97
}
98
 

powered by: WebSVN 2.1.0

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