File: retro_endianness.h

package info (click to toggle)
retroarch 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 67,792 kB
  • sloc: ansic: 1,075,189; cpp: 75,110; asm: 6,624; objc: 6,224; python: 3,535; sh: 2,734; makefile: 2,701; xml: 2,567; perl: 393; javascript: 9
file content (579 lines) | stat: -rw-r--r-- 14,905 bytes parent folder | download | duplicates (3)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/* Copyright  (C) 2010-2020 The RetroArch team
 *
 * ---------------------------------------------------------------------------------------
 * The following license statement only applies to this file (retro_endianness.h).
 * ---------------------------------------------------------------------------------------
 *
 * Permission is hereby granted, free of charge,
 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef __LIBRETRO_SDK_ENDIANNESS_H
#define __LIBRETRO_SDK_ENDIANNESS_H

#include <retro_inline.h>
#include <stdint.h>
#include <stdlib.h>

#if defined(_MSC_VER) && _MSC_VER > 1200
#define SWAP16 _byteswap_ushort
#define SWAP32 _byteswap_ulong
#else
static INLINE uint16_t SWAP16(uint16_t x)
{
  return ((x & 0x00ff) << 8) |
         ((x & 0xff00) >> 8);
}

static INLINE uint32_t SWAP32(uint32_t x)
{
  return ((x & 0x000000ff) << 24) |
         ((x & 0x0000ff00) <<  8) |
         ((x & 0x00ff0000) >>  8) |
         ((x & 0xff000000) >> 24);
}

#endif

#if defined(_MSC_VER) && _MSC_VER <= 1200
static INLINE uint64_t SWAP64(uint64_t val)
{
  return
      ((val & 0x00000000000000ff) << 56)
    | ((val & 0x000000000000ff00) << 40)
    | ((val & 0x0000000000ff0000) << 24)
    | ((val & 0x00000000ff000000) << 8)
    | ((val & 0x000000ff00000000) >> 8)
    | ((val & 0x0000ff0000000000) >> 24)
    | ((val & 0x00ff000000000000) >> 40)
    | ((val & 0xff00000000000000) >> 56);
}
#else
static INLINE uint64_t SWAP64(uint64_t val)
{
  return   ((val & 0x00000000000000ffULL) << 56)
	 | ((val & 0x000000000000ff00ULL) << 40)
	 | ((val & 0x0000000000ff0000ULL) << 24)
	 | ((val & 0x00000000ff000000ULL) << 8)
	 | ((val & 0x000000ff00000000ULL) >> 8)
	 | ((val & 0x0000ff0000000000ULL) >> 24)
	 | ((val & 0x00ff000000000000ULL) >> 40)
         | ((val & 0xff00000000000000ULL) >> 56);
}
#endif


#if defined (LSB_FIRST) || defined (MSB_FIRST)
#  warning Defining MSB_FIRST and LSB_FIRST in compile options is deprecated
#  undef LSB_FIRST
#  undef MSB_FIRST
#endif

#ifdef _MSC_VER
/* MSVC pre-defines macros depending on target arch */
#if defined (_M_IX86) || defined (_M_AMD64) || defined (_M_ARM) || defined (_M_ARM64)
#define LSB_FIRST 1
#elif _M_PPC
#define MSB_FIRST 1
#else
/* MSVC can run on _M_ALPHA and _M_IA64 too, but they're both bi-endian; need to find what mode MSVC runs them at */
#error "unknown platform, can't determine endianness"
#endif
#else
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define MSB_FIRST 1
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define LSB_FIRST 1
#else
#error "Invalid endianness macros"
#endif
#endif

#if defined(MSB_FIRST) && defined(LSB_FIRST)
#  error "Bug in LSB_FIRST/MSB_FIRST definition"
#endif

#if !defined(MSB_FIRST) && !defined(LSB_FIRST)
#  error "Bug in LSB_FIRST/MSB_FIRST definition"
#endif

#ifdef MSB_FIRST
#  define RETRO_IS_BIG_ENDIAN 1
#  define RETRO_IS_LITTLE_ENDIAN 0
/* For compatibility */
#  define WORDS_BIGENDIAN 1
#else
#  define RETRO_IS_BIG_ENDIAN 0
#  define RETRO_IS_LITTLE_ENDIAN 1
/* For compatibility */
#  undef WORDS_BIGENDIAN
#endif


/**
 * is_little_endian:
 *
 * Checks if the system is little endian or big-endian.
 *
 * Returns: greater than 0 if little-endian,
 * otherwise big-endian.
 **/
#define is_little_endian() RETRO_IS_LITTLE_ENDIAN

/**
 * swap_if_big64:
 * @val        : unsigned 64-bit value
 *
 * Byteswap unsigned 64-bit value if system is big-endian.
 *
 * Returns: Byteswapped value in case system is big-endian,
 * otherwise returns same value.
 **/

#if RETRO_IS_BIG_ENDIAN
#define swap_if_big64(val) (SWAP64(val))
#elif RETRO_IS_LITTLE_ENDIAN
#define swap_if_big64(val) (val)
#endif

/**
 * swap_if_big32:
 * @val        : unsigned 32-bit value
 *
 * Byteswap unsigned 32-bit value if system is big-endian.
 *
 * Returns: Byteswapped value in case system is big-endian,
 * otherwise returns same value.
 **/

#if RETRO_IS_BIG_ENDIAN
#define swap_if_big32(val) (SWAP32(val))
#elif RETRO_IS_LITTLE_ENDIAN
#define swap_if_big32(val) (val)
#endif

/**
 * swap_if_little64:
 * @val        : unsigned 64-bit value
 *
 * Byteswap unsigned 64-bit value if system is little-endian.
 *
 * Returns: Byteswapped value in case system is little-endian,
 * otherwise returns same value.
 **/

#if RETRO_IS_BIG_ENDIAN
#define swap_if_little64(val) (val)
#elif RETRO_IS_LITTLE_ENDIAN
#define swap_if_little64(val) (SWAP64(val))
#endif

/**
 * swap_if_little32:
 * @val        : unsigned 32-bit value
 *
 * Byteswap unsigned 32-bit value if system is little-endian.
 *
 * Returns: Byteswapped value in case system is little-endian,
 * otherwise returns same value.
 **/

#if RETRO_IS_BIG_ENDIAN
#define swap_if_little32(val) (val)
#elif RETRO_IS_LITTLE_ENDIAN
#define swap_if_little32(val) (SWAP32(val))
#endif

/**
 * swap_if_big16:
 * @val        : unsigned 16-bit value
 *
 * Byteswap unsigned 16-bit value if system is big-endian.
 *
 * Returns: Byteswapped value in case system is big-endian,
 * otherwise returns same value.
 **/

#if RETRO_IS_BIG_ENDIAN
#define swap_if_big16(val) (SWAP16(val))
#elif RETRO_IS_LITTLE_ENDIAN
#define swap_if_big16(val) (val)
#endif

/**
 * swap_if_little16:
 * @val        : unsigned 16-bit value
 *
 * Byteswap unsigned 16-bit value if system is little-endian.
 *
 * Returns: Byteswapped value in case system is little-endian,
 * otherwise returns same value.
 **/

#if RETRO_IS_BIG_ENDIAN
#define swap_if_little16(val) (val)
#elif RETRO_IS_LITTLE_ENDIAN
#define swap_if_little16(val) (SWAP16(val))
#endif

/**
 * store32be:
 * @addr        : pointer to unsigned 32-bit buffer
 * @data        : unsigned 32-bit value to write
 *
 * Write data to address. Endian-safe. Byteswaps the data
 * first if necessary before storing it.
 **/
static INLINE void store32be(uint32_t *addr, uint32_t data)
{
   *addr = swap_if_little32(data);
}

/**
 * load32be:
 * @addr        : pointer to unsigned 32-bit buffer
 *
 * Load value from address. Endian-safe.
 *
 * Returns: value from address, byte-swapped if necessary.
 **/
static INLINE uint32_t load32be(const uint32_t *addr)
{
   return swap_if_little32(*addr);
}

/**
 * retro_cpu_to_le16:
 * @val        : unsigned 16-bit value
 *
 * Convert unsigned 16-bit value from system to little-endian.
 *
 * Returns: Little-endian representation of val.
 **/

#define retro_cpu_to_le16(val) swap_if_big16(val)

/**
 * retro_cpu_to_le32:
 * @val        : unsigned 32-bit value
 *
 * Convert unsigned 32-bit value from system to little-endian.
 *
 * Returns: Little-endian representation of val.
 **/

#define retro_cpu_to_le32(val) swap_if_big32(val)

/**
 * retro_cpu_to_le64:
 * @val        : unsigned 64-bit value
 *
 * Convert unsigned 64-bit value from system to little-endian.
 *
 * Returns: Little-endian representation of val.
 **/

#define retro_cpu_to_le64(val) swap_if_big64(val)

/**
 * retro_le_to_cpu16:
 * @val        : unsigned 16-bit value
 *
 * Convert unsigned 16-bit value from little-endian to native.
 *
 * Returns: Native representation of little-endian val.
 **/

#define retro_le_to_cpu16(val) swap_if_big16(val)

/**
 * retro_le_to_cpu32:
 * @val        : unsigned 32-bit value
 *
 * Convert unsigned 32-bit value from little-endian to native.
 *
 * Returns: Native representation of little-endian val.
 **/

#define retro_le_to_cpu32(val) swap_if_big32(val)

/**
 * retro_le_to_cpu16:
 * @val        : unsigned 64-bit value
 *
 * Convert unsigned 64-bit value from little-endian to native.
 *
 * Returns: Native representation of little-endian val.
 **/

#define retro_le_to_cpu64(val) swap_if_big64(val)

/**
 * retro_cpu_to_be16:
 * @val        : unsigned 16-bit value
 *
 * Convert unsigned 16-bit value from system to big-endian.
 *
 * Returns: Big-endian representation of val.
 **/

#define retro_cpu_to_be16(val) swap_if_little16(val)

/**
 * retro_cpu_to_be32:
 * @val        : unsigned 32-bit value
 *
 * Convert unsigned 32-bit value from system to big-endian.
 *
 * Returns: Big-endian representation of val.
 **/

#define retro_cpu_to_be32(val) swap_if_little32(val)

/**
 * retro_cpu_to_be64:
 * @val        : unsigned 64-bit value
 *
 * Convert unsigned 64-bit value from system to big-endian.
 *
 * Returns: Big-endian representation of val.
 **/

#define retro_cpu_to_be64(val) swap_if_little64(val)

/**
 * retro_be_to_cpu16:
 * @val        : unsigned 16-bit value
 *
 * Convert unsigned 16-bit value from big-endian to native.
 *
 * Returns: Native representation of big-endian val.
 **/

#define retro_be_to_cpu16(val) swap_if_little16(val)

/**
 * retro_be_to_cpu32:
 * @val        : unsigned 32-bit value
 *
 * Convert unsigned 32-bit value from big-endian to native.
 *
 * Returns: Native representation of big-endian val.
 **/

#define retro_be_to_cpu32(val) swap_if_little32(val)

/**
 * retro_be_to_cpu64:
 * @val        : unsigned 64-bit value
 *
 * Convert unsigned 64-bit value from big-endian to native.
 *
 * Returns: Native representation of big-endian val.
 **/

#define retro_be_to_cpu64(val) swap_if_little64(val)

#ifdef  __GNUC__
/* This attribute means that the same memory may be referred through
   pointers to different size of the object (aliasing). E.g. that u8 *
   and u32 * may actually be pointing to the same object.  */
#define MAY_ALIAS  __attribute__((__may_alias__))
#else
#define MAY_ALIAS
#endif

#pragma pack(push, 1)
struct retro_unaligned_uint16_s
{
  uint16_t val;
} MAY_ALIAS;
struct retro_unaligned_uint32_s
{
  uint32_t val;
} MAY_ALIAS;
struct retro_unaligned_uint64_s
{
  uint64_t val;
} MAY_ALIAS;
#pragma pack(pop)

typedef struct retro_unaligned_uint16_s retro_unaligned_uint16_t;
typedef struct retro_unaligned_uint32_s retro_unaligned_uint32_t;
typedef struct retro_unaligned_uint64_s retro_unaligned_uint64_t;

/* L-value references to unaligned pointers.  */
#define retro_unaligned16(p) (((retro_unaligned_uint16_t *)p)->val)
#define retro_unaligned32(p) (((retro_unaligned_uint32_t *)p)->val)
#define retro_unaligned64(p) (((retro_unaligned_uint64_t *)p)->val)

/**
 * retro_get_unaligned_16be:
 * @addr        : pointer to unsigned 16-bit value
 *
 * Convert unsigned unaligned 16-bit value from big-endian to native.
 *
 * Returns: Native representation of big-endian val.
 **/

static INLINE uint16_t retro_get_unaligned_16be(void *addr) {
  return retro_be_to_cpu16(retro_unaligned16(addr));
}

/**
 * retro_get_unaligned_32be:
 * @addr        : pointer to unsigned 32-bit value
 *
 * Convert unsigned unaligned 32-bit value from big-endian to native.
 *
 * Returns: Native representation of big-endian val.
 **/

static INLINE uint32_t retro_get_unaligned_32be(void *addr) {
  return retro_be_to_cpu32(retro_unaligned32(addr));
}

/**
 * retro_get_unaligned_64be:
 * @addr        : pointer to unsigned 64-bit value
 *
 * Convert unsigned unaligned 64-bit value from big-endian to native.
 *
 * Returns: Native representation of big-endian val.
 **/

static INLINE uint64_t retro_get_unaligned_64be(void *addr) {
  return retro_be_to_cpu64(retro_unaligned64(addr));
}

/**
 * retro_get_unaligned_16le:
 * @addr        : pointer to unsigned 16-bit value
 *
 * Convert unsigned unaligned 16-bit value from little-endian to native.
 *
 * Returns: Native representation of little-endian val.
 **/

static INLINE uint16_t retro_get_unaligned_16le(void *addr) {
  return retro_le_to_cpu16(retro_unaligned16(addr));
}

/**
 * retro_get_unaligned_32le:
 * @addr        : pointer to unsigned 32-bit value
 *
 * Convert unsigned unaligned 32-bit value from little-endian to native.
 *
 * Returns: Native representation of little-endian val.
 **/

static INLINE uint32_t retro_get_unaligned_32le(void *addr) {
  return retro_le_to_cpu32(retro_unaligned32(addr));
}

/**
 * retro_get_unaligned_64le:
 * @addr        : pointer to unsigned 64-bit value
 *
 * Convert unsigned unaligned 64-bit value from little-endian to native.
 *
 * Returns: Native representation of little-endian val.
 **/

static INLINE uint64_t retro_get_unaligned_64le(void *addr) {
  return retro_le_to_cpu64(retro_unaligned64(addr));
}

/**
 * retro_set_unaligned_16le:
 * @addr        : pointer to unsigned 16-bit value
 * @val         : value to store
 *
 * Convert native value to unsigned unaligned 16-bit little-endian value
 *
 **/

static INLINE void retro_set_unaligned_16le(void *addr, uint16_t v) {
  retro_unaligned16(addr) = retro_cpu_to_le16(v);
}

/**
 * retro_set_unaligned_32le:
 * @addr        : pointer to unsigned 32-bit value
 * @val         : value to store
 *
 * Convert native value to unsigned unaligned 32-bit little-endian value
 *
 **/

static INLINE void retro_set_unaligned_32le(void *addr, uint32_t v) {
  retro_unaligned32(addr) = retro_cpu_to_le32(v);
}

/**
 * retro_set_unaligned_32le:
 * @addr        : pointer to unsigned 32-bit value
 * @val         : value to store
 *
 * Convert native value to unsigned unaligned 32-bit little-endian value
 *
 **/

static INLINE void retro_set_unaligned_64le(void *addr, uint64_t v) {
  retro_unaligned64(addr) = retro_cpu_to_le64(v);
}

/**
 * retro_set_unaligned_16be:
 * @addr        : pointer to unsigned 16-bit value
 * @val         : value to store
 *
 * Convert native value to unsigned unaligned 16-bit big-endian value
 *
 **/

static INLINE void retro_set_unaligned_16be(void *addr, uint16_t v) {
  retro_unaligned16(addr) = retro_cpu_to_be16(v);
}

/**
 * retro_set_unaligned_32be:
 * @addr        : pointer to unsigned 32-bit value
 * @val         : value to store
 *
 * Convert native value to unsigned unaligned 32-bit big-endian value
 *
 **/

static INLINE void retro_set_unaligned_32be(void *addr, uint32_t v) {
  retro_unaligned32(addr) = retro_cpu_to_be32(v);
}

/**
 * retro_set_unaligned_32be:
 * @addr        : pointer to unsigned 32-bit value
 * @val         : value to store
 *
 * Convert native value to unsigned unaligned 32-bit big-endian value
 *
 **/

static INLINE void retro_set_unaligned_64be(void *addr, uint64_t v) {
  retro_unaligned64(addr) = retro_cpu_to_be64(v);
}


#endif