1.Dd April 20, 2007
2.Dt CHECK_INT32_ADD 3
3.Os
4.Sh NAME
5.Nm check_int32_add ,
6.Nm check_uint32_add ,
7.Nm check_int64_add ,
8.Nm check_uint64_add ,
9.Nm check_int32_sub ,
10.Nm check_uint32_sub ,
11.Nm check_int64_sub ,
12.Nm check_uint64_sub ,
13.Nm check_int32_mul ,
14.Nm check_uint32_mul ,
15.Nm check_int64_mul ,
16.Nm check_uint64_mul ,
17.Nm check_int32_div ,
18.Nm check_uint32_div ,
19.Nm check_int64_div ,
20.Nm check_uint64_div ,
21.Nd detect overflow in arithmetic
22.Sh SYNOPSIS
23.In checkint.h
24.Ft int32_t
25.Fo check_int32_add
26.Fa "int x"
27.Fa "int y"
28.Fa "int *err" 
29.Fc
30.Ft uint32_t
31.Fo check_uint32_add
32.Fa "int x"
33.Fa "int y"
34.Fa "int *err" 
35.Fc
36.Ft int64_t
37.Fo check_int64_add
38.Fa "int x"
39.Fa "int y"
40.Fa "int *err" 
41.Fc
42.Ft uint64_t
43.Fo check_uint64_add
44.Fa "int x"
45.Fa "int y"
46.Fa "int *err" 
47.Fc
48.Ft int32_t
49.Fo check_int32_sub
50.Fa "int x"
51.Fa "int y"
52.Fa "int *err" 
53.Fc
54.Ft uint32_t
55.Fo check_uint32_sub
56.Fa "int x"
57.Fa "int y"
58.Fa "int *err" 
59.Fc
60.Ft int64_t
61.Fo check_int64_sub
62.Fa "int x"
63.Fa "int y"
64.Fa "int *err" 
65.Fc
66.Ft uint64_t
67.Fo check_uint64_sub
68.Fa "int x"
69.Fa "int y"
70.Fa "int *err" 
71.Fc
72.Ft int32_t
73.Fo check_int32_mul
74.Fa "int x"
75.Fa "int y"
76.Fa "int *err" 
77.Fc
78.Ft uint32_t
79.Fo check_uint32_mul
80.Fa "int x"
81.Fa "int y"
82.Fa "int *err" 
83.Fc
84.Ft int64_t
85.Fo check_int64_mul
86.Fa "int x"
87.Fa "int y"
88.Fa "int *err" 
89.Fc
90.Ft uint64_t
91.Fo check_uint64_mul
92.Fa "int x"
93.Fa "int y"
94.Fa "int *err" 
95.Fc
96.Ft int32_t
97.Fo check_int32_div
98.Fa "int x"
99.Fa "int y"
100.Fa "int *err" 
101.Fc
102.Ft uint32_t
103.Fo check_uint32_div
104.Fa "int x"
105.Fa "int y"
106.Fa "int *err" 
107.Fc
108.Ft int64_t
109.Fo check_int64_div
110.Fa "int x"
111.Fa "int y"
112.Fa "int *err" 
113.Fc
114.Ft uint64_t
115.Fo check_uint64_div
116.Fa "int x"
117.Fa "int y"
118.Fa "int *err" 
119.Fc
120.Sh DESCRIPTION
121The
122.Fn check_<type>_<operation> "x" "y" "err" 
123family of functions perform the specified arithmetic operation (addition, subtraction, 
124multiplication, or division) with the left operand of
125.Fa x
126and right operand of
127.Fa y 
128and return the arithmetic result with the specified type.   
129.Pp
130Either operand 
131.Fa x
132or 
133.Fa y
134(or both) can be of any type that is compatible to signed or unsigned
1358-bit, 16-bit, 32-bit, or 64-bit integers.
136.Pp
137The
138.Fa err
139argument is 
140.Em or Ns 'ed
141by flags in the function to indicate if an overflow has occurred. 
142The possible flag values are:
143.Pp
144.Bd -literal -offset indent -compact
145CHECKINT_NO_ERROR		no overflow has occurred
146CHECKINT_OVERFLOW_ERROR		overflow has occurred
147CHECKINT_TYPE_ERROR		operand is of an incompatible type
148.Ed
149.Pp
150The
151.Fa err
152argument is not cleared in calls to the 
153.Fn check_<type>_<operation> "x" "y" "err" 
154functions.  Detected overflow persists in the 
155.Fa err
156argument until
157.Fa err
158is reset to CHECKINT_NO_ERROR.
159.Sh RETURN VALUES
160If successful, the
161.Fn check_<type>_<operation> 
162functions will return the arithmetic result of performing the operation with left operand
163.Fa x
164and right operand
165.Fa y 
166(even when overflow error occurs). 
167.Pp
168If any other error occurs, the return value is -1
169and the argument
170.Fa err
171will be set to indicate the error.
172.Sh EXAMPLES
173.Bd -literal -offset indent
174/* Create a variable to store overflow flag */
175int32_t err = CHECKINT_NO_ERROR;
176/* Use checkint API to perform an arithmetic operation and
177 * store result in variable. */
178int32_t arithmetic_result = check_int32_add(UINT_MAX, 1, &err);
179/* Check status of overflow flag */
180if (err & CHECKINT_OVERFLOW_ERROR) {
181    /* Perform overflow resolution code */
182    fprintf(stderr, "Overflow detected!\\n");
183}
184/* Check for type error */
185else if (err & CHECKINT_TYPE_ERROR) {
186    /* Deal with incompatible types error */
187    fprintf(stderr, "Incompatible types!\\n");
188}
189/* Reset overflow flag for next operation */
190err = CHECKINT_NO_ERROR;
191 
192.Ed
193.Sh ERRORS
194The 
195.Fn check_<type>_<operation> 
196functions may fail if:
197.Pp
198.Bd -literal -offset indent -compact
199[CHECKINT_TYPE_ERROR]		operand is of an incompatible type
200.Ed
201.Sh HISTORY
202The
203.Fn checkint
204API was introduced in Mac OS X 10.5.
205