SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
Macros
stddef.h File Reference

Standard definitions for C and C++. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NULL   ((void *) 0)
 Null pointer constant.
 
#define offsetof(type, field)   ((size_t) (&((type *) 0)->field))
 Get the offset of a member within a structure.
 
#define container_of(ptr, type, member)
 Get a pointer to the container structure from a pointer to a member.
 
#define likely(expr)   (!!(expr))
 Likely expression hint for branch prediction.
 
#define unlikely(expr)   (!!(expr))
 
#define min(a, b)   (((a) < (b)) ? (a) : (b))
 Minimum of two values.
 
#define max(a, b)   (((a) > (b)) ? (a) : (b))
 Maximum of two values.
 
#define clamp(v, a, b)   min(max(a, v), b)
 Clamp a value between a minimum and maximum.
 
#define ifloor(x)   ((x) > 0 ? (int) (x) : (int) ((x) -0.9999999999))
 Integer floor of a floating-point value.
 
#define iround(x)   ((x) > 0 ? (int) ((x) + 0.5) : (int) ((x) -0.5))
 Integer rounding of a floating-point value.
 
#define iceil(x)   ((x) > 0 ? (int) ((x) + 0.9999999999) : (int) (x))
 Integer ceiling of a floating-point value.
 
#define idiv255(x)   ((((int) (x) + 1) * 257) >> 16)
 Divide an integer by 255 efficiently.
 

Detailed Description

Standard definitions for C and C++.

This file contains several macros and definitions used in both C and C++ code. It defines types, utility macros, and compiler-specific optimizations for portability across different compilers.

Macro Definition Documentation

◆ clamp

#define clamp (   v,
  a,
 
)    min(max(a, v), b)

Clamp a value between a minimum and maximum.

This macro ensures that the value v is between a and b. If v is smaller than a, it returns a, and if v is greater than b, it returns b.

Parameters
vValue to be clamped.
aMinimum bound.
bMaximum bound.
Returns
The clamped value.

◆ container_of

#define container_of (   ptr,
  type,
  member 
)
Value:
({ \
const typeof(((type *) 0)->member) *__mptr = (ptr); \
(type *) ((char *) __mptr - offsetof(type, member)); \
})
#define offsetof(type, field)
Definition stddef.h:13

Get a pointer to the container structure from a pointer to a member.

This macro computes the address of the container structure from a pointer to a member within the structure. This is useful for traversing linked data structures like lists or trees.

Parameters
ptrPointer to the member.
typeType of the structure containing the member.
memberName of the member in the structure.
Returns
Pointer to the container structure.

◆ iceil

#define iceil (   x)    ((x) > 0 ? (int) ((x) + 0.9999999999) : (int) (x))

Integer ceiling of a floating-point value.

Computes the ceiling of a floating-point value x, rounding up to the nearest integer.

Parameters
xThe floating-point value.
Returns
The smallest integer greater than or equal to x.

◆ idiv255

#define idiv255 (   x)    ((((int) (x) + 1) * 257) >> 16)

Divide an integer by 255 efficiently.

Performs division of an integer by 255, with an efficient algorithm using bit shifts. This is useful for applications where division by 255 is common, such as image processing.

Parameters
xThe integer value.
Returns
The result of dividing x by 255.

◆ ifloor

#define ifloor (   x)    ((x) > 0 ? (int) (x) : (int) ((x) -0.9999999999))

Integer floor of a floating-point value.

Computes the integer floor of a floating-point value x. If x is positive, it truncates towards zero; otherwise, it rounds down to the nearest integer.

Parameters
xThe floating-point value.
Returns
The integer floor of x.

◆ iround

#define iround (   x)    ((x) > 0 ? (int) ((x) + 0.5) : (int) ((x) -0.5))

Integer rounding of a floating-point value.

Rounds a floating-point value x to the nearest integer. Rounds up if x is positive and 0.5 or more, and rounds down otherwise.

Parameters
xThe floating-point value.
Returns
The nearest integer to x.

◆ likely

#define likely (   expr)    (!!(expr))

Likely expression hint for branch prediction.

Provides a hint to the compiler that the given expression is likely to be true. This helps the compiler optimize the code, improving performance in certain cases.

Parameters
exprExpression that is likely true.
Returns
The expression itself.

◆ max

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

Maximum of two values.

Returns the larger of two values, a and b.

Parameters
aFirst value.
bSecond value.
Returns
The larger value.

◆ min

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

Minimum of two values.

Returns the smaller of two values, a and b.

Parameters
aFirst value.
bSecond value.
Returns
The smaller value.

◆ NULL

#define NULL   ((void *) 0)

Null pointer constant.

Defines the value of NULL. In C++, NULL is defined as 0, while in C, it is defined as a void pointer of value 0. The NULL macro can be used to represent null pointers in both languages.

◆ offsetof

#define offsetof (   type,
  field 
)    ((size_t) (&((type *) 0)->field))

Get the offset of a member within a structure.

Returns the byte offset of a given member within a structure. This is useful when working with low-level memory manipulation or in custom memory allocation schemes.

Parameters
typeThe type of the structure.
memberThe member of the structure whose offset is to be calculated.
Returns
The offset of the member in bytes.

◆ unlikely

#define unlikely (   expr)    (!!(expr))