2 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
4 * This software may be freely used, copied, modified, and distributed
5 * provided that the above copyright notice is preserved in all copies of the
15 * endian.h - target endianness independent read/write primitives.
18 #ifndef angel_endian_h
19 #define angel_endian_h
22 * The endianness of the data being processed needs to be known, but
23 * the host endianness is not required (since the data is constructed
24 * using bytes). At the moment these are provided as macros. This
25 * gives the compiler freedom in optimising individual calls. However,
26 * if space is at a premium then functions should be provided.
28 * NOTE: These macros assume that the data has been packed in the same format
29 * as the packing on the build host. If this is not the case then
30 * the wrong addresses could be used when dealing with structures.
35 * For all the following routines the target endianness is defined by the
36 * following boolean definitions.
38 #define BE (1 == 1) /* TRUE : big-endian */
39 #define LE (1 == 0) /* FALSE : little-endian */
42 * The following type definitions are used by the endianness converting
45 typedef unsigned char U8
;
47 typedef const U8
*CP_U8
;
49 typedef unsigned short U16
;
52 typedef unsigned int U32
;
56 * If the endianness of the host and target are known (fixed) and the same
57 * then the following macro definitions can be used. These just directly copy
60 * #define READ(e,a) (a)
61 * #define WRITE(e,a,v) ((a) = (v))
62 * #define PREAD(e,a) (a)
63 * #define PWRITE(e,a,v) (*(a) = (v))
67 * These macros assume that a byte (char) is 8bits in size, and that the
68 * endianness is not important when reading or writing bytes.
70 #define PUT8(a,v) (*((P_U8)(a)) = (U8)(v))
71 #define PUT16LE(a,v) (PUT8(a,((v) & 0xFF)), \
72 PUT8((((P_U8)(a)) + sizeof(char)),((v) >> 8)))
73 #define PUT16BE(a,v) (PUT8(a,((v) >> 8)), \
74 PUT8((((P_U8)(a)) + sizeof(char)),((v) & 0xFF)))
75 #define PUT32LE(a,v) (PUT16LE(a,v), \
76 PUT16LE((((P_U8)(a)) + sizeof(short)),((v) >> 16)))
77 #define PUT32BE(a,v) (PUT16BE(a,((v) >> 16)), \
78 PUT16BE((((P_U8)(a)) + sizeof(short)),v))
80 #define GET8(a) (*((CP_U8)(a)))
81 #define GET16LE(a) (GET8(a) | (((U16)GET8(((CP_U8)(a)) + sizeof(char))) << 8))
82 #define GET16BE(a) ((((U16)GET8(a)) << 8) | GET8(((CP_U8)(a)) + sizeof(char)))
83 #define GET32LE(a) (GET16LE(a) | \
84 (((U32)GET16LE(((CP_U8)(a)) + sizeof(short))) << 16))
85 #define GET32BE(a) ((((U32)GET16BE(a)) << 16) | \
86 GET16BE(((CP_U8)(a)) + sizeof(short)))
89 * These macros simplify the code in respect to reading and writing the
90 * correct size data when dealing with endianness. "e" is TRUE if we are
91 * dealing with big-endian data, FALSE if we are dealing with little-endian.
94 /* void WRITE(int endianness, void *address, unsigned value); */
96 #define WRITE16(e,a,v) ((e) ? PUT16BE(&(a),v) : PUT16LE(&(a),v))
97 #define WRITE32(e,a,v) ((e) ? PUT32BE(&(a),v) : PUT32LE(&(a),v))
98 #define WRITE(e,a,v) ((sizeof(v) == sizeof(char)) ? \
99 PUT8(&(a),v) : ((sizeof(v) == sizeof(short)) ? \
100 WRITE16(e,a,v) : WRITE32(e,a,v)))
102 /* unsigned READ(int endianness, void *address) */
103 #define READ16(e,a) ((e) ? GET16BE(&(a)) : GET16LE(&(a)))
104 #define READ32(e,a) ((e) ? GET32BE(&(a)) : GET32LE(&(a)))
105 #define READ(e,a) ((sizeof(a) == sizeof(char)) ? \
106 GET8((CP_U8)&(a)) : ((sizeof(a) == sizeof(short)) ? \
107 READ16(e,a) : READ32(e,a)))
109 /* void PWRITE(int endianness, void *address, unsigned value); */
110 #define PWRITE16(e,a,v) ((e) ? PUT16BE(a,v) : PUT16LE(a,v))
111 #define PWRITE32(e,a,v) ((e) ? PUT32BE(a,v) : PUT32LE(a,v))
112 #define PWRITE(e,a,v) ((sizeof(v) == sizeof(char)) ? \
113 PUT8(a,v) : ((sizeof(v) == sizeof(short)) ? \
114 PWRITE16(e,a,v) : PWRITE32(e,a,v)))
116 /* unsigned PREAD(int endianness, void *address) */
117 #define PREAD16(e,a) ((e) ? GET16BE(a) : GET16LE(a))
118 #define PREAD32(e,a) ((e) ? GET32BE(a) : GET32LE(a))
119 #define PREAD(e,a) ((sizeof(*(a)) == sizeof(char)) ? \
120 GET8((CP_U8)a) : ((sizeof(*(a)) == sizeof(short)) ? \
121 PREAD16(e,a) : PREAD32(e,a)))
123 #endif /* !defined(angel_endian_h) */
This page took 0.032179 seconds and 4 git commands to generate.