Linux Source Analysis byte order (3)--Big_endian.h

Source: Internet
Author: User

This section mainly analyzes the endian byte order.


First, we need to understand the concept of big-endian and small-end:

Byte order refers to the order in memory of data that occupies more than one byte of memory, usually with a small, big-endian byte order. Small-endian byte-order refers to low-byte data stored at low memory address, high-byte data stored at high memory address; Big endian byte order is high-byte data is stored at low address, low-byte data is stored at high address. The PC based on X86 platform is small endian, while some embedded platforms are byte order. As a result, more than 1 bytes of data such as int, uint16, UInt32, etc., should be changed in the storage order on these embedded platforms. In general, we believe that the sequence of bytes transmitted in the air is the standard order of the network byte order, taking into account the consistency with the protocol and interoperability with other platform products, when the packet is sent in the program, the host byte order is converted to the network byte order, and the packet is converted to the host byte order by the receiving packet.
In fact, big Endian refers to the low address holds the highest effective byte (MSB), while the little endian is the low address holds the lowest effective byte (LSB).


Next, let's take a look at the file under path Include/linux/byteorder big_endian.h

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

#ifndef _linux_byteorder_big_endian_h
#define _linux_byteorder_big_endian_h

#ifndef __big_endian
#define __big_endian 4321
#endif
#ifndef __big_endian_bitfield
#define __big_endian_bitfield
#endif

#include <linux/types.h>
#include <linux/swab.h>

#define __CONSTANT_HTONL (x) ((__BE32) (__U32) (x))//The network byte order of converting a 32-digit number from host byte order to unsigned long shaping
#define __CONSTANT_NTOHL (x) ((__U32) (__BE32) (x))//Converts a 32-digit number from network byte order to host byte order
#define __CONSTANT_HTONS (x) ((__BE16) (__U16) (x))//network byte order converting a 16-digit number from host byte order to unsigned short shaping
#define __CONSTANT_NTOHS (x) ((__U16) (__BE16) (x))//Converts a 16-digit number from network byte order to host byte order
#define __CONSTANT_CPU_TO_LE64 (x) ((__le64) ___constant_swab64 ((x)))
#define __CONSTANT_LE64_TO_CPU (x) ___constant_swab64 ((__u64) (__LE64) (x))
#define __CONSTANT_CPU_TO_LE32 (x) ((__LE32) ___constant_swab32 ((x)))
#define __CONSTANT_LE32_TO_CPU (x) ___constant_swab32 ((__u32) (__LE32) (x))
#define __CONSTANT_CPU_TO_LE16 (x) ((__LE16) ___constant_swab16 ((x)))
#define __CONSTANT_LE16_TO_CPU (x) ___constant_swab16 ((__u16) (__LE16) (x))
#define __CONSTANT_CPU_TO_BE64 (x) ((__be64) (__u64) (x))
#define __CONSTANT_BE64_TO_CPU (x) ((__u64) (__BE64) (x))
#define __CONSTANT_CPU_TO_BE32 (x) ((__BE32) (__U32) (x))
#define __CONSTANT_BE32_TO_CPU (x) ((__U32) (__BE32) (x))
#define __CONSTANT_CPU_TO_BE16 (x) ((__BE16) (__U16) (x))
#define __CONSTANT_BE16_TO_CPU (x) ((__U16) (__BE16) (x))
#define __CPU_TO_LE64 (x) ((__le64) __swab64 ((x))//convert from CPU byte order to 64 bit small byte data (converted with a pointer to a value)
#define __LE64_TO_CPU (x) __swab64 ((__u64) (__LE64) (x)//Convert from 64-bit byte data to CPU byte order
#define __CPU_TO_LE32 (x) ((__LE32) __swab32 ((x))//convert from CPU byte order to 32-bit small-endian byte data
#define __LE32_TO_CPU (x) __swab32 ((__u32) (__LE32) (x)//convert from 32-bit byte data to CPU byte order
#define __CPU_TO_LE16 (x) ((__LE16) __swab16 ((x))//convert from CPU byte order to 16-bit small-endian byte data
#define __LE16_TO_CPU (x) __swab16 ((__u16) (__LE16) (x)//Convert from 16-bit byte data to CPU byte order
#define __CPU_TO_BE64 (x) ((__be64) (__u64) (x))//convert from CPU byte order to 64-bit endian byte data
#define __BE64_TO_CPU (x) ((__u64) (__BE64) (x))//Convert from 64-bit endian byte data to CPU byte order
#define __CPU_TO_BE32 (x) ((__BE32) (__U32) (x))//convert from CPU byte order to 32-bit endian byte data
#define __BE32_TO_CPU (x) ((__U32) (__BE32) (x))//convert from 32-bit endian byte data to CPU byte order
#define __CPU_TO_BE16 (x) ((__BE16) (__U16) (x))//convert from CPU byte order to 16-bit endian byte data
#define __BE16_TO_CPU (x) ((__U16) (__BE16) (x))//Convert from 16-bit endian byte data to CPU byte order

Static __inline__ __le64 __cpu_to_le64p (const __u64 *p)
{
Return (__LE64) __swab64p (p);
}
Static __inline__ __u64 __le64_to_cpup (const __le64 *p)
{
Return __swab64p ((__u64 *) p);
}
Static __inline__ __le32 __cpu_to_le32p (const __U32 *p)
{
Return (__LE32) __swab32p (p);
}
Static __inline__ __u32 __le32_to_cpup (const __LE32 *p)
{
Return __swab32p ((__U32 *) p);
}
Static __inline__ __le16 __cpu_to_le16p (const __U16 *p)
{
Return (__LE16) __swab16p (p);
}
Static __inline__ __u16 __le16_to_cpup (const __LE16 *p)
{
Return __swab16p ((__U16 *) p);
}
Static __inline__ __be64 __cpu_to_be64p (const __u64 *p)
{
Return (__BE64) *p;
}
Static __inline__ __u64 __be64_to_cpup (const __be64 *p)
{
Return (__u64) *p;
}
Static __inline__ __be32 __cpu_to_be32p (const __U32 *p)
{
Return (__BE32) *p;
}
Static __inline__ __u32 __be32_to_cpup (const __BE32 *p)
{
Return (__U32) *p;
}
Static __inline__ __be16 __cpu_to_be16p (const __U16 *p)
{
Return (__BE16) *p;
}
Static __inline__ __u16 __be16_to_cpup (const __BE16 *p)
{
Return (__U16) *p;
}
#define __CPU_TO_LE64S (x) __swab64s ((x))
#define __LE64_TO_CPUS (x) __swab64s ((x))
#define __CPU_TO_LE32S (x) __swab32s ((x))
#define __LE32_TO_CPUS (x) __swab32s ((x))
#define __CPU_TO_LE16S (x) __swab16s ((x))
#define __LE16_TO_CPUS (x) __swab16s ((x))
#define __CPU_TO_BE64S (x) do {(void) (x),} while (0)
#define __BE64_TO_CPUS (x) do {(void) (x),} while (0)
#define __CPU_TO_BE32S (x) do {(void) (x),} while (0)
#define __BE32_TO_CPUS (x) do {(void) (x),} while (0)
#define __CPU_TO_BE16S (x) do {(void) (x),} while (0)
#define __BE16_TO_CPUS (x) do {(void) (x),} while (0)


#endif/* _linux_byteorder_big_endian_h */

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


Summarize:

This section mainly analyzes the definition of big endian byte order, and the conversion function of network byte order and host byte order.

The Internet protocol uses the big endian byte order when processing these multibyte integers.

Linux Source Analysis byte order (3)--Big_endian.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.