Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_algo.h File Reference
#include "vl_compare.h"
#include "vl_numtypes.h"
+ Include dependency graph for vl_algo.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define VL_MIN(a, b)   ((a) < (b) ? (a) : (b))
 Returns the minimum of two values.
 
#define VL_MAX(a, b)   ((a) > (b) ? (a) : (b))
 Returns the maximum of two values.
 
#define VL_ABS(x)   ((x) < 0 ? -(x) : (x))
 Returns the absolute value of a number.
 
#define VL_CLAMP(x, min, max)   VL_MAX(min, VL_MIN(x, max))
 Clamps a value between a minimum and maximum range.
 

Functions

VL_API vl_uint_t vlAlgoPopCount8 (vl_uint8_t value)
 Counts the number of set bits (population count) in an 8-bit value.
 
VL_API vl_uint_t vlAlgoPopCount16 (vl_uint16_t value)
 Counts the number of set bits (population count) in a 16-bit value.
 
VL_API vl_uint_t vlAlgoPopCount32 (vl_uint32_t value)
 Counts the number of set bits (population count) in a 32-bit value.
 
VL_API vl_uint_t vlAlgoPopCount64 (vl_uint64_t value)
 Counts the number of set bits (population count) in a 64-bit value.
 
VL_API vl_uint_t vlAlgoCLZ8 (vl_uint8_t value)
 Counts the number of leading zero bits in an 8-bit value.
 
VL_API vl_uint_t vlAlgoCLZ16 (vl_uint16_t value)
 Counts the number of leading zero bits in a 16-bit value.
 
VL_API vl_uint_t vlAlgoCLZ32 (vl_uint32_t value)
 Counts the number of leading zero bits in a 32-bit value.
 
VL_API vl_uint_t vlAlgoCLZ64 (vl_uint64_t value)
 Counts the number of leading zero bits in a 64-bit value.
 
VL_API vl_uint_t vlAlgoCTZ8 (vl_uint8_t value)
 Counts the number of trailing zero bits in an 8-bit value.
 
VL_API vl_uint_t vlAlgoCTZ16 (vl_uint16_t value)
 Counts the number of trailing zero bits in a 16-bit value.
 
VL_API vl_uint_t vlAlgoCTZ32 (vl_uint32_t value)
 Counts the number of trailing zero bits in a 32-bit value.
 
VL_API vl_uint_t vlAlgoCTZ64 (vl_uint64_t value)
 Counts the number of trailing zero bits in a 64-bit value.
 
VL_API vl_uint_t vlAlgoNextPO2 (vl_ularge_t value)
 Computes the next power of 2 greater than or equal to the given value.
 
VL_API vl_bool_t vlAlgoIsPO2 (vl_ularge_t value)
 Tests whether a value is a power of 2.
 
VL_API vl_ularge_t vlAlgoGCD (vl_ularge_t a, vl_ularge_t b)
 Computes the Greatest Common Divisor (GCD) of two unsigned integers.
 
VL_API vl_ularge_t vlAlgoLCM (vl_ularge_t a, vl_ularge_t b)
 Computes the Least Common Multiple (LCM) of two unsigned integers.
 
VL_API vl_ilarge_t vlAlgoGCDSigned (vl_ilarge_t a, vl_ilarge_t b)
 Computes the Greatest Common Divisor (GCD) of two signed integers.
 
VL_API vl_ilarge_t vlAlgoLCMSigned (vl_ilarge_t a, vl_ilarge_t b)
 Computes the Least Common Multiple (LCM) of two signed integers.
 
VL_API vl_bool_t vlAlgoAddOverflow (vl_ularge_t a, vl_ularge_t b)
 Tests whether adding two unsigned integers would cause overflow.
 
VL_API vl_bool_t vlAlgoMulOverflow (vl_ularge_t a, vl_ularge_t b)
 Tests whether multiplying two unsigned integers would cause overflow.
 
VL_API vl_bool_t vlAlgoSubUnderflow (vl_ularge_t a, vl_ularge_t b)
 Tests whether subtracting two unsigned integers would cause underflow.
 

Macro Definition Documentation

◆ VL_ABS

#define VL_ABS (   x)    ((x) < 0 ? -(x) : (x))

Returns the absolute value of a number.

Parameters
xinput value
Returns
absolute value of x

◆ VL_CLAMP

#define VL_CLAMP (   x,
  min,
  max 
)    VL_MAX(min, VL_MIN(x, max))

Clamps a value between a minimum and maximum range.

Parameters
xvalue to clamp
minminimum allowed value
maxmaximum allowed value
Returns
x clamped to the range [min, max]

◆ VL_MAX

#define VL_MAX (   a,
 
)    ((a) > (b) ? (a) : (b))

Returns the maximum of two values.

Parameters
afirst value
bsecond value
Returns
the larger of the two values

◆ VL_MIN

#define VL_MIN (   a,
 
)    ((a) < (b) ? (a) : (b))

Returns the minimum of two values.

██ ██ ██ █████ ███████ █████ ██████ ███ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ███████ ███████ ███████ ██ ███ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ███████ ██ ██ ███████ ██ ██ ██████ ██ ████ ██ ██ ====—: A Data Structure and Algorithms library for C11. :—====

Copyright 2026 Jesse Walker, released under the MIT license. Git Repository: https://github.com/walkerje/veritable_lasagna

Parameters
afirst value
bsecond value
Returns
the smaller of the two values

Function Documentation

◆ vlAlgoAddOverflow()

VL_API vl_bool_t vlAlgoAddOverflow ( vl_ularge_t  a,
vl_ularge_t  b 
)

Tests whether adding two unsigned integers would cause overflow.

Uses compiler intrinsics when available for optimal performance and accuracy. Falls back to portable overflow detection otherwise.

Parameters
afirst unsigned integer
bsecond unsigned integer
Complexity O(1) constant
Returns
VL_TRUE if a + b would overflow, VL_FALSE otherwise

◆ vlAlgoCLZ16()

VL_API vl_uint_t vlAlgoCLZ16 ( vl_uint16_t  value)

Counts the number of leading zero bits in a 16-bit value.

Returns 16 if the input value is 0. Uses compiler intrinsics when available for optimal performance, with portable fallback implementations.

Parameters
value16-bit unsigned integer
Complexity O(1) constant
Returns
number of leading zero bits (0-16)
+ Here is the call graph for this function:

◆ vlAlgoCLZ32()

VL_API vl_uint_t vlAlgoCLZ32 ( vl_uint32_t  value)

Counts the number of leading zero bits in a 32-bit value.

Returns 32 if the input value is 0. Uses compiler intrinsics when available for optimal performance, with portable fallback implementations.

Parameters
value32-bit unsigned integer
Complexity O(1) constant
Returns
number of leading zero bits (0-32)
+ Here is the caller graph for this function:

◆ vlAlgoCLZ64()

VL_API vl_uint_t vlAlgoCLZ64 ( vl_uint64_t  value)

Counts the number of leading zero bits in a 64-bit value.

Returns 64 if the input value is 0. Uses compiler intrinsics when available for optimal performance, with portable fallback implementations.

Parameters
value64-bit unsigned integer
Complexity O(1) constant
Returns
number of leading zero bits (0-64)
+ Here is the call graph for this function:

◆ vlAlgoCLZ8()

VL_API vl_uint_t vlAlgoCLZ8 ( vl_uint8_t  value)

Counts the number of leading zero bits in an 8-bit value.

Returns 8 if the input value is 0. Uses compiler intrinsics when available for optimal performance, with portable fallback implementations.

Parameters
value8-bit unsigned integer
Complexity O(1) constant
Returns
number of leading zero bits (0-8)
+ Here is the caller graph for this function:

◆ vlAlgoCTZ16()

VL_API vl_uint_t vlAlgoCTZ16 ( vl_uint16_t  value)

Counts the number of trailing zero bits in a 16-bit value.

Returns 16 if the input value is 0. Uses compiler intrinsics when available for optimal performance, with portable fallback implementations.

Parameters
value16-bit unsigned integer
Complexity O(1) constant
Returns
number of trailing zero bits (0-16)
+ Here is the call graph for this function:

◆ vlAlgoCTZ32()

VL_API vl_uint_t vlAlgoCTZ32 ( vl_uint32_t  value)

Counts the number of trailing zero bits in a 32-bit value.

Returns 32 if the input value is 0. Uses compiler intrinsics when available for optimal performance, with portable fallback implementations.

Parameters
value32-bit unsigned integer
Complexity O(1) constant
Returns
number of trailing zero bits (0-32)
+ Here is the caller graph for this function:

◆ vlAlgoCTZ64()

VL_API vl_uint_t vlAlgoCTZ64 ( vl_uint64_t  value)

Counts the number of trailing zero bits in a 64-bit value.

Returns 64 if the input value is 0. Uses compiler intrinsics when available for optimal performance, with portable fallback implementations.

Parameters
value64-bit unsigned integer
Complexity O(1) constant
Returns
number of trailing zero bits (0-64)
+ Here is the call graph for this function:

◆ vlAlgoCTZ8()

VL_API vl_uint_t vlAlgoCTZ8 ( vl_uint8_t  value)

Counts the number of trailing zero bits in an 8-bit value.

Returns 8 if the input value is 0. Uses compiler intrinsics when available for optimal performance, with portable fallback implementations.

Parameters
value8-bit unsigned integer
Complexity O(1) constant
Returns
number of trailing zero bits (0-8)
+ Here is the caller graph for this function:

◆ vlAlgoGCD()

VL_API vl_ularge_t vlAlgoGCD ( vl_ularge_t  a,
vl_ularge_t  b 
)

Computes the Greatest Common Divisor (GCD) of two unsigned integers.

Uses the Euclidean algorithm for efficient computation. Handles edge cases where one or both inputs are zero appropriately.

Parameters
afirst unsigned integer
bsecond unsigned integer
Complexity O(log(min(a,b))) logarithmic
Returns
greatest common divisor of a and b
+ Here is the caller graph for this function:

◆ vlAlgoGCDSigned()

VL_API vl_ilarge_t vlAlgoGCDSigned ( vl_ilarge_t  a,
vl_ilarge_t  b 
)

Computes the Greatest Common Divisor (GCD) of two signed integers.

Converts inputs to absolute values and delegates to the unsigned version. Handles the edge case of the most negative integer value safely.

Parameters
afirst signed integer
bsecond signed integer
Complexity O(log(min(|a|,|b|))) logarithmic
Returns
greatest common divisor of |a| and |b|
+ Here is the call graph for this function:

◆ vlAlgoIsPO2()

VL_API vl_bool_t vlAlgoIsPO2 ( vl_ularge_t  value)

Tests whether a value is a power of 2.

Returns VL_FALSE for input value 0, as 0 is not considered a power of 2. Uses efficient bit manipulation to perform the test.

Contract

  • Ownership: None.
  • Lifetime: N/A.
  • Thread Safety: Thread-safe (stateless).
  • Nullability: N/A.
  • Error Conditions: None.
  • Undefined Behavior: None.
  • Memory Allocation Expectations: None.
  • Return-value Semantics: Returns VL_TRUE if the value is a power of 2, VL_FALSE otherwise.
Parameters
valueinput unsigned integer
Complexity O(1) constant
Returns
VL_TRUE if value is a power of 2, VL_FALSE otherwise

◆ vlAlgoLCM()

VL_API vl_ularge_t vlAlgoLCM ( vl_ularge_t  a,
vl_ularge_t  b 
)

Computes the Least Common Multiple (LCM) of two unsigned integers.

Uses the relationship LCM(a,b) = |a*b| / GCD(a,b) with overflow protection. Returns 0 if either input is 0 or if the result would overflow.

Parameters
afirst unsigned integer
bsecond unsigned integer
Complexity O(log(min(a,b))) logarithmic
Returns
least common multiple of a and b, or 0 on overflow
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlAlgoLCMSigned()

VL_API vl_ilarge_t vlAlgoLCMSigned ( vl_ilarge_t  a,
vl_ilarge_t  b 
)

Computes the Least Common Multiple (LCM) of two signed integers.

The result is always positive by mathematical definition. Converts inputs to absolute values and delegates to the unsigned version with overflow checking. Returns 0 if either input is 0 or if the result would overflow.

Parameters
afirst signed integer
bsecond signed integer
Complexity O(log(min(|a|,|b|))) logarithmic
Returns
least common multiple of |a| and |b|, or 0 on overflow
+ Here is the call graph for this function:

◆ vlAlgoMulOverflow()

VL_API vl_bool_t vlAlgoMulOverflow ( vl_ularge_t  a,
vl_ularge_t  b 
)

Tests whether multiplying two unsigned integers would cause overflow.

Uses compiler intrinsics when available for optimal performance and accuracy. Falls back to portable overflow detection otherwise.

Parameters
afirst unsigned integer
bsecond unsigned integer
Complexity O(1) constant
Returns
VL_TRUE if a * b would overflow, VL_FALSE otherwise

◆ vlAlgoNextPO2()

VL_API vl_uint_t vlAlgoNextPO2 ( vl_ularge_t  value)

Computes the next power of 2 greater than or equal to the given value.

If the input value is already a power of 2, it returns the same value. Returns 1 for input value 0. Returns 0 if the result would overflow.

Contract

  • Ownership: None.
  • Lifetime: N/A.
  • Thread Safety: Thread-safe (stateless).
  • Nullability: N/A.
  • Error Conditions: Returns 0 if the next power of 2 would exceed the range of vl_uint_t.
  • Undefined Behavior: None.
  • Memory Allocation Expectations: None.
  • Return-value Semantics: Returns the computed power of 2, or 0 on overflow.
Parameters
valueinput unsigned integer
Complexity O(1) constant
Returns
next power of 2, or 0 on overflow

◆ vlAlgoPopCount16()

VL_API vl_uint_t vlAlgoPopCount16 ( vl_uint16_t  value)

Counts the number of set bits (population count) in a 16-bit value.

Uses compiler intrinsics when available for optimal performance. Falls back to portable bit manipulation algorithms otherwise.

Parameters
value16-bit unsigned integer
Complexity O(1) constant
Returns
number of bits set to 1 in the value

◆ vlAlgoPopCount32()

VL_API vl_uint_t vlAlgoPopCount32 ( vl_uint32_t  value)

Counts the number of set bits (population count) in a 32-bit value.

Uses compiler intrinsics when available for optimal performance. Falls back to portable bit manipulation algorithms otherwise.

Parameters
value32-bit unsigned integer
Complexity O(1) constant
Returns
number of bits set to 1 in the value
+ Here is the caller graph for this function:

◆ vlAlgoPopCount64()

VL_API vl_uint_t vlAlgoPopCount64 ( vl_uint64_t  value)

Counts the number of set bits (population count) in a 64-bit value.

Uses compiler intrinsics when available for optimal performance. Falls back to portable bit manipulation algorithms otherwise.

Parameters
value64-bit unsigned integer
Complexity O(1) constant
Returns
number of bits set to 1 in the value
+ Here is the call graph for this function:

◆ vlAlgoPopCount8()

VL_API vl_uint_t vlAlgoPopCount8 ( vl_uint8_t  value)

Counts the number of set bits (population count) in an 8-bit value.

Uses compiler intrinsics when available for optimal performance. Falls back to portable bit manipulation algorithms otherwise.

Parameters
value8-bit unsigned integer
Complexity O(1) constant
Returns
number of bits set to 1 in the value

◆ vlAlgoSubUnderflow()

VL_API vl_bool_t vlAlgoSubUnderflow ( vl_ularge_t  a,
vl_ularge_t  b 
)

Tests whether subtracting two unsigned integers would cause underflow.

For unsigned integers, underflow occurs when the first operand is smaller than the second operand. Uses efficient comparison for detection.

Parameters
afirst unsigned integer
bsecond unsigned integer
Complexity O(1) constant
Returns
VL_TRUE if a - b would underflow, VL_FALSE otherwise