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

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [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 <l4/lib/bit.h>
7
#include INC_GLUE(memory.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
        /* Return bit just set */
35
        if (success)
36
                return i;
37
        else
38
                return -1;
39
}
40
 
41
int check_and_clear_bit(u32 *word, int bit)
42
{
43
        /* Check that bit was set */
44
        if (word[BITWISE_GETWORD(bit)] & BITWISE_GETBIT(bit)) {
45
                word[BITWISE_GETWORD(bit)] &= ~BITWISE_GETBIT(bit);
46
                return 0;
47
        } else {
48
                //printf("Trying to clear already clear bit\n");
49
                return -1;
50
        }
51
}
52
 
53
int check_and_set_bit(u32 *word, int bit)
54
{
55
        /* Check that bit was clear */
56
        if (!(word[BITWISE_GETWORD(bit)] & BITWISE_GETBIT(bit))) {
57
                word[BITWISE_GETWORD(bit)] |= BITWISE_GETBIT(bit);
58
                return 0;
59
        } else {
60
                //printf("Trying to set already set bit\n");
61
                return -1;
62
        }
63
}
64
 

powered by: WebSVN 2.1.0

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