Linux Source Analysis byte order (4)--Little_endian.h

Source: Internet
Author: User

This section focuses on the small-endian byte order.


First, we want to look back at the concept of the big, small end mentioned in the previous section:

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 little_endian.h

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

#ifndef _linux_byteorder_little_endian_h
#define _linux_byteorder_little_endian_h

#ifndef __little_endian
#define __little_endian 1234
#endif
#ifndef __little_endian_bitfield
#define __little_endian_bitfield
#endif

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

#define __CONSTANT_HTONL (x) ((__BE32) ___constant_swab32 ((x))//conversion function for network byte order and host byte order
#define __CONSTANT_NTOHL (x) ___constant_swab32 ((__BE32) (x))
#define __CONSTANT_HTONS (x) ((__BE16) ___constant_swab16 ((x)))
#define __CONSTANT_NTOHS (x) ___constant_swab16 ((__BE16) (x))
#define __CONSTANT_CPU_TO_LE64 (x) ((__le64) (__u64) (x))
#define __CONSTANT_LE64_TO_CPU (x) ((__u64) (__LE64) (x))
#define __CONSTANT_CPU_TO_LE32 (x) ((__LE32) (__U32) (x))
#define __CONSTANT_LE32_TO_CPU (x) ((__U32) (__LE32) (x))
#define __CONSTANT_CPU_TO_LE16 (x) ((__LE16) (__U16) (x))
#define __CONSTANT_LE16_TO_CPU (x) ((__U16) (__LE16) (x))
#define __CONSTANT_CPU_TO_BE64 (x) ((__be64) ___constant_swab64 ((x)))
#define __CONSTANT_BE64_TO_CPU (x) ___constant_swab64 ((__u64) (__BE64) (x))
#define __CONSTANT_CPU_TO_BE32 (x) ((__BE32) ___constant_swab32 ((x)))
#define __CONSTANT_BE32_TO_CPU (x) ___constant_swab32 ((__u32) (__BE32) (x))
#define __CONSTANT_CPU_TO_BE16 (x) ((__BE16) ___constant_swab16 ((x)))
#define __CONSTANT_BE16_TO_CPU (x) ___constant_swab16 ((__u16) (__BE16) (x))
#define __CPU_TO_LE64 (x) ((__le64) (__u64) (x))
#define __LE64_TO_CPU (x) ((__u64) (__LE64) (x))
#define __CPU_TO_LE32 (x) ((__LE32) (__U32) (x))
#define __LE32_TO_CPU (x) ((__U32) (__LE32) (x))
#define __CPU_TO_LE16 (x) ((__LE16) (__U16) (x))
#define __LE16_TO_CPU (x) ((__U16) (__LE16) (x))
#define __CPU_TO_BE64 (x) ((__be64) __swab64 ((x)))
#define __BE64_TO_CPU (x) __swab64 ((__u64) (__BE64) (x))
#define __CPU_TO_BE32 (x) ((__BE32) __swab32 ((x)))
#define __BE32_TO_CPU (x) __swab32 ((__u32) (__BE32) (x))
#define __CPU_TO_BE16 (x) ((__BE16) __swab16 ((x)))
#define __BE16_TO_CPU (x) __swab16 ((__u16) (__BE16) (x))

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


#endif/* _linux_byteorder_little_endian_h */

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


Summarize:

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

Add:

Two major CPU factions: Motorola's PowerPC series and Intel's x86 series.

The PowerPC series uses big endian to store data, while the x86 series uses little endian to store data. (This has been analyzed in the byte order (4) of the Linux Source code analysis)


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