|
1 | | -// MIT License |
2 | | - |
3 | | -// Copyright (c) 2021 biaks (ianiskr@gmail.com) |
4 | | - |
5 | | -// Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | | -// of this software and associated documentation files (the "Software"), to deal |
7 | | -// in the Software without restriction, including without limitation the rights |
8 | | -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9 | | -// copies of the Software, and to permit persons to whom the Software is |
10 | | -// furnished to do so, subject to the following conditions: |
11 | | - |
12 | | -// The above copyright notice and this permission notice shall be included in |
13 | | -// all copies or substantial portions of the Software. |
14 | | - |
15 | | -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | | -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | | -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18 | | -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 | | -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 | | -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 | | -// SOFTWARE. |
22 | | - |
23 | | -#pragma once |
24 | | - |
25 | | -#include <atomic> |
26 | | -#include <type_traits> |
27 | | - |
28 | | -namespace utl { |
29 | | - |
30 | | -template <typename FloatingType> |
31 | | -inline std::atomic<FloatingType>& atomic_add_for_floating_types( |
32 | | - std::atomic<FloatingType>& value, |
33 | | - const FloatingType& add) |
34 | | -{ |
35 | | - FloatingType desired; |
36 | | - FloatingType expected = value.load(std::memory_order_relaxed); |
37 | | - do { |
38 | | - desired = expected + add; |
39 | | - } while (!value.compare_exchange_weak(expected, desired)); |
40 | | - return value; |
41 | | -} |
42 | | - |
43 | | -template <typename FloatingType, |
44 | | - class = typename std:: |
45 | | - enable_if_t<std::is_floating_point<FloatingType>::value, int>> |
46 | | -inline std::atomic<FloatingType>& operator++(std::atomic<FloatingType>& value) |
47 | | -{ |
48 | | - return atomic_add_for_floating_types(value, 1.0); |
49 | | -} |
50 | | - |
51 | | -template <typename FloatingType, |
52 | | - class = typename std:: |
53 | | - enable_if_t<std::is_floating_point<FloatingType>::value, int>> |
54 | | -inline std::atomic<FloatingType>& operator+=(std::atomic<FloatingType>& value, |
55 | | - const FloatingType& val) |
56 | | -{ |
57 | | - return atomic_add_for_floating_types(value, val); |
58 | | -} |
59 | | - |
60 | | -template <typename FloatingType, |
61 | | - class = typename std:: |
62 | | - enable_if_t<std::is_floating_point<FloatingType>::value, int>> |
63 | | -inline std::atomic<FloatingType>& operator--(std::atomic<FloatingType>& value) |
64 | | -{ |
65 | | - return atomic_add_for_floating_types(value, -1.0); |
66 | | -} |
67 | | - |
68 | | -template <typename FloatingType, |
69 | | - class = typename std:: |
70 | | - enable_if_t<std::is_floating_point<FloatingType>::value, int>> |
71 | | -inline std::atomic<FloatingType>& operator-=(std::atomic<FloatingType>& value, |
72 | | - const FloatingType& val) |
73 | | -{ |
74 | | - return atomic_add_for_floating_types(value, -val); |
75 | | -} |
76 | | - |
77 | | -} // namespace utl |
| 1 | +// MIT License |
| 2 | + |
| 3 | +// Copyright (c) 2021 biaks (ianiskr@gmail.com) |
| 4 | + |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | + |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include <atomic> |
| 26 | +#include <type_traits> |
| 27 | + |
| 28 | +namespace utl { |
| 29 | + |
| 30 | +template <typename FloatingType> |
| 31 | +inline std::atomic<FloatingType>& atomic_add_for_floating_types( |
| 32 | + std::atomic<FloatingType>& value, |
| 33 | + const FloatingType& add) |
| 34 | +{ |
| 35 | + FloatingType desired; |
| 36 | + FloatingType expected = value.load(std::memory_order_relaxed); |
| 37 | + do { |
| 38 | + desired = expected + add; |
| 39 | + } while (!value.compare_exchange_weak(expected, desired)); |
| 40 | + return value; |
| 41 | +} |
| 42 | + |
| 43 | +template <typename FloatingType, |
| 44 | + class = typename std:: |
| 45 | + enable_if_t<std::is_floating_point<FloatingType>::value, int>> |
| 46 | +inline std::atomic<FloatingType>& operator++(std::atomic<FloatingType>& value) |
| 47 | +{ |
| 48 | + return atomic_add_for_floating_types(value, 1.0); |
| 49 | +} |
| 50 | + |
| 51 | +template <typename FloatingType, |
| 52 | + class = typename std:: |
| 53 | + enable_if_t<std::is_floating_point<FloatingType>::value, int>> |
| 54 | +inline std::atomic<FloatingType>& operator+=(std::atomic<FloatingType>& value, |
| 55 | + const FloatingType& val) |
| 56 | +{ |
| 57 | + return atomic_add_for_floating_types(value, val); |
| 58 | +} |
| 59 | + |
| 60 | +template <typename FloatingType, |
| 61 | + class = typename std:: |
| 62 | + enable_if_t<std::is_floating_point<FloatingType>::value, int>> |
| 63 | +inline std::atomic<FloatingType>& operator--(std::atomic<FloatingType>& value) |
| 64 | +{ |
| 65 | + return atomic_add_for_floating_types(value, -1.0); |
| 66 | +} |
| 67 | + |
| 68 | +template <typename FloatingType, |
| 69 | + class = typename std:: |
| 70 | + enable_if_t<std::is_floating_point<FloatingType>::value, int>> |
| 71 | +inline std::atomic<FloatingType>& operator-=(std::atomic<FloatingType>& value, |
| 72 | + const FloatingType& val) |
| 73 | +{ |
| 74 | + return atomic_add_for_floating_types(value, -val); |
| 75 | +} |
| 76 | + |
| 77 | +} // namespace utl |
0 commit comments