liveBook · Manning

Ckd_add

As explained in Modern C, Third Edition

ckd_add is a C23 type-generic checked-addition macro that stores the result through a pointer and reports whether the mathematical addition overflowed.

ckd_add is a C23 type-generic macro for checked addition. It stores the low-significance result through a pointer and returns true when the mathematical sum does not fit the pointed-to type. section 8.2

Unsigned addition

unsigned result = 0; bool overflow = ckd_add(&result, UINT_MAX, UINT_MAX); The stored result is UINT_MAX - 1, following unsigned modulo arithmetic, while overflow is true because the exact sum is 2 × UINT_MAX. section 8.2

Signed addition

signed result = 0; bool overflow = ckd_add(&result, -INT_MAX, -INT_MAX); The call remains well-defined: overflow is true and the stored result has the bit pattern for 2, even though the mathematical sum is negative. section 8.2

Table 8.5 Functions for integer arithmetic. Most of these are type-generic macros, but some also have function interfaces for specific types. \(x\) and \(y\) denote the arguments, \(\overline{x}\) is a bitwise complement, \(w\) the width, and \(m\) the maximum value of the type.
Function Description Remark
abs, labs, llabs \(|x|\) Type independent
div, ldiv, lldiv \(x\)/\(y\) and \(x\)%\(y\) Type independent
ckd_add \(x\)+\(y\) (LS bits) & overflow flag Target dependent
ckd_mul \(x\)*\(y\) (LS bits) & overflow flag Target dependent
ckd_sub \(x\)-\(y\) (LS bits) & overflow flag Target dependent
stdc_bit_ceil \(2^{\lceil \log_2 x\rceil}\) \(1\) if \(x \equiv 0\), \(0\) if \(x > m/2\)
stdc_bit_floor \(2^{\lfloor \log_2 x\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_bit_width \(1 + {\lfloor \log_2 x\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_count_ones Number of \(1\)-bits in \(x\) Type independent
stdc_count_zeros Number of \(1\)-bits in \(\overline{x}\) Type dependent
stdc_has_single_bit There is 𝑁 with \(x \equiv 2^n\) Type independent
stdc_first_leading_one \(w - {\big\lfloor \log_2 x\big\rfloor}\) \(0\) if \(x \equiv 0\)
stdc_first_leading_zero \(w - {\big\lfloor \log_2 \overline{x}\big\rfloor}\) \(0\) if \(x\) has all bits set
stdc_leading_ones \(w - {\big\lfloor \log_2 \overline{x}\big\rfloor} - 1\) \(w\) if \(x \equiv 0\)
stdc_leading_zeros \(w - {\big\lfloor \log_2 x\big\rfloor} - 1\) \(w\) if \(x \equiv 0\)
stdc_first_trailing_one One plus LS \(1\)-bit in \(x\) \(0\) if \(x \equiv 0\)
stdc_first_trailing_zero One plus LS \(0\)-bit in \(x\) \(0\) if \(x \equiv 0\)
stdc_trailing_ones One plus LS \(0\)-bit in \(x\) \(w\) if \(x \equiv 0\)
stdc_trailing_zeros One plus LS \(1\)-bit in \(x\) \(w\) if \(x \equiv 0\)

Compare ckd_add with the related checked multiplication and subtraction macros in the integer-arithmetic function table. section 8.2

How Modern C, Third Edition covers it

How deep it goes
Explains pointer-stored results, modulo behavior, overflow flags, and a signed edge case without detailing macro expansion.
Examples
Demonstrates UINT_MAX plus UINT_MAX and -INT_MAX plus -INT_MAX, showing stored results alongside overflow flags.
What you can do after
Diagnose addition overflow while obtaining the defined low-significance result for signed or unsigned integer types.
How it connects
Connects checked addition with integer arithmetic, checked multiplication, checked subtraction, and unsigned modulo arithmetic.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.