Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / bug-3607.c
blobab88515ac42a402043020f0c720476cf24aca34c
1 /*
2 bug-3607.c
3 */
5 #include <testfwk.h>
7 #include <stdint.h>
8 #include <stdbool.h>
10 //#ifdef __SDCC_mcs51
12 __xdata uint16_t g_arg_1 = 0;
13 #ifdef __SDCC_mcs51
14 __bit g_arg_2 = 0;
15 #else
16 _Bool g_arg_2 = 0;
17 #endif
19 #define sel_const1 (uint16_t)((g_arg_2 ? 0x1F09FFFFu : 0x101FFFFEu) >> 16)
20 #define sel_const2 (uint16_t)((g_arg_2 ? 0x101FFFF0u : 0x101FFFF1u) >> 16)
22 bool func_3 (void)
24 return true;
27 __xdata uint8_t func_1_count = 0;
28 __xdata uint8_t func_2_count = 0;
30 void func_1 (void)
32 ++func_1_count;
35 void func_2 (void)
37 ++func_2_count;
40 void func (uint32_t test_arg)
42 uint16_t t = test_arg >> 16;
44 if (func_3 ())
46 // the bug was the constant being loaded wrongly into the registers
47 // before the comparision, where one of the bytes got overwritten with
48 // another.
49 // this gets tested twice with different inputs so we get two mutually
50 // exclusive false positives if there is a bug, or two good hits if
51 // there is no bug.
53 // when g_arg_2 = 0:
54 // (t - g_arg_1 = 1020) > 101F T
55 // (t - g_arg_1 = 1020) > 1010 T (false positive)
56 // (t - g_arg_1 = 1020) > 1F1F F
57 // (t - g_arg_1 = 1020) > 1F10 F
59 // when g_arg_2 = 1:
60 // (t - g_arg_1 = 1F10) > 1F09 T
61 // (t - g_arg_1 = 1F10) > 1F1F F
62 // (t - g_arg_1 = 1F10) > 0909 T (false positive)
63 // (t - g_arg_1 = 1F10) > 091F F
65 if (t - g_arg_1 > sel_const1)
66 func_1 ();
68 else
70 if (t - g_arg_1 > sel_const2)
71 func_2 ();
74 //#endif
76 void testBug (void)
78 //#ifdef __SDCC_mcs51
80 g_arg_1 = 0;
81 g_arg_2 = 0;
82 func (0x10200000ul);
84 g_arg_1 = 0;
85 g_arg_2 = 1;
86 func (0x1F100000ul);
88 ASSERT (func_1_count == 2);
89 ASSERT (func_2_count == 0);
91 //#endif