-farray-widen-compare
Array width comparison optimization: A wide data type can be used to dereference the original array pointer (the array element pointed to is of the narrow type). In this way, multiple elements can be compared at a time, improving program performance.
For the following code scenario:
1 2 3 4 5 6 7 8 9 10 | #define my_min(x, y) ((x) < (y) ? (x) : (y)) uint32_t func (uint32_t n0, uint32_t n1, const uint32_t limit, const uint8_t * a, const uint8_t * b) { uint32_t n = my_min(n0, n1); while (++n != limit) if (a[n] != b[n]) break; return n; } |
After the option is added, the code can be optimized as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #define my_min(x, y) ((x) < (y) ? (x) : (y)) uint32_t func (uint32_t n0, uint32_t n1, const uint32_t limit, const uint8_t * a, const uint8_t * b) { uint32_t n = my_min(n0, n1); for (++n; n + sizeof(uint64_t) <= limit; n += sizeof(uint64_t)) { uint64_t k1 = *((uint64_t*)(a+n)); uint64_t k2 = *((uint64_t*)(b+n)); if(k1 != k2) { int lz = __builtin_ctzll(k1 ^ k2); n += lz/8; return n; } } for (;n != limit; ++n) { if (a[n] != b[n]) break; } return n; } |
Usage
Add the following content to the option:
-O3 -farray-widen-compare
Parent topic: Static Compilation Optimization