extra_fp_ops_emulated.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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) 2018 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/extra_fp_ops_emulated.hpp
  10. *
  11. * This header contains emulated (lock-based) implementation of the extra floating point atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <boost/memory_order.hpp>
  17. #include <boost/atomic/detail/config.hpp>
  18. #include <boost/atomic/detail/bitwise_fp_cast.hpp>
  19. #include <boost/atomic/detail/extra_fp_operations_fwd.hpp>
  20. #include <boost/atomic/detail/lockpool.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. namespace atomics {
  26. namespace detail {
  27. //! Generic implementation of extra floating point operations
  28. template< typename Base, typename Value, std::size_t Size >
  29. struct emulated_extra_fp_operations :
  30. public Base
  31. {
  32. typedef Base base_type;
  33. typedef typename base_type::storage_type storage_type;
  34. typedef Value value_type;
  35. static BOOST_FORCEINLINE value_type fetch_negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  36. {
  37. storage_type& s = const_cast< storage_type& >(storage);
  38. lockpool::scoped_lock lock(&storage);
  39. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  40. value_type new_val = -old_val;
  41. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  42. return old_val;
  43. }
  44. static BOOST_FORCEINLINE value_type negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  45. {
  46. storage_type& s = const_cast< storage_type& >(storage);
  47. lockpool::scoped_lock lock(&storage);
  48. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  49. value_type new_val = -old_val;
  50. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  51. return new_val;
  52. }
  53. static BOOST_FORCEINLINE value_type add(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
  54. {
  55. storage_type& s = const_cast< storage_type& >(storage);
  56. lockpool::scoped_lock lock(&storage);
  57. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  58. value_type new_val = old_val + v;
  59. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  60. return new_val;
  61. }
  62. static BOOST_FORCEINLINE value_type sub(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
  63. {
  64. storage_type& s = const_cast< storage_type& >(storage);
  65. lockpool::scoped_lock lock(&storage);
  66. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  67. value_type new_val = old_val - v;
  68. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  69. return new_val;
  70. }
  71. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  72. {
  73. fetch_negate(storage, order);
  74. }
  75. static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
  76. {
  77. base_type::fetch_add(storage, v, order);
  78. }
  79. static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
  80. {
  81. base_type::fetch_sub(storage, v, order);
  82. }
  83. };
  84. template< typename Base, typename Value, std::size_t Size >
  85. struct extra_fp_operations< Base, Value, Size, false > :
  86. public emulated_extra_fp_operations< Base, Value, Size >
  87. {
  88. };
  89. } // namespace detail
  90. } // namespace atomics
  91. } // namespace boost
  92. #endif // BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_HPP_INCLUDED_