1*e6615b10SDag-Erling Smørgrav.\"- 2*e6615b10SDag-Erling Smørgrav.\" Copyright (c) 2023 Dag-Erling Smørgrav 3*e6615b10SDag-Erling Smørgrav.\" 4*e6615b10SDag-Erling Smørgrav.\" SPDX-License-Identifier: BSD-2-Clause 5*e6615b10SDag-Erling Smørgrav.\" 6*e6615b10SDag-Erling Smørgrav.Dd September 5, 2023 7*e6615b10SDag-Erling Smørgrav.Dt STDCKDINT 3 8*e6615b10SDag-Erling Smørgrav.Os 9*e6615b10SDag-Erling Smørgrav.Sh NAME 10*e6615b10SDag-Erling Smørgrav.Nm stdckdint 11*e6615b10SDag-Erling Smørgrav.Nd checked integer arithmetic 12*e6615b10SDag-Erling Smørgrav.Sh SYNOPSIS 13*e6615b10SDag-Erling Smørgrav.In stdckdint.h 14*e6615b10SDag-Erling Smørgrav.Ft bool 15*e6615b10SDag-Erling Smørgrav.Fn ckd_add "type1 *result" "type2 a" "type3 b" 16*e6615b10SDag-Erling Smørgrav.Ft bool 17*e6615b10SDag-Erling Smørgrav.Fn ckd_sub "type1 *result" "type2 a" "type3 b" 18*e6615b10SDag-Erling Smørgrav.Ft bool 19*e6615b10SDag-Erling Smørgrav.Fn ckd_mul "type1 *result" "type2 a" "type3 b" 20*e6615b10SDag-Erling Smørgrav.Sh DESCRIPTION 21*e6615b10SDag-Erling SmørgravThe function-like macros 22*e6615b10SDag-Erling Smørgrav.Nm ckd_add , 23*e6615b10SDag-Erling Smørgrav.Nm ckd_sub , 24*e6615b10SDag-Erling Smørgravand 25*e6615b10SDag-Erling Smørgrav.Nm ckd_mul 26*e6615b10SDag-Erling Smørgravperform checked integer addition, subtraction, and multiplication, 27*e6615b10SDag-Erling Smørgravrespectively. 28*e6615b10SDag-Erling SmørgravIf the result of adding, subtracting, or multiplying 29*e6615b10SDag-Erling Smørgrav.Fa a 30*e6615b10SDag-Erling Smørgravand 31*e6615b10SDag-Erling Smørgrav.Fa b 32*e6615b10SDag-Erling Smørgravas if their respective types had infinite range fits in 33*e6615b10SDag-Erling Smørgrav.Ft type1 , 34*e6615b10SDag-Erling Smørgravit is stored in the location pointed to by 35*e6615b10SDag-Erling Smørgrav.Fa result 36*e6615b10SDag-Erling Smørgravand the macro evaluates to 37*e6615b10SDag-Erling Smørgrav.Dv false . 38*e6615b10SDag-Erling SmørgravOtherwise, the macro evaluates to 39*e6615b10SDag-Erling Smørgrav.Dv true 40*e6615b10SDag-Erling Smørgravand the contents of the location pointed to by 41*e6615b10SDag-Erling Smørgrav.Fa result 42*e6615b10SDag-Erling Smørgravis the result of the operation wrapped to the range of 43*e6615b10SDag-Erling Smørgrav.Ft type1 . 44*e6615b10SDag-Erling Smørgrav.Sh RETURN VALUES 45*e6615b10SDag-Erling SmørgravThe 46*e6615b10SDag-Erling Smørgrav.Nm ckd_add , 47*e6615b10SDag-Erling Smørgrav.Nm ckd_sub , 48*e6615b10SDag-Erling Smørgravand 49*e6615b10SDag-Erling Smørgrav.Nm ckd_mul 50*e6615b10SDag-Erling Smørgravmacros evaluate to 51*e6615b10SDag-Erling Smørgrav.Dv true 52*e6615b10SDag-Erling Smørgravif the requested operation overflowed the result type and 53*e6615b10SDag-Erling Smørgrav.Dv false 54*e6615b10SDag-Erling Smørgravotherwise. 55*e6615b10SDag-Erling Smørgrav.Sh EXAMPLES 56*e6615b10SDag-Erling Smørgrav.Bd -literal -offset indent 57*e6615b10SDag-Erling Smørgrav#include <assert.h> 58*e6615b10SDag-Erling Smørgrav#include <limits.h> 59*e6615b10SDag-Erling Smørgrav#include <stdckdint.h> 60*e6615b10SDag-Erling Smørgrav 61*e6615b10SDag-Erling Smørgravint main(void) 62*e6615b10SDag-Erling Smørgrav{ 63*e6615b10SDag-Erling Smørgrav int result; 64*e6615b10SDag-Erling Smørgrav 65*e6615b10SDag-Erling Smørgrav assert(!ckd_add(&result, INT_MAX, 0)); 66*e6615b10SDag-Erling Smørgrav assert(result == INT_MAX); 67*e6615b10SDag-Erling Smørgrav assert(ckd_add(&result, INT_MAX, 1)); 68*e6615b10SDag-Erling Smørgrav assert(result == INT_MIN); 69*e6615b10SDag-Erling Smørgrav 70*e6615b10SDag-Erling Smørgrav assert(!ckd_sub(&result, INT_MIN, 0)); 71*e6615b10SDag-Erling Smørgrav assert(result == INT_MIN); 72*e6615b10SDag-Erling Smørgrav assert(ckd_sub(&result, INT_MIN, 1)); 73*e6615b10SDag-Erling Smørgrav assert(result == INT_MAX); 74*e6615b10SDag-Erling Smørgrav 75*e6615b10SDag-Erling Smørgrav assert(!ckd_mul(&result, INT_MAX / 2, 2)); 76*e6615b10SDag-Erling Smørgrav assert(result == INT_MAX - 1); 77*e6615b10SDag-Erling Smørgrav assert(ckd_mul(&result, INT_MAX / 2 + 1, 2)); 78*e6615b10SDag-Erling Smørgrav assert(result == INT_MIN); 79*e6615b10SDag-Erling Smørgrav 80*e6615b10SDag-Erling Smørgrav return 0; 81*e6615b10SDag-Erling Smørgrav} 82*e6615b10SDag-Erling Smørgrav.Ed 83*e6615b10SDag-Erling Smørgrav.\" .Sh STANDARDS 84*e6615b10SDag-Erling Smørgrav.\" The 85*e6615b10SDag-Erling Smørgrav.\" .Nm ckd_add , 86*e6615b10SDag-Erling Smørgrav.\" .Nm ckd_sub , 87*e6615b10SDag-Erling Smørgrav.\" and 88*e6615b10SDag-Erling Smørgrav.\" .Nm ckd_mul 89*e6615b10SDag-Erling Smørgrav.\" macros conform to 90*e6615b10SDag-Erling Smørgrav.\" .St -isoC-23 . 91*e6615b10SDag-Erling Smørgrav.Sh HISTORY 92*e6615b10SDag-Erling SmørgravThe 93*e6615b10SDag-Erling Smørgrav.Nm ckd_add , 94*e6615b10SDag-Erling Smørgrav.Nm ckd_sub , 95*e6615b10SDag-Erling Smørgravand 96*e6615b10SDag-Erling Smørgrav.Nm ckd_mul 97*e6615b10SDag-Erling Smørgravmacros were first introduced in 98*e6615b10SDag-Erling Smørgrav.Fx 14.0 . 99*e6615b10SDag-Erling Smørgrav.Sh AUTHORS 100*e6615b10SDag-Erling SmørgravThe 101*e6615b10SDag-Erling Smørgrav.Nm ckd_add , 102*e6615b10SDag-Erling Smørgrav.Nm ckd_sub , 103*e6615b10SDag-Erling Smørgravand 104*e6615b10SDag-Erling Smørgrav.Nm ckd_mul 105*e6615b10SDag-Erling Smørgravmacros and this manual page were written by 106*e6615b10SDag-Erling Smørgrav.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 107