1 /* Test operandOperation() in SDCCicode.c
3 type: char, short, LONG
8 #if INT_MAX >= 2147483647
14 typedef signed {type
} stype
;
15 typedef unsigned {type
} utype
;
17 volatile char is8
= 8;
19 #if !defined(__SDCC_pdk14) // Lack of memory
26 volatile signed char vsc
;
27 volatile signed short vss
;
28 volatile signed LONG vsl
;
29 volatile unsigned char vuc
;
30 volatile unsigned short vus
;
31 volatile unsigned LONG vul
;
41 mulWrapper (int a
, int b
)
49 #if !defined( __SDCC_pdk14) && !defined( __SDCC_pdk15) // Lack of memory
50 /* mul signedness: usualBinaryConversions() */
62 ASSERT (ss
== 0x3f01);
64 ASSERT (ss
== 0x7e02);
66 ASSERT (ss
== (short) 0xfc04);
68 /* after promotion the result of the multiplication is 'signed int', which overflows! */
69 ASSERT(vuc
* vuc
< 1);
72 /* mul ast: valMult() */
73 ASSERT ((stype
) -3 * (stype
) -1 == (stype
) 3);
74 ASSERT ((stype
) -3 * (stype
) 1 == (stype
) -3);
75 ASSERT ((stype
) 3 * (stype
) -1 == (stype
) -3);
77 ASSERT ((stype
) 1 * (utype
) 0xfffffff7 == (utype
) 0xfffffff7);
79 ASSERT ((unsigned char ) 0xfffffff8 * (unsigned char ) 0xfffffff7 == 0xef48);
80 ASSERT (mulWrapper ((unsigned short) 0xfffffff8, (unsigned short) 0xfffffff7) == (sizeof(int) == 2 ? 0x0048 : (unsigned int)0xffef0048));
81 ASSERT ((unsigned LONG
) 0xfffffff8 * (unsigned LONG
) 0xfffffff7 == 0x0048);
83 ASSERT ((stype
) 0xfffffff8 * (stype
) 0xfffffff7 == 72);
85 ASSERT ((signed char ) -1 * (unsigned char ) 0xfffffff7 == (sizeof(int) == 2 ? (int)0xff09 : (int)0xffffff09));
86 ASSERT ((signed short) -1 * (unsigned short) 0xfffffff7 == (sizeof(int) == 2 ? 9u : 0xffff0009));
87 ASSERT ((signed LONG
) -1 * (unsigned LONG
) 0xfffffff7 == 9u);
89 ASSERT ((signed char ) -2 * (unsigned char ) 0x8004 == (sizeof(int) == 2 ? (int)0xfff8 : (int)0xfffffff8));
90 ASSERT ((signed short) -2 * (unsigned short) 0x8004 == (sizeof(int) == 2 ? (int)0xfff8 : (int)0xfffefff8));
91 ASSERT ((signed LONG
) -2 * (unsigned LONG
) 0x8004 == 0xfffefff8);
93 ASSERT (-1 * 0xfff7 == (sizeof(int) == 2 ? 9 : 0xffff0009)); // 0xfff7 is stored in 'unsigned int'
95 ASSERT (-1 * 65527 == -65527); // 65527 (== 0xfff7) is stored in 'signed LONG'
96 ASSERT (-1 * 33000 == -33000);
98 ASSERT (1 * 10000 * is8
== (sizeof(int) == 2 ? 14464 : 80000)); /* int */
99 ASSERT (1 * 10000l * is8
== 80000); /* LONG */
100 ASSERT (1 * 40000u * is8
== (sizeof(int) == 2 ? 57856u : 320000)); /* unsigned */
101 ASSERT (1 * 40000 * is8
== 320000); /* LONG */
102 ASSERT (1 * 0x4000 * is8
== (sizeof(int) == 2 ? 0 : 0x20000)); /* unsigned */
104 ASSERT (-2 * 1 < 1); /* comparison with 0 is optimized, so let's use 1 instead */
105 ASSERT (-2 * 1u > 1);
106 ASSERT (0x7fffu
* 2 > 1);
107 ASSERT (0x7fffffffu
* 2 > 1);
108 if (sizeof (int) == 2)
109 ASSERT (0x7fff * (unsigned char) 2 < 1);
111 ASSERT (mulWrapper (0x7fffffff, (unsigned char) 2) < 1);
112 ASSERT (mulWrapper (0x7fffffff, (unsigned short) 2) < 1);
114 /* mul icode: operandOperation() */
116 ASSERT (s
* (stype
) -1 == (stype
) 3);
117 ASSERT (s
* (stype
) 1 == (stype
) -3);
119 ASSERT (s
* (stype
) -1 == (stype
) -3);
122 ASSERT (s
* (utype
) 0xfffffff7 == (utype
) 0xfffffff7);
123 uc
= (unsigned char ) 0xfffffff8;
124 ASSERT (uc
* (unsigned char ) 0xfffffff7 == 0xef48);
125 us
= (unsigned short) 0xfffffff8;
126 if (sizeof (int) == 2 && CHAR_BIT
== 8)
127 ASSERT (us
* (unsigned short) 0xfffffff7 == 0x0048);
128 ul
= (unsigned LONG
) 0xfffffff8;
129 ASSERT (ul
* (unsigned LONG
) 0xfffffff7 == 0x0048);
130 ul
= (unsigned LONG
) 0xfffffff8;
132 ASSERT ((stype
) 0xfffffff8 * (stype
) 0xfffffff7 == 72);
134 ASSERT ((signed char ) -1 * (unsigned char ) 0xfffffff7 == (sizeof(int) == 2 ? (int)0xff09 : (int)0xffffff09));
135 ASSERT ((signed short) -1 * (unsigned short) 0xfffffff7 == (sizeof(int) == 2 ? 9u : 0xffff0009));
136 ASSERT ((signed LONG
) -1 * (unsigned LONG
) 0xfffffff7 == 9u);
138 ASSERT ((signed char ) -2 * (unsigned char ) 0x8004 == (sizeof(int) == 2 ? (int)0xfff8 : (int)0xfffffff8));
139 ASSERT ((signed short) -2 * (unsigned short) 0x8004 == (sizeof(int) == 2 ? (int)0xfff8 : (int)0xfffefff8));
140 ASSERT ((signed LONG
) -2 * (unsigned LONG
) 0x8004 == 0xfffefff8);
142 /* div ast: valDiv() */
143 ASSERT ((stype
) -12 / (stype
) -3 == (stype
) 4);
144 ASSERT ((stype
) -12 / (stype
) 3 == (stype
) -4);
145 ASSERT ((stype
) 12 / (stype
) -3 == (stype
) -4);
147 ASSERT ((unsigned char ) -12 / (signed char ) -3 == (sizeof(int) == 2 ? (int)0xffaf : (int)0xffffffaf));
148 ASSERT ((unsigned short) -12 / (signed short) -3 == (sizeof(int) == 2 ? 0 : 0xffffaaaf));
149 ASSERT ((unsigned LONG
) -12 / (signed LONG
) -3 == 0);
150 ASSERT ((utype
) -12 / (stype
) 3 == (stype
) 0x55555551);
151 ASSERT ((unsigned char ) 12 / (signed char ) -3 == -4);
152 ASSERT ((unsigned short) 12 / (signed short) -3 == (sizeof(int) == 2 ? 0 : 0xfffffffc));
153 ASSERT ((unsigned LONG
) 12 / (signed LONG
) -3 == 0);
155 ASSERT ((stype
) -12 / (utype
) -3 == 0);
156 ASSERT ((signed char ) -12 / (unsigned char ) 3 == -4);
157 ASSERT ((signed short) -12 / (unsigned short) 3 == (sizeof(int) == 2 ? 0x5551 : -4));
158 ASSERT ((signed LONG
) -12 / (unsigned LONG
) 3 == 0x55555551);
159 ASSERT ((stype
) 12 / (utype
) -3 == 0);
161 ASSERT (12u / 3 * 10000 == 40000);
165 /* div icode: operandOperation() */
167 ASSERT (s
/ (stype
) -3 == (stype
) 4);
169 ASSERT (s
/ (stype
) 3 == (stype
) -4);
171 ASSERT (s
/ (stype
) -3 == (stype
) -4);
174 ASSERT (uc
/ (signed char ) -3 == (sizeof(int) == 2 ? (int)0xffaf : (int)0xffffffaf));
176 ASSERT (us
/ (signed short) -3 == (sizeof(int) == 2 ? 0 : 0xffffaaaf));
178 ASSERT (ul
/ (signed LONG
) -3 == 0);
180 ASSERT (u
/ (stype
) 3 == (stype
) 0x55555551);
182 ASSERT (uc
/ (signed char ) -3 == -4);
184 ASSERT (us
/ (signed short) -3 == (sizeof(int) == 2 ? 0 : 0xfffffffc));
186 ASSERT (ul
/ (signed LONG
) -3 == 0);
189 ASSERT (s
/ (utype
) -3 == 0);
191 ASSERT (sc
/ (unsigned char ) 3 == -4);
193 ASSERT (ss
/ (unsigned short) 3 == (sizeof(int) == 2 ? 0x5551 : -4));
195 ASSERT (sl
/ (unsigned LONG
) 3 == 0x55555551);
197 ASSERT (s
/ (utype
) -3 == 0);
200 /* mod ast: valMod() */
203 ASSERT ((stype
) -17 % (stype
) -11 == (stype
) -6);
204 ASSERT ((stype
) -17 % (stype
) 11 == (stype
) -6);
205 ASSERT ((stype
) 17 % (stype
) -11 == (stype
) 6);
206 ASSERT ((unsigned char ) -17 % (signed char ) -11 == 8);
207 ASSERT ((unsigned short) -17 % (signed short) -11 == (sizeof(int) == 2 ? -17 : 3));
208 ASSERT ((unsigned LONG
) -17 % (signed LONG
) -11 == -17);
209 ASSERT ((unsigned char ) -17 % (signed char ) 11 == 8);
210 ASSERT ((unsigned short) -17 % (signed short) 11 == 3);
211 ASSERT ((unsigned LONG
) -17 % (signed LONG
) 11 == 9);
212 ASSERT ((unsigned char ) 17 % (signed char ) -11 == 6);
213 ASSERT ((unsigned short) 17 % (signed short) -11 == (sizeof(int) == 2 ? 17 : 6));
214 ASSERT ((unsigned LONG
) 17 % (signed LONG
) -11 == 17);
220 ASSERT ( 80 + 80 == 160);
221 ASSERT (150 + 150 == 300);
222 ASSERT (160u + 160u == 320);