openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

bitop.h (9806B)


      1 /*
      2  * 
      3  * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      4  * 
      5  * Copyright 2007-2019 Broadcom Inc. All rights reserved.
      6  *
      7  * Bit Array Operations
      8  */
      9 
     10 #ifndef _SHR_BITOP_H
     11 #define _SHR_BITOP_H
     12 
     13 #include <sal/types.h>
     14 
     15 /* Base type for declarations */
     16 #define    SHR_BITDCL        uint32
     17 #define    SHR_BITWID        32
     18 
     19 /* (internal) Number of SHR_BITDCLs needed to contain _max bits */
     20 #define    _SHR_BITDCLSIZE(_max)    (((_max) + SHR_BITWID - 1) / SHR_BITWID)
     21 
     22 /* Size for giving to malloc and memset to handle _max bits */
     23 #define    SHR_BITALLOCSIZE(_max) (_SHR_BITDCLSIZE(_max) * sizeof (SHR_BITDCL))
     24 
     25 
     26 /* (internal) Number of SHR_BITDCLs needed to contain from start bit to start bit + range */
     27 #define _SHR_BITDCLSIZE_FROM_START_BIT(_start_bit, _range) (_range + _start_bit -1)/SHR_BITWID - _start_bit/SHR_BITWID + 1
     28 
     29 /* Size of SHR_BITDCLs needed to contain from start bit to start bit + range.
     30    Needed when you want to do autosync */
     31 #define SHR_BITALLOCSIZE_FROM_START_BIT(_start_bit, _range) (_SHR_BITDCLSIZE_FROM_START_BIT(_start_bit, _range) * sizeof (SHR_BITDCL))
     32 
     33 
     34 
     35 /* Declare bit array _n of size _max bits */
     36 #define    SHR_BITDCLNAME(_n, _max) SHR_BITDCL    _n[_SHR_BITDCLSIZE(_max)]
     37 /* Declare bit array _n of size _max bits, and clear it */
     38 #define    SHR_BIT_DCL_CLR_NAME(_n, _max) SHR_BITDCL _n[_SHR_BITDCLSIZE(_max)] = {0}
     39 
     40 /* (internal) Generic operation macro on bit array _a, with bit _b */
     41 #define    _SHR_BITOP(_a, _b, _op)    \
     42         (((_a)[(_b) / SHR_BITWID]) _op (1U << ((_b) % SHR_BITWID)))
     43 
     44 /* Specific operations */
     45 #define    SHR_BITGET(_a, _b)    _SHR_BITOP(_a, _b, &)
     46 #define    SHR_BITSET(_a, _b)    _SHR_BITOP(_a, _b, |=)
     47 #define    SHR_BITCLR(_a, _b)    _SHR_BITOP(_a, _b, &= ~)
     48 #define    SHR_BITWRITE(_a, _b, _val)    ((_val) ? SHR_BITSET(_a, _b) : SHR_BITCLR(_a, _b))
     49 #define    SHR_BIT_ITER(_a, _max, _b)            \
     50            for ((_b) = 0; (_b) < (_max); (_b)++) \
     51                if ((_a)[(_b) / SHR_BITWID] == 0) \
     52                    (_b) += (SHR_BITWID - 1);     \
     53                else if (SHR_BITGET((_a), (_b)))
     54 
     55 
     56 /* clear _c bits starting from _b in bit array _a */
     57 extern void shr_bitop_range_clear(SHR_BITDCL *a, CONST int b, CONST int c);
     58 #define SHR_BITCLR_RANGE(_a, _b, _c)            \
     59     (shr_bitop_range_clear(_a, _b, _c))
     60 
     61 /* set _c bits starting from _b in bit array _a */
     62 extern void shr_bitop_range_set(SHR_BITDCL *a, CONST int b, CONST int c);
     63 #define SHR_BITSET_RANGE(_a, _b, _c)            \
     64     (shr_bitop_range_set(_a, _b, _c))
     65 
     66 /*
     67  * Copy _num_bits bits from bit array _src offset _src_offset to bit array _dest offset _dest_offset
     68  * There should be no overlap between source _src and desstination _dest
     69  * _dest[_dest_offset:_dest_offset + _num_bits] = _src[_src_offset:_src_offset + _num_bits]
     70  */
     71 extern void shr_bitop_range_copy(SHR_BITDCL *a,
     72                                  CONST int b,
     73                                  CONST SHR_BITDCL *c,
     74                                  CONST int d,
     75                                  CONST int e);
     76 #define SHR_BITCOPY_RANGE(_dest, _dest_offset,_src, _src_offset, _num_bits)   \
     77     (shr_bitop_range_copy(_dest, _dest_offset, _src, _src_offset, _num_bits))
     78 
     79 /* Result is 0 only if all bits in the range are 0 */
     80 #define SHR_BITTEST_RANGE(_bits, _first, _bit_count, _result) \
     81     (_result) = !(shr_bitop_range_null(_bits, _first, _bit_count))
     82 
     83 extern void shr_bitop_range_and(CONST SHR_BITDCL *bits1,
     84                                 CONST SHR_BITDCL *bits2,
     85                                 CONST int first,
     86                                 CONST int bit_count,
     87                                 SHR_BITDCL *dest);
     88 extern void shr_bitop_range_or(CONST SHR_BITDCL *bits1,
     89                                CONST SHR_BITDCL *bits2,
     90                                CONST int first,
     91                                CONST int bit_count,
     92                                SHR_BITDCL *dest);
     93 extern void shr_bitop_range_xor(CONST SHR_BITDCL *bits1,
     94                                 CONST SHR_BITDCL *bits2,
     95                                 CONST int first,
     96                                 CONST int bit_count,
     97                                 SHR_BITDCL *dest);
     98 extern void shr_bitop_range_remove(CONST SHR_BITDCL *bits1,
     99                                    CONST SHR_BITDCL *bits2,
    100                                    CONST int first,
    101                                    CONST int bit_count,
    102                                    SHR_BITDCL *dest);
    103 
    104 #define SHR_BITAND_RANGE(_bits1, _bits2, _first, _bit_count, _dest) \
    105     (shr_bitop_range_and(_bits1, _bits2, _first, _bit_count, _dest))
    106 
    107 #define SHR_BITOR_RANGE(_bits1, _bits2, _first, _bit_count, _dest) \
    108     (shr_bitop_range_or(_bits1, _bits2, _first, _bit_count, _dest))
    109 
    110 #define SHR_BITXOR_RANGE(_bits1, _bits2, _first, _bit_count, _dest) \
    111     (shr_bitop_range_xor(_bits1, _bits2, _first, _bit_count, _dest))
    112 
    113 #define SHR_BITREMOVE_RANGE(_bits1, _bits2, _first, _bit_count, _dest) \
    114     (shr_bitop_range_remove(_bits1, _bits2, _first, _bit_count, _dest))
    115 
    116 extern void shr_bitop_range_negate(CONST SHR_BITDCL *bits1,
    117                                    CONST int first,
    118                                    CONST int bit_count,
    119                                    SHR_BITDCL *dest);
    120 
    121 #define SHR_BITNEGATE_RANGE(_bits1, _first, _bit_count, _dest) \
    122     (shr_bitop_range_negate(_bits1, _first, _bit_count, _dest))
    123 
    124 extern int shr_bitop_range_null(CONST SHR_BITDCL *a, CONST int first, CONST int bit_count);
    125 extern int shr_bitop_range_eq(CONST SHR_BITDCL *bits1, CONST SHR_BITDCL *bits2,
    126                          CONST int first, CONST int range);
    127 extern void shr_bitop_range_count(CONST SHR_BITDCL *bits, CONST int first,
    128                                  CONST int range, int *count);
    129 
    130 #define SHR_BITNULL_RANGE(_bits, _first, _range) \
    131     (shr_bitop_range_null(_bits, _first, _range))
    132 #define SHR_BITEQ_RANGE(_bits1, _bits2, _first, _range) \
    133     (shr_bitop_range_eq(_bits1, _bits2, _first, _range))
    134 #define SHR_BITCOUNT_RANGE(_bits, _count, _first, _range) \
    135     shr_bitop_range_count(_bits, _first, _range, &(_count))
    136 
    137 extern int shr_bitop_str_decode(char *str_value,  SHR_BITDCL *dst_ptr, int max_words);
    138 
    139 /* the number of chars requires to represent a bitmap word in string with the following format :"0xffffffff "
    140  * not including '\0' at the end of the str */
    141 #define SHR_BITWORD_HEX_STR_CHARS 11
    142 
    143 /*
    144  * encoding a bitmap to string using the following format: "0x00000002 0x00000fff 0x00000023 0x00005869"
    145  * if source bitmap cannot fit into destination_str, destination_str will be filled with an empty string.
    146  * bit_count is the number of bits in source_bitmap
    147  * destination_str_max_length is the number of chars in destination_str
    148  */
    149 void
    150 shr_bitop_str_encode(
    151     SHR_BITDCL *source_bitmap,
    152     int bit_count,
    153     char *destination_str,
    154     int destination_str_max_length);
    155 
    156 /*Will return either -1 if number of bits is bigger or less than minimum 
    157   number of bits needed to represent given number.
    158   Will return postive number only if it is exactly the number of bits needed to represent a given number*/
    159 #define SHR_IS_REPRESENTIBLE_IN_D_BITS(D, N)                \
    160   (( N >= (1UL << (D - 1)) && N < (1UL << D)) ? D : -1)
    161 /**
    162  *  MACRO to return the minimum number of bits needed to
    163  *  represent a given number
    164  *  Can be used in precompiler time.
    165     */
    166 /* Each SHR_IS_REPRESENTIBLE_IN_D_BITS will return either "-1" or the number spoecified in the first input paramter
    167 This will result in the end in an equation (31 + 31*(-1) + MIN_NOF_BITS_TO_REPRESENT)*/
    168 #define SHR_BITS_TO_REPRESENT(N)                            \
    169   (N == 0 ? 1 : (31                                     \
    170                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 1, N)    \
    171                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 2, N)    \
    172                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 3, N)    \
    173                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 4, N)    \
    174                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 5, N)    \
    175                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 6, N)    \
    176                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 7, N)    \
    177                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 8, N)    \
    178                  + SHR_IS_REPRESENTIBLE_IN_D_BITS( 9, N)    \
    179                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(10, N)    \
    180                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(11, N)    \
    181                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(12, N)    \
    182                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(13, N)    \
    183                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(14, N)    \
    184                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(15, N)    \
    185                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(16, N)    \
    186                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(17, N)    \
    187                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(18, N)    \
    188                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(19, N)    \
    189                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(20, N)    \
    190                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(21, N)    \
    191                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(22, N)    \
    192                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(23, N)    \
    193                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(24, N)    \
    194                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(25, N)    \
    195                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(26, N)    \
    196                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(27, N)    \
    197                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(28, N)    \
    198                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(29, N)    \
    199                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(30, N)    \
    200                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(31, N)    \
    201                  + SHR_IS_REPRESENTIBLE_IN_D_BITS(32, N)    \
    202                  )                                      \
    203    )
    204 
    205 #endif    /* !_SHR_BITOP_H */