Sven Eckelmann wrote:
Now it gets real cruel, but who cares... C++ wasn't accept in the kernel, so we have to deal with it using some GNUish stuff.
#define seq_before(x,y) ({typeof(x) _dummy = (x - y); \ _dummy > 1u << (7u + 8u * (sizeof(_dummy) - 1u));}) #define seq_after(x,y) ({typeof(x) _dummy = (y - x); \ _dummy >= 1u << (7u + 8u * (sizeof(_dummy) - 1u));})
After a small discussion with Marek I would like to suggest a easier readable and symmetric definition
/* Returns the smallest signed integer in two's complement with the sizeof x */ #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
/* Checks if a sequence number x is a predecessor/successor of y. they handle overflows/underflows and can correctly check for a predecessor/successor unless the variable sequence number has grown by more then 2**(bitwidth(x)-1)-1. This means that for an uint8_t with the maximum value 255, it would think: * when adding nothing - it is neither a predecessor nor a successor * before adding more than 127 to the starting value - it is a predecessor, * when adding 128 - it is neither a predecessor nor a successor, * after adding more than 127 to the starting value - it is a successor */ #define seq_before(x,y) ({typeof(x) _dummy = (x - y); \ _dummy > smallest_signed_int(_dummy); }) #define seq_after(x,y) seq_before(y,x)
Best regards, Sven