atomic64_test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Testsuite for atomic64_t functions
  3. *
  4. * Copyright © 2010 Luca Barbieri
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <asm/atomic.h>
  14. #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
  15. static __init int test_atomic64(void)
  16. {
  17. long long v0 = 0xaaa31337c001d00dLL;
  18. long long v1 = 0xdeadbeefdeafcafeLL;
  19. long long v2 = 0xfaceabadf00df001LL;
  20. long long onestwos = 0x1111111122222222LL;
  21. long long one = 1LL;
  22. atomic64_t v = ATOMIC64_INIT(v0);
  23. long long r = v0;
  24. BUG_ON(v.counter != r);
  25. atomic64_set(&v, v1);
  26. r = v1;
  27. BUG_ON(v.counter != r);
  28. BUG_ON(atomic64_read(&v) != r);
  29. INIT(v0);
  30. atomic64_add(onestwos, &v);
  31. r += onestwos;
  32. BUG_ON(v.counter != r);
  33. INIT(v0);
  34. atomic64_add(-one, &v);
  35. r += -one;
  36. BUG_ON(v.counter != r);
  37. INIT(v0);
  38. r += onestwos;
  39. BUG_ON(atomic64_add_return(onestwos, &v) != r);
  40. BUG_ON(v.counter != r);
  41. INIT(v0);
  42. r += -one;
  43. BUG_ON(atomic64_add_return(-one, &v) != r);
  44. BUG_ON(v.counter != r);
  45. INIT(v0);
  46. atomic64_sub(onestwos, &v);
  47. r -= onestwos;
  48. BUG_ON(v.counter != r);
  49. INIT(v0);
  50. atomic64_sub(-one, &v);
  51. r -= -one;
  52. BUG_ON(v.counter != r);
  53. INIT(v0);
  54. r -= onestwos;
  55. BUG_ON(atomic64_sub_return(onestwos, &v) != r);
  56. BUG_ON(v.counter != r);
  57. INIT(v0);
  58. r -= -one;
  59. BUG_ON(atomic64_sub_return(-one, &v) != r);
  60. BUG_ON(v.counter != r);
  61. INIT(v0);
  62. atomic64_inc(&v);
  63. r += one;
  64. BUG_ON(v.counter != r);
  65. INIT(v0);
  66. r += one;
  67. BUG_ON(atomic64_inc_return(&v) != r);
  68. BUG_ON(v.counter != r);
  69. INIT(v0);
  70. atomic64_dec(&v);
  71. r -= one;
  72. BUG_ON(v.counter != r);
  73. INIT(v0);
  74. r -= one;
  75. BUG_ON(atomic64_dec_return(&v) != r);
  76. BUG_ON(v.counter != r);
  77. INIT(v0);
  78. BUG_ON(atomic64_xchg(&v, v1) != v0);
  79. r = v1;
  80. BUG_ON(v.counter != r);
  81. INIT(v0);
  82. BUG_ON(atomic64_cmpxchg(&v, v0, v1) != v0);
  83. r = v1;
  84. BUG_ON(v.counter != r);
  85. INIT(v0);
  86. BUG_ON(atomic64_cmpxchg(&v, v2, v1) != v0);
  87. BUG_ON(v.counter != r);
  88. INIT(v0);
  89. BUG_ON(atomic64_add_unless(&v, one, v0));
  90. BUG_ON(v.counter != r);
  91. INIT(v0);
  92. BUG_ON(!atomic64_add_unless(&v, one, v1));
  93. r += one;
  94. BUG_ON(v.counter != r);
  95. #if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  96. defined(CONFIG_S390) || defined(_ASM_GENERIC_ATOMIC64_H) || defined(CONFIG_ARM)
  97. INIT(onestwos);
  98. BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
  99. r -= one;
  100. BUG_ON(v.counter != r);
  101. INIT(0);
  102. BUG_ON(atomic64_dec_if_positive(&v) != -one);
  103. BUG_ON(v.counter != r);
  104. INIT(-one);
  105. BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
  106. BUG_ON(v.counter != r);
  107. #else
  108. #warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above
  109. #endif
  110. INIT(onestwos);
  111. BUG_ON(!atomic64_inc_not_zero(&v));
  112. r += one;
  113. BUG_ON(v.counter != r);
  114. INIT(0);
  115. BUG_ON(atomic64_inc_not_zero(&v));
  116. BUG_ON(v.counter != r);
  117. INIT(-one);
  118. BUG_ON(!atomic64_inc_not_zero(&v));
  119. r += one;
  120. BUG_ON(v.counter != r);
  121. #ifdef CONFIG_X86
  122. printk(KERN_INFO "atomic64 test passed for %s platform %s CX8 and %s SSE\n",
  123. #ifdef CONFIG_X86_64
  124. "x86-64",
  125. #elif defined(CONFIG_X86_CMPXCHG64)
  126. "i586+",
  127. #else
  128. "i386+",
  129. #endif
  130. boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
  131. boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
  132. #else
  133. printk(KERN_INFO "atomic64 test passed\n");
  134. #endif
  135. return 0;
  136. }
  137. core_initcall(test_atomic64);