Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:33:57

0001 /*
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * Copyright (c) 2018 Andrey Semashev
0007  */
0008 /*!
0009  * \file   atomic/detail/fp_ops_generic.hpp
0010  *
0011  * This header contains generic implementation of the floating point atomic operations.
0012  */
0013 
0014 #ifndef BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_
0015 #define BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_
0016 
0017 #include <cstddef>
0018 #include <boost/memory_order.hpp>
0019 #include <boost/atomic/detail/config.hpp>
0020 #include <boost/atomic/detail/bitwise_fp_cast.hpp>
0021 #include <boost/atomic/detail/storage_traits.hpp>
0022 #include <boost/atomic/detail/fp_operations_fwd.hpp>
0023 #include <boost/atomic/detail/header.hpp>
0024 
0025 #ifdef BOOST_HAS_PRAGMA_ONCE
0026 #pragma once
0027 #endif
0028 
0029 namespace boost {
0030 namespace atomics {
0031 namespace detail {
0032 
0033 //! Generic implementation of floating point operations
0034 template< typename Base, typename Value, std::size_t Size >
0035 struct fp_operations_generic :
0036     public Base
0037 {
0038     typedef Base base_type;
0039     typedef typename base_type::storage_type storage_type;
0040     typedef Value value_type;
0041 
0042     static BOOST_FORCEINLINE value_type fetch_add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
0043     {
0044         storage_type old_storage, new_storage;
0045         value_type old_val, new_val;
0046         atomics::detail::non_atomic_load(storage, old_storage);
0047         do
0048         {
0049             old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
0050             new_val = old_val + v;
0051             new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
0052         }
0053         while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
0054         return old_val;
0055     }
0056 
0057     static BOOST_FORCEINLINE value_type fetch_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
0058     {
0059         storage_type old_storage, new_storage;
0060         value_type old_val, new_val;
0061         atomics::detail::non_atomic_load(storage, old_storage);
0062         do
0063         {
0064             old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
0065             new_val = old_val - v;
0066             new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
0067         }
0068         while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
0069         return old_val;
0070     }
0071 };
0072 
0073 // Default fp_operations template definition will be used unless specialized for a specific platform
0074 template< typename Base, typename Value, std::size_t Size >
0075 struct fp_operations< Base, Value, Size, true > :
0076     public fp_operations_generic< Base, Value, Size >
0077 {
0078 };
0079 
0080 } // namespace detail
0081 } // namespace atomics
0082 } // namespace boost
0083 
0084 #include <boost/atomic/detail/footer.hpp>
0085 
0086 #endif // BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_