xadd.c revision e739ac0589b4fb43561f801c4faba8c1b89f8680
1
2#include "config.h"
3#include <stdio.h>
4#include <assert.h>
5
6/* Simple test program, no race.
7   Tests the 'xadd' exchange-and-add instruction with {r,r} operands, which is rarely generated by compilers. */
8
9#undef PLAT_x86_linux
10#undef PLAT_amd64_linux
11#undef PLAT_ppc32_linux
12#undef PLAT_ppc64_linux
13#undef PLAT_ppc32_aix5
14#undef PLAT_ppc64_aix5
15
16#if !defined(_AIX) && defined(__i386__)
17#  define PLAT_x86_linux 1
18#elif !defined(_AIX) && defined(__x86_64__)
19#  define PLAT_amd64_linux 1
20#endif
21
22
23#if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux)
24#  define XADD_R_R(_addr,_lval) \
25	__asm__ __volatile__( \
26	"xadd %1, %0" \
27	: /*out*/ "=r"(_lval),"=r"(_addr) \
28	: /*in*/  "0"(_lval),"1"(_addr) \
29	: "flags" \
30	)
31#else
32#  error "Unsupported architecture"
33#endif
34
35int main ( void )
36{
37   long d = 20, s = 2;
38   long xadd_r_r_res;
39#define XADD_R_R_RES 42
40
41   XADD_R_R(s, d);
42   xadd_r_r_res = s + d;
43   assert(xadd_r_r_res == XADD_R_R_RES);
44
45   if (xadd_r_r_res == XADD_R_R_RES)
46      printf("success\n");
47   else
48      printf("failure\n");
49
50   return xadd_r_r_res;
51}
52