Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / swap.c
blob0f531532f08f6464950af2735b8935fe97cb9d72
1 /** various ways to swap nibbles/bytes/words
2 */
3 #include <testfwk.h>
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 #ifdef __SDCC
12 typedef unsigned int uint16;
13 typedef unsigned long uint32;
14 #else
15 /* on 32 and 64 bit machines */
16 typedef unsigned short uint16;
17 typedef unsigned int uint32;
18 #endif
20 typedef union {uint16 i; unsigned char c[2];} WORD;
21 typedef union {uint32 l; unsigned char c[4];} LONG;
23 static void testSwap_4(void)
25 volatile unsigned char t=TEST_VECT_8;
26 unsigned char tt;
28 tt = t;
29 tt = SWAP_4(tt);
30 ASSERT( tt == SWAP_4(TEST_VECT_8));
33 #ifndef __SDCC_pdk14 // Lack of memory -see RFE #611
35 #define SWAP_8(x) ((((x)<<8)|((x)>>8)) & 0xffff)
37 static void testSwap_8(void)
39 volatile unsigned int t=TEST_VECT_16;
40 unsigned int tt;
41 WORD x;
43 tt = t;
44 tt = SWAP_8(tt);
45 ASSERT( tt == SWAP_8(TEST_VECT_16));
47 x.i = t;
48 x.i = SWAP_8(x.i);
49 ASSERT( x.i == SWAP_8(TEST_VECT_16));
51 #if defined (__SDCC_mcs51)
52 /* this was filed as bug #1638622 (rejected) */
53 x.i = t;
54 x.i = x.c[1] + 256*x.c[0];
55 ASSERT( x.i == SWAP_8(TEST_VECT_16));
57 /* and with OR instead of ADD */
58 x.i = t;
59 x.i = x.c[1] | 256*x.c[0];
60 ASSERT( x.i == SWAP_8(TEST_VECT_16));
62 /* swapping union with little register pressure */
64 unsigned char tmp;
65 x.i = t;
67 tmp = x.c[0];
68 x.c[0]=x.c[1];
69 x.c[1]=tmp;
71 ASSERT( x.i == SWAP_8(TEST_VECT_16));
73 #endif
77 #define SWAP_16(x) ((((x)<<16) | ((x)>>16)) & 0xffffFFFF)
79 static void testSwap_16(void)
81 volatile unsigned long t=TEST_VECT_32;
82 unsigned long tt;
83 LONG x;
85 tt = t;
86 tt = SWAP_16(tt);
87 ASSERT( tt == SWAP_16(TEST_VECT_32));
89 /* swapping union with little register pressure */
91 unsigned char c;
92 x.l = t;
94 c = x.c[0];
95 x.c[0]=x.c[2];
96 x.c[2]=c;
97 c = x.c[1];
98 x.c[1]=x.c[3];
99 x.c[3]=c;
101 ASSERT( x.l == SWAP_16(TEST_VECT_32));
105 /* now for something ugly */
106 static void testSwap_16_ptr(void)
108 #if defined (__SDCC)
109 #include <sdcc-lib.h> /* just to get _AUTOMEM or _STATMEM */
110 #if defined (__SDCC_STACK_AUTO)
111 #define MY_STATIC static
112 #else
113 #define MY_STATIC
114 #endif
115 MY_STATIC unsigned long _STATMEM tt=TEST_VECT_32;
117 /* swapping with little register pressure */
119 unsigned char c;
121 /* ugliness += 1 */
122 c = *(0+(unsigned char _STATMEM *)&tt);
123 *(0+(unsigned char _STATMEM *)&tt) = *(2+(unsigned char _STATMEM *)&tt);
124 *(2+(unsigned char _STATMEM *)&tt) = c;
125 c = *(1+(unsigned char _STATMEM *)&tt);
126 *(1+(unsigned char _STATMEM *)&tt) = *(3+(unsigned char _STATMEM *)&tt);
127 *(3+(unsigned char _STATMEM *)&tt) = c;
128 /* ugliness -= 1 */
130 ASSERT( tt == SWAP_16(TEST_VECT_32));
131 #endif
133 #endif
135 static void
136 testSwap(void)
138 testSwap_4();
139 #ifndef __SDCC_pdk14 // Lack of memory -see RFE #611
140 testSwap_8();
141 testSwap_16();
142 testSwap_16_ptr();
143 #endif