GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
Macros
gp_common.h File Reference

Common macros. More...

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <core/gp_compiler.h>

Go to the source code of this file.

Macros

#define GP_MIN(a, b)
 Returns a minimum of the two numbers.
 
#define GP_MIN3(a, b, c)
 Returns a minimum of the three numbers.
 
#define GP_MAX(a, b)
 Returns a maximum of the two numbers.
 
#define GP_MAX3(a, b, c)
 Returns a maximum of the three numbers.
 
#define GP_ABS(a)
 Returns an absolute value.
 
#define GP_ABS_DIFF(a, b)
 Computes an absolute value of a difference.
 
#define GP_ALIGN2(a)
 Aligns a value to be even.
 
#define GP_SWAP(a, b)
 Swaps a and b.
 
#define GP_ARRAY_SIZE(array)   (sizeof(array) / sizeof(*array))
 Computes number of elements of a statically defined array size.
 
#define GP_CONTAINER_OF(ptr, structure, member)    ((structure *)((char *)(ptr) - offsetof(structure, member)))
 Converts from a pointer to a struct field to a pointer to the struct itself.
 

Detailed Description

Common macros.

Definition in file gp_common.h.

Macro Definition Documentation

◆ GP_ABS

#define GP_ABS (   a)
Value:
({ \
typeof(a) gp_a__ = a; \
gp_a__ > 0 ? gp_a__ : - gp_a__; \
})

Returns an absolute value.

Parameters
aA number.
Returns
An absolute value of a.

Definition at line 86 of file gp_common.h.

◆ GP_ABS_DIFF

#define GP_ABS_DIFF (   a,
 
)
Value:
({ \
typeof(a) gp_a__ = a; \
typeof(b) gp_b__ = b; \
gp_a__ > gp_b__ ? gp_a__ - gp_b__ : gp_b__ - gp_a__; \
})

Computes an absolute value of a difference.

Parameters
aA number.
bA number.
Returns
An absolute value of a difference between a and b.

Definition at line 99 of file gp_common.h.

◆ GP_ALIGN2

#define GP_ALIGN2 (   a)
Value:
({ \
typeof(a) gp_a__ = a; \
gp_a__ + (gp_a__%2); \
})

Aligns a value to be even.

Parameters
aAn integer number.
Returns
A value aligned to be divisible by two.

Definition at line 111 of file gp_common.h.

◆ GP_ARRAY_SIZE

#define GP_ARRAY_SIZE (   array)    (sizeof(array) / sizeof(*array))

Computes number of elements of a statically defined array size.

Parameters
arrayAn array.
Returns
A number of array elements.

Definition at line 144 of file gp_common.h.

◆ GP_CONTAINER_OF

#define GP_CONTAINER_OF (   ptr,
  structure,
  member 
)     ((structure *)((char *)(ptr) - offsetof(structure, member)))

Converts from a pointer to a struct field to a pointer to the struct itself.

The inverse functions is part of the C language and is called offsetof().

Parameters
ptrA pointer to a structure member.
structureA structure C type e.g. struct foo.
memberA structure member name.

Definition at line 156 of file gp_common.h.

◆ GP_MAX

#define GP_MAX (   a,
 
)
Value:
({ \
typeof(a) gp_a__ = (a); \
typeof(b) gp_b__ = (b); \
gp_a__ > gp_b__ ? gp_a__ : gp_b__; \
})

Returns a maximum of the two numbers.

Parameters
aA number.
bA number.
Returns
A maximum of a and b.

Definition at line 59 of file gp_common.h.

◆ GP_MAX3

#define GP_MAX3 (   a,
  b,
 
)
Value:
({ \
typeof(a) gp_a__ = (a); \
typeof(b) gp_b__ = (b); \
typeof(c) gp_c__ = (c); \
gp_a__ > gp_b__ ? (gp_a__ > gp_c__ ? gp_a__ : gp_c__) : (gp_b__ > gp_c__ ? gp_b__ : gp_c__); \
})

Returns a maximum of the three numbers.

Parameters
aA number.
bA number.
cA number.
Returns
A maximum of a, b and c.

Definition at line 73 of file gp_common.h.

◆ GP_MIN

#define GP_MIN (   a,
 
)
Value:
({ \
typeof(a) gp_a__ = (a); \
typeof(b) gp_b__ = (b); \
gp_a__ < gp_b__ ? gp_a__ : gp_b__; \
})

Returns a minimum of the two numbers.

Parameters
aA number.
bA number.
Returns
A minimum of a and b.

Definition at line 31 of file gp_common.h.

◆ GP_MIN3

#define GP_MIN3 (   a,
  b,
 
)
Value:
({ \
typeof(a) gp_a__ = (a); \
typeof(b) gp_b__ = (b); \
typeof(c) gp_c__ = (c); \
gp_a__ < gp_b__ ? (gp_a__ < gp_c__ ? gp_a__ : gp_c__) : (gp_b__ < gp_c__ ? gp_b__ : gp_c__); \
})

Returns a minimum of the three numbers.

Parameters
aA number.
bA number.
cA number.
Returns
A minimum of a, b and c.

Definition at line 45 of file gp_common.h.

◆ GP_SWAP

#define GP_SWAP (   a,
 
)
Value:
do { \
typeof(b) gp_b__ = b; \
b = a; \
a = gp_b__; \
} while (0)

Swaps a and b.

Attention
Modifies the variables passed as parameters.
Parameters
aA value to be swapped.
bA value to be swapped.

Definition at line 124 of file gp_common.h.