Linux Source Analysis byte order (5)--Swab.h

Source: Internet
Author: User

In the Linux source code analysis of the byte sequence (3), the Linux source code analysis of the byte sequence (4) are seen, the source contains the

#include <linux/swab.h>

This header function describes the specific method of byte exchange. Let's take a look at the specific code:

---------------------------------------------------------------------------------------------------------------

#ifndef _linux_swab_h
#define _linux_swab_h

#include <linux/types.h>

#include <asm/swab.h>

/*
* Casts is necessary for constants, because we never know what for sure
* How U/ul/ull maps to __u16, __u32, __u64. At least a portable.
*/

Swaps the byte order of the _U16 type, such as short
#define ___CONSTANT_SWAB16 (x) ((__u16) (\
((__U16) (x) & (__U16) 0x00ffu) << 8) | \
(((__U16) (x) & (__U16) 0xff00u) >> 8)))
Swap byte order of _u32 type
#define ___CONSTANT_SWAB32 (x) ((__u32) (\
((__U32) (x) & (__U32) 0x000000fful) << 24) | \
((__U32) (x) & (__U32) 0x0000ff00ul) << 8) | \
((__U32) (x) & (__U32) 0x00ff0000ul) >> 8) | \
(((__U32) (x) & (__U32) 0xff000000ul) >> 24)))
Swap byte order of _u64 type
#define ___CONSTANT_SWAB64 (x) ((__u64) (\
((__u64) (x) & (__u64) 0x00000000000000ffull) << 56) | \
((__u64) (x) & (__u64) 0x000000000000ff00ull) << 40) | \
((__u64) (x) & (__u64) 0x0000000000ff0000ull) << 24) | \
((__u64) (x) & (__u64) 0x00000000ff000000ull) << 8) | \
((__u64) (x) & (__u64) 0x000000ff00000000ull) >> 8) | \
((__u64) (x) & (__u64) 0x0000ff0000000000ull) >> 24) | \
((__u64) (x) & (__u64) 0x00ff000000000000ull) >> 40) | \
(((__u64) (x) & (__u64) 0xff00000000000000ull) >> 56)))

#define ___CONSTANT_SWAHW32 (x) ((__u32) (\
((__U32) (x) & (__U32) 0x0000fffful) << 16) | \
(((__U32) (x) & (__U32) 0xffff0000ul) >> 16)))

#define ___CONSTANT_SWAHB32 (x) ((__u32) (\
((__U32) (x) & (__U32) 0x00ff00fful) << 8) | \
(((__U32) (x) & (__U32) 0xff00ff00ul) >> 8)))

/*
* Implement the following as Inlines, but define the interface using
* Macros to allow constant folding when possible:
* ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32
*/

Static __inline__ __u16 __fswab16 (__u16 val)
{
#ifdef __ARCH_SWAB16
Return __arch_swab16 (Val);
#else
Return ___constant_swab16 (Val);
#endif
}

Static __inline__ __u32 __fswab32 (__u32 val)
{
#ifdef __ARCH_SWAB32
Return __arch_swab32 (Val);
#else
Return ___constant_swab32 (Val);
#endif
}

Static __inline__ __u64 __fswab64 (__u64 val)
{
#ifdef __ARCH_SWAB64
Return __arch_swab64 (Val);
#elif defined (__swab_64_thru_32__)
__u32 h = Val >> 32;
__u32 L = val & ((1ULL << 32)-1);
Return (((__u64) __fswab32 (L)) << 32) | ((__u64) (__fswab32 (h)));
#else
Return ___constant_swab64 (Val);
#endif
}

Static __inline__ __u32 __fswahw32 (__u32 val)
{
#ifdef __ARCH_SWAHW32
Return __arch_swahw32 (Val);
#else
Return ___constant_swahw32 (Val);
#endif
}

Static __inline__ __u32 __fswahb32 (__u32 val)
{
#ifdef __ARCH_SWAHB32
Return __arch_swahb32 (Val);
#else
Return ___constant_swahb32 (Val);
#endif
}

/**
* __swab16-return a byteswapped 16-bit value
* @x:value to Byteswap
*/
#define __SWAB16 (x) \
(__builtin_constant_p (__U16) (x))? \
___CONSTANT_SWAB16 (x): \
__FSWAB16 (x))

/**
* __swab32-return a byteswapped 32-bit value
* @x:value to Byteswap
*/
#define __SWAB32 (x) \
(__builtin_constant_p (__U32) (x))? \
___CONSTANT_SWAB32 (x): \
__FSWAB32 (x))

/**
* __swab64-return a byteswapped 64-bit value
* @x:value to Byteswap
*/
#define __SWAB64 (x) \
(__builtin_constant_p (__u64) (x))? \
___CONSTANT_SWAB64 (x): \
__FSWAB64 (x))

/**
* __swahw32-return a word-swapped 32-bit value
* @x:value to Wordswap
*
* __SWAHW32 (0x12340000) is 0x00001234
*/
#define __SWAHW32 (x) \
(__builtin_constant_p (__U32) (x))? \
___constant_swahw32 (x): \
__fswahw32 (x))

/**
* __swahb32-return a high and low byte-swapped 32-bit value
* @x:value to Byteswap
*
* __SWAHB32 (0x12345678) is 0x34127856
*/
#define __SWAHB32 (x) \
(__builtin_constant_p (__U32) (x))? \
___CONSTANT_SWAHB32 (x): \
__FSWAHB32 (x))

/**
* __swab16p-return a byteswapped 16-bit value from a pointer
* @p:pointer to a naturally-aligned 16-bit value
*/
Static __inline__ __u16 __swab16p (const __U16 *p)
{
#ifdef __arch_swab16p
Return __arch_swab16p (P);
#else
Return __swab16 (*P);
#endif
}

/**
* __swab32p-return a byteswapped 32-bit value from a pointer
* @p:pointer to a naturally-aligned 32-bit value
*/
Static __inline__ __u32 __swab32p (const __U32 *p)
{
#ifdef __arch_swab32p
Return __arch_swab32p (P);
#else
Return __swab32 (*P);
#endif
}

/**
* __swab64p-return a byteswapped 64-bit value from a pointer
* @p:pointer to a naturally-aligned 64-bit value
*/
Static __inline__ __u64 __swab64p (const __u64 *p)
{
#ifdef __arch_swab64p
Return __arch_swab64p (P);
#else
Return __swab64 (*P);
#endif
}

/**
* __swahw32p-return a wordswapped 32-bit value from a pointer
* @p:pointer to a naturally-aligned 32-bit value
*
* See __SWAHW32 () for details of wordswapping.
*/
Static __inline__ __u32 __swahw32p (const __U32 *p)
{
#ifdef __arch_swahw32p
Return __arch_swahw32p (P);
#else
Return __swahw32 (*P);
#endif
}

/**
* __swahb32p-return a high and low byteswapped 32-bit value from a pointe R
* @p:pointer to a naturally-aligned 32-bit value
*
* See __SWAHB32 () for details of High/low byteswapping.
*/
Static __inline__ __u32 __swahb32p (const __U32 *p)
{
#ifdef __arch_swahb32p
Return __arch_swahb32p (P);
#else
Return __swahb32 (*P);
#endif
}
/**
* __swab16s-byteswap a 16-bit value in-place215 * @p:pointer to a naturally-aligned 16-bit value
*/
static __inline__ void __swab16s (__u16 *p)
{
#ifdef __arch_swab16s
__arch_swab16s (P);
#else
*p = __swab16p (p);
#endif
}
/**
* __swab32s-byteswap a 32-bit value in-place
* @p:pointer to a naturally-aligned 32-bit value
*/
static __inline__ void __swab32s (__u32 *p)
{
#ifdef __arch_swab32s
__arch_swab32s (P);
#else
*p = __swab32p (p);
#endif
}

/**
* __swab64s-byteswap a 64-bit value in-place
* @p:pointer to a naturally-aligned 64-bit value
*/
static __inline__ void __swab64s (__u64 *p)
{
#ifdef __arch_swab64s
__arch_swab64s (P);
#else
*p = __swab64p (p);
#endif
}

/**
* __swahw32s-wordswap a 32-bit value in-place
* @p:pointer to a naturally-aligned 32-bit value
*
* See __SWAHW32 () for details of wordswapping
*/
static __inline__ void __swahw32s (__u32 *p)
{
#ifdef __arch_swahw32s
__arch_swahw32s (P);
#else
*p = __swahw32p (p);
#endif
}

/**
* __swahb32s-high and low Byteswap a 32-bit value in-place
* @p:pointer to a naturally-aligned 32-bit value
*
* See __SWAHB32 () for details about high and low byte swapping
*/
static __inline__ void __swahb32s (__u32 *p)
{
#ifdef __arch_swahb32s
__arch_swahb32s (P);
#else
*p = __swahb32p (p);
#endif
}


#endif/* _linux_swab_h */

---------------------------------------------------------------------------------------------------------------


Summary:

This section mainly introduces some methods of byte exchange, including the byte exchange of shaping data, pointers, etc.

Linux Source Analysis byte order (5)--Swab.h

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.