|
6 | 6 |
|
7 | 7 | #include <algorithm>
|
8 | 8 | #include <cstdint>
|
| 9 | +#include <limits> |
9 | 10 |
|
10 | 11 | #include "Shared/funcannotations.h"
|
11 | 12 |
|
| 13 | +namespace { |
| 14 | +constexpr float HDK_FLT_MIN = std::numeric_limits<float>::min(); |
| 15 | +constexpr float HDK_FLT_MAX = std::numeric_limits<float>::max(); |
| 16 | +constexpr double HDK_DBL_MIN = std::numeric_limits<double>::min(); |
| 17 | +constexpr double HDK_DBL_MAX = std::numeric_limits<double>::max(); |
| 18 | +inline int32_t hdk_float_as_int32_t(const float x) { |
| 19 | + return *reinterpret_cast<const int32_t*>(&x); |
| 20 | +} |
| 21 | +inline float hdk_int32_t_as_float(const int32_t x) { |
| 22 | + return *reinterpret_cast<const float*>(&x); |
| 23 | +} |
| 24 | +inline int64_t hdk_double_as_int64_t(const double x) { |
| 25 | + return *reinterpret_cast<const int64_t*>(&x); |
| 26 | +} |
| 27 | +inline double hdk_int64_t_as_double(const int64_t x) { |
| 28 | + return *reinterpret_cast<const double*>(&x); |
| 29 | +} |
| 30 | + |
| 31 | +template <class T> |
| 32 | +inline constexpr T hdk_min(const T lhs, const T rhs) { |
| 33 | + return lhs < rhs ? lhs : rhs; |
| 34 | +} |
| 35 | +} // namespace |
| 36 | + |
12 | 37 | extern "C" {
|
13 | 38 | int64_t atomic_cas_int_64(GENERIC_ADDR_SPACE int64_t*, int64_t, int64_t);
|
14 | 39 | int32_t atomic_cas_int_32(GENERIC_ADDR_SPACE int32_t*, int32_t, int32_t);
|
@@ -58,19 +83,73 @@ void agg_min_double_shared(GENERIC_ADDR_SPACE int64_t* agg, const double val) {
|
58 | 83 | atomic_min_double(reinterpret_cast<GENERIC_ADDR_SPACE double*>(agg), val);
|
59 | 84 | }
|
60 | 85 |
|
| 86 | +double atomic_min_float(GENERIC_ADDR_SPACE float* addr, const float val) { |
| 87 | + GENERIC_ADDR_SPACE int32_t* address_as_ull = |
| 88 | + reinterpret_cast<GENERIC_ADDR_SPACE int32_t*>(addr); |
| 89 | + int32_t old = *address_as_ull, assumed; |
| 90 | + |
| 91 | + do { |
| 92 | + assumed = old; |
| 93 | + old = atomic_cas_int_32( |
| 94 | + address_as_ull, |
| 95 | + assumed, |
| 96 | + hdk_float_as_int32_t(hdk_min(val, hdk_int32_t_as_float(assumed)))); |
| 97 | + } while (assumed != old); |
| 98 | + |
| 99 | + return hdk_int32_t_as_float(old); |
| 100 | +} |
| 101 | + |
| 102 | +double atomic_min_double(GENERIC_ADDR_SPACE double* addr, const double val) { |
| 103 | + GENERIC_ADDR_SPACE int64_t* address_as_ull = |
| 104 | + reinterpret_cast<GENERIC_ADDR_SPACE int64_t*>(addr); |
| 105 | + int64_t old = *address_as_ull, assumed; |
| 106 | + |
| 107 | + do { |
| 108 | + assumed = old; |
| 109 | + old = atomic_cas_int_64( |
| 110 | + address_as_ull, |
| 111 | + assumed, |
| 112 | + hdk_double_as_int64_t(hdk_min(val, hdk_int64_t_as_double(assumed)))); |
| 113 | + } while (assumed != old); |
| 114 | + |
| 115 | + return hdk_int64_t_as_double(old); |
| 116 | +} |
| 117 | + |
| 118 | +void atomicMinFltSkipVal(GENERIC_ADDR_SPACE int32_t* addr, |
| 119 | + const float val, |
| 120 | + const float skip_val) { |
| 121 | + const int32_t flt_max = hdk_float_as_int32_t(HDK_FLT_MAX); |
| 122 | + int32_t old = atomic_xchg_int_32(addr, flt_max); |
| 123 | + agg_min_float_shared(addr, |
| 124 | + old == hdk_float_as_int32_t(skip_val) |
| 125 | + ? val |
| 126 | + : hdk_min(hdk_int32_t_as_float(old), val)); |
| 127 | +} |
| 128 | + |
| 129 | +void atomicMinDblSkipVal(GENERIC_ADDR_SPACE int64_t* addr, |
| 130 | + const double val, |
| 131 | + const double skip_val) { |
| 132 | + const int64_t dbl_max = hdk_double_as_int64_t(HDK_DBL_MAX); |
| 133 | + int64_t old = atomic_xchg_int_64(addr, dbl_max); |
| 134 | + agg_min_double_shared(addr, |
| 135 | + old == hdk_double_as_int64_t(skip_val) |
| 136 | + ? val |
| 137 | + : hdk_min(hdk_int64_t_as_double(old), val)); |
| 138 | +} |
| 139 | + |
61 | 140 | void agg_min_float_skip_val_shared(GENERIC_ADDR_SPACE int32_t* agg,
|
62 | 141 | const float val,
|
63 | 142 | const float skip_val) {
|
64 |
| - if (val != skip_val) { |
65 |
| - agg_min_float_shared(agg, val); |
| 143 | + if (hdk_float_as_int32_t(val) != hdk_float_as_int32_t(skip_val)) { |
| 144 | + atomicMinFltSkipVal(agg, val, skip_val); |
66 | 145 | }
|
67 | 146 | }
|
68 | 147 |
|
69 | 148 | void agg_min_double_skip_val_shared(GENERIC_ADDR_SPACE int64_t* agg,
|
70 | 149 | const double val,
|
71 | 150 | const double skip_val) {
|
72 |
| - if (val != skip_val) { |
73 |
| - agg_min_double_shared(agg, val); |
| 151 | + if (hdk_double_as_int64_t(val) != hdk_double_as_int64_t(skip_val)) { |
| 152 | + atomicMinDblSkipVal(agg, val, skip_val); |
74 | 153 | }
|
75 | 154 | }
|
76 | 155 |
|
|
0 commit comments