1 | */* SPDX-License-Identifier: GPL-2.0 */* |

2 | __#ifndef _LINUX_HELPER_MACROS_H___ |

3 | __#define _LINUX_HELPER_MACROS_H___ |

4 | |

5 | __#define __find_closest(x, a, as, op) \__ |

6 | __({ \__ |

7 | __ typeof(as) __fc_i, __fc_as = (as) - 1; \__ |

8 | __ typeof(x) __fc_x = (x); \__ |

9 | __ typeof(*a) const *__fc_a = (a); \__ |

10 | __ for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \__ |

11 | __ if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] + \__ |

12 | __ __fc_a[__fc_i + 1], 2)) \__ |

13 | __ break; \__ |

14 | __ } \__ |

15 | __ (__fc_i); \__ |

16 | __})__ |

17 | |

18 | */*** |

19 | * * find_closest - locate the closest element in a sorted array* |

20 | * * @x: The reference value.* |

21 | * * @a: The array in which to look for the closest element. Must be sorted* |

22 | * * in ascending order.* |

23 | * * @as: Size of 'a'.* |

24 | * ** |

25 | * * Returns the index of the element closest to 'x'.* |

26 | * */* |

27 | __#define find_closest(x, a, as) __find_closest(x, a, as, <=)__ |

28 | |

29 | */*** |

30 | * * find_closest_descending - locate the closest element in a sorted array* |

31 | * * @x: The reference value.* |

32 | * * @a: The array in which to look for the closest element. Must be sorted* |

33 | * * in descending order.* |

34 | * * @as: Size of 'a'.* |

35 | * ** |

36 | * * Similar to find_closest() but 'a' is expected to be sorted in descending* |

37 | * * order.* |

38 | * */* |

39 | __#define find_closest_descending(x, a, as) __find_closest(x, a, as, >=)__ |

40 | |

41 | __#endif__ |

42 | |