3 ms·
It'd look something like cmp r0,#0 bgt .1f mov r0,#0 b .2f 1: mov r0,#1 2: The big deal is the conditional branch (the bgt). I
by monocasa 8y ago
It'd look something like
cmp r0,#0
bgt .1f
mov r0,#0
b .2f
1:
mov r0,#1
2:
The big deal is the conditional branch (the bgt). If the processor gets it wrong it's a pipeline flush. And best case you still have extra instructions for the branches. The conditional mov example is a fixed cost of a single "wasted" cycle, which matches the best case of the branching example (branch correctly predicted to mov r0,#1 and fall through). The worst case for the branching version is probably somewhere ~15 cycles depending on the uArch, but is still 1 cycle for the conditional move.
All of that being said, the branching version tends to be nicer for OoO cores since there aren't data dependencies on the flag registers any more, hence why you see RISC ISAs designed for OoO cores removing conditional execution for most instructions (AArch64 and RISC-V standout here).
- fanf2 8y agoIn the ARM2 era (probably the same for ARM1?) a basic ALU instruction such as MOV took 1 cycle, and a branch took 4 (if taken) or 1 (if not). (There were extra DRAM page cycles every 4 words too) So for a simple if/else, it was usually both less code and faster to use a straight line of conditional instructions. In more complicated cases, if the programmer was feeling clever, it was possible to update the status flags to get three-way (or more!) conditionals in straight-line branchless code. Fun!