summaryrefslogtreecommitdiffstats
path: root/libhb/bits.h
blob: 150a8db6014cfbefa889730d7cc8d987153c8782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* bits.h

   Copyright (c) 2003-2018 HandBrake Team
   This file is part of the HandBrake source code
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License v2.
   For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
 */

#ifndef HB_BITS_H
#define HB_BITS_H

static inline int
allbits_set(uint32_t *bitmap, int num_words)
{
    unsigned int i;
    for( i = 0; i < num_words; i++ )
    {
        if( bitmap[i] != 0xFFFFFFFF )
            return (0);
    }
    return (1);
}

static inline int
bit_is_set( uint32_t *bit_map, int bit_pos )
{
    return( ( bit_map[bit_pos >> 5] & (0x1 << (bit_pos & 0x1F) ) ) != 0 );
}

static inline int
bit_is_clear( uint32_t *bit_map, int bit_pos )
{
    return( ( bit_map[bit_pos >> 5] & ( 0x1 << (bit_pos & 0x1F) )  ) == 0 );
}

static inline void
bit_set( uint32_t *bit_map, int bit_pos )
{
    bit_map[bit_pos >> 5] |= 0x1 << (bit_pos & 0x1F);
}

static inline void
bit_clear(uint32_t *bit_map, int bit_pos)
{
    bit_map[bit_pos >> 5] &= ~( 0x1 << ( bit_pos & 0x1F ) );
}

static inline void
bit_nclear(uint32_t *bit_map, int start_pos, int stop_pos)
{
    int start_word = start_pos >> 5;
    int stop_word  = stop_pos >> 5;

    if ( start_word == stop_word )
    {

        bit_map[start_word] &= ( ( 0x7FFFFFFF >> ( 31 - (start_pos & 0x1F ) ) )
                             |  ( 0xFFFFFFFE << ( stop_pos & 0x1F ) ) );
    }
    else
    {
        bit_map[start_word] &= ( 0x7FFFFFFF >> ( 31 - ( start_pos & 0x1F ) ) );
        while (++start_word < stop_word)
            bit_map[start_word] = 0;
        bit_map[stop_word]  &= 0xFFFFFFFE << ( stop_pos & 0x1F );
    }
}

static inline void
bit_nset(uint32_t *bit_map, int start_pos, int stop_pos)
{
    int start_word = start_pos >> 5;
    int stop_word  = stop_pos >> 5;

    if ( start_word == stop_word )
    {
        bit_map[start_word] |= ( ( 0xFFFFFFFF << ( start_pos & 0x1F ) )
                             &  ( 0xFFFFFFFF >> ( 31 - ( stop_pos & 0x1F ) ) ) );
    }
    else
    {
        bit_map[start_word] |= 0xFFFFFFFF << ( start_pos & 0x1F );
        while (++start_word < stop_word)
            bit_map[start_word] = 0xFFFFFFFF;
        bit_map[stop_word]  |= 0xFFFFFFFF >> ( 31 - ( stop_pos & 0x1F ) );
    }
}

#endif /* HB_BITS_H */