1 /* bits.h
2 
3    Copyright (c) 2003-2021 HandBrake Team
4    This file is part of the HandBrake source code
5    Homepage: <http://handbrake.fr/>.
6    It may be used under the terms of the GNU General Public License v2.
7    For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8  */
9 
10 #ifndef HANDBRAKE_BITS_H
11 #define HANDBRAKE_BITS_H
12 
13 static inline int
allbits_set(uint32_t * bitmap,int num_words)14 allbits_set(uint32_t *bitmap, int num_words)
15 {
16     unsigned int i;
17     for( i = 0; i < num_words; i++ )
18     {
19         if( bitmap[i] != 0xFFFFFFFF )
20             return (0);
21     }
22     return (1);
23 }
24 
25 static inline int
bit_is_set(uint32_t * bit_map,int bit_pos)26 bit_is_set( uint32_t *bit_map, int bit_pos )
27 {
28     return( ( bit_map[bit_pos >> 5] & (0x1 << (bit_pos & 0x1F) ) ) != 0 );
29 }
30 
31 static inline int
bit_is_clear(uint32_t * bit_map,int bit_pos)32 bit_is_clear( uint32_t *bit_map, int bit_pos )
33 {
34     return( ( bit_map[bit_pos >> 5] & ( 0x1 << (bit_pos & 0x1F) )  ) == 0 );
35 }
36 
37 static inline void
bit_set(uint32_t * bit_map,int bit_pos)38 bit_set( uint32_t *bit_map, int bit_pos )
39 {
40     bit_map[bit_pos >> 5] |= 0x1 << (bit_pos & 0x1F);
41 }
42 
43 static inline void
bit_clear(uint32_t * bit_map,int bit_pos)44 bit_clear(uint32_t *bit_map, int bit_pos)
45 {
46     bit_map[bit_pos >> 5] &= ~( 0x1 << ( bit_pos & 0x1F ) );
47 }
48 
49 static inline void
bit_nclear(uint32_t * bit_map,int start_pos,int stop_pos)50 bit_nclear(uint32_t *bit_map, int start_pos, int stop_pos)
51 {
52     int start_word = start_pos >> 5;
53     int stop_word  = stop_pos >> 5;
54 
55     if ( start_word == stop_word )
56     {
57 
58         bit_map[start_word] &= ( ( 0x7FFFFFFF >> ( 31 - (start_pos & 0x1F ) ) )
59                              |  ( 0xFFFFFFFE << ( stop_pos & 0x1F ) ) );
60     }
61     else
62     {
63         bit_map[start_word] &= ( 0x7FFFFFFF >> ( 31 - ( start_pos & 0x1F ) ) );
64         while (++start_word < stop_word)
65             bit_map[start_word] = 0;
66         bit_map[stop_word]  &= 0xFFFFFFFE << ( stop_pos & 0x1F );
67     }
68 }
69 
70 static inline void
bit_nset(uint32_t * bit_map,int start_pos,int stop_pos)71 bit_nset(uint32_t *bit_map, int start_pos, int stop_pos)
72 {
73     int start_word = start_pos >> 5;
74     int stop_word  = stop_pos >> 5;
75 
76     if ( start_word == stop_word )
77     {
78         bit_map[start_word] |= ( ( 0xFFFFFFFF << ( start_pos & 0x1F ) )
79                              &  ( 0xFFFFFFFF >> ( 31 - ( stop_pos & 0x1F ) ) ) );
80     }
81     else
82     {
83         bit_map[start_word] |= 0xFFFFFFFF << ( start_pos & 0x1F );
84         while (++start_word < stop_word)
85             bit_map[start_word] = 0xFFFFFFFF;
86         bit_map[stop_word]  |= 0xFFFFFFFF >> ( 31 - ( stop_pos & 0x1F ) );
87     }
88 }
89 
90 #endif /* HANDBRAKE_BITS_H */
91