Hi,
在 2025/05/06 10:34, Coly Li 写道:
For the above code, I don’t see how only unlikely(previous) help on branch prediction and prefetch,
IMHO the following form may help to achieve expected unlikely() result,
if (unlikely(previous && !stripe_ahead_of_reshape(mddev, conf, sh))) {
Thanks.
What you mean you don't see *only unlikely*, for example:
int test0(int a, int b)
{
if (a && b)
return 1;
return 0;
}
int test1(int a, int b)
{
if (unlikely(a) && b)
return 1;
return 0;
}
You can see unlikely will generate setne/movzbl/test assemble code:
test0:
0x0000000000401130 <+10>: cmpl $0x0,-0x4(%rbp)
0x0000000000401134 <+14>: je 0x401143 <test0+29>
0x0000000000401136 <+16>: cmpl $0x0,-0x8(%rbp)
0x000000000040113a <+20>: je 0x401143 <test0+29>
test1:
0x0000000000401154 <+10>: cmpl $0x0,-0x4(%rbp)
0x0000000000401158 <+14>: setne %al
0x000000000040115b <+17>: movzbl %al,%eax
0x000000000040115e <+20>: test %rax,%rax
0x0000000000401161 <+23>: je 0x401170 <test1+38>
0x0000000000401163 <+25>: cmpl $0x0,-0x8(%rbp)
BTW, what you suggested is the same as:
if (unlikely(previous) && unlikely(!stripe_ahead_of_reshape(mddev, conf,
sh)))
Thanks,
Kuai