atomic64_test.c 3.5 KB

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