File: endian.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (802 lines) | stat: -rw-r--r-- 23,455 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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef COMMON_ENDIAN_H
#define COMMON_ENDIAN_H

#include "common/scummsys.h"


/**
 * @defgroup common_endian Endian conversions
 * @ingroup common
 *
 * @brief  Functions and macros for endian conversions and byteswap conversions.
 *
 * @details
 *  - SWAP_BYTES_??(a) - Reverse byte order
 *  - SWAP_CONSTANT_??(a) - Reverse byte order, implemented as a macro.
 *                          Use with compile-time constants only, the result will be a compile-time constant as well.
 *                          Unlike most other functions, these can be used for e.g. switch-case labels.
 *  - READ_UINT??(a)   - Read native value from pointer @p a.
 *  - READ_??_UINT??(a) - Read LE/BE value from pointer @p a and convert it to native.
 *  - WRITE_??_UINT??(a, v) - Write a native value @p v to pointer @p a with LE/BE encoding.
 *  - TO_??_??(a) - Convert native value @p v to LE/BE.
 *  - FROM_??_??(a) - Convert LE/BE value @p v to native.
 *  - CONSTANT_??_??(a) - Convert LE/BE value @p v to native, implemented as a macro.
 *                        Use with compile-time constants only, the result will be a compile-time constant as well.
 *                        Unlike most other functions these, can be used for e.g. switch-case labels.
 *
 * @{
 */

// Sanity check
#if !defined(SCUMM_LITTLE_ENDIAN) && !defined(SCUMM_BIG_ENDIAN)
#	error No endianness defined
#endif

/**
 * Swap the bytes in a 64-bit word in order to convert LE encoded data to BE
 * and vice versa. Use with compile-time constants only.
 */
#define SWAP_CONSTANT_64(a) \
	((uint64)((((a) >> 56) & 0x000000FF) | \
	          (((a) >> 40) & 0x0000FF00) | \
	          (((a) >> 24) & 0x00FF0000) | \
	          (((a) >>  8) & 0xFF000000) | \
	          (((a) & 0xFF000000) <<  8) | \
	          (((a) & 0x00FF0000) << 24) | \
	          (((a) & 0x0000FF00) << 40) | \
	          (((a) & 0x000000FF) << 56) ))

/**
 * Swap the bytes in a 32-bit word in order to convert LE encoded data to BE
 * and vice versa. Use with compile-time constants only.
 */
#define SWAP_CONSTANT_32(a) \
	((uint32)((((a) >> 24) & 0x00FF) | \
	          (((a) >>  8) & 0xFF00) | \
	          (((a) & 0xFF00) <<  8) | \
	          (((a) & 0x00FF) << 24) ))

/**
 * Swap the bytes in a 16-bit word in order to convert LE encoded data to BE
 * and vice versa. Use with compile-time constants only.
 */
#define SWAP_CONSTANT_16(a) \
	((uint16)((((a) >>  8) & 0x00FF) | \
	          (((a) <<  8) & 0xFF00) ))



/**
 * Swap the bytes in a 16-bit word in order to convert LE encoded data to BE
 * and vice versa.
 */

// compiler-specific variants come first, fallback last
#if GCC_ATLEAST(4, 8) || defined(__clang__)

	FORCEINLINE uint16 SWAP_BYTES_16(uint16 a) {
		return __builtin_bswap16(a);
	}

#elif defined(_MSC_VER)

	FORCEINLINE uint16 SWAP_BYTES_16(uint16 a) {
		return _byteswap_ushort(a);
	}

#else

	inline uint16 SWAP_BYTES_16(const uint16 a) {
		return (a >> 8) | (a << 8);
	}
#endif



/**
 * Swap the bytes in a 32-bit word in order to convert LE encoded data to BE
 * and vice versa.
 */

// compiler-specific variants come first, fallback last
#if defined(__GNUC__)

	FORCEINLINE uint32 SWAP_BYTES_32(uint32 a) {
		return __builtin_bswap32(a);
	}

#elif defined(_MSC_VER)

	FORCEINLINE uint32 SWAP_BYTES_32(uint32 a) {
		return _byteswap_ulong(a);
	}

// generic fallback
#else

	inline uint32 SWAP_BYTES_32(uint32 a) {
		const uint16 low = (uint16)a, high = (uint16)(a >> 16);
		return ((uint32)(uint16)((low >> 8) | (low << 8)) << 16)
			   | (uint16)((high >> 8) | (high << 8));
	}
#endif

/**
 * Swap the bytes in a 64-bit word in order to convert LE encoded data to BE
 * and vice versa.
 */

// compiler-specific variants come first, fallback last
#if defined(__GNUC__)

	FORCEINLINE uint64 SWAP_BYTES_64(uint64 a) {
		return __builtin_bswap64(a);
	}

#elif defined(_MSC_VER)

	FORCEINLINE uint64 SWAP_BYTES_64(uint64 a) {
		return _byteswap_uint64(a);
	}

// generic fallback
#else

	inline uint64 SWAP_BYTES_64(uint64 a) {
		uint32 low = (uint32)a, high = (uint32)(a >> 32);
		uint16 lowLow = (uint16)low, lowHigh = (uint16)(low >> 16),
		       highLow = (uint16)high, highHigh = (uint16)(high >> 16);

		return ((uint64)(((uint32)(uint16)((lowLow   >> 8) | (lowLow   << 8)) << 16) |
		                          (uint16)((lowHigh  >> 8) | (lowHigh  << 8))) << 32) |
		                (((uint32)(uint16)((highLow  >> 8) | (highLow  << 8)) << 16) |
		                          (uint16)((highHigh >> 8) | (highHigh << 8)));
	}
#endif



/**
 * A wrapper macro used around four character constants, like 'DATA', to
 * ensure portability. Typical usage: MKTAG('D','A','T','A').
 *
 * This is required because the C/C++ standard does not define the endianess to
 * be used for character constants. Hence, if one uses multi-byte character
 * constants, a potential portability problem opens up.
 */
#define MKTAG(a0,a1,a2,a3) ((uint32)((a3) | ((a2) << 8) | ((a1) << 16) | ((a0) << 24)))

/**
 * A wrapper macro used around two character constants, like 'wb', to
 * ensure portability. Typical usage: MKTAG16('w','b').
 */
#define MKTAG16(a0,a1) ((uint16)((a1) | ((a0) << 8)))

/** @name Functions for reading and writing native integers
 *  @brief Functions for reading and writing native integer values.
 *         They also transparently handle the need for alignment.
 *  @{
 */

// Test for GCC and compatible. These implementations will automatically use
// CPU-specific instructions for unaligned data when they are available (eg.
// MIPS).
#if defined(__GNUC__)

	FORCEINLINE uint16 READ_UINT16(const void *ptr) {
		struct Unaligned16 { uint16 val; } __attribute__ ((__packed__, __may_alias__));
		return ((const Unaligned16 *)ptr)->val;
	}

	FORCEINLINE uint32 READ_UINT32(const void *ptr) {
		struct Unaligned32 { uint32 val; } __attribute__ ((__packed__, __may_alias__));
		return ((const Unaligned32 *)ptr)->val;
	}

	FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) {
		struct Unaligned16 { uint16 val; } __attribute__ ((__packed__, __may_alias__));
		((Unaligned16 *)ptr)->val = value;
	}

	FORCEINLINE void WRITE_UINT32(void *ptr, uint32 value) {
		struct Unaligned32 { uint32 val; } __attribute__ ((__packed__, __may_alias__));
		((Unaligned32 *)ptr)->val = value;
	}

	FORCEINLINE uint64 READ_UINT64(const void *ptr) {
		struct Unaligned64 { uint64 val; } __attribute__ ((__packed__, __may_alias__));
		return ((const Unaligned64 *)ptr)->val;
	}

	FORCEINLINE void WRITE_UINT64(void *ptr, uint64 value) {
		struct Unaligned64 { uint64 val; } __attribute__((__packed__, __may_alias__));
		((Unaligned64 *)ptr)->val = value;
	}

#elif !defined(SCUMM_NEED_ALIGNMENT)

	FORCEINLINE uint16 READ_UINT16(const void *ptr) {
		return *(const uint16 *)(ptr);
	}

	FORCEINLINE uint32 READ_UINT32(const void *ptr) {
		return *(const uint32 *)(ptr);
	}

	FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) {
		*(uint16 *)(ptr) = value;
	}

	FORCEINLINE void WRITE_UINT32(void *ptr, uint32 value) {
		*(uint32 *)(ptr) = value;
	}

	FORCEINLINE uint64 READ_UINT64(const void *ptr) {
		return *(const uint64 *)(ptr);
	}

	FORCEINLINE void WRITE_UINT64(void *ptr, uint64 value) {
		*(uint64 *)(ptr) = value;
	}


// use software fallback by loading each byte explicitely
#else

#	if defined(SCUMM_LITTLE_ENDIAN)

		inline uint16 READ_UINT16(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return (b[1] << 8) | b[0];
		}
		inline uint32 READ_UINT32(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
		}
		inline void WRITE_UINT16(void *ptr, uint16 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >> 0);
			b[1] = (uint8)(value >> 8);
		}
		inline void WRITE_UINT32(void *ptr, uint32 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >>  0);
			b[1] = (uint8)(value >>  8);
			b[2] = (uint8)(value >> 16);
			b[3] = (uint8)(value >> 24);
		}
		inline uint64 READ_UINT64(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return ((uint64)b[7] << 56) | ((uint64)b[6] << 48) | ((uint64)b[5] << 40) | ((uint64)b[4] << 32) | ((uint64)b[3] << 24) | ((uint64)b[2] << 16) | ((uint64)b[1] << 8) | ((uint64)b[0]);
		}
		inline void WRITE_UINT64(void *ptr, uint64 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >>  0);
			b[1] = (uint8)(value >>  8);
			b[2] = (uint8)(value >> 16);
			b[3] = (uint8)(value >> 24);
			b[4] = (uint8)(value >> 32);
			b[5] = (uint8)(value >> 40);
			b[6] = (uint8)(value >> 48);
			b[7] = (uint8)(value >> 56);
		}

#	elif defined(SCUMM_BIG_ENDIAN)

		inline uint16 READ_UINT16(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return (b[0] << 8) | b[1];
		}
		inline uint32 READ_UINT32(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
		}
		inline void WRITE_UINT16(void *ptr, uint16 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >> 8);
			b[1] = (uint8)(value >> 0);
		}
		inline void WRITE_UINT32(void *ptr, uint32 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >> 24);
			b[1] = (uint8)(value >> 16);
			b[2] = (uint8)(value >>  8);
			b[3] = (uint8)(value >>  0);
		}
		inline uint64 READ_UINT64(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return ((uint64)b[0] << 56) | ((uint64)b[1] << 48) | ((uint64)b[2] << 40) | ((uint64)b[3] << 32) | ((uint64)b[4] << 24) | ((uint64)b[5] << 16) | ((uint64)b[6] << 8) | ((uint64)b[7]);
		}
		inline void WRITE_UINT64(void *ptr, uint64 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >> 56);
			b[1] = (uint8)(value >> 48);
			b[2] = (uint8)(value >> 40);
			b[3] = (uint8)(value >> 32);
			b[4] = (uint8)(value >> 24);
			b[5] = (uint8)(value >> 16);
			b[6] = (uint8)(value >>  8);
			b[7] = (uint8)(value >>  0);
		}

#	endif
/** @} */
#endif


/** @name  Map functions for reading/writing BE/LE integers depending on native endianess
 * @{
 */

#if defined(SCUMM_LITTLE_ENDIAN)

	#define READ_LE_UINT16(a) READ_UINT16(a)
	#define READ_LE_UINT32(a) READ_UINT32(a)

	#define WRITE_LE_UINT16(a, v) WRITE_UINT16(a, v)
	#define WRITE_LE_UINT32(a, v) WRITE_UINT32(a, v)

	#define FROM_LE_32(a) ((uint32)(a))
	#define FROM_LE_16(a) ((uint16)(a))

	#define FROM_BE_32(a) SWAP_BYTES_32(a)
	#define FROM_BE_16(a) SWAP_BYTES_16(a)

	#define TO_LE_32(a) ((uint32)(a))
	#define TO_LE_16(a) ((uint16)(a))

	#define TO_BE_32(a) SWAP_BYTES_32(a)
	#define TO_BE_16(a) SWAP_BYTES_16(a)

	#define CONSTANT_LE_32(a) ((uint32)(a))
	#define CONSTANT_LE_16(a) ((uint16)(a))

	#define CONSTANT_BE_32(a) SWAP_CONSTANT_32(a)
	#define CONSTANT_BE_16(a) SWAP_CONSTANT_16(a)

	#define READ_LE_UINT64(a) READ_UINT64(a)
	#define WRITE_LE_UINT64(a, v) WRITE_UINT64(a, v)
	#define FROM_LE_64(a) ((uint64)(a))
	#define FROM_BE_64(a) SWAP_BYTES_64(a)
	#define TO_LE_64(a) ((uint64)(a))
	#define TO_BE_64(a) SWAP_BYTES_64(a)
	#define CONSTANT_LE_64(a) ((uint64)(a))
	#define CONSTANT_BE_64(a) SWAP_CONSTANT_64(a)
/** @} */

/** @name  Functions for directly reading/writing and inverting
 *  @brief Use these in case the unaligned load and byteswap take
 *         a lot of instructions.
 * @{
 */
#	if defined(SCUMM_NEED_ALIGNMENT) && !defined(__mips__)

		inline uint16 READ_BE_UINT16(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return (b[0] << 8) | b[1];
		}
		inline uint32 READ_BE_UINT32(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
		}
		inline void WRITE_BE_UINT16(void *ptr, uint16 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >> 8);
			b[1] = (uint8)(value >> 0);
		}
		inline void WRITE_BE_UINT32(void *ptr, uint32 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >> 24);
			b[1] = (uint8)(value >> 16);
			b[2] = (uint8)(value >>  8);
			b[3] = (uint8)(value >>  0);
		}
		inline uint64 READ_BE_UINT64(const void *ptr) {
			const uint8 *b = (const uint8 *)ptr;
			return ((uint64)b[0] << 56) | ((uint64)b[1] << 48) | ((uint64)b[2] << 40) | ((uint64)b[3] << 32) | ((uint64)b[4] << 24) | ((uint64)b[5] << 16) | ((uint64)b[6] << 8) | ((uint64)b[7]);
		}
		inline void WRITE_BE_UINT64(void *ptr, uint64 value) {
			uint8 *b = (uint8 *)ptr;
			b[0] = (uint8)(value >> 56);
			b[1] = (uint8)(value >> 48);
			b[2] = (uint8)(value >> 40);
			b[3] = (uint8)(value >> 32);
			b[4] = (uint8)(value >> 24);
			b[5] = (uint8)(value >> 16);
			b[6] = (uint8)(value >> 8);
			b[7] = (uint8)(value >> 0);
		}

#	else

		inline uint16 READ_BE_UINT16(const void *ptr) {
			return SWAP_BYTES_16(READ_UINT16(ptr));
		}
		inline uint32 READ_BE_UINT32(const void *ptr) {
			return SWAP_BYTES_32(READ_UINT32(ptr));
		}
		inline void WRITE_BE_UINT16(void *ptr, uint16 value) {
			WRITE_UINT16(ptr, SWAP_BYTES_16(value));
		}
		inline void WRITE_BE_UINT32(void *ptr, uint32 value) {
			WRITE_UINT32(ptr, SWAP_BYTES_32(value));
		}
		inline uint64 READ_BE_UINT64(const void *ptr) {
			return SWAP_BYTES_64(READ_UINT64(ptr));
		}
		inline void WRITE_BE_UINT64(void *ptr, uint64 value) {
			WRITE_UINT64(ptr, SWAP_BYTES_64(value));
		}

#	endif	// if defined(SCUMM_NEED_ALIGNMENT)

#elif defined(SCUMM_BIG_ENDIAN)

	#define READ_BE_UINT16(a) READ_UINT16(a)
	#define READ_BE_UINT32(a) READ_UINT32(a)

	#define WRITE_BE_UINT16(a, v) WRITE_UINT16(a, v)
	#define WRITE_BE_UINT32(a, v) WRITE_UINT32(a, v)

	#define FROM_LE_32(a) SWAP_BYTES_32(a)
	#define FROM_LE_16(a) SWAP_BYTES_16(a)

	#define FROM_BE_32(a) ((uint32)(a))
	#define FROM_BE_16(a) ((uint16)(a))

	#define TO_LE_32(a) SWAP_BYTES_32(a)
	#define TO_LE_16(a) SWAP_BYTES_16(a)

	#define TO_BE_32(a) ((uint32)(a))
	#define TO_BE_16(a) ((uint16)(a))

	#define CONSTANT_LE_32(a) SWAP_CONSTANT_32(a)
	#define CONSTANT_LE_16(a) SWAP_CONSTANT_16(a)

	#define CONSTANT_BE_32(a) ((uint32)(a))
	#define CONSTANT_BE_16(a) ((uint16)(a))

	#define READ_BE_UINT64(a) READ_UINT64(a)
	#define WRITE_BE_UINT64(a, v) WRITE_UINT64(a, v)
	#define FROM_LE_64(a) SWAP_BYTES_64(a)
	#define FROM_BE_64(a) ((uint64)(a))
	#define TO_LE_64(a) SWAP_BYTES_64(a)
	#define TO_BE_64(a) ((uint64)(a))
	#define CONSTANT_LE_64(a) SWAP_CONSTANT_64(a)
	#define CONSTANT_BE_64(a) ((uint64)(a))

// if the unaligned load and the byteswap take a lot of instructions its better to directly read and invert
#	if defined(SCUMM_NEED_ALIGNMENT) && !defined(__mips__)

	inline uint16 READ_LE_UINT16(const void *ptr) {
		const uint8 *b = (const uint8 *)ptr;
		return (b[1] << 8) | b[0];
	}
	inline uint32 READ_LE_UINT32(const void *ptr) {
		const uint8 *b = (const uint8 *)ptr;
		return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
	}
	inline void WRITE_LE_UINT16(void *ptr, uint16 value) {
		uint8 *b = (uint8 *)ptr;
		b[0] = (uint8)(value >> 0);
		b[1] = (uint8)(value >> 8);
	}
	inline void WRITE_LE_UINT32(void *ptr, uint32 value) {
		uint8 *b = (uint8 *)ptr;
		b[0] = (uint8)(value >>  0);
		b[1] = (uint8)(value >>  8);
		b[2] = (uint8)(value >> 16);
		b[3] = (uint8)(value >> 24);
	}

	inline uint64 READ_LE_UINT64(const void *ptr) {
		const uint8 *b = (const uint8 *)ptr;
		return ((uint64)b[7] << 56) | ((uint64)b[6] << 48) | ((uint64)b[5] << 40) | ((uint64)b[4] << 32) | ((uint64)b[3] << 24) | ((uint64)b[2] << 16) | ((uint64)b[1] << 8) | ((uint64)b[0]);
	}
	inline void WRITE_LE_UINT64(void *ptr, uint64 value) {
		uint8 *b = (uint8 *)ptr;
		b[0] = (uint8)(value >>  0);
		b[1] = (uint8)(value >>  8);
		b[2] = (uint8)(value >> 16);
		b[3] = (uint8)(value >> 24);
		b[4] = (uint8)(value >> 32);
		b[5] = (uint8)(value >> 40);
		b[6] = (uint8)(value >> 48);
		b[7] = (uint8)(value >> 56);
	}

#	else

	inline uint16 READ_LE_UINT16(const void *ptr) {
		return SWAP_BYTES_16(READ_UINT16(ptr));
	}
	inline uint32 READ_LE_UINT32(const void *ptr) {
		return SWAP_BYTES_32(READ_UINT32(ptr));
	}
	inline void WRITE_LE_UINT16(void *ptr, uint16 value) {
		WRITE_UINT16(ptr, SWAP_BYTES_16(value));
	}
	inline void WRITE_LE_UINT32(void *ptr, uint32 value) {
		WRITE_UINT32(ptr, SWAP_BYTES_32(value));
	}
	inline uint64 READ_LE_UINT64(const void *ptr) {
		return SWAP_BYTES_64(READ_UINT64(ptr));
	}
	inline void WRITE_LE_UINT64(void *ptr, uint64 value) {
		WRITE_UINT64(ptr, SWAP_BYTES_64(value));
	}

#	endif	// if defined(SCUMM_NEED_ALIGNMENT)

#endif	// if defined(SCUMM_LITTLE_ENDIAN)

inline uint32 READ_LE_UINT24(const void *ptr) {
	const uint8 *b = (const uint8 *)ptr;
	return (b[2] << 16) | (b[1] << 8) | (b[0]);
}

inline void WRITE_LE_UINT24(void *ptr, uint32 value) {
	uint8 *b = (uint8 *)ptr;
	b[0] = (uint8)(value >> 0);
	b[1] = (uint8)(value >> 8);
	b[2] = (uint8)(value >> 16);
}

inline uint32 READ_BE_UINT24(const void *ptr) {
	const uint8 *b = (const uint8 *)ptr;
	return (b[0] << 16) | (b[1] << 8) | (b[2]);
}

inline void WRITE_BE_UINT24(void *ptr, uint32 value) {
	uint8 *b = (uint8 *)ptr;
	b[0] = (uint8)(value >> 16);
	b[1] = (uint8)(value >>  8);
	b[2] = (uint8)(value >>  0);
}

#ifdef SCUMM_LITTLE_ENDIAN
#define READ_UINT24(a) READ_LE_UINT24(a)
#define WRITE_UINT24(a,b) WRITE_LE_UINT24(a,b)
#else
#define READ_UINT24(a) READ_BE_UINT24(a)
#define WRITE_UINT24(a,b) WRITE_BE_UINT24(a,b)
#endif

union SwapFloat {
	float f;
	uint32 u32;
};

STATIC_ASSERT(sizeof(float) == sizeof(uint32), Unexpected_size_of_float);

inline float READ_LE_FLOAT32(const void *ptr) {
	SwapFloat swap;
	swap.u32 = READ_LE_UINT32(ptr);
	return swap.f;
}

inline void WRITE_LE_FLOAT32(void *ptr, float value) {
	SwapFloat swap;
	swap.f = value;
	WRITE_LE_UINT32(ptr, swap.u32);
}

inline float READ_BE_FLOAT32(const void *ptr) {
	SwapFloat swap;
	swap.u32 = READ_BE_UINT32(ptr);
	return swap.f;
}

inline void WRITE_BE_FLOAT32(void *ptr, float value) {
	SwapFloat swap;
	swap.f = value;
	WRITE_BE_UINT32(ptr, swap.u32);
}

#ifdef SCUMM_LITTLE_ENDIAN
#define READ_FLOAT32(a) READ_LE_FLOAT32(a)
#define WRITE_FLOAT32(a,b) WRITE_LE_FLOAT32(a,b)
#else
#define READ_FLOAT32(a) READ_BE_FLOAT32(a)
#define WRITE_FLOAT32(a,b) WRITE_BE_FLOAT32(a,b)
#endif

#ifdef SCUMM_FLOAT_WORD_LITTLE_ENDIAN
union SwapDouble {
	double d;
	uint64 u64;
	struct {
		uint32 low, high;
	} u32;
};
#else
union SwapDouble {
	double d;
	uint64 u64;
	struct {
		uint32 high, low;
	} u32;
};
#endif

STATIC_ASSERT(sizeof(double) == sizeof(uint64) || sizeof(double) == sizeof(uint32), Unexpected_size_of_double);

template<size_t n> inline double READ_DOUBLE(const SwapDouble& sw);
template<size_t n> inline void WRITE_DOUBLE(SwapDouble &sw, double d);

// 64-bit double
template<> inline double READ_DOUBLE<sizeof(uint64)>(const SwapDouble& sd)
{
  return sd.d;
}

template<> inline void WRITE_DOUBLE<sizeof(uint64)>(SwapDouble &sd, double d)
{
  sd.d = d;
}

// 32-bit double
template<> inline double READ_DOUBLE<sizeof(uint32)>(const SwapDouble& sd)
{
  SwapFloat sf;
  uint32 e = (sd.u32.high >> 20) & 0x7ff;
  if (e <= 896) {
    // Too small for normalized, create a zero with the correct sign
    // (FIXME: Create denormalized numbers instead when possible?)
    sf.u32 = (sd.u32.high & 0x80000000U); // sign bit
    return sf.f;
  } else if(e >= 1151) {
    // Overflow, infinity or NaN
    if (e < 2047) {
      // Overflow; make sure result is infinity and not NaN
      sf.u32 = (sd.u32.high & 0x80000000U) | // sign bit
        (255 << 23); // exponent
      return sf.f;
    }
    e = 255;
  } else
    e -= 896;
  sf.u32 = (sd.u32.high & 0x80000000U) | // sign bit
    (e << 23) | // exponent
    ((sd.u32.high & 0xfffff) << 3) | (sd.u32.low >> 29); // mantissa
  return sf.f;
}

template<> inline void WRITE_DOUBLE<sizeof(uint32)>(SwapDouble &sd, double d)
{
  SwapFloat sf;
  sf.f = d;
  uint32 e = (sf.u32 >> 23) & 0xff;
  if (!e) {
    // Denormalized or zero, create a zero with the correct sign
    // (FIXME: Convert denormalized 32-bit to normalized 64-bit?)
    sd.u32.high = (sf.u32 & 0x80000000U); // sign bit
    sd.u32.low = 0;
    return;
  } else if (e == 255) {
    // Infinity or NaN
    e = 2047;
  } else
    e += 896;
  sd.u32.high = (sf.u32 & 0x80000000U) | // sign bit
    (e << 20) | // exponent
    ((sf.u32 >> 3) & 0xfffff); // mantissa
  sd.u32.low = sf.u32 << 29;
}

inline double READ_LE_FLOAT64(const void *ptr) {
	SwapDouble swap;
	const uint8 *b = (const uint8 *)ptr;
	swap.u32.low  = READ_LE_UINT32(b);
	swap.u32.high = READ_LE_UINT32(b + 4);
	return READ_DOUBLE<sizeof(double)>(swap);
}

inline void WRITE_LE_FLOAT64(void *ptr, double value) {
	SwapDouble swap;
	WRITE_DOUBLE<sizeof(double)>(swap, value);
	uint8 *b = (uint8 *)ptr;
	WRITE_LE_UINT32(b,     swap.u32.low);
	WRITE_LE_UINT32(b + 4, swap.u32.high);
}

inline double READ_BE_FLOAT64(const void *ptr) {
	SwapDouble swap;
	const uint8 *b = (const uint8 *)ptr;
	swap.u32.high = READ_BE_UINT32(b);
	swap.u32.low  = READ_BE_UINT32(b + 4);
	return READ_DOUBLE<sizeof(double)>(swap);
}

inline void WRITE_BE_FLOAT64(void *ptr, double value) {
	SwapDouble swap;
	WRITE_DOUBLE<sizeof(double)>(swap, value);
	uint8 *b = (uint8 *)ptr;
	WRITE_BE_UINT32(b,     swap.u32.high);
	WRITE_BE_UINT32(b + 4, swap.u32.low);
}

inline double READ_FPA_FLOAT64(const void *ptr) {
	SwapDouble swap;
	const uint8 *b = (const uint8 *)ptr;
	swap.u32.high = READ_LE_UINT32(b);
	swap.u32.low  = READ_LE_UINT32(b + 4);
	return READ_DOUBLE<sizeof(double)>(swap);
}

inline void WRITE_FPA_FLOAT64(void *ptr, double value) {
	SwapDouble swap;
	WRITE_DOUBLE<sizeof(double)>(swap, value);
	uint8 *b = (uint8 *)ptr;
	WRITE_LE_UINT32(b,     swap.u32.high);
	WRITE_LE_UINT32(b + 4, swap.u32.low);
}

inline double READ_FLOAT64(const void *ptr) {
	SwapDouble swap;
	swap.u64 = READ_UINT64(ptr);
	return READ_DOUBLE<sizeof(double)>(swap);
}

inline void WRITE_FLOAT64(void *ptr, double value) {
	SwapDouble swap;
	WRITE_DOUBLE<sizeof(double)>(swap, value);
	WRITE_UINT64(ptr, swap.u64);
}

inline int16 READ_LE_INT16(const void *ptr) {
	return static_cast<int16>(READ_LE_UINT16(ptr));
}

inline void WRITE_LE_INT16(void *ptr, int16 value) {
	WRITE_LE_UINT16(ptr, static_cast<uint16>(value));
}

inline int16 READ_BE_INT16(const void *ptr) {
	return static_cast<int16>(READ_BE_UINT16(ptr));
}

inline void WRITE_BE_INT16(void *ptr, int16 value) {
	WRITE_BE_UINT16(ptr, static_cast<uint16>(value));
}

inline int32 READ_LE_INT32(const void *ptr) {
	return static_cast<int32>(READ_LE_UINT32(ptr));
}

inline void WRITE_LE_INT32(void *ptr, int32 value) {
	WRITE_LE_UINT32(ptr, static_cast<uint32>(value));
}

inline int32 READ_BE_INT32(const void *ptr) {
	return static_cast<int32>(READ_BE_UINT32(ptr));
}

inline void WRITE_BE_INT32(void *ptr, int32 value) {
	WRITE_BE_UINT32(ptr, static_cast<uint32>(value));
}
/** @} */
/** @} */

#endif