blob: e92cc8bae7c008ddb9cb3af9bc2306451e0028e3 [file] [log] [blame]
Julius Werner886d29b2014-09-24 15:40:49 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef __SRC_INCLUDE_BASE3_H__
21#define __SRC_INCLUDE_BASE3_H__
22
23/* We translate a floating pin (Z) as the ternary digit 2. */
24#define Z 2
25
26/*
27 * This provides a variadic macro BASE3() that can be used to translate a set of
28 * pin states into its base-3 integer representation, even in the context of a
29 * static initializer. You can call it with any number of up to 6 arguments,
30 * e.g. BASE3(1, Z) -> 5 or BASE3(0, Z, 1, 0) -> 21. Just don't look too closely
31 * at how the sausage is made. (Pay extra attention to typos when expanding it!)
32 */
33#define _BASE3_IMPL_1(arg0, arg1, arg2, arg3, arg4, arg5) arg0
34#define _BASE3_IMPL_2(arg0, arg1, arg2, arg3, arg4, arg5) \
35 (arg1 + (3 * _BASE3_IMPL_1(arg0, arg1, arg2, arg3, arg4, arg5)))
36#define _BASE3_IMPL_3(arg0, arg1, arg2, arg3, arg4, arg5) \
37 (arg2 + (3 * _BASE3_IMPL_2(arg0, arg1, arg2, arg3, arg4, arg5)))
38#define _BASE3_IMPL_4(arg0, arg1, arg2, arg3, arg4, arg5) \
39 (arg3 + (3 * _BASE3_IMPL_3(arg0, arg1, arg2, arg3, arg4, arg5)))
40#define _BASE3_IMPL_5(arg0, arg1, arg2, arg3, arg4, arg5) \
41 (arg4 + (3 * _BASE3_IMPL_4(arg0, arg1, arg2, arg3, arg4, arg5)))
42#define _BASE3_IMPL_6(arg0, arg1, arg2, arg3, arg4, arg5) \
43 (arg5 + (3 * _BASE3_IMPL_5(arg0, arg1, arg2, arg3, arg4, arg5)))
44#define _BASE3_IMPL(arg0, arg1, arg2, arg3, arg4, arg5, NARGS, ...) \
45 _BASE3_IMPL##NARGS(arg0, arg1, arg2, arg3, arg4, arg5)
46#define BASE3(...) _BASE3_IMPL(__VA_ARGS__, _6, _5, _4, _3, _2, _1)
47
48#endif /* __SRC_INCLUDE_BASE3_H__ */