|
| 1 | +#pragma once |
| 2 | +#include "filter/filter.h" |
| 3 | +#include <time.h> |
| 4 | +namespace PS { |
| 5 | + |
| 6 | +class FixingFloatFilter : public Filter { |
| 7 | + public: |
| 8 | + void encode(const MessagePtr& msg) { |
| 9 | + auto conf = CHECK_NOTNULL(find(FilterConfig::FIXING_FLOAT, msg))->fixed_point(); |
| 10 | + int n = msg->value.size(); |
| 11 | + CHECK_EQ(n, msg->task.value_type_size()); |
| 12 | + for (int i = 0; i < n; ++i) { |
| 13 | + auto type = msg->task.value_type(i); |
| 14 | + if (type == DataType::FLOAT) { |
| 15 | + msg->value[i] = encode(SArray<float>(msg->value[i]), conf); |
| 16 | + } |
| 17 | + if (type == DataType::DOUBLE) { |
| 18 | + msg->value[i] = encode(SArray<double>(msg->value[i]), conf); |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + void decode(const MessagePtr& msg) { |
| 24 | + auto conf = CHECK_NOTNULL(find(FilterConfig::FIXING_FLOAT, msg))->fixed_point(); |
| 25 | + int n = msg->value.size(); |
| 26 | + CHECK_EQ(n, msg->task.value_type_size()); |
| 27 | + for (int i = 0; i < n; ++i) { |
| 28 | + auto type = msg->task.value_type(i); |
| 29 | + if (type == DataType::FLOAT) { |
| 30 | + msg->value[i] = decode<float>(msg->value[i], conf); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private: |
| 36 | + inline bool boolrand(int* seed) { |
| 37 | + *seed = (214013 * *seed + 2531011); |
| 38 | + return ((*seed >> 16) & 0x1) == 0; |
| 39 | + } |
| 40 | + |
| 41 | + template <typename V> |
| 42 | + SArray<char> encode(const SArray<V>& data, const FilterConfig::FixedConfig& conf) { |
| 43 | + int nbytes = conf.num_bytes(); |
| 44 | + CHECK_GT(nbytes, 0); |
| 45 | + CHECK_LT(nbytes, 8); |
| 46 | + V ratio = static_cast<V>(1<<(nbytes*4)); |
| 47 | + V min_v = static_cast<V>(conf.min_value()); |
| 48 | + V max_v = static_cast<V>(conf.max_value()); |
| 49 | + V bin = max_v - min_v; |
| 50 | + CHECK_GT(bin, 0); |
| 51 | + |
| 52 | + SArray<char> res(data.size() * nbytes); |
| 53 | + char* res_ptr = res.data(); |
| 54 | + int seed = time(NULL); |
| 55 | + |
| 56 | + for (int i = 0; i < data.size(); ++i) { |
| 57 | + V proj = data[i] > max_v ? max_v : data[i] < min_v ? min_v : data[i]; |
| 58 | + V tmp = (proj - min_v) / bin * ratio; |
| 59 | + uint64 r = static_cast<uint64>(floor(tmp)) + boolrand(&seed); |
| 60 | + |
| 61 | + for (int j = 0; j < nbytes; ++j) { |
| 62 | + *(res_ptr++) = static_cast<char>(r & 0xFF); |
| 63 | + r = r >> 8; |
| 64 | + } |
| 65 | + } |
| 66 | + return res; |
| 67 | + } |
| 68 | + |
| 69 | + template <typename V> |
| 70 | + SArray<V> decode(const SArray<char>& data, const FilterConfig::FixedConfig& conf) { |
| 71 | + int nbytes = conf.num_bytes(); |
| 72 | + V ratio = static_cast<V>(1<<(nbytes*4)); |
| 73 | + V min_v = static_cast<V>(conf.min_value()); |
| 74 | + V max_v = static_cast<V>(conf.max_value()); |
| 75 | + V bin = max_v - min_v; |
| 76 | + |
| 77 | + int n = data.size() / nbytes; |
| 78 | + SArray<V> res(n); |
| 79 | + char* data_ptr = data.data(); |
| 80 | + for (int i = 0; i < n; ++i) { |
| 81 | + V r = 0; |
| 82 | + for (int j = 0; j < nbytes; ++j) { |
| 83 | + r += static_cast<uint64>(*(data_ptr++)) << 8 * j; |
| 84 | + } |
| 85 | + res[i] = static_cast<V>(r) / ratio * bin + min_v; |
| 86 | + } |
| 87 | + return res; |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +} // namespace PS |
0 commit comments