in spite of my comments about optimization i did it anyway, at least a subset. to optimize code you need to include the context. in our case here, what are the statistical frequencies of the two arguments n32_1 and n32_2?
the optimal solution will deal with the most frequent case first, which has led me to the optimal answer.
i am showing the optimal solution for the case where both arguments are
random in the range INT_MIN through INT_MAX inclusive. brave words considering how flawless my track record is to date.
i assert the best thing to do first is to take the difference of the two arguments. then discover the most common case, which is they do NOT
fall in the interesting space. that leads to the answer quite nicely:
int64_t d = (int64_t)n32_1 - (int64_t)n32_2;
if (d > 1 || d < -1)
return 0;
return 1;
now whether using if or ? is faster i do not know. as chqrlie's solution shows, there is a better way to do this that doesn't have to consider anything about the input probabilities and produces much less assembler code to get the job done. i would write it this way
return (uint64_t)1 + n32_1 - n32_2 <= 2;
it is amazing how much strange assembler code is generated when you promote the int32 to int64 instead of promoting it to uint64.