Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / tests / swap.c
1 /** various ways to swap nibbles/bytes/words
2 */
3 #include <testfwk.h>
4
5 #define TEST_VECT_8 0x12
6 #define TEST_VECT_16 0x1234
7 #define TEST_VECT_32 0x12345678
8
9 #define SWAP_4(x) ((unsigned char)((x)<<4)|(unsigned char)((x)>>4))
10     
11 typedef union {unsigned  int i; unsigned char c[2];} WORD;
12 typedef union {unsigned long l; unsigned char c[4];} LONG;
13
14 static void testSwap_4(void)
15 {
16     volatile unsigned char t=TEST_VECT_8;
17     unsigned char tt;
18
19     tt = t;
20     tt = SWAP_4(tt);
21     ASSERT( tt == SWAP_4(TEST_VECT_8));
22 }
23
24
25 #define SWAP_8(x) ((((x)<<8)|((x)>>8)) & 0xffff)
26
27 static void testSwap_8(void)
28 {
29     volatile unsigned int t=TEST_VECT_16;
30     unsigned int tt;
31     WORD x;
32
33     tt = t;
34     tt = SWAP_8(tt);
35     ASSERT( tt == SWAP_8(TEST_VECT_16));
36
37     x.i = t;
38     x.i = SWAP_8(x.i);
39     ASSERT( x.i == SWAP_8(TEST_VECT_16));
40
41 #if defined (SDCC_mcs51)
42     /* this was filed as bug #1638622 (rejected) */
43     x.i = t;
44     x.i = x.c[1] + 256*x.c[0];
45     ASSERT( x.i == SWAP_8(TEST_VECT_16));
46
47     /* and with OR instead of ADD */
48     x.i = t;
49     x.i = x.c[1] | 256*x.c[0];
50     ASSERT( x.i == SWAP_8(TEST_VECT_16));
51
52     /* swapping union with little register pressure */
53     {
54         unsigned char tmp;
55         x.i = t;
56
57         tmp = x.c[0];
58         x.c[0]=x.c[1];
59         x.c[1]=tmp;
60
61         ASSERT( x.i == SWAP_8(TEST_VECT_16));
62     }
63 #endif
64 }
65
66
67 #define SWAP_16(x) ((((x)<<16) | ((x)>>16)) & 0xffffFFFF)
68
69 static void testSwap_16(void)
70 {
71     volatile unsigned long t=TEST_VECT_32;
72     unsigned long tt;
73     LONG x;
74
75     tt = t;
76     tt = SWAP_16(tt);
77     ASSERT( tt == SWAP_16(TEST_VECT_32));
78
79     /* swapping union with little register pressure */
80     {
81         unsigned char c;
82         x.l = t;
83
84         c = x.c[0];
85         x.c[0]=x.c[2];
86         x.c[2]=c;
87         c = x.c[1];
88         x.c[1]=x.c[3];
89         x.c[3]=c;
90
91         ASSERT( x.l == SWAP_16(TEST_VECT_32));
92     }
93 }
94
95 /* now for something ugly */
96 static void testSwap_16_ptr(void)
97 {
98 #if defined (SDCC)
99 #include <sdcc-lib.h> /* just to get _AUTOMEM or _STATMEM */
100 #if defined (SDCC_STACK_AUTO)
101 #define MY_STATIC static
102 #else
103 #define MY_STATIC
104 #endif
105     MY_STATIC unsigned long _STATMEM tt=TEST_VECT_32;
106
107     /* swapping with little register pressure */
108     {
109         unsigned char c;
110
111         /* uglyness += 1 */
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;
118         /* uglyness -= 1 */
119     }
120     ASSERT( tt == SWAP_16(TEST_VECT_32));
121 #endif
122 }
123
124
125 static void
126 testSwap(void)
127 {
128    testSwap_4();
129    testSwap_8();
130    testSwap_16();
131    testSwap_16_ptr();
132 }