File: overflow-test.cpp

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (222 lines) | stat: -rw-r--r-- 4,995 bytes parent folder | download | duplicates (4)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#define outer 20000
#define inner 5000

#include <stdio.h>
#include "overflow.hpp"
#include "assert.h"
#include <stdlib.h>
#include <string.h>

#if 0
     return 0;
               x = safe::add(j,s); //  /2000000000 repetitions
// this pair of timings shows that there is virtually no loop overhead
#define stmt x = 1, x = 1  // 0m8.973s/5000000000 repetitions, no power
#define stmt x = 1         // 0m4.488s/5000000000 repetitions, no power

#define stmt y = j + x  // 0m 9.065s/10000000000
#define stmt \
  y = safe::add(j, x)  // 0m18.569s/10000000000, no long long, pretty good!
#define TRYLONGLONG
#define stmt y = safe::add(j, x)  // 1m46.455s/10000000000, long long, too slow

#define stmt y = j * x             //   0m2.408s/1000000000
#define stmt y = safe::mult(j, x)  // 0m15.361s/1000000000, no long long
#define TRYLONGLONG
#define stmt y = safe::mult(j, x)  // 0m15.369s/1000000000, long long

#define stmt y = -x              //  0m9.057s/10000000000
#define stmt y = safe::minus(x)  // 0m15.205s/10000000000, no long long

#endif

//=============================================================================

#define stmt x = 1

volatile int s0 = 100, x = 200;

int main(int argc, char **argv)
{
  int i, j;
  int s = s0;
  s0 = s;  // use s at least once
  //   use one of these to see if overflows are really detected.  Each one
  //   should generate an overflow and a program termination.
  if (argc > 1)
    {
      if (0 == strcmp(argv[1], "sub"))
        {
          printf("test failed, result %x\n",
                 (unsigned int)safe::sub(0x80000000, 1 - x + x));
          exit(1);
        }
      else if (0 == strcmp(argv[1], "add"))
        {
          printf("test failed, result %x\n",
                 (unsigned int)safe::add(0x7fffffff, 1 - x + x));
          exit(1);
        }
      else if (0 == strcmp(argv[1], "mult"))
        {
          printf("test failed, result %x\n",
                 (unsigned int)safe::mult(0x8000, 0x10000 - x + x));
          exit(1);
        }
      else if (0 == strcmp(argv[1], "div"))
        {
          printf("test failed, result %x\n",
                 (unsigned int)safe::div(0x80000000, -1 - x + x));
          exit(1);
        }
      else if (0 == strcmp(argv[1], "minus"))
        {
          printf("test failed, result %x\n",
                 (unsigned int)safe::minus(0x80000000 - x + x));
          exit(1);
        }
      else if (0 == strcmp(argv[1], "throw"))
        {
          safe::ov("throw overflow exception");
          printf("test failed\n");
          exit(1);
        }
      else
        {
          printf("unknown test: %s\n", argv[1]);
          exit(1);
        }
    }
#define roll 100
  printf("%lld repetitions\n", (long long)inner * outer * roll);
  for (i = outer; i > 0; i--)
    for (j = 0; j < inner; j++)
      {  // 100 times ( == roll, defined above )
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
        {
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt;
          stmt; /* 10 times */
        }
      }
}

// Local Variables:
// compile-command: "cd $M2BUILDDIR/Macaulay2/e && make DEPENDS=no overflow-test
// && time ./overflow-test"
// indent-tabs-mode: nil
// End: