github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/bpf/include/linux/swab.h (about)

     1  #ifndef _LINUX_SWAB_H
     2  #define _LINUX_SWAB_H
     3  
     4  #include <linux/type_mapper.h>
     5  
     6  /*
     7   * casts are necessary for constants, because we never know how for sure
     8   * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
     9   */
    10  #define ___constant_swab16(x) ((__u16)(				\
    11  	(((__u16)(x) & (__u16)0x00ffU) << 8) |			\
    12  	(((__u16)(x) & (__u16)0xff00U) >> 8)))
    13  
    14  #define ___constant_swab32(x) ((__u32)(				\
    15  	(((__u32)(x) & (__u32)0x000000ffUL) << 24) |		\
    16  	(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) |		\
    17  	(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) |		\
    18  	(((__u32)(x) & (__u32)0xff000000UL) >> 24)))
    19  
    20  #define ___constant_swab64(x) ((__u64)(				\
    21  	(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) |	\
    22  	(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) |	\
    23  	(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) |	\
    24  	(((__u64)(x) & (__u64)0x00000000ff000000ULL) <<  8) |	\
    25  	(((__u64)(x) & (__u64)0x000000ff00000000ULL) >>  8) |	\
    26  	(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) |	\
    27  	(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) |	\
    28  	(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
    29  
    30  #define ___constant_swahw32(x) ((__u32)(			\
    31  	(((__u32)(x) & (__u32)0x0000ffffUL) << 16) |		\
    32  	(((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
    33  
    34  #define ___constant_swahb32(x) ((__u32)(			\
    35  	(((__u32)(x) & (__u32)0x00ff00ffUL) << 8) |		\
    36  	(((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
    37  
    38  /*
    39   * Implement the following as inlines, but define the interface using
    40   * macros to allow constant folding when possible:
    41   * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32
    42   */
    43  
    44  static __inline__  __u16 __fswab16(__u16 val)
    45  {
    46  #ifdef __HAVE_BUILTIN_BSWAP16__
    47  	return __builtin_bswap16(val);
    48  #elif defined (__arch_swab16)
    49  	return __arch_swab16(val);
    50  #else
    51  	return ___constant_swab16(val);
    52  #endif
    53  }
    54  
    55  static __inline__  __u32 __fswab32(__u32 val)
    56  {
    57  #ifdef __HAVE_BUILTIN_BSWAP32__
    58  	return __builtin_bswap32(val);
    59  #elif defined(__arch_swab32)
    60  	return __arch_swab32(val);
    61  #else
    62  	return ___constant_swab32(val);
    63  #endif
    64  }
    65  
    66  static __inline__  __u64 __fswab64(__u64 val)
    67  {
    68  #ifdef __HAVE_BUILTIN_BSWAP64__
    69  	return __builtin_bswap64(val);
    70  #elif defined (__arch_swab64)
    71  	return __arch_swab64(val);
    72  #elif defined(__SWAB_64_THRU_32__)
    73  	__u32 h = val >> 32;
    74  	__u32 l = val & ((1ULL << 32) - 1);
    75  	return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h)));
    76  #else
    77  	return ___constant_swab64(val);
    78  #endif
    79  }
    80  
    81  static __inline__  __u32 __fswahw32(__u32 val)
    82  {
    83  #ifdef __arch_swahw32
    84  	return __arch_swahw32(val);
    85  #else
    86  	return ___constant_swahw32(val);
    87  #endif
    88  }
    89  
    90  static __inline__  __u32 __fswahb32(__u32 val)
    91  {
    92  #ifdef __arch_swahb32
    93  	return __arch_swahb32(val);
    94  #else
    95  	return ___constant_swahb32(val);
    96  #endif
    97  }
    98  
    99  /**
   100   * __swab16 - return a byteswapped 16-bit value
   101   * @x: value to byteswap
   102   */
   103  #define __swab16(x)				\
   104  	(__builtin_constant_p((__u16)(x)) ?	\
   105  	___constant_swab16(x) :			\
   106  	__fswab16(x))
   107  
   108  /**
   109   * __swab32 - return a byteswapped 32-bit value
   110   * @x: value to byteswap
   111   */
   112  #define __swab32(x)				\
   113  	(__builtin_constant_p((__u32)(x)) ?	\
   114  	___constant_swab32(x) :			\
   115  	__fswab32(x))
   116  
   117  /**
   118   * __swab64 - return a byteswapped 64-bit value
   119   * @x: value to byteswap
   120   */
   121  #define __swab64(x)				\
   122  	(__builtin_constant_p((__u64)(x)) ?	\
   123  	___constant_swab64(x) :			\
   124  	__fswab64(x))
   125  
   126  /**
   127   * __swahw32 - return a word-swapped 32-bit value
   128   * @x: value to wordswap
   129   *
   130   * __swahw32(0x12340000) is 0x00001234
   131   */
   132  #define __swahw32(x)				\
   133  	(__builtin_constant_p((__u32)(x)) ?	\
   134  	___constant_swahw32(x) :		\
   135  	__fswahw32(x))
   136  
   137  /**
   138   * __swahb32 - return a high and low byte-swapped 32-bit value
   139   * @x: value to byteswap
   140   *
   141   * __swahb32(0x12345678) is 0x34127856
   142   */
   143  #define __swahb32(x)				\
   144  	(__builtin_constant_p((__u32)(x)) ?	\
   145  	___constant_swahb32(x) :		\
   146  	__fswahb32(x))
   147  
   148  /**
   149   * __swab16p - return a byteswapped 16-bit value from a pointer
   150   * @p: pointer to a naturally-aligned 16-bit value
   151   */
   152  static __inline__ __u16 __swab16p(const __u16 *p)
   153  {
   154  #ifdef __arch_swab16p
   155  	return __arch_swab16p(p);
   156  #else
   157  	return __swab16(*p);
   158  #endif
   159  }
   160  
   161  /**
   162   * __swab32p - return a byteswapped 32-bit value from a pointer
   163   * @p: pointer to a naturally-aligned 32-bit value
   164   */
   165  static __inline__ __u32 __swab32p(const __u32 *p)
   166  {
   167  #ifdef __arch_swab32p
   168  	return __arch_swab32p(p);
   169  #else
   170  	return __swab32(*p);
   171  #endif
   172  }
   173  
   174  /**
   175   * __swab64p - return a byteswapped 64-bit value from a pointer
   176   * @p: pointer to a naturally-aligned 64-bit value
   177   */
   178  static __inline__ __u64 __swab64p(const __u64 *p)
   179  {
   180  #ifdef __arch_swab64p
   181  	return __arch_swab64p(p);
   182  #else
   183  	return __swab64(*p);
   184  #endif
   185  }
   186  
   187  /**
   188   * __swahw32p - return a wordswapped 32-bit value from a pointer
   189   * @p: pointer to a naturally-aligned 32-bit value
   190   *
   191   * See __swahw32() for details of wordswapping.
   192   */
   193  static __inline__ __u32 __swahw32p(const __u32 *p)
   194  {
   195  #ifdef __arch_swahw32p
   196  	return __arch_swahw32p(p);
   197  #else
   198  	return __swahw32(*p);
   199  #endif
   200  }
   201  
   202  /**
   203   * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
   204   * @p: pointer to a naturally-aligned 32-bit value
   205   *
   206   * See __swahb32() for details of high/low byteswapping.
   207   */
   208  static __inline__ __u32 __swahb32p(const __u32 *p)
   209  {
   210  #ifdef __arch_swahb32p
   211  	return __arch_swahb32p(p);
   212  #else
   213  	return __swahb32(*p);
   214  #endif
   215  }
   216  
   217  /**
   218   * __swab16s - byteswap a 16-bit value in-place
   219   * @p: pointer to a naturally-aligned 16-bit value
   220   */
   221  static __inline__ void __swab16s(__u16 *p)
   222  {
   223  #ifdef __arch_swab16s
   224  	__arch_swab16s(p);
   225  #else
   226  	*p = __swab16p(p);
   227  #endif
   228  }
   229  /**
   230   * __swab32s - byteswap a 32-bit value in-place
   231   * @p: pointer to a naturally-aligned 32-bit value
   232   */
   233  static __inline__ void __swab32s(__u32 *p)
   234  {
   235  #ifdef __arch_swab32s
   236  	__arch_swab32s(p);
   237  #else
   238  	*p = __swab32p(p);
   239  #endif
   240  }
   241  
   242  /**
   243   * __swab64s - byteswap a 64-bit value in-place
   244   * @p: pointer to a naturally-aligned 64-bit value
   245   */
   246  static __inline__ void __swab64s(__u64 *p)
   247  {
   248  #ifdef __arch_swab64s
   249  	__arch_swab64s(p);
   250  #else
   251  	*p = __swab64p(p);
   252  #endif
   253  }
   254  
   255  /**
   256   * __swahw32s - wordswap a 32-bit value in-place
   257   * @p: pointer to a naturally-aligned 32-bit value
   258   *
   259   * See __swahw32() for details of wordswapping
   260   */
   261  static __inline__ void __swahw32s(__u32 *p)
   262  {
   263  #ifdef __arch_swahw32s
   264  	__arch_swahw32s(p);
   265  #else
   266  	*p = __swahw32p(p);
   267  #endif
   268  }
   269  
   270  /**
   271   * __swahb32s - high and low byteswap a 32-bit value in-place
   272   * @p: pointer to a naturally-aligned 32-bit value
   273   *
   274   * See __swahb32() for details of high and low byte swapping
   275   */
   276  static __inline__ void __swahb32s(__u32 *p)
   277  {
   278  #ifdef __arch_swahb32s
   279  	__arch_swahb32s(p);
   280  #else
   281  	*p = __swahb32p(p);
   282  #endif
   283  }
   284  
   285  
   286  #endif /* _LINUX_SWAB_H */