Debian Codegraph @

gcc/testsuite/gcc.target/aarch64/pr123017_1.c

raw
1 /* { dg-do compile } */
2 /* { dg-options "-O2" } */
3 /* { dg-final { scan-assembler-times "csel\t" 2 } } */
4
5 #include <stdint.h>
6 #include <stddef.h>
7
8 // Branchy: compiler can emit a short compare+branch.
9 size_t branchy(size_t cur, size_t dist, size_t fallback, const uint8_t *src, uint8_t *dst, size_t len) {
10 if (dist < cur && dist < fallback) {
11 const uint8_t *p = src - dist;
12 for (size_t i = 0; i < len; ++i)
13 dst[i] = p[i];
14 return dist;
15 } else {
16 const uint8_t *p = src - fallback;
17 for (size_t i = 0; i < len; ++i)
18 dst[i] = p[i];
19 return fallback;
20 }
21 }
22
23 // CSEL-heavy: chain ternaries so both paths stay live; compilers emit cmps + csel.
24 size_t selecty(size_t cur, size_t dist, size_t fallback, const uint8_t *src, uint8_t *dst, size_t len) {
25 // Compute both candidates unconditionally, then pick with ternaries.
26 size_t candA_off = dist;
27 size_t candB_off = fallback;
28 const uint8_t *candA = src - candA_off;
29 const uint8_t *candB = src - candB_off;
30
31 size_t useA = (dist < cur && dist < fallback);
32 const uint8_t *p = useA ? candA : candB;
33 size_t chosen = useA ? candA_off : candB_off;
34
35 for (size_t i = 0; i < len; ++i)
36 dst[i] = p[i];
37 return chosen;
38 }