GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
STDCKDINT(3) FreeBSD Library Functions Manual STDCKDINT(3)

stdckdintchecked integer arithmetic

#include <stdckdint.h>

bool
ckd_add(type1 *result, type2 a, type3 b);

bool
ckd_sub(type1 *result, type2 a, type3 b);

bool
ckd_mul(type1 *result, type2 a, type3 b);

The function-like macros ckd_add, ckd_sub, and ckd_mul perform checked integer addition, subtraction, and multiplication, respectively. If the result of adding, subtracting, or multiplying a and b as if their respective types had infinite range fits in type1, it is stored in the location pointed to by result and the macro evaluates to false. Otherwise, the macro evaluates to true and the contents of the location pointed to by result is the result of the operation wrapped to the range of type1.

The ckd_add, ckd_sub, and ckd_mul macros evaluate to true if the requested operation overflowed the result type and false otherwise.

#include <assert.h>
#include <limits.h>
#include <stdckdint.h>

int main(void)
{
	int result;

	assert(!ckd_add(&result, INT_MAX, 0));
	assert(result == INT_MAX);
	assert(ckd_add(&result, INT_MAX, 1));
	assert(result == INT_MIN);

	assert(!ckd_sub(&result, INT_MIN, 0));
	assert(result == INT_MIN);
	assert(ckd_sub(&result, INT_MIN, 1));
	assert(result == INT_MAX);

	assert(!ckd_mul(&result, INT_MAX / 2, 2));
	assert(result == INT_MAX - 1);
	assert(ckd_mul(&result, INT_MAX / 2 + 1, 2));
	assert(result == INT_MIN);

	return 0;
}

The ckd_add, ckd_sub, and ckd_mul macros were first introduced in FreeBSD 14.0.

The ckd_add, ckd_sub, and ckd_mul macros and this manual page were written by Dag-Erling Smørgrav <des@FreeBSD.org>.

September 5, 2023 FreeBSD 14.3-RELEASE

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.