4.49
it’s a brilliant idea, 3 xor is a normal swap
if x = 9, y = 10
x = x ^ y x:9^10; y:10
y = x ^ y x:9^10; y:9
x = x ^ y x:10; y:9
if change y after step 1
tmp = x x:9; y:10; tmp:9 # store x
x = x ^ y x:9^10; y:10
y = tmp x:9^10; y:9 # set y to origin x
y = x ^ y x:9^10; y:10
x = x ^ y x:9; y:10
no swap happens. that’s core in code below
!INCLUDE "./code/bubble-sort/bubble-sort-pointer-1-cmove.ys"
test and output, watch memory changes:
../sim/misc/yas bubble-sort-pointer-1-cmove.ys
../sim/misc/yis bubble-sort-pointer-1-cmove.yo
Stopped in 141 steps at PC = 0x13. Status 'HLT', CC Z=1 S=0 O=0
Changes to registers:
%rax: 0x0000000000000000 0x0000000000000020
%rsp: 0x0000000000000000 0x0000000000000200
%rsi: 0x0000000000000000 0x0000000000000018
%rdi: 0x0000000000000000 0x0000000000000018
%r9: 0x0000000000000000 0x0000000000000002
%r10: 0x0000000000000000 0x0000000000000001
%r11: 0x0000000000000000 0x0000000000000002
Changes to memory:
0x0018: 0x0000000000000004 0x0000000000000001
0x0020: 0x0000000000000003 0x0000000000000002
0x0028: 0x0000000000000002 0x0000000000000003
0x0030: 0x0000000000000001 0x0000000000000004
当前内容版权归 DreamAndDead 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 DreamAndDead .