Macaulay2 Engine
Loading...
Searching...
No Matches
overflow-test.cpp
Go to the documentation of this file.
1
31
32#define outer 20000
33#define inner 5000
34
35#include <stdio.h>
36#include "overflow.hpp"
37#include "assert.h"
38#include <stdlib.h>
39#include <string.h>
40
41#if 0
42 return 0;
43 x = safe::add(j,s); // /2000000000 repetitions
44// this pair of timings shows that there is virtually no loop overhead
45#define stmt x = 1, x = 1 // 0m8.973s/5000000000 repetitions, no power
46#define stmt x = 1 // 0m4.488s/5000000000 repetitions, no power
47
48#define stmt y = j + x // 0m 9.065s/10000000000
49#define stmt \
50 y = safe::add(j, x) // 0m18.569s/10000000000, no long long, pretty good!
51#define TRYLONGLONG
52#define stmt y = safe::add(j, x) // 1m46.455s/10000000000, long long, too slow
53
54#define stmt y = j * x // 0m2.408s/1000000000
55#define stmt y = safe::mult(j, x) // 0m15.361s/1000000000, no long long
56#define TRYLONGLONG
57#define stmt y = safe::mult(j, x) // 0m15.369s/1000000000, long long
58
59#define stmt y = -x // 0m9.057s/10000000000
60#define stmt y = safe::minus(x) // 0m15.205s/10000000000, no long long
61
62#endif
63
64//=============================================================================
65
66#define stmt x = 1
67
68volatile int s0 = 100, x = 200;
69
70int main(int argc, char **argv)
71{
72 int i, j;
73 int s = s0;
74 s0 = s; // use s at least once
75 // use one of these to see if overflows are really detected. Each one
76 // should generate an overflow and a program termination.
77 if (argc > 1)
78 {
79 if (0 == strcmp(argv[1], "sub"))
80 {
81 printf("test failed, result %x\n",
82 (unsigned int)safe::sub(0x80000000, 1 - x + x));
83 exit(1);
84 }
85 else if (0 == strcmp(argv[1], "add"))
86 {
87 printf("test failed, result %x\n",
88 (unsigned int)safe::add(0x7fffffff, 1 - x + x));
89 exit(1);
90 }
91 else if (0 == strcmp(argv[1], "mult"))
92 {
93 printf("test failed, result %x\n",
94 (unsigned int)safe::mult(0x8000, 0x10000 - x + x));
95 exit(1);
96 }
97 else if (0 == strcmp(argv[1], "div"))
98 {
99 printf("test failed, result %x\n",
100 (unsigned int)safe::div(0x80000000, -1 - x + x));
101 exit(1);
102 }
103 else if (0 == strcmp(argv[1], "minus"))
104 {
105 printf("test failed, result %x\n",
106 (unsigned int)safe::minus(0x80000000 - x + x));
107 exit(1);
108 }
109 else if (0 == strcmp(argv[1], "throw"))
110 {
111 safe::ov("throw overflow exception");
112 printf("test failed\n");
113 exit(1);
114 }
115 else
116 {
117 printf("unknown test: %s\n", argv[1]);
118 exit(1);
119 }
120 }
121#define roll 100
122 printf("%lld repetitions\n", (long long)inner * outer * roll);
123 for (i = outer; i > 0; i--)
124 for (j = 0; j < inner; j++)
125 { // 100 times ( == roll, defined above )
126 {
127 stmt;
128 stmt;
129 stmt;
130 stmt;
131 stmt;
132 stmt;
133 stmt;
134 stmt;
135 stmt;
136 stmt; /* 10 times */
137 }
138 {
139 stmt;
140 stmt;
141 stmt;
142 stmt;
143 stmt;
144 stmt;
145 stmt;
146 stmt;
147 stmt;
148 stmt; /* 10 times */
149 }
150 {
151 stmt;
152 stmt;
153 stmt;
154 stmt;
155 stmt;
156 stmt;
157 stmt;
158 stmt;
159 stmt;
160 stmt; /* 10 times */
161 }
162 {
163 stmt;
164 stmt;
165 stmt;
166 stmt;
167 stmt;
168 stmt;
169 stmt;
170 stmt;
171 stmt;
172 stmt; /* 10 times */
173 }
174 {
175 stmt;
176 stmt;
177 stmt;
178 stmt;
179 stmt;
180 stmt;
181 stmt;
182 stmt;
183 stmt;
184 stmt; /* 10 times */
185 }
186 {
187 stmt;
188 stmt;
189 stmt;
190 stmt;
191 stmt;
192 stmt;
193 stmt;
194 stmt;
195 stmt;
196 stmt; /* 10 times */
197 }
198 {
199 stmt;
200 stmt;
201 stmt;
202 stmt;
203 stmt;
204 stmt;
205 stmt;
206 stmt;
207 stmt;
208 stmt; /* 10 times */
209 }
210 {
211 stmt;
212 stmt;
213 stmt;
214 stmt;
215 stmt;
216 stmt;
217 stmt;
218 stmt;
219 stmt;
220 stmt; /* 10 times */
221 }
222 {
223 stmt;
224 stmt;
225 stmt;
226 stmt;
227 stmt;
228 stmt;
229 stmt;
230 stmt;
231 stmt;
232 stmt; /* 10 times */
233 }
234 {
235 stmt;
236 stmt;
237 stmt;
238 stmt;
239 stmt;
240 stmt;
241 stmt;
242 stmt;
243 stmt;
244 stmt; /* 10 times */
245 }
246 }
247}
248
249// Local Variables:
250// compile-command: "cd $M2BUILDDIR/Macaulay2/e && make DEPENDS=no overflow-test
251// && time ./overflow-test"
252// indent-tabs-mode: nil
253// End:
int main()
void size_t s
Definition m2-mem.cpp:271
static int32_t mult(int32_t x, int32_t y, const char *msg)
Definition overflow.hpp:236
static int32_t add(int32_t x, int32_t y, const char *msg)
Definition overflow.hpp:116
static int32_t div(int32_t x, int32_t y, const char *msg)
Definition overflow.hpp:263
static int32_t sub(int32_t x, int32_t y, const char *msg)
Definition overflow.hpp:144
void ov(const char *msg)
Definition overflow.cpp:5
static int32_t minus(int32_t x, const char *msg)
Definition overflow.hpp:185
#define roll
#define stmt
#define outer
volatile int x
volatile int s0
#define inner
Overflow-checked integer arithmetic for monomial exponents and degree sums.