OpenNSL API Guide and Reference Manual
bitop.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * (C) Copyright Broadcom Corporation 2013-2016
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  **********************************************************************
18  * File: bitop.h
19  * Details: Bit Array Operations
20  *********************************************************************/
21 
22 #ifndef _SHR_BITOP_H
23 #define _SHR_BITOP_H
24 
25 #include <sal/types.h>
26 
27 /* Base type for declarations */
28 #define SHR_BITDCL uint32
29 #define SHR_BITWID 32
30 
31 /* (internal) Number of SHR_BITDCLs needed to contain _max bits */
32 #define _SHR_BITDCLSIZE(_max) (((_max) + SHR_BITWID - 1) / SHR_BITWID)
33 
34 /* Size for giving to malloc and memset to handle _max bits */
35 #define SHR_BITALLOCSIZE(_max) (_SHR_BITDCLSIZE(_max) * sizeof (SHR_BITDCL))
36 
37 
38 /* (internal) Number of SHR_BITDCLs needed to contain from start bit to start bit + range */
39 #define _SHR_BITDCLSIZE_FROM_START_BIT(_start_bit, _range) (_range + _start_bit -1)/SHR_BITWID - _start_bit/SHR_BITWID + 1
40 
41 /* Size of SHR_BITDCLs needed to contain from start bit to start bit + range.
42  Needed when you want to do autosync */
43 #define SHR_BITALLOCSIZE_FROM_START_BIT(_start_bit, _range) (_SHR_BITDCLSIZE_FROM_START_BIT(_start_bit, _range) * sizeof (SHR_BITDCL))
44 
45 
46 
47 /* Declare bit array _n of size _max bits */
48 #define SHR_BITDCLNAME(_n, _max) SHR_BITDCL _n[_SHR_BITDCLSIZE(_max)]
49 /* Declare bit array _n of size _max bits, and clear it */
50 #define SHR_BIT_DCL_CLR_NAME(_n, _max) SHR_BITDCL _n[_SHR_BITDCLSIZE(_max)] = {0}
51 /* __doxy_func_body_end__ */
52 
53 /* (internal) Generic operation macro on bit array _a, with bit _b */
54 #define _SHR_BITOP(_a, _b, _op) \
55  (((_a)[(_b) / SHR_BITWID]) _op (1U << ((_b) % SHR_BITWID)))
56 
57 /* Specific operations */
58 #define SHR_BITGET(_a, _b) _SHR_BITOP(_a, _b, &)
59 #define SHR_BITSET(_a, _b) _SHR_BITOP(_a, _b, |=)
60 #define SHR_BITCLR(_a, _b) _SHR_BITOP(_a, _b, &= ~)
61 #define SHR_BITWRITE(_a, _b, _val) ((_val) ? SHR_BITSET(_a, _b) : SHR_BITCLR(_a, _b))
62 #define SHR_BIT_ITER(_a, _max, _b) \
63  for ((_b) = 0; (_b) < (_max); (_b)++) \
64  if ((_a)[(_b) / SHR_BITWID] == 0) \
65  (_b) += (SHR_BITWID - 1); \
66  else if (SHR_BITGET((_a), (_b)))
67 
68 
69 /* clear _c bits starting from _b in bit array _a */
70 extern void shr_bitop_range_clear(SHR_BITDCL *a, CONST int b, CONST int c);
71 #define SHR_BITCLR_RANGE(_a, _b, _c) \
72  (shr_bitop_range_clear(_a, _b, _c))
73 
74 /* set _c bits starting from _b in bit array _a */
75 extern void shr_bitop_range_set(SHR_BITDCL *a, CONST int b, CONST int c);
76 #define SHR_BITSET_RANGE(_a, _b, _c) \
77  (shr_bitop_range_set(_a, _b, _c))
78 
79 /*
80  * Copy _num_bits bits from bit array _src offset _src_offset to bit array _dest offset _dest_offset
81  * There should be no overlap between source _src and desstination _dest
82  * _dest[_dest_offset:_dest_offset + _num_bits] = _src[_src_offset:_src_offset + _num_bits]
83  */
84 extern void shr_bitop_range_copy(SHR_BITDCL *a,
85  CONST int b,
86  CONST SHR_BITDCL *c,
87  CONST int d,
88  CONST int e);
89 #define SHR_BITCOPY_RANGE(_dest, _dest_offset,_src, _src_offset, _num_bits) \
90  (shr_bitop_range_copy(_dest, _dest_offset, _src, _src_offset, _num_bits))
91 
92 /* Result is 0 only if all bits in the range are 0 */
93 #define SHR_BITTEST_RANGE(_bits, _first, _bit_count, _result) \
94  (_result) = !(shr_bitop_range_null(_bits, _first, _bit_count))
95 
96 extern void shr_bitop_range_and(CONST SHR_BITDCL *bits1,
97  CONST SHR_BITDCL *bits2,
98  CONST int first,
99  CONST int bit_count,
100  SHR_BITDCL *dest);
101 extern void shr_bitop_range_or(CONST SHR_BITDCL *bits1,
102  CONST SHR_BITDCL *bits2,
103  CONST int first,
104  CONST int bit_count,
105  SHR_BITDCL *dest);
106 extern void shr_bitop_range_xor(CONST SHR_BITDCL *bits1,
107  CONST SHR_BITDCL *bits2,
108  CONST int first,
109  CONST int bit_count,
110  SHR_BITDCL *dest);
111 extern void shr_bitop_range_remove(CONST SHR_BITDCL *bits1,
112  CONST SHR_BITDCL *bits2,
113  CONST int first,
114  CONST int bit_count,
115  SHR_BITDCL *dest);
116 
117 #define SHR_BITAND_RANGE(_bits1, _bits2, _first, _bit_count, _dest) \
118  (shr_bitop_range_and(_bits1, _bits2, _first, _bit_count, _dest))
119 
120 #define SHR_BITOR_RANGE(_bits1, _bits2, _first, _bit_count, _dest) \
121  (shr_bitop_range_or(_bits1, _bits2, _first, _bit_count, _dest))
122 
123 #define SHR_BITXOR_RANGE(_bits1, _bits2, _first, _bit_count, _dest) \
124  (shr_bitop_range_xor(_bits1, _bits2, _first, _bit_count, _dest))
125 
126 #define SHR_BITREMOVE_RANGE(_bits1, _bits2, _first, _bit_count, _dest) \
127  (shr_bitop_range_remove(_bits1, _bits2, _first, _bit_count, _dest))
128 
129 extern void shr_bitop_range_negate(CONST SHR_BITDCL *bits1,
130  CONST int first,
131  CONST int bit_count,
132  SHR_BITDCL *dest);
133 
134 #define SHR_BITNEGATE_RANGE(_bits1, _first, _bit_count, _dest) \
135  (shr_bitop_range_negate(_bits1, _first, _bit_count, _dest))
136 
137 extern int shr_bitop_range_null(CONST SHR_BITDCL *a, CONST int first, CONST int bit_count);
138 extern int shr_bitop_range_eq(CONST SHR_BITDCL *bits1, CONST SHR_BITDCL *bits2,
139  CONST int first, CONST int range);
140 extern void shr_bitop_range_count(CONST SHR_BITDCL *bits, CONST int first,
141  CONST int range, int *count);
142 #define SHR_BITNULL_RANGE(_bits, _first, _range) \
143  (shr_bitop_range_null(_bits, _first, _range))
144 #define SHR_BITEQ_RANGE(_bits1, _bits2, _first, _range) \
145  (shr_bitop_range_eq(_bits1, _bits2, _first, _range))
146 #define SHR_BITCOUNT_RANGE(_bits, _count, _first, _range) \
147  shr_bitop_range_count(_bits, _first, _range, &(_count))
148 
149 #endif /* !_SHR_BITOP_H */