atomic-op-1.c 11 KB

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