xref: /linux/include/drm/intel/pick.h (revision 4a57e0913e8c7fff407e97909f4ae48caa84d612)
1 /* SPDX-License-Identifier: MIT */
2 /* Copyright © 2026 Intel Corporation */
3 
4 #ifndef _PICK_H_
5 #define _PICK_H_
6 
7 /*
8  * Given the first two numbers __a and __b of arbitrarily many evenly spaced
9  * numbers, pick the 0-based __index'th value.
10  *
11  * Always prefer this over _PICK() if the numbers are evenly spaced.
12  */
13 #define _PICK_EVEN(__index, __a, __b) ((__a) + (__index) * ((__b) - (__a)))
14 
15 /*
16  * Like _PICK_EVEN(), but supports 2 ranges of evenly spaced address offsets.
17  * @__c_index corresponds to the index in which the second range starts to be
18  * used. Using math interval notation, the first range is used for indexes [ 0,
19  * @__c_index), while the second range is used for [ @__c_index, ... ). Example:
20  *
21  * #define _FOO_A			0xf000
22  * #define _FOO_B			0xf004
23  * #define _FOO_C			0xf008
24  * #define _SUPER_FOO_A			0xa000
25  * #define _SUPER_FOO_B			0xa100
26  * #define FOO(x)			_MMIO(_PICK_EVEN_2RANGES(x, 3,		\
27  *					      _FOO_A, _FOO_B,			\
28  *					      _SUPER_FOO_A, _SUPER_FOO_B))
29  *
30  * This expands to:
31  *	0: 0xf000,
32  *	1: 0xf004,
33  *	2: 0xf008,
34  *	3: 0xa000,
35  *	4: 0xa100,
36  *	5: 0xa200,
37  *	...
38  */
39 #define _PICK_EVEN_2RANGES(__index, __c_index, __a, __b, __c, __d)		\
40 	(BUILD_BUG_ON_ZERO(!__is_constexpr(__c_index)) +			\
41 	 ((__index) < (__c_index) ? _PICK_EVEN(__index, __a, __b) :		\
42 				   _PICK_EVEN((__index) - (__c_index), __c, __d)))
43 
44 /*
45  * Given the arbitrary numbers in varargs, pick the 0-based __index'th number.
46  *
47  * Always prefer _PICK_EVEN() over this if the numbers are evenly spaced.
48  */
49 #define _PICK(__index, ...) (((const u32 []){ __VA_ARGS__ })[__index])
50 
51 #endif
52