1 /** various ways to swap nibbles/bytes/words
5 #define TEST_VECT_8 0x12
6 #define TEST_VECT_16 0x1234
7 #define TEST_VECT_32 0x12345678
9 #define SWAP_4(x) ((unsigned char)((x)<<4)|(unsigned char)((x)>>4))
11 typedef union {unsigned int i; unsigned char c[2];} WORD;
12 typedef union {unsigned long l; unsigned char c[4];} LONG;
14 static void testSwap_4(void)
16 volatile unsigned char t=TEST_VECT_8;
21 ASSERT( tt == SWAP_4(TEST_VECT_8));
25 #define SWAP_8(x) ((((x)<<8)|((x)>>8)) & 0xffff)
27 static void testSwap_8(void)
29 volatile unsigned int t=TEST_VECT_16;
35 ASSERT( tt == SWAP_8(TEST_VECT_16));
39 ASSERT( x.i == SWAP_8(TEST_VECT_16));
41 #if defined (SDCC_mcs51)
42 /* this was filed as bug #1638622 (rejected) */
44 x.i = x.c[1] + 256*x.c[0];
45 ASSERT( x.i == SWAP_8(TEST_VECT_16));
47 /* and with OR instead of ADD */
49 x.i = x.c[1] | 256*x.c[0];
50 ASSERT( x.i == SWAP_8(TEST_VECT_16));
52 /* swapping union with little register pressure */
61 ASSERT( x.i == SWAP_8(TEST_VECT_16));
67 #define SWAP_16(x) ((((x)<<16) | ((x)>>16)) & 0xffffFFFF)
69 static void testSwap_16(void)
71 volatile unsigned long t=TEST_VECT_32;
77 ASSERT( tt == SWAP_16(TEST_VECT_32));
79 /* swapping union with little register pressure */
91 ASSERT( x.l == SWAP_16(TEST_VECT_32));
95 /* now for something ugly */
96 static void testSwap_16_ptr(void)
99 #include <sdcc-lib.h> /* just to get _AUTOMEM or _STATMEM */
100 #if defined (SDCC_STACK_AUTO)
101 #define MY_STATIC static
105 MY_STATIC unsigned long _STATMEM tt=TEST_VECT_32;
107 /* swapping with little register pressure */
112 c = *(0+(unsigned char _STATMEM *)&tt);
113 *(0+(unsigned char _STATMEM *)&tt) = *(2+(unsigned char _STATMEM *)&tt);
114 *(2+(unsigned char _STATMEM *)&tt) = c;
115 c = *(1+(unsigned char _STATMEM *)&tt);
116 *(1+(unsigned char _STATMEM *)&tt) = *(3+(unsigned char _STATMEM *)&tt);
117 *(3+(unsigned char _STATMEM *)&tt) = c;
120 ASSERT( tt == SWAP_16(TEST_VECT_32));