4.49

    it’s a brilliant idea, 3 xor is a normal swap

    if x = 9, y = 10

    1. x = x ^ y x:9^10; y:10
    2. y = x ^ y x:9^10; y:9
    3. x = x ^ y x:10; y:9

    if change y after step 1

    1. tmp = x x:9; y:10; tmp:9 # store x
    2. x = x ^ y x:9^10; y:10
    3. y = tmp x:9^10; y:9 # set y to origin x
    4. y = x ^ y x:9^10; y:10
    5. x = x ^ y x:9; y:10

    no swap happens. that’s core in code below

    1. !INCLUDE "./code/bubble-sort/bubble-sort-pointer-1-cmove.ys"

    test and output, watch memory changes:

    1. ../sim/misc/yas bubble-sort-pointer-1-cmove.ys
    2. ../sim/misc/yis bubble-sort-pointer-1-cmove.yo
    3. Stopped in 141 steps at PC = 0x13. Status 'HLT', CC Z=1 S=0 O=0
    4. Changes to registers:
    5. %rax: 0x0000000000000000 0x0000000000000020
    6. %rsp: 0x0000000000000000 0x0000000000000200
    7. %rsi: 0x0000000000000000 0x0000000000000018
    8. %rdi: 0x0000000000000000 0x0000000000000018
    9. %r9: 0x0000000000000000 0x0000000000000002
    10. %r10: 0x0000000000000000 0x0000000000000001
    11. %r11: 0x0000000000000000 0x0000000000000002
    12. Changes to memory:
    13. 0x0018: 0x0000000000000004 0x0000000000000001
    14. 0x0020: 0x0000000000000003 0x0000000000000002
    15. 0x0028: 0x0000000000000002 0x0000000000000003
    16. 0x0030: 0x0000000000000001 0x0000000000000004