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))
12 typedef unsigned int uint16
;
13 typedef unsigned long uint32
;
15 /* on 32 and 64 bit machines */
16 typedef unsigned short uint16
;
17 typedef unsigned int uint32
;
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
;
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
;
45 ASSERT( tt
== SWAP_8(TEST_VECT_16
));
49 ASSERT( x
.i
== SWAP_8(TEST_VECT_16
));
51 #if defined (__SDCC_mcs51)
52 /* this was filed as bug #1638622 (rejected) */
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 */
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 */
71 ASSERT( x
.i
== SWAP_8(TEST_VECT_16
));
77 #define SWAP_16(x) ((((x)<<16) | ((x)>>16)) & 0xffffFFFF)
79 static void testSwap_16(void)
81 volatile unsigned long t
=TEST_VECT_32
;
87 ASSERT( tt
== SWAP_16(TEST_VECT_32
));
89 /* swapping union with little register pressure */
101 ASSERT( x
.l
== SWAP_16(TEST_VECT_32
));
105 /* now for something ugly */
106 static void testSwap_16_ptr(void)
109 #include <sdcc-lib.h> /* just to get _AUTOMEM or _STATMEM */
110 #if defined (__SDCC_STACK_AUTO)
111 #define MY_STATIC static
115 MY_STATIC
unsigned long _STATMEM tt
=TEST_VECT_32
;
117 /* swapping with little register pressure */
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
;
130 ASSERT( tt
== SWAP_16(TEST_VECT_32
));
139 #ifndef __SDCC_pdk14 // Lack of memory -see RFE #611