+#if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_ARM64)
+static rte_int128_t count128;
+
+/*
+ * rte_atomic128_cmp_exchange() should update a 128 bits counter's first 64
+ * bits by 2 and the second 64 bits by 1 in this test. It should return true
+ * if the compare exchange operation is successful.
+ * This test repeats 128 bits compare and swap operations N rounds. In each
+ * iteration it runs compare and swap operation with different memory models.
+ */
+static int
+test_atomic128_cmp_exchange(__attribute__((unused)) void *arg)
+{
+ rte_int128_t expected;
+ int success;
+ unsigned int i;
+
+ while (rte_atomic32_read(&synchro) == 0)
+ ;
+
+ expected = count128;
+
+ for (i = 0; i < N; i++) {
+ do {
+ rte_int128_t desired;
+
+ desired.val[0] = expected.val[0] + 2;
+ desired.val[1] = expected.val[1] + 1;
+
+ success = rte_atomic128_cmp_exchange(&count128,
+ &expected, &desired, 1,
+ __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
+ } while (success == 0);
+
+ do {
+ rte_int128_t desired;
+
+ desired.val[0] = expected.val[0] + 2;
+ desired.val[1] = expected.val[1] + 1;
+
+ success = rte_atomic128_cmp_exchange(&count128,
+ &expected, &desired, 1,
+ __ATOMIC_RELEASE, __ATOMIC_RELAXED);
+ } while (success == 0);
+
+ do {
+ rte_int128_t desired;
+
+ desired.val[0] = expected.val[0] + 2;
+ desired.val[1] = expected.val[1] + 1;
+
+ success = rte_atomic128_cmp_exchange(&count128,
+ &expected, &desired, 1,
+ __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
+ } while (success == 0);
+
+ do {
+ rte_int128_t desired;
+
+ desired.val[0] = expected.val[0] + 2;
+ desired.val[1] = expected.val[1] + 1;
+
+ success = rte_atomic128_cmp_exchange(&count128,
+ &expected, &desired, 1,
+ __ATOMIC_RELAXED, __ATOMIC_RELAXED);
+ } while (success == 0);
+ }
+
+ return 0;
+}
+#endif
+
+/*
+ * Helper definitions/variables/functions for
+ * atomic exchange tests
+ */
+typedef union {
+ uint16_t u16;
+ uint8_t u8[2];
+} test16_t;
+
+typedef union {
+ uint32_t u32;
+ uint16_t u16[2];
+ uint8_t u8[4];
+} test32_t;
+
+typedef union {
+ uint64_t u64;
+ uint32_t u32[2];
+ uint16_t u16[4];
+ uint8_t u8[8];
+} test64_t;
+
+const uint8_t CRC8_POLY = 0x91;
+uint8_t crc8_table[256];
+
+volatile uint16_t token16;
+volatile uint32_t token32;
+volatile uint64_t token64;
+
+static void
+build_crc8_table(void)
+{
+ uint8_t val;
+ int i, j;
+
+ for (i = 0; i < 256; i++) {
+ val = i;
+ for (j = 0; j < 8; j++) {
+ if (val & 1)
+ val ^= CRC8_POLY;
+ val >>= 1;
+ }
+ crc8_table[i] = val;
+ }
+}
+
+static uint8_t
+get_crc8(uint8_t *message, int length)
+{
+ uint8_t crc = 0;
+ int i;
+
+ for (i = 0; i < length; i++)
+ crc = crc8_table[crc ^ message[i]];
+ return crc;
+}
+
+/*
+ * The atomic exchange test sets up a token in memory and
+ * then spins up multiple lcores whose job is to generate
+ * new tokens, exchange that new token for the old one held
+ * in memory, and then verify that the old token is still
+ * valid (i.e. the exchange did not corrupt the token).
+ *
+ * A token is made up of random data and 8 bits of crc
+ * covering that random data. The following is an example
+ * of a 64bit token.
+ *
+ * +------------+------------+
+ * | 63 56 | 55 0 |
+ * +------------+------------+
+ * | CRC8 | Data |
+ * +------------+------------+
+ */
+static int
+test_atomic_exchange(__attribute__((unused)) void *arg)
+{
+ int i;
+ test16_t nt16, ot16; /* new token, old token */
+ test32_t nt32, ot32;
+ test64_t nt64, ot64;
+
+ /* Wait until all of the other threads have been dispatched */
+ while (rte_atomic32_read(&synchro) == 0)
+ ;
+
+ /*
+ * Let the battle begin! Every thread attempts to steal the current
+ * token with an atomic exchange operation and install its own newly
+ * generated token. If the old token is valid (i.e. it has the
+ * appropriate crc32 hash for the data) then the test iteration has
+ * passed. If the token is invalid, increment the counter.
+ */
+ for (i = 0; i < N; i++) {
+
+ /* Test 64bit Atomic Exchange */
+ nt64.u64 = rte_rand();
+ nt64.u8[7] = get_crc8(&nt64.u8[0], sizeof(nt64) - 1);
+ ot64.u64 = rte_atomic64_exchange(&token64, nt64.u64);
+ if (ot64.u8[7] != get_crc8(&ot64.u8[0], sizeof(ot64) - 1))
+ rte_atomic64_inc(&count);
+
+ /* Test 32bit Atomic Exchange */
+ nt32.u32 = (uint32_t)rte_rand();
+ nt32.u8[3] = get_crc8(&nt32.u8[0], sizeof(nt32) - 1);
+ ot32.u32 = rte_atomic32_exchange(&token32, nt32.u32);
+ if (ot32.u8[3] != get_crc8(&ot32.u8[0], sizeof(ot32) - 1))
+ rte_atomic64_inc(&count);
+
+ /* Test 16bit Atomic Exchange */
+ nt16.u16 = (uint16_t)rte_rand();
+ nt16.u8[1] = get_crc8(&nt16.u8[0], sizeof(nt16) - 1);
+ ot16.u16 = rte_atomic16_exchange(&token16, nt16.u16);
+ if (ot16.u8[1] != get_crc8(&ot16.u8[0], sizeof(ot16) - 1))
+ rte_atomic64_inc(&count);
+ }
+
+ return 0;
+}
+static int