blob: 1c19274541fe268d610389bc6098e7fcb0216bd0 [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.
Julius Werner886d29b2014-09-24 15:40:49 -070014 */
15
16#ifndef __SRC_INCLUDE_BASE3_H__
17#define __SRC_INCLUDE_BASE3_H__
18
19/* We translate a floating pin (Z) as the ternary digit 2. */
20#define Z 2
21
22/*
23 * This provides a variadic macro BASE3() that can be used to translate a set of
24 * pin states into its base-3 integer representation, even in the context of a
25 * static initializer. You can call it with any number of up to 6 arguments,
26 * e.g. BASE3(1, Z) -> 5 or BASE3(0, Z, 1, 0) -> 21. Just don't look too closely
27 * at how the sausage is made. (Pay extra attention to typos when expanding it!)
28 */
29#define _BASE3_IMPL_1(arg0, arg1, arg2, arg3, arg4, arg5) arg0
30#define _BASE3_IMPL_2(arg0, arg1, arg2, arg3, arg4, arg5) \
31 (arg1 + (3 * _BASE3_IMPL_1(arg0, arg1, arg2, arg3, arg4, arg5)))
32#define _BASE3_IMPL_3(arg0, arg1, arg2, arg3, arg4, arg5) \
33 (arg2 + (3 * _BASE3_IMPL_2(arg0, arg1, arg2, arg3, arg4, arg5)))
34#define _BASE3_IMPL_4(arg0, arg1, arg2, arg3, arg4, arg5) \
35 (arg3 + (3 * _BASE3_IMPL_3(arg0, arg1, arg2, arg3, arg4, arg5)))
36#define _BASE3_IMPL_5(arg0, arg1, arg2, arg3, arg4, arg5) \
37 (arg4 + (3 * _BASE3_IMPL_4(arg0, arg1, arg2, arg3, arg4, arg5)))
38#define _BASE3_IMPL_6(arg0, arg1, arg2, arg3, arg4, arg5) \
39 (arg5 + (3 * _BASE3_IMPL_5(arg0, arg1, arg2, arg3, arg4, arg5)))
40#define _BASE3_IMPL(arg0, arg1, arg2, arg3, arg4, arg5, NARGS, ...) \
41 _BASE3_IMPL##NARGS(arg0, arg1, arg2, arg3, arg4, arg5)
42#define BASE3(...) _BASE3_IMPL(__VA_ARGS__, _6, _5, _4, _3, _2, _1)
43
44#endif /* __SRC_INCLUDE_BASE3_H__ */