util_macros.h: add find_closest() macro
This series unduplicates the code used to find the member in an array closest to 'x'. The first patch adds a macro implementing the algorithm in two flavors - for arrays sorted in ascending and descending order. The second updates Documentation/CodingStyle on the naming convention for local variables in macros resembling functions. Other three patches replace duplicated code with calls to one of these macros in some hwmon drivers. This patch (of 5): Searching for the member of an array closest to 'x' is duplicated in several places. Add a new include - util_macros.h - and two macros that implement this algorithm for arrays sorted both in ascending and descending order. Uses linear search. Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> Cc: Guenter Roeck <linux@roeck-us.net> Cc: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
		
					parent
					
						
							
								a7a2c02a40
							
						
					
				
			
			
				commit
				
					
						95d119528b
					
				
			
		
					 1 changed files with 40 additions and 0 deletions
				
			
		
							
								
								
									
										40
									
								
								include/linux/util_macros.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								include/linux/util_macros.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,40 @@
 | 
				
			||||||
 | 
					#ifndef _LINUX_HELPER_MACROS_H_
 | 
				
			||||||
 | 
					#define _LINUX_HELPER_MACROS_H_
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define __find_closest(x, a, as, op)					\
 | 
				
			||||||
 | 
					({									\
 | 
				
			||||||
 | 
						typeof(as) __fc_i, __fc_as = (as) - 1;				\
 | 
				
			||||||
 | 
						typeof(x) __fc_x = (x);						\
 | 
				
			||||||
 | 
						typeof(*a) *__fc_a = (a);					\
 | 
				
			||||||
 | 
						for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) {			\
 | 
				
			||||||
 | 
							if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] +	\
 | 
				
			||||||
 | 
											__fc_a[__fc_i + 1], 2))	\
 | 
				
			||||||
 | 
								break;						\
 | 
				
			||||||
 | 
						}								\
 | 
				
			||||||
 | 
						(__fc_i);							\
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * find_closest - locate the closest element in a sorted array
 | 
				
			||||||
 | 
					 * @x: The reference value.
 | 
				
			||||||
 | 
					 * @a: 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'.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define find_closest(x, a, as) __find_closest(x, a, as, <=)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * find_closest_descending - locate the closest element in a sorted array
 | 
				
			||||||
 | 
					 * @x: The reference value.
 | 
				
			||||||
 | 
					 * @a: 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.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define find_closest_descending(x, a, as) __find_closest(x, a, as, >=)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue