about summary refs log tree commit diff
path: root/lib/util/intcode.h
blob: 1066ee9bd0b202f710b63f8f15feba3bc41e77e2 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef INTCODE_H_INCLUDED
#define INTCODE_H_INCLUDED

#include "pm_config.h"  /* For uint32_t, BYTE_ORDER, HAVE_INT64 */

static unsigned int const pm_byteOrder = BYTE_ORDER;

typedef struct {
/*----------------------------------------------------------------------------
   This is a big-endian representation of a 16 bit integer.  I.e.
   bytes[0] is the most significant 8 bits; bytes[1] is the least
   significant 8 bits of the number in pure binary.

   On a big-endian machines, this is bit for bit identical to uint16_t.
   On a little-endian machine, it isn't.

   This is an important data type because decent file formats use
   big-endian -- they don't care if some CPU happens to use some other
   code for its own work.
-----------------------------------------------------------------------------*/
    unsigned char bytes[2];
} bigend16;


static __inline__ uint16_t
pm_uintFromBigend16(bigend16 const arg) {

    uint16_t retval;

    switch (pm_byteOrder) {
    case BIG_ENDIAN: {
        union {
            bigend16 bigend;
            uint16_t native;
        } converter;
        converter.bigend = arg;
        retval = converter.native;
    } break;

    default: {
        retval =
            (arg.bytes[0] << 8) |
            (arg.bytes[1] << 0);
    }
    }
    return retval;
}



static __inline__ bigend16
pm_bigendFromUint16(uint16_t const arg) {

    bigend16 retval;

    switch (pm_byteOrder) {
    case BIG_ENDIAN: {
        union {
            bigend16 bigend;
            uint16_t native;
        } converter;
        converter.native = arg;
        retval = converter.bigend;
    } break;

    default: {
        uint16_t shift;
        shift = arg;
        retval.bytes[1] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[0] = shift;  /* Takes lower 8 bits */
    }
    }
    return retval;
}

typedef struct {
/*----------------------------------------------------------------------------
   This is a big-endian representation of a 32 bit integer.  I.e.
   bytes[0] is the most significant 8 bits; bytes[3] is the least
   significant 8 bits of the number in pure binary.

   On a big-endian machines, this is bit for bit identical to uint32_t.
   On a little-endian machine, it isn't.

   This is an important data type because decent file formats use
   big-endian -- they don't care if some CPU happens to use some other
   code for its own work.
-----------------------------------------------------------------------------*/
    unsigned char bytes[4];
} bigend32;


static __inline__ uint32_t
pm_uintFromBigend32(bigend32 const arg) {

    uint32_t retval;

    switch (pm_byteOrder) {
    case BIG_ENDIAN: {
        union {
            bigend32 bigend;
            uint32_t native;
        } converter;
        converter.bigend = arg;
        retval = converter.native;
    } break;

#if HAVE_GCC_BSWAP
    case LITTLE_ENDIAN: {
        /* Use GCC built-in */  
        union {
            bigend32 bigend;
            uint32_t native;
        } converter;
        converter.bigend = arg;
        retval = __builtin_bswap32(converter.native);
    } break;
#endif
    
    default:
        retval =
            (arg.bytes[0] << 24) |
            (arg.bytes[1] << 16) |
            (arg.bytes[2] <<  8) |
            (arg.bytes[3] <<  0);
    }

    return retval;
}



static __inline__ bigend32
pm_bigendFromUint32(uint32_t const arg) {

    bigend32 retval;

    switch (pm_byteOrder) {
    case BIG_ENDIAN: {
        union {
            bigend32 bigend;
            uint32_t native;
        } converter;
        converter.native = arg;
        retval = converter.bigend;
    } break;

#if HAVE_GCC_BSWAP    
    case LITTLE_ENDIAN: {
        /* Use GCC built-in */  
        union {
            bigend32 bigend;
            uint32_t native;
        } converter;
        converter.native = __builtin_bswap32(arg);
        retval = converter.bigend;
    } break;
#endif    

    default: {
        uint32_t shift;
        shift = arg;
        retval.bytes[3] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[2] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[1] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[0] = shift;  /* Takes lower 8 bits */
    }
    }
    return retval;
}


#if HAVE_INT64

typedef struct {
/*----------------------------------------------------------------------------
   This is a big-endian representation of a 64 bit integer.  I.e.
   bytes[0] is the most significant 8 bits; bytes[7] is the least
   significant 8 bits of the number in pure binary.

   On a big-endian machines, this is bit for bit identical to uint64_t.
   On a little-endian machine, it isn't.

   This is an important data type because decent file formats use
   big-endian -- they don't care if some CPU happens to use some other
   code for its own work.
-----------------------------------------------------------------------------*/
    unsigned char bytes[8];
} bigend64;


static __inline__ uint64_t
pm_uintFromBigend64(bigend64 const arg) {

    uint64_t retval;

    switch (pm_byteOrder) {
    case BIG_ENDIAN: {
        union {
            bigend64 bigend;
            uint64_t native;
        } converter;
        converter.bigend = arg;
        retval = converter.native;
    } break;

#if HAVE_GCC_BSWAP    
    case LITTLE_ENDIAN: {
        /* Use GCC built-in */  
        union {
            bigend64 bigend;
            uint64_t native;
        } converter;
        converter.bigend = arg;
        retval = __builtin_bswap64(converter.native);
    } break;
#endif
    default:
        retval =
            ((uint64_t)arg.bytes[0] << 56) | ((uint64_t)arg.bytes[1] << 48) |
            ((uint64_t)arg.bytes[2] << 40) | ((uint64_t)arg.bytes[3] << 32) |
            ((uint64_t)arg.bytes[4] << 24) | ((uint64_t)arg.bytes[5] << 16) |
            ((uint64_t)arg.bytes[6] <<  8) | ((uint64_t)arg.bytes[7] <<  0);
    }
    return retval;
}



static __inline__ bigend64
pm_bigendFromUint64(uint64_t const arg) {

    bigend64 retval;

    switch (pm_byteOrder) {
    case BIG_ENDIAN: {
        union {
            bigend64 bigend;
            uint64_t native;
        } converter;
        converter.native = arg;
        retval = converter.bigend;
    } break;

#if HAVE_GCC_BSWAP
    case LITTLE_ENDIAN: {

        /* Use GCC built-in */  
        union {
            bigend64 bigend;
            uint64_t native;
        } converter;
        converter.native = __builtin_bswap64(arg);
        retval = converter.bigend;
    } break;
#endif
    
    default: {
        uint64_t shift;
        shift = arg;
        retval.bytes[7] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[6] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[5] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[4] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[3] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[2] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[1] = shift;  /* Takes lower 8 bits */
        shift >>= 8;
        retval.bytes[0] = shift;  /* Takes lower 8 bits */
    }
    }
    return retval;
}

#endif /* HAVE_INT64 */

#endif