extra_ops_msvc_x86.hpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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 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. typedef extra_operations_generic< Base, 1u, Signed > base_type;
  35. typedef typename base_type::storage_type storage_type;
  36. static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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. typedef extra_operations_generic< Base, 2u, Signed > base_type;
  395. typedef typename base_type::storage_type storage_type;
  396. static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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. typedef extra_operations_generic< Base, 4u, Signed > base_type;
  799. typedef typename base_type::storage_type storage_type;
  800. #if defined(_M_IX86)
  801. static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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) BOOST_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. typedef extra_operations_generic< Base, 8u, Signed > base_type;
  1219. typedef typename base_type::storage_type storage_type;
  1220. static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_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) BOOST_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_