extra_ops_msvc_x86.hpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2017-2025 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/extra_ops_msvc_x86.hpp
  10. *
  11. * This header contains implementation of the extra atomic operations for x86.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_EXTRA_OPS_MSVC_X86_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_EXTRA_OPS_MSVC_X86_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <boost/memory_order.hpp>
  17. #include <boost/atomic/detail/config.hpp>
  18. #include <boost/atomic/detail/interlocked.hpp>
  19. #include <boost/atomic/detail/storage_traits.hpp>
  20. #include <boost/atomic/detail/extra_operations_fwd.hpp>
  21. #include <boost/atomic/detail/extra_ops_generic.hpp>
  22. #include <boost/atomic/detail/header.hpp>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. #pragma once
  25. #endif
  26. namespace boost {
  27. namespace atomics {
  28. namespace detail {
  29. #if defined(_M_IX86)
  30. template< typename Base, bool Signed >
  31. struct extra_operations< Base, 1u, Signed, true > :
  32. public extra_operations_generic< Base, 1u, Signed >
  33. {
  34. using base_type = extra_operations_generic< Base, 1u, Signed >;
  35. using storage_type = typename base_type::storage_type;
  36. static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) noexcept
  37. {
  38. base_type::fence_before(order);
  39. storage_type old_val;
  40. __asm
  41. {
  42. mov ecx, storage
  43. movzx eax, byte ptr [ecx]
  44. align 16
  45. again:
  46. mov edx, eax
  47. neg dl
  48. lock cmpxchg byte ptr [ecx], dl
  49. jne again
  50. mov old_val, al
  51. };
  52. base_type::fence_after(order);
  53. return old_val;
  54. }
  55. static BOOST_FORCEINLINE storage_type negate(storage_type volatile& storage, memory_order order) noexcept
  56. {
  57. base_type::fence_before(order);
  58. storage_type new_val;
  59. __asm
  60. {
  61. mov ecx, storage
  62. movzx eax, byte ptr [ecx]
  63. align 16
  64. again:
  65. mov edx, eax
  66. neg dl
  67. lock cmpxchg byte ptr [ecx], dl
  68. jne again
  69. mov new_val, dl
  70. };
  71. base_type::fence_after(order);
  72. return new_val;
  73. }
  74. static BOOST_FORCEINLINE bool negate_and_test(storage_type volatile& storage, memory_order order) noexcept
  75. {
  76. base_type::fence_before(order);
  77. bool result;
  78. __asm
  79. {
  80. mov ecx, storage
  81. movzx eax, byte ptr [ecx]
  82. align 16
  83. again:
  84. mov edx, eax
  85. neg dl
  86. lock cmpxchg byte ptr [ecx], dl
  87. jne again
  88. test dl, dl
  89. setnz result
  90. };
  91. base_type::fence_after(order);
  92. return result;
  93. }
  94. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) noexcept
  95. {
  96. base_type::fence_before(order);
  97. __asm
  98. {
  99. mov ecx, storage
  100. movzx eax, byte ptr [ecx]
  101. align 16
  102. again:
  103. mov edx, eax
  104. neg dl
  105. lock cmpxchg byte ptr [ecx], dl
  106. jne again
  107. };
  108. base_type::fence_after(order);
  109. }
  110. static BOOST_FORCEINLINE storage_type bitwise_and(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  111. {
  112. base_type::fence_before(order);
  113. __asm
  114. {
  115. mov edi, storage
  116. movzx ecx, v
  117. xor edx, edx
  118. movzx eax, byte ptr [edi]
  119. align 16
  120. again:
  121. mov dl, al
  122. and dl, cl
  123. lock cmpxchg byte ptr [edi], dl
  124. jne again
  125. mov v, dl
  126. };
  127. base_type::fence_after(order);
  128. return v;
  129. }
  130. static BOOST_FORCEINLINE storage_type bitwise_or(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  131. {
  132. base_type::fence_before(order);
  133. __asm
  134. {
  135. mov edi, storage
  136. movzx ecx, v
  137. xor edx, edx
  138. movzx eax, byte ptr [edi]
  139. align 16
  140. again:
  141. mov dl, al
  142. or dl, cl
  143. lock cmpxchg byte ptr [edi], dl
  144. jne again
  145. mov v, dl
  146. };
  147. base_type::fence_after(order);
  148. return v;
  149. }
  150. static BOOST_FORCEINLINE storage_type bitwise_xor(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  151. {
  152. base_type::fence_before(order);
  153. __asm
  154. {
  155. mov edi, storage
  156. movzx ecx, v
  157. xor edx, edx
  158. movzx eax, byte ptr [edi]
  159. align 16
  160. again:
  161. mov dl, al
  162. xor dl, cl
  163. lock cmpxchg byte ptr [edi], dl
  164. jne again
  165. mov v, dl
  166. };
  167. base_type::fence_after(order);
  168. return v;
  169. }
  170. static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) noexcept
  171. {
  172. base_type::fence_before(order);
  173. storage_type old_val;
  174. __asm
  175. {
  176. mov ecx, storage
  177. movzx eax, byte ptr [ecx]
  178. align 16
  179. again:
  180. mov edx, eax
  181. not dl
  182. lock cmpxchg byte ptr [ecx], dl
  183. jne again
  184. mov old_val, al
  185. };
  186. base_type::fence_after(order);
  187. return old_val;
  188. }
  189. static BOOST_FORCEINLINE storage_type bitwise_complement(storage_type volatile& storage, memory_order order) noexcept
  190. {
  191. base_type::fence_before(order);
  192. storage_type new_val;
  193. __asm
  194. {
  195. mov ecx, storage
  196. movzx eax, byte ptr [ecx]
  197. align 16
  198. again:
  199. mov edx, eax
  200. not dl
  201. lock cmpxchg byte ptr [ecx], dl
  202. jne again
  203. mov new_val, dl
  204. };
  205. base_type::fence_after(order);
  206. return new_val;
  207. }
  208. static BOOST_FORCEINLINE bool complement_and_test(storage_type volatile& storage, memory_order order) noexcept
  209. {
  210. base_type::fence_before(order);
  211. bool result;
  212. __asm
  213. {
  214. mov ecx, storage
  215. movzx eax, byte ptr [ecx]
  216. align 16
  217. again:
  218. mov edx, eax
  219. not dl
  220. lock cmpxchg byte ptr [ecx], dl
  221. jne again
  222. test dl, dl
  223. setnz result
  224. };
  225. base_type::fence_after(order);
  226. return result;
  227. }
  228. static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) noexcept
  229. {
  230. base_type::fence_before(order);
  231. __asm
  232. {
  233. mov ecx, storage
  234. movzx eax, byte ptr [ecx]
  235. align 16
  236. again:
  237. mov edx, eax
  238. not dl
  239. lock cmpxchg byte ptr [ecx], dl
  240. jne again
  241. };
  242. base_type::fence_after(order);
  243. }
  244. static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  245. {
  246. base_type::fence_before(order);
  247. __asm
  248. {
  249. mov edx, storage
  250. movzx eax, v
  251. lock add byte ptr [edx], al
  252. };
  253. base_type::fence_after(order);
  254. }
  255. static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  256. {
  257. base_type::fence_before(order);
  258. __asm
  259. {
  260. mov edx, storage
  261. movzx eax, v
  262. lock sub byte ptr [edx], al
  263. };
  264. base_type::fence_after(order);
  265. }
  266. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) noexcept
  267. {
  268. base_type::fence_before(order);
  269. __asm
  270. {
  271. mov edx, storage
  272. lock neg byte ptr [edx]
  273. };
  274. base_type::fence_after(order);
  275. }
  276. static BOOST_FORCEINLINE void opaque_and(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  277. {
  278. base_type::fence_before(order);
  279. __asm
  280. {
  281. mov edx, storage
  282. movzx eax, v
  283. lock and byte ptr [edx], al
  284. };
  285. base_type::fence_after(order);
  286. }
  287. static BOOST_FORCEINLINE void opaque_or(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  288. {
  289. base_type::fence_before(order);
  290. __asm
  291. {
  292. mov edx, storage
  293. movzx eax, v
  294. lock or byte ptr [edx], al
  295. };
  296. base_type::fence_after(order);
  297. }
  298. static BOOST_FORCEINLINE void opaque_xor(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  299. {
  300. base_type::fence_before(order);
  301. __asm
  302. {
  303. mov edx, storage
  304. movzx eax, v
  305. lock xor byte ptr [edx], al
  306. };
  307. base_type::fence_after(order);
  308. }
  309. static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) noexcept
  310. {
  311. base_type::fence_before(order);
  312. __asm
  313. {
  314. mov edx, storage
  315. lock not byte ptr [edx]
  316. };
  317. base_type::fence_after(order);
  318. }
  319. static BOOST_FORCEINLINE bool add_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  320. {
  321. base_type::fence_before(order);
  322. bool result;
  323. __asm
  324. {
  325. mov edx, storage
  326. movzx eax, v
  327. lock add byte ptr [edx], al
  328. setnz result
  329. };
  330. base_type::fence_after(order);
  331. return result;
  332. }
  333. static BOOST_FORCEINLINE bool sub_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  334. {
  335. base_type::fence_before(order);
  336. bool result;
  337. __asm
  338. {
  339. mov edx, storage
  340. movzx eax, v
  341. lock sub byte ptr [edx], al
  342. setnz result
  343. };
  344. base_type::fence_after(order);
  345. return result;
  346. }
  347. static BOOST_FORCEINLINE bool and_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  348. {
  349. base_type::fence_before(order);
  350. bool result;
  351. __asm
  352. {
  353. mov edx, storage
  354. movzx eax, v
  355. lock and byte ptr [edx], al
  356. setnz result
  357. };
  358. base_type::fence_after(order);
  359. return result;
  360. }
  361. static BOOST_FORCEINLINE bool or_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  362. {
  363. base_type::fence_before(order);
  364. bool result;
  365. __asm
  366. {
  367. mov edx, storage
  368. movzx eax, v
  369. lock or byte ptr [edx], al
  370. setnz result
  371. };
  372. base_type::fence_after(order);
  373. return result;
  374. }
  375. static BOOST_FORCEINLINE bool xor_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  376. {
  377. base_type::fence_before(order);
  378. bool result;
  379. __asm
  380. {
  381. mov edx, storage
  382. movzx eax, v
  383. lock xor byte ptr [edx], al
  384. setnz result
  385. };
  386. base_type::fence_after(order);
  387. return result;
  388. }
  389. };
  390. template< typename Base, bool Signed >
  391. struct extra_operations< Base, 2u, Signed, true > :
  392. public extra_operations_generic< Base, 2u, Signed >
  393. {
  394. using base_type = extra_operations_generic< Base, 2u, Signed >;
  395. using storage_type = typename base_type::storage_type;
  396. static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) noexcept
  397. {
  398. base_type::fence_before(order);
  399. storage_type old_val;
  400. __asm
  401. {
  402. mov ecx, storage
  403. movzx eax, word ptr [ecx]
  404. align 16
  405. again:
  406. mov edx, eax
  407. neg dx
  408. lock cmpxchg word ptr [ecx], dx
  409. jne again
  410. mov old_val, ax
  411. };
  412. base_type::fence_after(order);
  413. return old_val;
  414. }
  415. static BOOST_FORCEINLINE storage_type negate(storage_type volatile& storage, memory_order order) noexcept
  416. {
  417. base_type::fence_before(order);
  418. storage_type new_val;
  419. __asm
  420. {
  421. mov ecx, storage
  422. movzx eax, word ptr [ecx]
  423. align 16
  424. again:
  425. mov edx, eax
  426. neg dx
  427. lock cmpxchg word ptr [ecx], dx
  428. jne again
  429. mov new_val, dx
  430. };
  431. base_type::fence_after(order);
  432. return new_val;
  433. }
  434. static BOOST_FORCEINLINE bool negate_and_test(storage_type volatile& storage, memory_order order) noexcept
  435. {
  436. base_type::fence_before(order);
  437. bool result;
  438. __asm
  439. {
  440. mov ecx, storage
  441. movzx eax, word ptr [ecx]
  442. align 16
  443. again:
  444. mov edx, eax
  445. neg dx
  446. lock cmpxchg word ptr [ecx], dx
  447. jne again
  448. test dx, dx
  449. setnz result
  450. };
  451. base_type::fence_after(order);
  452. return result;
  453. }
  454. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) noexcept
  455. {
  456. base_type::fence_before(order);
  457. __asm
  458. {
  459. mov ecx, storage
  460. movzx eax, word ptr [ecx]
  461. align 16
  462. again:
  463. mov edx, eax
  464. neg dx
  465. lock cmpxchg word ptr [ecx], dx
  466. jne again
  467. };
  468. base_type::fence_after(order);
  469. }
  470. static BOOST_FORCEINLINE storage_type bitwise_and(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  471. {
  472. base_type::fence_before(order);
  473. __asm
  474. {
  475. mov edi, storage
  476. movzx ecx, v
  477. xor edx, edx
  478. movzx eax, word ptr [edi]
  479. align 16
  480. again:
  481. mov dx, ax
  482. and dx, cx
  483. lock cmpxchg word ptr [edi], dx
  484. jne again
  485. mov v, dx
  486. };
  487. base_type::fence_after(order);
  488. return v;
  489. }
  490. static BOOST_FORCEINLINE storage_type bitwise_or(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  491. {
  492. base_type::fence_before(order);
  493. __asm
  494. {
  495. mov edi, storage
  496. movzx ecx, v
  497. xor edx, edx
  498. movzx eax, word ptr [edi]
  499. align 16
  500. again:
  501. mov dx, ax
  502. or dx, cx
  503. lock cmpxchg word ptr [edi], dx
  504. jne again
  505. mov v, dx
  506. };
  507. base_type::fence_after(order);
  508. return v;
  509. }
  510. static BOOST_FORCEINLINE storage_type bitwise_xor(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  511. {
  512. base_type::fence_before(order);
  513. __asm
  514. {
  515. mov edi, storage
  516. movzx ecx, v
  517. xor edx, edx
  518. movzx eax, word ptr [edi]
  519. align 16
  520. again:
  521. mov dx, ax
  522. xor dx, cx
  523. lock cmpxchg word ptr [edi], dx
  524. jne again
  525. mov v, dx
  526. };
  527. base_type::fence_after(order);
  528. return v;
  529. }
  530. static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) noexcept
  531. {
  532. base_type::fence_before(order);
  533. storage_type old_val;
  534. __asm
  535. {
  536. mov ecx, storage
  537. movzx eax, word ptr [ecx]
  538. align 16
  539. again:
  540. mov edx, eax
  541. not dx
  542. lock cmpxchg word ptr [ecx], dx
  543. jne again
  544. mov old_val, ax
  545. };
  546. base_type::fence_after(order);
  547. return old_val;
  548. }
  549. static BOOST_FORCEINLINE storage_type bitwise_complement(storage_type volatile& storage, memory_order order) noexcept
  550. {
  551. base_type::fence_before(order);
  552. storage_type new_val;
  553. __asm
  554. {
  555. mov ecx, storage
  556. movzx eax, word ptr [ecx]
  557. align 16
  558. again:
  559. mov edx, eax
  560. not dx
  561. lock cmpxchg word ptr [ecx], dx
  562. jne again
  563. mov new_val, dx
  564. };
  565. base_type::fence_after(order);
  566. return new_val;
  567. }
  568. static BOOST_FORCEINLINE bool complement_and_test(storage_type volatile& storage, memory_order order) noexcept
  569. {
  570. base_type::fence_before(order);
  571. bool result;
  572. __asm
  573. {
  574. mov ecx, storage
  575. movzx eax, word ptr [ecx]
  576. align 16
  577. again:
  578. mov edx, eax
  579. not dx
  580. lock cmpxchg word ptr [ecx], dx
  581. jne again
  582. test dx, dx
  583. setnz result
  584. };
  585. base_type::fence_after(order);
  586. return result;
  587. }
  588. static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) noexcept
  589. {
  590. base_type::fence_before(order);
  591. __asm
  592. {
  593. mov ecx, storage
  594. movzx eax, word ptr [ecx]
  595. align 16
  596. again:
  597. mov edx, eax
  598. not dx
  599. lock cmpxchg word ptr [ecx], dx
  600. jne again
  601. };
  602. base_type::fence_after(order);
  603. }
  604. static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  605. {
  606. base_type::fence_before(order);
  607. __asm
  608. {
  609. mov edx, storage
  610. movzx eax, v
  611. lock add word ptr [edx], ax
  612. };
  613. base_type::fence_after(order);
  614. }
  615. static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  616. {
  617. base_type::fence_before(order);
  618. __asm
  619. {
  620. mov edx, storage
  621. movzx eax, v
  622. lock sub word ptr [edx], ax
  623. };
  624. base_type::fence_after(order);
  625. }
  626. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) noexcept
  627. {
  628. base_type::fence_before(order);
  629. __asm
  630. {
  631. mov edx, storage
  632. lock neg word ptr [edx]
  633. };
  634. base_type::fence_after(order);
  635. }
  636. static BOOST_FORCEINLINE void opaque_and(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  637. {
  638. base_type::fence_before(order);
  639. __asm
  640. {
  641. mov edx, storage
  642. movzx eax, v
  643. lock and word ptr [edx], ax
  644. };
  645. base_type::fence_after(order);
  646. }
  647. static BOOST_FORCEINLINE void opaque_or(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  648. {
  649. base_type::fence_before(order);
  650. __asm
  651. {
  652. mov edx, storage
  653. movzx eax, v
  654. lock or word ptr [edx], ax
  655. };
  656. base_type::fence_after(order);
  657. }
  658. static BOOST_FORCEINLINE void opaque_xor(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  659. {
  660. base_type::fence_before(order);
  661. __asm
  662. {
  663. mov edx, storage
  664. movzx eax, v
  665. lock xor word ptr [edx], ax
  666. };
  667. base_type::fence_after(order);
  668. }
  669. static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) noexcept
  670. {
  671. base_type::fence_before(order);
  672. __asm
  673. {
  674. mov edx, storage
  675. lock not word ptr [edx]
  676. };
  677. base_type::fence_after(order);
  678. }
  679. static BOOST_FORCEINLINE bool add_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  680. {
  681. base_type::fence_before(order);
  682. bool result;
  683. __asm
  684. {
  685. mov edx, storage
  686. movzx eax, v
  687. lock add word ptr [edx], ax
  688. setnz result
  689. };
  690. base_type::fence_after(order);
  691. return result;
  692. }
  693. static BOOST_FORCEINLINE bool sub_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  694. {
  695. base_type::fence_before(order);
  696. bool result;
  697. __asm
  698. {
  699. mov edx, storage
  700. movzx eax, v
  701. lock sub word ptr [edx], ax
  702. setnz result
  703. };
  704. base_type::fence_after(order);
  705. return result;
  706. }
  707. static BOOST_FORCEINLINE bool and_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  708. {
  709. base_type::fence_before(order);
  710. bool result;
  711. __asm
  712. {
  713. mov edx, storage
  714. movzx eax, v
  715. lock and word ptr [edx], ax
  716. setnz result
  717. };
  718. base_type::fence_after(order);
  719. return result;
  720. }
  721. static BOOST_FORCEINLINE bool or_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  722. {
  723. base_type::fence_before(order);
  724. bool result;
  725. __asm
  726. {
  727. mov edx, storage
  728. movzx eax, v
  729. lock or word ptr [edx], ax
  730. setnz result
  731. };
  732. base_type::fence_after(order);
  733. return result;
  734. }
  735. static BOOST_FORCEINLINE bool xor_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  736. {
  737. base_type::fence_before(order);
  738. bool result;
  739. __asm
  740. {
  741. mov edx, storage
  742. movzx eax, v
  743. lock xor word ptr [edx], ax
  744. setnz result
  745. };
  746. base_type::fence_after(order);
  747. return result;
  748. }
  749. static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order order) noexcept
  750. {
  751. base_type::fence_before(order);
  752. bool result;
  753. __asm
  754. {
  755. mov edx, storage
  756. mov eax, bit_number
  757. lock bts word ptr [edx], ax
  758. setc result
  759. };
  760. base_type::fence_after(order);
  761. return result;
  762. }
  763. static BOOST_FORCEINLINE bool bit_test_and_reset(storage_type volatile& storage, unsigned int bit_number, memory_order order) noexcept
  764. {
  765. base_type::fence_before(order);
  766. bool result;
  767. __asm
  768. {
  769. mov edx, storage
  770. mov eax, bit_number
  771. lock btr word ptr [edx], ax
  772. setc result
  773. };
  774. base_type::fence_after(order);
  775. return result;
  776. }
  777. static BOOST_FORCEINLINE bool bit_test_and_complement(storage_type volatile& storage, unsigned int bit_number, memory_order order) noexcept
  778. {
  779. base_type::fence_before(order);
  780. bool result;
  781. __asm
  782. {
  783. mov edx, storage
  784. mov eax, bit_number
  785. lock btc word ptr [edx], ax
  786. setc result
  787. };
  788. base_type::fence_after(order);
  789. return result;
  790. }
  791. };
  792. #endif // defined(_M_IX86)
  793. #if defined(_M_IX86) || (defined(BOOST_ATOMIC_INTERLOCKED_BTS) && defined(BOOST_ATOMIC_INTERLOCKED_BTR))
  794. template< typename Base, bool Signed >
  795. struct extra_operations< Base, 4u, Signed, true > :
  796. public extra_operations_generic< Base, 4u, Signed >
  797. {
  798. using base_type = extra_operations_generic< Base, 4u, Signed >;
  799. using storage_type = typename base_type::storage_type;
  800. #if defined(_M_IX86)
  801. static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) noexcept
  802. {
  803. base_type::fence_before(order);
  804. storage_type old_val;
  805. __asm
  806. {
  807. mov ecx, storage
  808. mov eax, dword ptr [ecx]
  809. align 16
  810. again:
  811. mov edx, eax
  812. neg edx
  813. lock cmpxchg dword ptr [ecx], edx
  814. jne again
  815. mov old_val, eax
  816. };
  817. base_type::fence_after(order);
  818. return old_val;
  819. }
  820. static BOOST_FORCEINLINE storage_type negate(storage_type volatile& storage, memory_order order) noexcept
  821. {
  822. base_type::fence_before(order);
  823. storage_type new_val;
  824. __asm
  825. {
  826. mov ecx, storage
  827. mov eax, dword ptr [ecx]
  828. align 16
  829. again:
  830. mov edx, eax
  831. neg edx
  832. lock cmpxchg dword ptr [ecx], edx
  833. jne again
  834. mov new_val, edx
  835. };
  836. base_type::fence_after(order);
  837. return new_val;
  838. }
  839. static BOOST_FORCEINLINE bool negate_and_test(storage_type volatile& storage, memory_order order) noexcept
  840. {
  841. base_type::fence_before(order);
  842. bool result;
  843. __asm
  844. {
  845. mov ecx, storage
  846. mov eax, dword ptr [ecx]
  847. align 16
  848. again:
  849. mov edx, eax
  850. neg edx
  851. lock cmpxchg dword ptr [ecx], edx
  852. jne again
  853. test edx, edx
  854. setnz result
  855. };
  856. base_type::fence_after(order);
  857. return result;
  858. }
  859. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) noexcept
  860. {
  861. base_type::fence_before(order);
  862. __asm
  863. {
  864. mov ecx, storage
  865. mov eax, dword ptr [ecx]
  866. align 16
  867. again:
  868. mov edx, eax
  869. neg edx
  870. lock cmpxchg dword ptr [ecx], edx
  871. jne again
  872. };
  873. base_type::fence_after(order);
  874. }
  875. static BOOST_FORCEINLINE storage_type bitwise_and(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  876. {
  877. base_type::fence_before(order);
  878. __asm
  879. {
  880. mov edi, storage
  881. mov ecx, v
  882. xor edx, edx
  883. mov eax, dword ptr [edi]
  884. align 16
  885. again:
  886. mov edx, eax
  887. and edx, ecx
  888. lock cmpxchg dword ptr [edi], edx
  889. jne again
  890. mov v, edx
  891. };
  892. base_type::fence_after(order);
  893. return v;
  894. }
  895. static BOOST_FORCEINLINE storage_type bitwise_or(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  896. {
  897. base_type::fence_before(order);
  898. __asm
  899. {
  900. mov edi, storage
  901. mov ecx, v
  902. xor edx, edx
  903. mov eax, dword ptr [edi]
  904. align 16
  905. again:
  906. mov edx, eax
  907. or edx, ecx
  908. lock cmpxchg dword ptr [edi], edx
  909. jne again
  910. mov v, edx
  911. };
  912. base_type::fence_after(order);
  913. return v;
  914. }
  915. static BOOST_FORCEINLINE storage_type bitwise_xor(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  916. {
  917. base_type::fence_before(order);
  918. __asm
  919. {
  920. mov edi, storage
  921. mov ecx, v
  922. xor edx, edx
  923. mov eax, dword ptr [edi]
  924. align 16
  925. again:
  926. mov edx, eax
  927. xor edx, ecx
  928. lock cmpxchg dword ptr [edi], edx
  929. jne again
  930. mov v, edx
  931. };
  932. base_type::fence_after(order);
  933. return v;
  934. }
  935. static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) noexcept
  936. {
  937. base_type::fence_before(order);
  938. storage_type old_val;
  939. __asm
  940. {
  941. mov ecx, storage
  942. mov eax, dword ptr [ecx]
  943. align 16
  944. again:
  945. mov edx, eax
  946. not edx
  947. lock cmpxchg dword ptr [ecx], edx
  948. jne again
  949. mov old_val, eax
  950. };
  951. base_type::fence_after(order);
  952. return old_val;
  953. }
  954. static BOOST_FORCEINLINE storage_type bitwise_complement(storage_type volatile& storage, memory_order order) noexcept
  955. {
  956. base_type::fence_before(order);
  957. storage_type new_val;
  958. __asm
  959. {
  960. mov ecx, storage
  961. mov eax, dword ptr [ecx]
  962. align 16
  963. again:
  964. mov edx, eax
  965. not edx
  966. lock cmpxchg dword ptr [ecx], edx
  967. jne again
  968. mov new_val, edx
  969. };
  970. base_type::fence_after(order);
  971. return new_val;
  972. }
  973. static BOOST_FORCEINLINE bool complement_and_test(storage_type volatile& storage, memory_order order) noexcept
  974. {
  975. base_type::fence_before(order);
  976. bool result;
  977. __asm
  978. {
  979. mov ecx, storage
  980. mov eax, dword ptr [ecx]
  981. align 16
  982. again:
  983. mov edx, eax
  984. not edx
  985. lock cmpxchg dword ptr [ecx], edx
  986. jne again
  987. test edx, edx
  988. setnz result
  989. };
  990. base_type::fence_after(order);
  991. return result;
  992. }
  993. static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) noexcept
  994. {
  995. base_type::fence_before(order);
  996. __asm
  997. {
  998. mov ecx, storage
  999. mov eax, dword ptr [ecx]
  1000. align 16
  1001. again:
  1002. mov edx, eax
  1003. not edx
  1004. lock cmpxchg dword ptr [ecx], edx
  1005. jne again
  1006. };
  1007. base_type::fence_after(order);
  1008. }
  1009. static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1010. {
  1011. base_type::fence_before(order);
  1012. __asm
  1013. {
  1014. mov edx, storage
  1015. mov eax, v
  1016. lock add dword ptr [edx], eax
  1017. };
  1018. base_type::fence_after(order);
  1019. }
  1020. static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1021. {
  1022. base_type::fence_before(order);
  1023. __asm
  1024. {
  1025. mov edx, storage
  1026. mov eax, v
  1027. lock sub dword ptr [edx], eax
  1028. };
  1029. base_type::fence_after(order);
  1030. }
  1031. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) noexcept
  1032. {
  1033. base_type::fence_before(order);
  1034. __asm
  1035. {
  1036. mov edx, storage
  1037. lock neg dword ptr [edx]
  1038. };
  1039. base_type::fence_after(order);
  1040. }
  1041. static BOOST_FORCEINLINE void opaque_and(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1042. {
  1043. base_type::fence_before(order);
  1044. __asm
  1045. {
  1046. mov edx, storage
  1047. mov eax, v
  1048. lock and dword ptr [edx], eax
  1049. };
  1050. base_type::fence_after(order);
  1051. }
  1052. static BOOST_FORCEINLINE void opaque_or(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1053. {
  1054. base_type::fence_before(order);
  1055. __asm
  1056. {
  1057. mov edx, storage
  1058. mov eax, v
  1059. lock or dword ptr [edx], eax
  1060. };
  1061. base_type::fence_after(order);
  1062. }
  1063. static BOOST_FORCEINLINE void opaque_xor(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1064. {
  1065. base_type::fence_before(order);
  1066. __asm
  1067. {
  1068. mov edx, storage
  1069. mov eax, v
  1070. lock xor dword ptr [edx], eax
  1071. };
  1072. base_type::fence_after(order);
  1073. }
  1074. static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) noexcept
  1075. {
  1076. base_type::fence_before(order);
  1077. __asm
  1078. {
  1079. mov edx, storage
  1080. lock not dword ptr [edx]
  1081. };
  1082. base_type::fence_after(order);
  1083. }
  1084. static BOOST_FORCEINLINE bool add_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1085. {
  1086. base_type::fence_before(order);
  1087. bool result;
  1088. __asm
  1089. {
  1090. mov edx, storage
  1091. mov eax, v
  1092. lock add dword ptr [edx], eax
  1093. setnz result
  1094. };
  1095. base_type::fence_after(order);
  1096. return result;
  1097. }
  1098. static BOOST_FORCEINLINE bool sub_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1099. {
  1100. base_type::fence_before(order);
  1101. bool result;
  1102. __asm
  1103. {
  1104. mov edx, storage
  1105. mov eax, v
  1106. lock sub dword ptr [edx], eax
  1107. setnz result
  1108. };
  1109. base_type::fence_after(order);
  1110. return result;
  1111. }
  1112. static BOOST_FORCEINLINE bool and_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1113. {
  1114. base_type::fence_before(order);
  1115. bool result;
  1116. __asm
  1117. {
  1118. mov edx, storage
  1119. mov eax, v
  1120. lock and dword ptr [edx], eax
  1121. setnz result
  1122. };
  1123. base_type::fence_after(order);
  1124. return result;
  1125. }
  1126. static BOOST_FORCEINLINE bool or_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1127. {
  1128. base_type::fence_before(order);
  1129. bool result;
  1130. __asm
  1131. {
  1132. mov edx, storage
  1133. mov eax, v
  1134. lock or dword ptr [edx], eax
  1135. setnz result
  1136. };
  1137. base_type::fence_after(order);
  1138. return result;
  1139. }
  1140. static BOOST_FORCEINLINE bool xor_and_test(storage_type volatile& storage, storage_type v, memory_order order) noexcept
  1141. {
  1142. base_type::fence_before(order);
  1143. bool result;
  1144. __asm
  1145. {
  1146. mov edx, storage
  1147. mov eax, v
  1148. lock xor dword ptr [edx], eax
  1149. setnz result
  1150. };
  1151. base_type::fence_after(order);
  1152. return result;
  1153. }
  1154. static BOOST_FORCEINLINE bool bit_test_and_complement(storage_type volatile& storage, unsigned int bit_number, memory_order order) noexcept
  1155. {
  1156. base_type::fence_before(order);
  1157. bool result;
  1158. __asm
  1159. {
  1160. mov edx, storage
  1161. mov eax, bit_number
  1162. lock btc dword ptr [edx], eax
  1163. setc result
  1164. };
  1165. base_type::fence_after(order);
  1166. return result;
  1167. }
  1168. #endif // defined(_M_IX86)
  1169. #if defined(BOOST_ATOMIC_INTERLOCKED_BTS)
  1170. static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order) noexcept
  1171. {
  1172. return !!BOOST_ATOMIC_INTERLOCKED_BTS(&storage, bit_number);
  1173. }
  1174. #elif defined(_M_IX86)
  1175. static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order order) noexcept
  1176. {
  1177. base_type::fence_before(order);
  1178. bool result;
  1179. __asm
  1180. {
  1181. mov edx, storage
  1182. mov eax, bit_number
  1183. lock bts dword ptr [edx], eax
  1184. setc result
  1185. };
  1186. base_type::fence_after(order);
  1187. return result;
  1188. }
  1189. #endif
  1190. #if defined(BOOST_ATOMIC_INTERLOCKED_BTR)
  1191. static BOOST_FORCEINLINE bool bit_test_and_reset(storage_type volatile& storage, unsigned int bit_number, memory_order) noexcept
  1192. {
  1193. return !!BOOST_ATOMIC_INTERLOCKED_BTR(&storage, bit_number);
  1194. }
  1195. #elif defined(_M_IX86)
  1196. static BOOST_FORCEINLINE bool bit_test_and_reset(storage_type volatile& storage, unsigned int bit_number, memory_order order) noexcept
  1197. {
  1198. base_type::fence_before(order);
  1199. bool result;
  1200. __asm
  1201. {
  1202. mov edx, storage
  1203. mov eax, bit_number
  1204. lock btr dword ptr [edx], eax
  1205. setc result
  1206. };
  1207. base_type::fence_after(order);
  1208. return result;
  1209. }
  1210. #endif
  1211. };
  1212. #endif // defined(_M_IX86) || (defined(BOOST_ATOMIC_INTERLOCKED_BTS) && defined(BOOST_ATOMIC_INTERLOCKED_BTR))
  1213. #if defined(BOOST_ATOMIC_INTERLOCKED_BTS64) && defined(BOOST_ATOMIC_INTERLOCKED_BTR64)
  1214. template< typename Base, bool Signed >
  1215. struct extra_operations< Base, 8u, Signed, true > :
  1216. public extra_operations_generic< Base, 8u, Signed >
  1217. {
  1218. using base_type = extra_operations_generic< Base, 8u, Signed >;
  1219. using storage_type = typename base_type::storage_type;
  1220. static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order order) noexcept
  1221. {
  1222. return !!BOOST_ATOMIC_INTERLOCKED_BTS64(&storage, bit_number);
  1223. }
  1224. static BOOST_FORCEINLINE bool bit_test_and_reset(storage_type volatile& storage, unsigned int bit_number, memory_order order) noexcept
  1225. {
  1226. return !!BOOST_ATOMIC_INTERLOCKED_BTR64(&storage, bit_number);
  1227. }
  1228. };
  1229. #endif // defined(BOOST_ATOMIC_INTERLOCKED_BTS64) && defined(BOOST_ATOMIC_INTERLOCKED_BTR64)
  1230. } // namespace detail
  1231. } // namespace atomics
  1232. } // namespace boost
  1233. #include <boost/atomic/detail/footer.hpp>
  1234. #endif // BOOST_ATOMIC_DETAIL_EXTRA_OPS_MSVC_X86_HPP_INCLUDED_