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

bits_bytes_macros.h (4777B)


      1 /* 
      2  * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      3  * 
      4  * Copyright 2007-2020 Broadcom Inc. All rights reserved.
      5 */
      6 #ifndef BITS_BYTES_MACROS_INCLUDED
      7 /* { */
      8 #define BITS_BYTES_MACROS_INCLUDED
      9 
     10 
     11 
     12 #define BITS_IN_BYTE    8
     13 #define BITS_IN_LONG    32
     14 
     15 
     16 #ifndef __KERNEL__ /* for kernel, already defined in bitops.h */
     17 #define BIT(x)          (1<<(x))
     18 #endif
     19 #define RBIT(x)         (~(1<<(x)))
     20 
     21 #define VAL_MIN(x,y) ( (x)<(y)? (x) : (y) )
     22 
     23 /*******************************************
     24  * SET ALL BITS, BEGINNING AT BIT X, TO 1  *
     25  */
     26 #define FROM_BIT(x)     (RBIT(x) + 1)
     27 
     28 /*******************************************
     29  * SET ALL BITS UP TO BIT X (NOT INCLUDING *
     30  * BIT X) TO 1.                            *
     31  */
     32 #define UPTO_BIT(x)     (~(FROM_BIT(x)))
     33 
     34 /***************************************************
     35 * MACROS FOR ACCESSING SPECIFIC BYTES              *
     36  */
     37 
     38 /***************************************************
     39  * MACROS TO COMPUTE THE UI BITS              *
     40  */
     41 /* All the bits up until bit x with a verification if x > 31 */
     42 #define UNTIL_BIT(x)               (((x)>= (BITS_IN_LONG-1))? 0xffffffff : ((unsigned  long) (1<<((VAL_MIN((x)%BITS_IN_LONG, 30))+1))-1))
     43 /* All the bits from y to x */
     44 #define BIT_UP_RANGE(x,y)          (UNTIL_BIT(x)-UNTIL_BIT(y))
     45 
     46 /* For the UI, the right output in the bit entry for a global number num */
     47 #define BIT_N(num, bit) \
     48   ((num >= (BITS_IN_LONG * (bit+1))) || (num < (BITS_IN_LONG * bit))) ? 0 : BIT(num % BITS_IN_LONG) 
     49 
     50 /* The 16-entry array for the UI function or parameter*/
     51 #define BIT_UI_16(num) \
     52  {BIT_N(num, 0), BIT_N(num, 1), BIT_N(num, 2), BIT_N(num, 3), BIT_N(num, 4), BIT_N(num, 5), BIT_N(num, 6), BIT_N(num, 7), BIT_N(num, 8), BIT_N(num, 9), BIT_N(num, 10), BIT_N(num, 11), BIT_N(num, 12), BIT_N(num, 13), BIT_N(num, 14), BIT_N(num, 15)}
     53 
     54 #define BIT_N_RNG(nmin, nmax, bit) \
     55   (nmax < (BITS_IN_LONG * bit)) ? 0 : ((nmin >= (BITS_IN_LONG * (bit+1))) ? 0 : ((nmin < (BITS_IN_LONG * bit))? UNTIL_BIT((nmax>=(BITS_IN_LONG * (bit+1)))? BITS_IN_LONG:(nmax%BITS_IN_LONG)) : BIT_UP_RANGE(((nmax>=(BITS_IN_LONG * (bit+1)))? BITS_IN_LONG:(nmax%BITS_IN_LONG)),(nmin%BITS_IN_LONG))))
     56 
     57 #define BIT_UI_16_RNG(nmin, nmax) \
     58   {BIT_N_RNG(nmin, nmax, 0), BIT_N_RNG(nmin, nmax, 1), BIT_N_RNG(nmin, nmax, 2), BIT_N_RNG(nmin, nmax, 3), BIT_N_RNG(nmin, nmax, 4), BIT_N_RNG(nmin, nmax, 5), BIT_N_RNG(nmin, nmax, 6), BIT_N_RNG(nmin, nmax, 7), BIT_N_RNG(nmin, nmax, 8), BIT_N_RNG(nmin, nmax, 9), BIT_N_RNG(nmin, nmax, 10), BIT_N_RNG(nmin, nmax, 11), BIT_N_RNG(nmin, nmax, 12), BIT_N_RNG(nmin, nmax, 13), BIT_N_RNG(nmin, nmax, 14), BIT_N_RNG(nmin, nmax, 15)}
     59 
     60 /**************************************************************
     61  * MACRO TO GET THE OFFSET OF AN ELEMENT IN A                 *
     62  * GIVEN STRUCTURE TYPE.                                      *
     63  * 'X' IS THE TYPE OF THE STRUCTURE.                          *
     64  * 'Y' IS THE THE ELEMENT (IN THE STRUCTURE) WHOSE            *
     65  *     OFFSET WE NEED.                                        *
     66  * THE OFFSET RETURNS AS 'unsigned long'.                     *
     67  */
     68 #define OFFSETOF(x,y) ((unsigned long)(&(((x *)0)->y)))
     69 /*
     70  * Macros related to handling of 32 bits registers.
     71  * {
     72  */
     73 /*
     74  * Take value and put it in its proper location within a 'long'
     75  * register (and make sure it does not effect bits outside its
     76  * predefined mask).
     77  */
     78 /*
     79  * Take value and add it in its proper location within a 'long'
     80  * register ('target') and make sure it does not effect bits outside its
     81  * predefined mask.
     82  */
     83 /*
     84  * Get a value out of location within a 'long' register (and make sure it
     85  * is not effected by bits outside its predefined mask).
     86  */
     87 /*
     88  * }
     89  */
     90 /**************************************************************
     91  * MACRO TO GET THE SIZE OF AN ELEMENT IN A                   *
     92  * GIVEN STRUCTURE TYPE.                                      *
     93  * 'X' IS THE TYPE OF THE STRUCTURE.                          *
     94  * 'Y' IS THE  ELEMENT (IN THE STRUCTURE) WHOSE               *
     95  *     SIZE (IN BYTES) WE NEED.                               *
     96  * THE SIZE RETURNS AS 'unsigned long'.                       *
     97  */
     98 #define SIZEOF(x,y) ((unsigned long)(sizeof(((x *)0)->y)))
     99 /**************************************************************
    100  * MACRO TO CONVERT BIG ENDIAN LONG VARIABLE TO LITTLE ENDIAN *
    101  * 'X' IS THE INPUT VARIABLE.                                 *
    102  * 'Y' IS THE OUTPUT VARIABLE                                 *
    103  */
    104 
    105 /**************************************************************
    106  * MACRO TO CONVERT littel ENDIAN LONG VARIABLE TO BIG ENDIAN *
    107  * 'X' IS THE INPUT VARIABLE.                                 *
    108  * 'Y' IS THE OUTPUT VARIABLE                                 *
    109  */
    110 
    111 /* } */
    112 #endif
    113 
    114