-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuaddcheck.c
More file actions
61 lines (52 loc) · 1.56 KB
/
uaddcheck.c
File metadata and controls
61 lines (52 loc) · 1.56 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <limits.h>
/* $begin uadd_ok_proto */
/* Determine whether arguments can be added without overflow */
int uadd_ok(unsigned x, unsigned y);
/* $end uadd_ok_proto */
/* $begin uadd_ok */
/* Determine whether arguments can be added without overflow */
int uadd_ok(unsigned x, unsigned y) {
unsigned sum = x+y;
return sum >= x;
}
/* $end uadd_ok */
/* $begin uadd_ovf_proto */
/* Determine whether adding arguments would cause overflow */
int uadd_ovf(unsigned x, unsigned y);
/* $end uadd_ovf_proto */
/* $begin uadd_ovf */
/* Determine whether adding arguments would cause overflow */
int uadd_ovf(unsigned x, unsigned y) {
unsigned sum = x+y;
return sum < x;
}
/* $end uadd_ovf */
/* Test function */
int test_uadd_ok(unsigned x, unsigned y) {
uint64_t lx = (uint64_t) x;
uint64_t ly = (uint64_t) y;
return (lx + ly) == (x + y);
}
/* Test function */
int test_uadd_ovf(unsigned x, unsigned y) {
uint64_t lx = (uint64_t) x;
uint64_t ly = (uint64_t) y;
return (lx + ly) != (x + y);
}
int main(int argc, char *argv[]) {
unsigned x = argc > 1 ? strtoul(argv[1], NULL, 0) : UINT_MAX;
unsigned y = argc > 1 ? strtoul(argv[2], NULL, 0) : UINT_MAX;
int ok = uadd_ok(x, y);
int ovf = uadd_ovf(x, y);
int tok = test_uadd_ok(x, y);
int tovf = test_uadd_ovf(x, y);
printf("x = %u, y = %u, x+y = %u\n", x, y, x+y);
if (ok != tok)
printf("\tok = %d, tok = %d\n", ok, tok);
if (ovf != tovf)
printf("\tovf = %d, tovf = %d\n", ovf, tovf);
return 0;
}