aboutsummaryrefslogtreecommitdiff
path: root/gdb/rdi-share/endian.h
blob: 3d32d9a6e60bb81f6dbec291f4f199e7091cbf3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* 
 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
 * 
 * This software may be freely used, copied, modified, and distributed
 * provided that the above copyright notice is preserved in all copies of the
 * software.
 */

/* -*-C-*-
 *
 * $Revision$
 *     $Date$
 *
 *
 * endian.h - target endianness independent read/write primitives.
 */

#ifndef angel_endian_h
#define angel_endian_h

/*
 * The endianness of the data being processed needs to be known, but
 * the host endianness is not required (since the data is constructed
 * using bytes).  At the moment these are provided as macros. This
 * gives the compiler freedom in optimising individual calls. However,
 * if space is at a premium then functions should be provided.
 *
 * NOTE: These macros assume that the data has been packed in the same format
 *       as the packing on the build host. If this is not the case then
 *       the wrong addresses could be used when dealing with structures.
 *
 */

/*
 * For all the following routines the target endianness is defined by the
 * following boolean definitions.
 */
#define BE (1 == 1) /* TRUE  : big-endian */
#define LE (1 == 0) /* FALSE : little-endian */

/*
 * The following type definitions are used by the endianness converting
 * macros.
 */
typedef unsigned char U8;
typedef U8 *P_U8;
typedef const U8 *CP_U8;

typedef unsigned short U16;
typedef U16 *P_U16;

typedef unsigned int U32;
typedef U32 *P_U32;

/*
 * If the endianness of the host and target are known (fixed) and the same
 * then the following macro definitions can be used. These just directly copy
 * the data.
 *
 * #define READ(e,a)       (a)
 * #define WRITE(e,a,v)    ((a) = (v))
 * #define PREAD(e,a)      (a)
 * #define PWRITE(e,a,v)   (*(a) = (v))
 */

/*
 * These macros assume that a byte (char) is 8bits in size, and that the
 * endianness is not important when reading or writing bytes.
 */
#define PUT8(a,v)       (*((P_U8)(a)) = (U8)(v))
#define PUT16LE(a,v)    (PUT8(a,((v) & 0xFF)), \
                         PUT8((((P_U8)(a)) + sizeof(char)),((v) >> 8)))
#define PUT16BE(a,v)    (PUT8(a,((v) >> 8)), \
                         PUT8((((P_U8)(a)) + sizeof(char)),((v) & 0xFF)))
#define PUT32LE(a,v)    (PUT16LE(a,v), \
                         PUT16LE((((P_U8)(a)) + sizeof(short)),((v) >> 16)))
#define PUT32BE(a,v)    (PUT16BE(a,((v) >> 16)), \
                         PUT16BE((((P_U8)(a)) + sizeof(short)),v))

#define GET8(a)     (*((CP_U8)(a)))
#define GET16LE(a)  (GET8(a) | (((U16)GET8(((CP_U8)(a)) + sizeof(char))) << 8))
#define GET16BE(a)  ((((U16)GET8(a)) << 8) | GET8(((CP_U8)(a)) + sizeof(char)))
#define GET32LE(a)  (GET16LE(a) | \
                     (((U32)GET16LE(((CP_U8)(a)) + sizeof(short))) << 16))
#define GET32BE(a)  ((((U32)GET16BE(a)) << 16) | \
                     GET16BE(((CP_U8)(a)) + sizeof(short)))

/*
 * These macros simplify the code in respect to reading and writing the
 * correct size data when dealing with endianness. "e" is TRUE if we are
 * dealing with big-endian data, FALSE if we are dealing with little-endian.
 */

/* void WRITE(int endianness, void *address, unsigned value); */

#define WRITE16(e,a,v) ((e) ? PUT16BE(&(a),v) : PUT16LE(&(a),v))
#define WRITE32(e,a,v) ((e) ? PUT32BE(&(a),v) : PUT32LE(&(a),v))
#define WRITE(e,a,v)   ((sizeof(v) == sizeof(char)) ? \
                        PUT8(&(a),v) : ((sizeof(v) == sizeof(short)) ? \
                                        WRITE16(e,a,v) : WRITE32(e,a,v)))

/* unsigned READ(int endianness, void *address) */
#define READ16(e,a) ((e) ? GET16BE(&(a)) : GET16LE(&(a)))
#define READ32(e,a) ((e) ? GET32BE(&(a)) : GET32LE(&(a)))
#define READ(e,a) ((sizeof(a) == sizeof(char)) ? \
                   GET8((CP_U8)&(a)) : ((sizeof(a) == sizeof(short)) ? \
                                       READ16(e,a) : READ32(e,a)))

/* void PWRITE(int endianness, void *address, unsigned value); */
#define PWRITE16(e,a,v) ((e) ? PUT16BE(a,v) : PUT16LE(a,v))
#define PWRITE32(e,a,v) ((e) ? PUT32BE(a,v) : PUT32LE(a,v))
#define PWRITE(e,a,v)   ((sizeof(v) == sizeof(char)) ? \
                         PUT8(a,v) : ((sizeof(v) == sizeof(short)) ? \
                                      PWRITE16(e,a,v) : PWRITE32(e,a,v)))

/* unsigned PREAD(int endianness, void *address) */
#define PREAD16(e,a) ((e) ? GET16BE(a) : GET16LE(a))
#define PREAD32(e,a) ((e) ? GET32BE(a) : GET32LE(a))
#define PREAD(e,a) ((sizeof(*(a)) == sizeof(char)) ? \
                    GET8((CP_U8)a) : ((sizeof(*(a)) == sizeof(short)) ? \
                                     PREAD16(e,a) : PREAD32(e,a)))

#endif /* !defined(angel_endian_h) */

/* EOF endian.h */