libptp-endian.h

来自「AMLOGIC DPF source code」· C头文件 代码 · 共 78 行

H
78
字号
/*******************************************************************
 * 
 *  Copyright C 2005 by Amlogic, Inc. All Rights Reserved.
 *
 *  Description: 
 *
 *  Author: Amlogic Software
 *  Created: Fri Nov 11 00:08:19 2005
 *
 *******************************************************************/
#ifndef __BYTEORDER_H
#define __BYTEORDER_H

/* Define generic byte swapping functions */


/* The byte swapping macros have the form: */
/*   EENN[a]toh or htoEENN[a] where EE is be (big endian) or */
/* le (little-endian), NN is 16 or 32 (number of bits) and a, */
/* if present, indicates that the endian side is a pointer to an */
/* array of uint8_t bytes instead of an integer of the specified length. */
/* h refers to the host's ordering method. */

/* So, to convert a 32-bit integer stored in a buffer in little-endian */
/* format into a uint32_t usable on this machine, you could use: */
/*   uint32_t value = le32atoh(&buf[3]); */
/* To put that value back into the buffer, you could use: */
/*   htole32a(&buf[3], value); */

/* Define aliases for the standard byte swapping macros */
/* Arguments to these macros must be properly aligned on natural word */
/* boundaries in order to work properly on all architectures */
#define htobe16(x) htons(x)
#define htobe32(x) htonl(x)
#define be16toh(x) ntohs(x)
#define be32toh(x) ntohl(x)

/* On little endian machines, these macros are null */
#define htole16(x)      (x)
#define htole32(x)      (x)
#define htole64(x)      (x)
#define le16toh(x)      (x)
#define le32toh(x)      (x)
#define le64toh(x)      (x)

/* Define the C99 standard length-specific integer types */
//#include "_stdint.h"

/* Here are some macros to create integers from a byte array */
/* These are used to get and put integers from/into a uint8_t array */
/* with a specific endianness.  This is the most portable way to generate */
/* and read messages to a network or serial device.  Each member of a */
/* packet structure must be handled separately. */

/* The i386 and compatibles can handle unaligned memory access, */
/* so use the optimized macros above to do this job */
#define be16atoh(x)     be16toh(*(uint16_t*)(x))
#define be32atoh(x)     be32toh(*(uint32_t*)(x))
#define be64atoh(x)     be64toh(*(uint64_t*)(x))
//#define le16atoh(x)     le16toh(*(uint16_t*)(x))
//#define le32atoh(x)     le32toh(*(uint32_t*)(x))
//#define le64atoh(x)     le64toh(*(uint64_t*)(x))
#define le16atoh(x)     ((*(x+1)<<8)+*(x))
#define le32atoh(x)     (((le16atoh(x+2))<<16)+le16atoh(x))
#define le64atoh(x)     (((le32atoh(x+4))<<32)+le32atoh(x))

#define htobe16a(a,x)   *(uint16_t*)(a) = htobe16(x)
#define htobe32a(a,x)   *(uint32_t*)(a) = htobe32(x)
#define htobe64a(a,x)   *(uint64_t*)(a) = htobe64(x)
//#define htole16a(a,x)   *(uint16_t*)(a) = htole16(x)
//#define htole32a(a,x)   *(uint32_t*)(a) = htole32(x)
//#define htole64a(a,x)   *(uint64_t*)(a) = htole64(x)

#define htole16a(a,x)		{a[0]=htole16(x);a[1]=htole16(x)>>8;}
#define htole32a(a,x)		{a[0]=htole32(x);a[1]=htole32(x)>>8;a[2]=htole32(x)>>16;a[3]=htole32(x)>>24;}
#define htole64a(a,x)		{htole32a(a,htole64(x));htole32a(a+4,htole64(x)>>32)}
#endif /*__BYTEORDER_H*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?