XenevaOS
Loading...
Searching...
No Matches
Macros
util_macros.h File Reference
#include <linux/compiler_attributes.h>
#include <linux/math.h>
#include <linux/typecheck.h>
#include <linux/stddef.h>
Include dependency graph for util_macros.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define for_each_if(condition)   if (!(condition)) {} else
 
#define find_closest(x, a, as)
 
#define find_closest_descending(x, a, as)
 

Macro Definition Documentation

◆ find_closest

#define find_closest (   x,
  a,
  as 
)
Value:
({ \
typeof(as) __fc_i, __fc_as = (as) - 1; \
long __fc_mid_x, __fc_x = (x); \
long __fc_left, __fc_right; \
typeof(*a) const *__fc_a = (a); \
for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \
__fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i + 1]) / 2; \
if (__fc_x <= __fc_mid_x) { \
__fc_left = __fc_x - __fc_a[__fc_i]; \
__fc_right = __fc_a[__fc_i + 1] - __fc_x; \
if (__fc_right < __fc_left) \
__fc_i++; \
break; \
} \
} \
(__fc_i); \
})
size_t a
Definition main.cpp:156
int x
Definition layout.c:118

find_closest - locate the closest element in a sorted array @x: The reference value. : The array in which to look for the closest element. Must be sorted in ascending order. @as: Size of 'a'.

Returns the index of the element closest to 'x'. Note: If using an array of negative numbers (or mixed positive numbers), then be sure that 'x' is of a signed-type to get good results.

◆ find_closest_descending

#define find_closest_descending (   x,
  a,
  as 
)
Value:
({ \
typeof(as) __fc_i, __fc_as = (as) - 1; \
long __fc_mid_x, __fc_x = (x); \
long __fc_left, __fc_right; \
typeof(*a) const *__fc_a = (a); \
for (__fc_i = __fc_as; __fc_i >= 1; __fc_i--) { \
__fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i - 1]) / 2; \
if (__fc_x <= __fc_mid_x) { \
__fc_left = __fc_x - __fc_a[__fc_i]; \
__fc_right = __fc_a[__fc_i - 1] - __fc_x; \
if (__fc_right < __fc_left) \
__fc_i--; \
break; \
} \
} \
(__fc_i); \
})

find_closest_descending - locate the closest element in a sorted array @x: The reference value. : The array in which to look for the closest element. Must be sorted in descending order. @as: Size of 'a'.

Similar to find_closest() but 'a' is expected to be sorted in descending order. The iteration is done in reverse order, so that the comparison of '__fc_right' & '__fc_left' also works for unsigned numbers.

◆ for_each_if

#define for_each_if (   condition)    if (!(condition)) {} else

for_each_if - helper for handling conditionals in various for_each macros @condition: The condition to check

Typical use::

define for_each_foo_bar(x, y) \' list_for_each_entry(x, y->list, head) \' for_each_if(x->something == SOMETHING)

The for_each_if() macro makes the use of for_each_foo_bar() less error prone.