contributed by <yaohwang99
>
In Linux kernel,GENMASK
gernerates bitmask for the given range.
For the above 2 examples to be true, we can deduct that LEFT
is 63 - h
and RIGHT
is l
.
LEFT
must be 63 - h
.RIGHT
is l
.However, I believe that the code is not correct because:
0xFFFFFFFFFFFFFFFF & 0x00FFFFFFFFFFFF00 == 0x00FFFFFFFFFFFF00
which is not the correct result.I believe the following code is correct.
The function to_fd
use the parameter v
, align down to 4, and use it as the address of *foo
.
Align down to 4 means that we clear the last 2 bits of v
, therefore, EXP1 is v & ~3
.
0xCC = 110011002
0xAA = 101010102
line 4 swaps the most significant 4 bits with the less significant 4 bits
line 5 swaps every 2 bits
line 6 swaps every bit.
EXP2: x & 0x33) << 2
EXP3: x & 0x55) << 2
From C99 Standard
6.5.17 Comma operator
Syntax
- expression:
+ assignment-expression
+ expression , assignment-expression
Semantics
The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.
For example, the functionf(a, (t=3, t+2), c)
has three arguments, the second of which has the value 5.
unsigned _foreach_i = (((i) = ((int[]){__VA_ARGS__})[0]), 0)
assigns ((int[]){__VA_ARGS__})[0])
to i
, then assign the second 0
to _foreach_i
Just like a common for(int i = 0; i < n; ++i)
, we can deduct that EXP4 and EXP5 is ++_foreach_1
.
Line 3 to 7 counts the number of points in a vertical line and horizontal line for a given point. For example, if hori_cnts[1]
is 5, that means points[1]
is in a horizontal line consist of other 5 points and itself.
Line 9 and 10 counts the number of duplicated points for a given point. For example, if dup_cnts[2]
is 2
, that means there are 2 other identical points.
Line 12 to 28 uses different slope as hash and put the points into the hash. Therefore, EXP9 is list_add(pn, &heads[hash])
In line 22, we first check if we can insert the pair.
To deduct EXP8, consider the following example.
The traverse order is <points[1]
, points[2]
> <points[1]
, points[3]
> <points[1]
, points[4]
> ,<points[2]
, points[3]
> <points[2]
, points[4]
> <points[3], points[4]
>. To count the correct number of points in the line, we need each pair consist of the point with the smallest index(because of the traverse order). Therefore, we check if we can insert by EXP8: p1 == pn->p1
.
If v > 0
, then ret
must be greater or equal to 1.
Check if any of the 32 more significant bits are set.
If so, then ret
must be greater then 32.
Set v
as the remaining 32 bits.
Check if any of the 16 more significant bits are set.
If so, then ret
must be greater then 16.
Set v
as the remaining 16 bits.
Repeat the same process for 8,4,2 more significant bits.
Therefore, EXP10 is (v > 0x3) << 1
v
then becomes 012, 102,or 112
We will need one more bit if v > 1
, therefore, EXP11 is ret += v > 1
Use the technique of indirect pointer.
Traverse to left or right child according to current value.
EXP12: p = &(*p)->left
EXP13: p = &(*p)->right
For a binary tree, after removing the target, move the smallest child element in the right subtree to target, or move the largest element in the left subtree to target.
In the following code, we move the smallest child element in the right subtree to target.
Therefore, EXP14 is r = &(*r)->left
Using the fact that
x round up to 16
Therefore, MMM is 1
, NNN is MAX_ALIGNMENT-1
If
(__x)+(__d)>>1
-(__x)+(__d)>>1
Therefore, RRR is (__x)+(__d)>>1
, SSS is -(__x)+(__d)>>1
.
fls()
returns the position of the most significant bit.
The following code is used fore calculating
To understand how the code works, we need to know how to calculate the square root of a binary number.
source
y
corresponds the leftmost column.m = 1UL << (fls(x) & ~1UL);
the return value of fls() is rounded down to even number.For the above example, , ,
y
of the next iteration is equal to (y+m)>>1
if x > y + m
.
Therefore,XXX is y >>= 1
, YYY is x -= b
, ZZZ is m >>= 2
.