# Does the time_after() macro work only for certain range of values?

Posted by VY
 Does the time_after() macro work only for certain range of values? February 22, 2018 04:12PM
```I have just learned about the time_after() macro in <linux/jiffies.h>.

#define time_after(a,b)         \
(typecheck(unsigned long, a) && \
typecheck(unsigned long, b) && \
((long)(b) - (long)(a) < 0))

It appears to me that this macro may give incorrect results for certain values of 'a' and 'b'.

For the sake of an example, assume a 'long' is just 4-bits wide, to have the following possible values
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - UNSIGNED long
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7                       - SIGNED long

Now, for all of the below values for 'a' and 'b', I was expecting the macro to return true.
a   b   (long)b   (long)a   ((long)b - (long)a)
-----------------------------------------------
5   4      4          5       (4-5)    = -1 < 0 --> GOOD
6   4      4          6       (4-6)    = -2 < 0 --> GOOD
7   4      4          7       (4-7)    = -3 < 0 --> GOOD
8   4      4         -8       (4-(-8)) = 12(unsigned) = -4 (singed) < 0 --> GOOD
9   4      4         -7       (4-(-7)) = 11(unsigned) = -5 (singed) < 0 --> GOOD
10   4      4         -6       (4-(-6)) = 10(unsigned) = -6 (singed) < 0 --> GOOD
11   4      4         -5       (4-(-5)) =  9(unsigned) = -7 (singed) < 0 --> GOOD
12   4      4         -4       (4-(-4)) =  8(unsigned) = -8 (singed) < 0 --> GOOD

13   4      4         -3       (4-(-3)) =  7(unsigned) =  7 (singed) > 0 --> BAD
14   4      4         -2       (4-(-2)) =  6(unsigned) =  6 (singed) > 0 --> BAD
15   4      4         -1       (4-(-1)) =  5(unsigned) =  5 (singed) > 0 --> BAD

But, as you can see, the last three cases would return false. What am I missing here? Are there any implicit assumptions to use the macro?

Thanks
V
```
Author: