generic-2.c 3.0 KB

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