1 /* $Id: byteswap.h,v 1.12 2004-08-28 23:17:45 schaffner Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * code to swap bytes because of big/little endian problems.
18 * contains the macros:
19 * SWAP{INT,SHORT}(x): returns a swapped version of x
20 * INTEL_{INT,SHORT}(x): returns x after conversion to/from little endian
21 * GET_INTEL_{INT,SHORT}(src): gets value from little-endian buffer src
22 * PUT_INTEL_{INT,SHORT}(dest, src): puts src into little-endian buffer dest
24 * the GET/PUT macros are safe to use on platforms which segfault on unaligned word access
33 #define SWAPSHORT(x) (((ubyte)(x) << 8) | (((ushort)(x)) >> 8))
34 #define SWAPINT(x) (((x)<<24) | (((uint)(x)) >> 24) | (((x) &0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8))
36 #ifndef WORDS_BIGENDIAN
37 #define INTEL_INT(x) x
38 #define INTEL_SHORT(x) x
39 #else // ! WORDS_BIGENDIAN
40 #define INTEL_INT(x) SWAPINT(x)
41 #define INTEL_SHORT(x) SWAPSHORT(x)
42 #endif // ! WORDS_BIGENDIAN
44 #ifndef WORDS_NEED_ALIGNMENT
45 #define GET_INTEL_INT(s) INTEL_INT(*(uint *)(s))
46 #define GET_INTEL_SHORT(s) INTEL_SHORT(*(ushort *)(s))
47 #define PUT_INTEL_INT(d, s) { *(uint *)(d) = INTEL_INT((uint)(s)); }
48 #define PUT_INTEL_SHORT(d, s) { *(ushort *)(d) = INTEL_SHORT((ushort)(s)); }
49 #else // ! WORDS_NEED_ALIGNMENT
50 static inline uint GET_INTEL_INT(void *s)
53 memcpy((void *)&tmp, s, 4);
54 return INTEL_INT(tmp);
56 static inline uint GET_INTEL_SHORT(void *s)
59 memcpy((void *)&tmp, s, 2);
60 return INTEL_SHORT(tmp);
62 #define PUT_INTEL_INT(d, s) { uint tmp = INTEL_INT(s); \
63 memcpy((void *)(d), (void *)&tmp, 4); }
64 #define PUT_INTEL_SHORT(d, s) { ushort tmp = INTEL_SHORT(s); \
65 memcpy((void *)(d), (void *)&tmp, 2); }
66 #endif // ! WORDS_NEED_ALIGNMENT
68 #endif // ! _BYTESWAP_H