]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/rdi-share/endian.h
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / gdb / rdi-share / endian.h
1 /*
2 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
3 *
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
6 * software.
7 */
8
9 /* -*-C-*-
10 *
11 * $Revision$
12 * $Date$
13 *
14 *
15 * endian.h - target endianness independent read/write primitives.
16 */
17
18 #ifndef angel_endian_h
19 #define angel_endian_h
20
21 /*
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.
27 *
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.
31 *
32 */
33
34 /*
35 * For all the following routines the target endianness is defined by the
36 * following boolean definitions.
37 */
38 #define BE (1 == 1) /* TRUE : big-endian */
39 #define LE (1 == 0) /* FALSE : little-endian */
40
41 /*
42 * The following type definitions are used by the endianness converting
43 * macros.
44 */
45 typedef unsigned char U8;
46 typedef U8 *P_U8;
47 typedef const U8 *CP_U8;
48
49 typedef unsigned short U16;
50 typedef U16 *P_U16;
51
52 typedef unsigned int U32;
53 typedef U32 *P_U32;
54
55 /*
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
58 * the data.
59 *
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))
64 */
65
66 /*
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.
69 */
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))
79
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)))
87
88 /*
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.
92 */
93
94 /* void WRITE(int endianness, void *address, unsigned value); */
95
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)))
101
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)))
108
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)))
115
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)))
122
123 #endif /* !defined(angel_endian_h) */
124
125 /* EOF endian.h */