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
|
/*
* Generate sequences of values with wrapping for testing how
* tools deal with this. Note: wrapping of *signed* integers
* is undefined (according to gcc folks) and they take that as
* meaning "anything can happen". For some compilers, that's
* taken literally at higher optimisation levels, resulting in
* crashes, infinite loops, early exit; so *anything goes* for
* this test program. Do not rely on its output when signed
* integers are being used (the default, without -u).
*
* http://thiemonagel.de/2010/01/signed-integer-overflow/
* https://patchwork.kernel.org/patch/34925/
*/
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#define NUM_SAMPLES 20
#ifndef LONGLONG_MAX
#define LONGLONG_MAX 9223372036854775807LL /* max "long long int" */
#endif
#ifndef ULONGLONG_MAX
#define ULONGLONG_MAX 18446744073709551615LLU /* max "unsigned long long" */
#endif
int
main(int argc, char *argv[])
{
int use_longlong = 0;
int num_samples = NUM_SAMPLES;
int notsigned = 0;
int c;
extern char *optarg;
while ((c = getopt(argc, argv, "ln:u")) != EOF) {
switch (c) {
case 'l':
/* use long long */
use_longlong = 1;
break;
case 'n':
num_samples = atoi(optarg);
break;
case 'u':
notsigned = 1;
break;
}
}
if (! notsigned ) {
if (! use_longlong) {
int x = 0;
int prev = 0;
int i;
double a;
for (i=0;i<=num_samples;i++) {
x += INT_MAX/2 - 1;
a = (double)x - (double)prev;
prev = x;
if (i > 0)
printf("%.4g\n", a);
}
}
else {
long long x = 0;
long long prev = 0;
int i;
double a;
for (i=0;i<=num_samples;i++) {
x += LONGLONG_MAX/2 - 1;
a = (double)x - (double)prev;
prev = x;
if (i > 0)
printf("%.4g\n", a);
}
}
}
else {
if (! use_longlong) {
unsigned int x = 0;
unsigned int prev = 0;
int i;
double a;
for (i=0;i<=num_samples;i++) {
x += UINT_MAX/2 - 1;
a = (double)x - (double)prev;
prev = x;
if (i > 0)
printf("%.4g\n", a);
}
}
else {
unsigned long long x = 0;
unsigned long long prev = 0;
int i;
double a;
for (i=0;i<=num_samples;i++) {
x += ULONGLONG_MAX/2 - 1;
a = (double)x - (double)prev;
prev = x;
if (i > 0)
printf("%.4g\n", a);
}
}
}
exit(0);
}
|