Rate This Document
Findability
Accuracy
Completeness
Readability

Replacing the x86 cmpxchgl Assembly Instruction

Symptom

Error: {standard input}: Assembler messages:

{standard input}:1222: Error: unknown mnemonic 'cmpxchgl '

Cause

Similar to xchgl, cmpxchgl is an x86 assembly instruction used to compare and exchange operands. There is no corresponding instruction on the Kunpeng platform. You can use the GCC's atomic operation interface __atomic_compare_exchange_n instead.

Procedure

Example code on the x86 platform:

inline bool nBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
{
    unsigned char ret;
    /* Atomic operation. If the value of m_value is equal to expectedValue, load newValue to m_value and return ret=true.
     * Otherwise, keep the value of m_value unchanged and return ret=false
     */
    asm volatile("lock\n"
                 "cmpxchgl %3,%2\n"
                 "sete %1\n"
                 : "=a" (newValue), "=qm" (ret), "+m" (m_value)
                 : "r" (newValue), "0" (expectedValue)
                 : "memory");
    return ret != 0;
}

Replace the code as follows on the Kunpeng platform:

inline bool nBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
{
    unsigned char ret;
    /* Atomic operation. If the original value of m_value is equal to expectedValue, load newValue to _value and return ret=true. Otherwise, keep the value of m_value unchanged and return ret=false. */
    return __atomic_compare_exchange_n(&m_value, &expectedValue, newValue, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
}