summaryrefslogtreecommitdiff
path: root/support/regression/tests/muldiv.c
blob: 82b209acde8d0f239f98e349e5411af373c0ce5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/** Simple test for the mul/div/mod operations.

    type: int, char, short, long
    storage: static,
    attr: volatile,
*/
#include <testfwk.h>

void
testUnsignedModDiv(void)
{
#ifndef __SDCC_pdk14 // Lack of memory
    {attr} {storage} unsigned {type} i;
    unsigned {type} result;

    i = 100;

    result = i/3;
    ASSERT(result == 33);

    result = i/12;
    ASSERT(result == 8);

    result = i%7;
    ASSERT(result == 2);

    result = i%34;
    ASSERT(result == 32);
#endif
}

void
testUnsignedMul(void)
{
#ifndef __SDCC_pdk14 // Lack of memory
    {attr} {storage} unsigned {type} i;
    unsigned {type} result;

    i = 37;

    LOG(("i*3 == 111 = %u\n", (int)(i*3)));
    result = i*3;
    ASSERT(result == 111);

    result = i*12;
    ASSERT(result == ((unsigned {type})444));
#endif
}

void
testMul(void)
{
#ifndef __SDCC_pdk14 // Lack of memory
    {attr} {storage} signed {type} i;
    signed {type} result;

    i = 5;

    LOG(("i*5 == 25 = %d\n", (int)(i*5)));
    result = i*5;
    ASSERT(result == 25);
    LOG(("i*-4 == -20 = %d\n", (int)(i*-4)));
    ASSERT(i*-4 == -20);
    i = -10;
#ifndef __SDCC_pic16
    LOG(("i*12 == -120 = %d\n", (int)(i*12)));
    ASSERT(i*12 == -120);
    LOG(("i*-3 == 30 = %d\n", (int)(i*-3)));
    ASSERT(i*-3 == 30);
#endif
#endif
}

void mark(void)
{
}

void
testDiv(void)
{
#if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
    {attr} {storage} signed {type} i;

    i = 100;
    LOG(("i/5 == 20 = %d\n", (int)i/5));
    ASSERT(i/5 == 20);
    LOG(("i/-4 == -25 = %d\n", (int)i/-4));
    mark();
    ASSERT(i/-4 == -25);

    i = -50;
    LOG(("i/25 == -2 = %d\n", (int)i/25));
    ASSERT(i/25 == -2);
    LOG(("i/-12 == 4 = %d\n", (int)i/-12));
    ASSERT(i/-12 == 4);
    //power of 2
    ASSERT(i/4 == -12);
#endif
}

void 
test16to32(void)
{
#if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
   {attr} {storage} int i, j;
   {attr} {storage} unsigned int ui, uj;

   i = 42;
   j = 42;
   ASSERT((long)i * (long)j == 42l * 42l);
   i = -i;
   ASSERT((long)i * (long)j == -42l * 42l);
   j = -j;
   ASSERT((long)i * (long)j == -42l * -42l);
   i = 2342;
   j = 4223;
   ASSERT((unsigned long)i * (unsigned long)j == 2342ul * 4223ul);
   ASSERT((long)i * (long)j == 2342l * 4223l);
   j = -j;
   ASSERT((long)i * (long)j == 2342l * -4223l);
   i = -i;
   ASSERT((long)i * (long)j == -2342l * -4223l);

   ui = 42;
   uj = 42;
   ASSERT((unsigned long)ui * (unsigned long)uj == 42ul * 42ul);
   ui = 2342;
   uj = 4223;
   ASSERT((unsigned long)ui * (unsigned long)uj == 2342ul * 4223ul);
   ui = 0xffff;
   uj = 0x8000;
   ASSERT((unsigned long)ui * (unsigned long)uj == 0xfffful * 0x8000ul);
#endif
}

void
testMod(void)
{
#if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
    {attr} {storage} signed {type} i;

    // Disabled the LOG functions due to a bug in sdcc involving
    // vaargs.
    i = 100;
    //    LOG(("i%%17 == 15 = %u\n", (int)(i%9)));
    ASSERT(i%17 == 15);

    //    LOG(("i%%-7 == 2 = %u\n", (int)i%-7));
    ASSERT(i%-7 == 2);
    //power of 2
    ASSERT(i%-8 == 4);

    i = -49;
    //    LOG(("i%%3 == -1 = %u\n", (int)i%3));
    ASSERT(i%3 == -1);
    //    LOG(("i%%-5 == -4 = %u\n", (int)i%-5));
    ASSERT(i%-5 == -4);
    //power of 2
    ASSERT(i%4 == -1);
#endif
}