a cautionary tale about simple-looking macros
Posted: Sun Jul 03, 2011 6:10 pm
To try and understand how Ippolit works, I have been reformatting it to make it readable (renaming things, grouping related functions in files, etc).
Anyone who has looked at the Ippolit code knows that it looks a bit machine-generated. In particular, there are parts which could only be the output of some kind of macro expander (probably the C preprocessor, though it could be other things as well). I came across a real gem of one of these, which I spent the last two hours translating back into something human-readable! So I will describe it here.
The code in question can be found in IPP_ENG.c around line 6169. It looks like this:
...Well actually, it only looks like that in a text editor that doesnt wrap lines. The full code is so long I'll put it in a separate reply. But just to give you an idea, its over 22KB of source code, and contains 5,162 parentheses characters. It also contains 168 usages of the ternary operator (x ? y : z).
More after the break.
Anyone who has looked at the Ippolit code knows that it looks a bit machine-generated. In particular, there are parts which could only be the output of some kind of macro expander (probably the C preprocessor, though it could be other things as well). I came across a real gem of one of these, which I spent the last two hours translating back into something human-readable! So I will describe it here.
The code in question can be found in IPP_ENG.c around line 6169. It looks like this:
Code: Select all
if ((tower_dynamics->flag & 128))
{
if (score > 0)
{
if (position_fixed.bitboard[enumerated_white_bright])
score -= 20 * (((((((((((((A8) & 7) - ((position_fixed.black_king) & 7)) >= 0) ? (((A8) & 7) - ((pos....
else
score -= 20 * (((((((((((((A1) & 7) - ((position_fixed.black_king) & 7)) >= 0) ? (((A1) & 7) - ((pos....
}
else
{
if (position_fixed.bitboard[enumerated_black_bright])
score += 20 * (((((((((((((A8) & 7) - ((position_fixed.white_king) & 7)) >= 0) ? (((A8) & 7) - ((pos....
else
score += 20 * (((((((((((((A1) & 7) - ((position_fixed.white_king) & 7)) >= 0) ? (((A1) & 7) - ((pos....
}
}
More after the break.