atomic-op-4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* Test __atomic routines for existence and proper execution on 8 byte
  2. values with each valid memory model. */
  3. /* { dg-do run } */
  4. /* { dg-options "" } */
  5. /* Test the execution of the __atomic_*OP builtin routines for long long. */
  6. extern void abort(void);
  7. long long v, count, res;
  8. const long long init = ~0;
  9. /* The fetch_op routines return the original value before the operation. */
  10. void
  11. test_fetch_add ()
  12. {
  13. v = 0;
  14. count = 1;
  15. if (__atomic_fetch_add (&v, count, __ATOMIC_RELAXED) != 0)
  16. abort ();
  17. if (__atomic_fetch_add (&v, 1, __ATOMIC_CONSUME) != 1)
  18. abort ();
  19. if (__atomic_fetch_add (&v, count, __ATOMIC_ACQUIRE) != 2)
  20. abort ();
  21. if (__atomic_fetch_add (&v, 1, __ATOMIC_RELEASE) != 3)
  22. abort ();
  23. if (__atomic_fetch_add (&v, count, __ATOMIC_ACQ_REL) != 4)
  24. abort ();
  25. if (__atomic_fetch_add (&v, 1, __ATOMIC_SEQ_CST) != 5)
  26. abort ();
  27. }
  28. void
  29. test_fetch_sub()
  30. {
  31. v = res = 20;
  32. count = 0;
  33. if (__atomic_fetch_sub (&v, count + 1, __ATOMIC_RELAXED) != res--)
  34. abort ();
  35. if (__atomic_fetch_sub (&v, 1, __ATOMIC_CONSUME) != res--)
  36. abort ();
  37. if (__atomic_fetch_sub (&v, count + 1, __ATOMIC_ACQUIRE) != res--)
  38. abort ();
  39. if (__atomic_fetch_sub (&v, 1, __ATOMIC_RELEASE) != res--)
  40. abort ();
  41. if (__atomic_fetch_sub (&v, count + 1, __ATOMIC_ACQ_REL) != res--)
  42. abort ();
  43. if (__atomic_fetch_sub (&v, 1, __ATOMIC_SEQ_CST) != res--)
  44. abort ();
  45. }
  46. void
  47. test_fetch_and ()
  48. {
  49. v = init;
  50. if (__atomic_fetch_and (&v, 0, __ATOMIC_RELAXED) != init)
  51. abort ();
  52. if (__atomic_fetch_and (&v, init, __ATOMIC_CONSUME) != 0)
  53. abort ();
  54. if (__atomic_fetch_and (&v, 0, __ATOMIC_ACQUIRE) != 0)
  55. abort ();
  56. v = ~v;
  57. if (__atomic_fetch_and (&v, init, __ATOMIC_RELEASE) != init)
  58. abort ();
  59. if (__atomic_fetch_and (&v, 0, __ATOMIC_ACQ_REL) != init)
  60. abort ();
  61. if (__atomic_fetch_and (&v, 0, __ATOMIC_SEQ_CST) != 0)
  62. abort ();
  63. }
  64. void
  65. test_fetch_nand ()
  66. {
  67. v = init;
  68. if (__atomic_fetch_nand (&v, 0, __ATOMIC_RELAXED) != init)
  69. abort ();
  70. if (__atomic_fetch_nand (&v, init, __ATOMIC_CONSUME) != init)
  71. abort ();
  72. if (__atomic_fetch_nand (&v, 0, __ATOMIC_ACQUIRE) != 0 )
  73. abort ();
  74. if (__atomic_fetch_nand (&v, init, __ATOMIC_RELEASE) != init)
  75. abort ();
  76. if (__atomic_fetch_nand (&v, init, __ATOMIC_ACQ_REL) != 0)
  77. abort ();
  78. if (__atomic_fetch_nand (&v, 0, __ATOMIC_SEQ_CST) != init)
  79. abort ();
  80. }
  81. void
  82. test_fetch_xor ()
  83. {
  84. v = init;
  85. count = 0;
  86. if (__atomic_fetch_xor (&v, count, __ATOMIC_RELAXED) != init)
  87. abort ();
  88. if (__atomic_fetch_xor (&v, ~count, __ATOMIC_CONSUME) != init)
  89. abort ();
  90. if (__atomic_fetch_xor (&v, 0, __ATOMIC_ACQUIRE) != 0)
  91. abort ();
  92. if (__atomic_fetch_xor (&v, ~count, __ATOMIC_RELEASE) != 0)
  93. abort ();
  94. if (__atomic_fetch_xor (&v, 0, __ATOMIC_ACQ_REL) != init)
  95. abort ();
  96. if (__atomic_fetch_xor (&v, ~count, __ATOMIC_SEQ_CST) != init)
  97. abort ();
  98. }
  99. void
  100. test_fetch_or ()
  101. {
  102. v = 0;
  103. count = 1;
  104. if (__atomic_fetch_or (&v, count, __ATOMIC_RELAXED) != 0)
  105. abort ();
  106. count *= 2;
  107. if (__atomic_fetch_or (&v, 2, __ATOMIC_CONSUME) != 1)
  108. abort ();
  109. count *= 2;
  110. if (__atomic_fetch_or (&v, count, __ATOMIC_ACQUIRE) != 3)
  111. abort ();
  112. count *= 2;
  113. if (__atomic_fetch_or (&v, 8, __ATOMIC_RELEASE) != 7)
  114. abort ();
  115. count *= 2;
  116. if (__atomic_fetch_or (&v, count, __ATOMIC_ACQ_REL) != 15)
  117. abort ();
  118. count *= 2;
  119. if (__atomic_fetch_or (&v, count, __ATOMIC_SEQ_CST) != 31)
  120. abort ();
  121. }
  122. /* The OP_fetch routines return the new value after the operation. */
  123. void
  124. test_add_fetch ()
  125. {
  126. v = 0;
  127. count = 1;
  128. if (__atomic_add_fetch (&v, count, __ATOMIC_RELAXED) != 1)
  129. abort ();
  130. if (__atomic_add_fetch (&v, 1, __ATOMIC_CONSUME) != 2)
  131. abort ();
  132. if (__atomic_add_fetch (&v, count, __ATOMIC_ACQUIRE) != 3)
  133. abort ();
  134. if (__atomic_add_fetch (&v, 1, __ATOMIC_RELEASE) != 4)
  135. abort ();
  136. if (__atomic_add_fetch (&v, count, __ATOMIC_ACQ_REL) != 5)
  137. abort ();
  138. if (__atomic_add_fetch (&v, count, __ATOMIC_SEQ_CST) != 6)
  139. abort ();
  140. }
  141. void
  142. test_sub_fetch ()
  143. {
  144. v = res = 20;
  145. count = 0;
  146. if (__atomic_sub_fetch (&v, count + 1, __ATOMIC_RELAXED) != --res)
  147. abort ();
  148. if (__atomic_sub_fetch (&v, 1, __ATOMIC_CONSUME) != --res)
  149. abort ();
  150. if (__atomic_sub_fetch (&v, count + 1, __ATOMIC_ACQUIRE) != --res)
  151. abort ();
  152. if (__atomic_sub_fetch (&v, 1, __ATOMIC_RELEASE) != --res)
  153. abort ();
  154. if (__atomic_sub_fetch (&v, count + 1, __ATOMIC_ACQ_REL) != --res)
  155. abort ();
  156. if (__atomic_sub_fetch (&v, count + 1, __ATOMIC_SEQ_CST) != --res)
  157. abort ();
  158. }
  159. void
  160. test_and_fetch ()
  161. {
  162. v = init;
  163. if (__atomic_and_fetch (&v, 0, __ATOMIC_RELAXED) != 0)
  164. abort ();
  165. v = init;
  166. if (__atomic_and_fetch (&v, init, __ATOMIC_CONSUME) != init)
  167. abort ();
  168. if (__atomic_and_fetch (&v, 0, __ATOMIC_ACQUIRE) != 0)
  169. abort ();
  170. v = ~v;
  171. if (__atomic_and_fetch (&v, init, __ATOMIC_RELEASE) != init)
  172. abort ();
  173. if (__atomic_and_fetch (&v, 0, __ATOMIC_ACQ_REL) != 0)
  174. abort ();
  175. v = ~v;
  176. if (__atomic_and_fetch (&v, 0, __ATOMIC_SEQ_CST) != 0)
  177. abort ();
  178. }
  179. void
  180. test_nand_fetch ()
  181. {
  182. v = init;
  183. if (__atomic_nand_fetch (&v, 0, __ATOMIC_RELAXED) != init)
  184. abort ();
  185. if (__atomic_nand_fetch (&v, init, __ATOMIC_CONSUME) != 0)
  186. abort ();
  187. if (__atomic_nand_fetch (&v, 0, __ATOMIC_ACQUIRE) != init)
  188. abort ();
  189. if (__atomic_nand_fetch (&v, init, __ATOMIC_RELEASE) != 0)
  190. abort ();
  191. if (__atomic_nand_fetch (&v, init, __ATOMIC_ACQ_REL) != init)
  192. abort ();
  193. if (__atomic_nand_fetch (&v, 0, __ATOMIC_SEQ_CST) != init)
  194. abort ();
  195. }
  196. void
  197. test_xor_fetch ()
  198. {
  199. v = init;
  200. count = 0;
  201. if (__atomic_xor_fetch (&v, count, __ATOMIC_RELAXED) != init)
  202. abort ();
  203. if (__atomic_xor_fetch (&v, ~count, __ATOMIC_CONSUME) != 0)
  204. abort ();
  205. if (__atomic_xor_fetch (&v, 0, __ATOMIC_ACQUIRE) != 0)
  206. abort ();
  207. if (__atomic_xor_fetch (&v, ~count, __ATOMIC_RELEASE) != init)
  208. abort ();
  209. if (__atomic_xor_fetch (&v, 0, __ATOMIC_ACQ_REL) != init)
  210. abort ();
  211. if (__atomic_xor_fetch (&v, ~count, __ATOMIC_SEQ_CST) != 0)
  212. abort ();
  213. }
  214. void
  215. test_or_fetch ()
  216. {
  217. v = 0;
  218. count = 1;
  219. if (__atomic_or_fetch (&v, count, __ATOMIC_RELAXED) != 1)
  220. abort ();
  221. count *= 2;
  222. if (__atomic_or_fetch (&v, 2, __ATOMIC_CONSUME) != 3)
  223. abort ();
  224. count *= 2;
  225. if (__atomic_or_fetch (&v, count, __ATOMIC_ACQUIRE) != 7)
  226. abort ();
  227. count *= 2;
  228. if (__atomic_or_fetch (&v, 8, __ATOMIC_RELEASE) != 15)
  229. abort ();
  230. count *= 2;
  231. if (__atomic_or_fetch (&v, count, __ATOMIC_ACQ_REL) != 31)
  232. abort ();
  233. count *= 2;
  234. if (__atomic_or_fetch (&v, count, __ATOMIC_SEQ_CST) != 63)
  235. abort ();
  236. }
  237. /* Test the OP routines with a result which isn't used. Use both variations
  238. within each function. */
  239. void
  240. test_add ()
  241. {
  242. v = 0;
  243. count = 1;
  244. __atomic_add_fetch (&v, count, __ATOMIC_RELAXED);
  245. if (v != 1)
  246. abort ();
  247. __atomic_fetch_add (&v, count, __ATOMIC_CONSUME);
  248. if (v != 2)
  249. abort ();
  250. __atomic_add_fetch (&v, 1 , __ATOMIC_ACQUIRE);
  251. if (v != 3)
  252. abort ();
  253. __atomic_fetch_add (&v, 1, __ATOMIC_RELEASE);
  254. if (v != 4)
  255. abort ();
  256. __atomic_add_fetch (&v, count, __ATOMIC_ACQ_REL);
  257. if (v != 5)
  258. abort ();
  259. __atomic_fetch_add (&v, count, __ATOMIC_SEQ_CST);
  260. if (v != 6)
  261. abort ();
  262. }
  263. void
  264. test_sub()
  265. {
  266. v = res = 20;
  267. count = 0;
  268. __atomic_sub_fetch (&v, count + 1, __ATOMIC_RELAXED);
  269. if (v != --res)
  270. abort ();
  271. __atomic_fetch_sub (&v, count + 1, __ATOMIC_CONSUME);
  272. if (v != --res)
  273. abort ();
  274. __atomic_sub_fetch (&v, 1, __ATOMIC_ACQUIRE);
  275. if (v != --res)
  276. abort ();
  277. __atomic_fetch_sub (&v, 1, __ATOMIC_RELEASE);
  278. if (v != --res)
  279. abort ();
  280. __atomic_sub_fetch (&v, count + 1, __ATOMIC_ACQ_REL);
  281. if (v != --res)
  282. abort ();
  283. __atomic_fetch_sub (&v, count + 1, __ATOMIC_SEQ_CST);
  284. if (v != --res)
  285. abort ();
  286. }
  287. void
  288. test_and ()
  289. {
  290. v = init;
  291. __atomic_and_fetch (&v, 0, __ATOMIC_RELAXED);
  292. if (v != 0)
  293. abort ();
  294. v = init;
  295. __atomic_fetch_and (&v, init, __ATOMIC_CONSUME);
  296. if (v != init)
  297. abort ();
  298. __atomic_and_fetch (&v, 0, __ATOMIC_ACQUIRE);
  299. if (v != 0)
  300. abort ();
  301. v = ~v;
  302. __atomic_fetch_and (&v, init, __ATOMIC_RELEASE);
  303. if (v != init)
  304. abort ();
  305. __atomic_and_fetch (&v, 0, __ATOMIC_ACQ_REL);
  306. if (v != 0)
  307. abort ();
  308. v = ~v;
  309. __atomic_fetch_and (&v, 0, __ATOMIC_SEQ_CST);
  310. if (v != 0)
  311. abort ();
  312. }
  313. void
  314. test_nand ()
  315. {
  316. v = init;
  317. __atomic_fetch_nand (&v, 0, __ATOMIC_RELAXED);
  318. if (v != init)
  319. abort ();
  320. __atomic_fetch_nand (&v, init, __ATOMIC_CONSUME);
  321. if (v != 0)
  322. abort ();
  323. __atomic_nand_fetch (&v, 0, __ATOMIC_ACQUIRE);
  324. if (v != init)
  325. abort ();
  326. __atomic_nand_fetch (&v, init, __ATOMIC_RELEASE);
  327. if (v != 0)
  328. abort ();
  329. __atomic_fetch_nand (&v, init, __ATOMIC_ACQ_REL);
  330. if (v != init)
  331. abort ();
  332. __atomic_nand_fetch (&v, 0, __ATOMIC_SEQ_CST);
  333. if (v != init)
  334. abort ();
  335. }
  336. void
  337. test_xor ()
  338. {
  339. v = init;
  340. count = 0;
  341. __atomic_xor_fetch (&v, count, __ATOMIC_RELAXED);
  342. if (v != init)
  343. abort ();
  344. __atomic_fetch_xor (&v, ~count, __ATOMIC_CONSUME);
  345. if (v != 0)
  346. abort ();
  347. __atomic_xor_fetch (&v, 0, __ATOMIC_ACQUIRE);
  348. if (v != 0)
  349. abort ();
  350. __atomic_fetch_xor (&v, ~count, __ATOMIC_RELEASE);
  351. if (v != init)
  352. abort ();
  353. __atomic_fetch_xor (&v, 0, __ATOMIC_ACQ_REL);
  354. if (v != init)
  355. abort ();
  356. __atomic_xor_fetch (&v, ~count, __ATOMIC_SEQ_CST);
  357. if (v != 0)
  358. abort ();
  359. }
  360. void
  361. test_or ()
  362. {
  363. v = 0;
  364. count = 1;
  365. __atomic_or_fetch (&v, count, __ATOMIC_RELAXED);
  366. if (v != 1)
  367. abort ();
  368. count *= 2;
  369. __atomic_fetch_or (&v, count, __ATOMIC_CONSUME);
  370. if (v != 3)
  371. abort ();
  372. count *= 2;
  373. __atomic_or_fetch (&v, 4, __ATOMIC_ACQUIRE);
  374. if (v != 7)
  375. abort ();
  376. count *= 2;
  377. __atomic_fetch_or (&v, 8, __ATOMIC_RELEASE);
  378. if (v != 15)
  379. abort ();
  380. count *= 2;
  381. __atomic_or_fetch (&v, count, __ATOMIC_ACQ_REL);
  382. if (v != 31)
  383. abort ();
  384. count *= 2;
  385. __atomic_fetch_or (&v, count, __ATOMIC_SEQ_CST);
  386. if (v != 63)
  387. abort ();
  388. }
  389. int
  390. main ()
  391. {
  392. test_fetch_add ();
  393. test_fetch_sub ();
  394. test_fetch_and ();
  395. test_fetch_nand ();
  396. test_fetch_xor ();
  397. test_fetch_or ();
  398. test_add_fetch ();
  399. test_sub_fetch ();
  400. test_and_fetch ();
  401. test_nand_fetch ();
  402. test_xor_fetch ();
  403. test_or_fetch ();
  404. test_add ();
  405. test_sub ();
  406. test_and ();
  407. test_nand ();
  408. test_xor ();
  409. test_or ();
  410. return 0;
  411. }