1 /* $Id: byteswap.h,v 1.11 2004-04-22 21:05:16 btb 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
27 * Revision 1.4 1995/08/23 21:28:15 allender
28 * fix mcc compiler warning
30 * Revision 1.3 1995/08/18 15:51:42 allender
31 * put back in old byteswapping code
33 * Revision 1.2 1995/05/04 20:10:18 allender
36 * Revision 1.1 1995/03/30 15:02:11 allender
46 #define SWAPSHORT(x) (((ubyte)(x) << 8) | (((ushort)(x)) >> 8))
47 #define SWAPINT(x) (((x)<<24) | (((uint)(x)) >> 24) | (((x) &0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8))
49 #ifndef WORDS_BIGENDIAN
50 #define INTEL_INT(x) x
51 #define INTEL_SHORT(x) x
52 #else // ! WORDS_BIGENDIAN
53 #define INTEL_INT(x) SWAPINT(x)
54 #define INTEL_SHORT(x) SWAPSHORT(x)
55 #endif // ! WORDS_BIGENDIAN
57 #ifndef WORDS_NEED_ALIGNMENT
58 #define GET_INTEL_INT(s) INTEL_INT(*(uint *)(s))
59 #define GET_INTEL_SHORT(s) INTEL_SHORT(*(ushort *)(s))
60 #define PUT_INTEL_INT(d, s) { *(uint *)(d) = INTEL_INT((uint)(s)); }
61 #define PUT_INTEL_SHORT(d, s) { *(ushort *)(d) = INTEL_SHORT((ushort)(s)); }
62 #else // ! WORDS_NEED_ALIGNMENT
63 static inline uint GET_INTEL_INT(void *s)
66 memcpy((void *)&tmp, s, 4);
67 return INTEL_INT(tmp);
69 static inline uint GET_INTEL_SHORT(void *s)
72 memcpy((void *)&tmp, s, 2);
73 return INTEL_SHORT(tmp);
75 #define PUT_INTEL_INT(d, s) { uint tmp = INTEL_INT(s); \
76 memcpy((void *)(d), (void *)&tmp, 4); }
77 #define PUT_INTEL_SHORT(d, s) { ushort tmp = INTEL_SHORT(s); \
78 memcpy((void *)(d), (void *)&tmp, 2); }
79 #endif // ! WORDS_NEED_ALIGNMENT
81 #endif // ! _BYTESWAP_H