第四篇 ARM C 高效编程 - 多条件判断

/**
 * 结论:
 *     1. 编译器对多个条件处理为依次计算每个条件,条件不符合就执行跳转
 *     2. 尽量将多个条件合为一个条件,减少比较指令以及跳转指令
 * compile with arm-linux-gnueabihf-gcc v8.3
 * flags: -O3 -mcpu=cortex-a7 -mtune=cortex-a7 -mfloat-abi=hard -mfpu=neon
 * 000103d8 :
 * 103d8:       e24dd008        sub     sp, sp, #8
 * 103dc:       e28d3008        add     r3, sp, #8
 * 103e0:       e9030003        stmdb   r3, {r0, r1}
 * 103e4:       e5921000        ldr     r1, [r2]
 * 103e8:       e59d3000        ldr     r3, [sp]
 * 103ec:       e1510003        cmp     r1, r3
 * 103f0:       83a00000        movhi   r0, #0
 * 103f4:       8a000003        bhi     10408 
 * 103f8:       e5920004        ldr     r0, [r2, #4]
 * 103fc:       e1500003        cmp     r0, r3
 * 10400:       93a00000        movls   r0, #0
 * 10404:       83a00001        movhi   r0, #1
 * 10408:       e28dd008        add     sp, sp, #8
 * 1040c:       e12fff1e        bx      lr
 * 00010410 :
 * 10410:       e24dd008        sub     sp, sp, #8
 * 10414:       e28d3008        add     r3, sp, #8
 * 10418:       e9030003        stmdb   r3, {r0, r1}
 * 1041c:       e5921000        ldr     r1, [r2]
 * 10420:       e59d3000        ldr     r3, [sp]
 * 10424:       e5920004        ldr     r0, [r2, #4]
 * 10428:       e0433001        sub     r3, r3, r1
 * 1042c:       e1530000        cmp     r3, r0
 * 10430:       23a00000        movcs   r0, #0
 * 10434:       33a00001        movcc   r0, #1
 * 10438:       e28dd008        add     sp, sp, #8
 * 1043c:       e12fff1e        bx      lr
 */

typedef struct point {
	uint32_t x;
	uint32_t y;
} point_t;

typedef struct rect {
	uint32_t x;
	uint32_t x_max;
} rect_t;

uint32_t x_in_rect(point_t p, rect_t *r)
{
	return (p.x >= r->x && p.x < r->x_max);
}

uint32_t x_in_rect1(point_t p, rect_t *r)
{
	return (uint32_t)(p.x - r->x) <  r->x_max;
}

你可能感兴趣的:(ARM-高效C编程,arm,c语言)