(root)/
gcc-13.2.0/
libatomic/
testsuite/
libatomic.c/
generic-2.c
       1  /* { dg-do run } */
       2  
       3  #include <stdlib.h>
       4  #include <string.h>
       5  #include <stdbool.h>
       6  
       7  /* Avoid using the builtins, calling directly to the library functions
       8     of the same name, so that we get direct access to the size_t and
       9     don't have to create myriad types of different sizes.  */
      10  
      11  #define C2_(X,Y)	X ## Y
      12  #define C2(X,Y)		C2_(X,Y)
      13  
      14  #define S2(X)		#X
      15  #define S(X)		S2(X)
      16  
      17  #define ASMNAME(X)	__asm__(S(C2(__USER_LABEL_PREFIX__,X)))
      18  #define MAN(X)		ASMNAME(C2(__atomic_,X))
      19  
      20  void libat_load (size_t, void *, void *, int) MAN(load);
      21  void libat_store (size_t, void *, void *, int) MAN(store);
      22  void libat_exchange (size_t, void *, void *, void *, int) MAN(exchange);
      23  bool libat_compare_exchange (size_t, void *, void *, void *, int, int)
      24  	MAN(compare_exchange);
      25  bool libat_is_lock_free (size_t, void *) MAN(is_lock_free);
      26  
      27  
      28  #define ALIGN  16
      29  #define MAX    4*ALIGN
      30  
      31  static char a[MAX];
      32  static char b[MAX];
      33  static char c[MAX];
      34  static char pa[MAX];
      35  static char pb[MAX];
      36  
      37  static void test_load(void)
      38  {
      39    int i, j;
      40    for (i = ALIGN; i < 2*ALIGN; ++i)
      41      for (j = 1; j <= 2*ALIGN; ++j)
      42        {
      43          memcpy(b, pa, MAX);
      44          memcpy(b + i, pb, j);
      45          libat_load (j, b + i, a, 0);
      46          if (memcmp (a, pb, j) != 0) abort ();
      47        }
      48  }
      49  
      50  static void test_store(void)
      51  {
      52    int i, j;
      53    for (i = ALIGN; i < 2*ALIGN; ++i)
      54      for (j = 1; j <= 2*ALIGN; ++j)
      55        {
      56          memcpy(a, pa, MAX);
      57          memcpy(b, pa, MAX);
      58          memcpy(b + i, pb, j);
      59          libat_store (j, a + i, pb, 0);
      60          if (memcmp (a, b, MAX) != 0) abort ();
      61        }
      62  }
      63  
      64  static void test_exch(void)
      65  {
      66    int i, j;
      67    for (i = ALIGN; i < 2 * ALIGN; ++i)
      68      for (j = 1; j <= 2*ALIGN; ++j)
      69        {
      70  	memcpy(a, pa, MAX);
      71          memcpy(b, pa, MAX);
      72          memcpy(b + i, pb, j);
      73          libat_exchange (j, a + i, pb, c, 0);
      74          if (memcmp (a, b, MAX) != 0) abort ();
      75          if (memcmp (c, pa + i, j) != 0) abort ();
      76  
      77          memcpy(a, pa, MAX);
      78          memcpy(c, pb, MAX);
      79          libat_exchange (j, a + i, c + i, c + i, 0);
      80          memcpy(b, pa, MAX);
      81          memcpy(b + i, pb + i, j);
      82          if (memcmp (b, a, MAX) != 0) abort ();
      83          memcpy(b, pb, MAX);
      84          memcpy(b + i, pa + i, j);
      85  	if (memcmp (b, c, MAX) != 0) abort ();
      86        }
      87  }
      88  
      89  static void test_cas(void)
      90  {
      91    int i, j;
      92    for (i = ALIGN; i < 2 * ALIGN; ++i)
      93      for (j = 1; j <= 2*ALIGN; ++j)
      94        {
      95  	memcpy(a, pa, MAX);
      96  	memcpy(b, pa, MAX);
      97  	memcpy(c, pa, MAX);
      98  	memcpy(b + i, pb, j);
      99          if (!libat_compare_exchange (j, a + i, c + i, pb, 0, 0)) abort ();
     100  	if (memcmp (c, pa, MAX) != 0) abort ();
     101          if (memcmp (a, b, MAX) != 0) abort ();
     102  
     103  	memcpy(a, pb, MAX);
     104  	memcpy(b, pa, MAX);
     105  	memcpy(c, pa, MAX);
     106  	memcpy(b + i, pb + i, j);
     107          if (libat_compare_exchange (j, a + i, c + i, pb, 0, 0)) abort ();
     108          if (memcmp (a, pb, MAX) != 0) abort ();
     109          if (memcmp (b, c, MAX) != 0) abort ();
     110        }
     111  }
     112  
     113  int main (void)
     114  {
     115    int i;
     116    for (i = 0; i < MAX; ++i)
     117      {
     118        pa[i] = i * 2;
     119        pb[i] = i * 2 + 1;
     120      }
     121  
     122    test_load ();
     123    test_store ();
     124    test_exch ();
     125    test_cas ();
     126  
     127    return 0;
     128  }