[libc-commits] [libc] [libc][stdlib] Optimize bsearch() by reordering comparison branches (PR #230823)

Kuan-Wei Chiu via libc-commits libc-commits at lists.llvm.org
Sat Oct 10 11:20:31 PDT 2026


visitorckw wrote:

FWIW, I left the arm64 assembly diff out of the commit message since it's not as short as x86 and riscv.

```diff
        mov     x24, xzr
-       cbz     x0, .LBB0_8
-       mov     x22, x1
-       cbz     x1, .LBB0_8
-       mov     x23, x2
-       cbz     x2, .LBB0_8
+       cbz     x0, .LBB0_10
+       mov     x23, x1
+       cbz     x1, .LBB0_10
+       mov     x22, x2
+       cbz     x2, .LBB0_10
        mov     x20, x3
-       cbz     x3, .LBB0_8
+       cbz     x3, .LBB0_10
        mov     x19, x4
        mov     x21, x0
+       b       .LBB0_6
 .LBB0_5:
-       lsr     x25, x23, #1
+       mov     x22, x25
+       cbz     x22, .LBB0_9
+.LBB0_6:
+       lsr     x25, x22, #1
        mov     x0, x21
-       madd    x24, x25, x20, x22
+       madd    x24, x25, x20, x23
        mov     x1, x24
        blr     x19
-       cbz     w0, .LBB0_8
+       tbnz    w0, #31, .LBB0_5
+       cbz     w0, .LBB0_10
        mvn     x8, x25
-       cmp     w0, #0
-       add     x9, x24, x20
-       add     x8, x23, x8
-       csel    x22, x22, x9, lt
-       csel    x23, x25, x8, lt
-       cbnz    x23, .LBB0_5
+       add     x23, x24, x20
+       add     x22, x22, x8
+       cbnz    x22, .LBB0_6
+.LBB0_9:
        mov     x24, xzr
-.LBB0_8:
+.LBB0_10:
        mov     x0, x24
```

https://github.com/llvm/llvm-project/pull/230823


More information about the libc-commits mailing list