-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumeric.cpp
240 lines (198 loc) · 6.58 KB
/
numeric.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wredundant-move"
#include <arrow/util/decimal.h>
#pragma GCC diagnostic pop
#include "numeric.hpp"
#include "agg.h"
#include "dec.hpp"
int count_digit(std::uint64_t n) noexcept;
void avg_numeric(void *transdata, const void *data, xrg_attr_t *attr) {
avg_trans_t *accum = (avg_trans_t *) transdata;
const avg_trans_t *rec = (const avg_trans_t *) data;
arrow::Decimal128 *sum = (arrow::Decimal128 *) &accum->sum.i128;
arrow::Decimal128 *value = (arrow::Decimal128 *) &rec->sum.i128;
accum->count += rec->count;
*sum += *value;
}
/* sum */
void sum_numeric(void *transdata, const void *data, xrg_attr_t *attr) {
arrow::Decimal128 *accum = (arrow::Decimal128 *) transdata;
const arrow::Decimal128 *dec = (const arrow::Decimal128 *) data;
*accum += *dec;
}
/* min */
void min_numeric(void *transdata, const void *data, xrg_attr_t *attr) {
if (attr->ptyp == XRG_PTYP_INT64) {
int64_t i64accum = *((int64_t *) transdata);
int64_t i64data = *((int64_t *) data);
arrow::Decimal128 accum(i64accum);
arrow::Decimal128 dec(i64data);
if (accum > dec) {
*((int64_t *) transdata) = static_cast<int64_t>(dec);
}
} else if (attr->ptyp == XRG_PTYP_INT128) {
arrow::Decimal128 *accum = (arrow::Decimal128 *) transdata;
const arrow::Decimal128 *dec = (const arrow::Decimal128 *) data;
if (*accum > *dec) {
*accum = *dec;
}
}
}
/* max */
void max_numeric(void *transdata, const void *data, xrg_attr_t *attr) {
if (attr->ptyp == XRG_PTYP_INT64) {
int64_t i64accum = *((int64_t *) transdata);
int64_t i64data = *((int64_t *) data);
arrow::Decimal128 accum(i64accum);
arrow::Decimal128 dec(i64data);
if (accum < dec) {
*((int64_t *) transdata) = static_cast<int64_t>(dec);
}
} else if (attr->ptyp == XRG_PTYP_INT128) {
arrow::Decimal128 *accum = (arrow::Decimal128 *) transdata;
const arrow::Decimal128 *dec = (const arrow::Decimal128 *) data;
if (*accum < *dec) {
*accum = *dec;
}
}
}
int count_digit(std::uint64_t n) noexcept {
int count = 0;
for (count = 0; n ; count++) {
n /= 10;
}
return count;
}
int get_precision(const arrow::Decimal128 &dec) {
arrow::Decimal128 out;
int precision;
std::string s = dec.ToIntegerString();
arrow::Decimal128::FromString(s, &out, &precision);
return precision;
}
int avg_numeric_finalize(char *data, xrg_attr_t *attr, __int128_t *avg, int *precision, int *scale) {
try {
avg_trans_t *accum = (avg_trans_t *) data;
arrow::Decimal128 *ret = (arrow::Decimal128 *)avg;
arrow::Decimal128 *sum = (arrow::Decimal128 *) &accum->sum.i128;
arrow::Decimal128 count(0, accum->count);
int p1 = get_precision(*sum);
int s1 = attr->scale;
int p2 = count_digit(static_cast<uint64_t>(accum->count));
int s2 = 0;
auto [p3, s3] = dec_DIV_precision_scale(p1, s1, p2, s2);
count = count.IncreaseScaleBy(s1);
*ret = div_scalar(*sum, count, p3, s3);
*precision = p3;
*scale = s3;
} catch (const std::runtime_error &ex) {
std::cerr << ex.what() << std::endl;
return 1;
} catch (...) {
return 2;
}
return 0;
}
int avg_int64_finalize(int64_t i64sum, int64_t i64count, __int128_t *avg, int *precision, int *scale) {
try {
arrow::Decimal128 *ret = (arrow::Decimal128 *)avg;
arrow::Decimal128 sum(i64sum);
arrow::Decimal128 count(i64count);
int p1 = count_digit(static_cast<uint64_t>(i64sum));
int s1 = 0;
int p2 = count_digit(static_cast<uint64_t>(i64count));
int s2 = 0;
auto [p3, s3] = dec_DIV_precision_scale(p1, s1, p2, s2);
*ret = div_scalar(sum, count, p3, s3);
*precision = p3;
*scale = s3;
} catch (const std::runtime_error &ex) {
std::cerr << ex.what() << std::endl;
return 1;
} catch (...) {
return 2;
}
return 0;
}
int avg_int128_finalize(__int128_t i128sum, int64_t i64count, __int128_t *avg, int *precision, int *scale) {
try {
arrow::Decimal128 *ret = (arrow::Decimal128 *)avg;
arrow::Decimal128 sum((uint8_t*) &i128sum);
arrow::Decimal128 count(i64count);
int p1 = get_precision(sum);
int s1 = 0;
int p2 = count_digit(static_cast<uint64_t>(i64count));
int s2 = 0;
auto [p3, s3] = dec_DIV_precision_scale(p1, s1, p2, s2);
*ret = div_scalar(sum, count, p3, s3);
*precision = p3;
*scale = s3;
} catch (const std::runtime_error &ex) {
std::cerr << ex.what() << std::endl;
return 1;
} catch (...) {
return 2;
}
return 0;
}
#if 0
int get_precision(const arrow::Decimal128 &dec) {
arrow::Decimal128 abs = arrow::Decimal128::Abs(dec);
if (abs.high_bits() == 0) {
return count_digit(abs.low_bits());
}
return count_digit(static_cast<uint64_t>(abs.high_bits())) + 20;
}
static constexpr int floor_log10_pow2(int e) noexcept {
return (e * 1262611) >> 22;
}
static constexpr int ceil_log10_pow2(int e) noexcept {
return e == 0 ? 0 : floor_log10_pow2(e) + 1;
}
struct digit_count_table_holder_t {
std::uint64_t entry[64];
};
static constexpr digit_count_table_holder_t generate_digit_count_table() {
digit_count_table_holder_t table{ {} };
constexpr std::uint64_t pow10[] = {
1ull,
10ull,
100ull,
1000ull,
1'0000ull,
10'0000ull,
100'0000ull,
1000'0000ull,
1'0000'0000ull,
10'0000'0000ull,
100'0000'0000ull,
1000'0000'0000ull,
1'0000'0000'0000ull,
10'0000'0000'0000ull,
100'0000'0000'0000ull,
1000'0000'0000'0000ull,
1'0000'0000'0000'0000ull,
10'0000'0000'0000'0000ull,
100'0000'0000'0000'0000ull,
1000'0000'0000'0000'0000ull
};
for (int i = 0; i < 64; ++i) {
auto const ub = std::uint64_t(ceil_log10_pow2(i));
assert(ub <= 19);
table.entry[i] = ((ub + 1) << 52) - (pow10[ub] >> (i / 4));
}
return table;
}
static constexpr inline auto digit_count_table = generate_digit_count_table();
static int floor_log2(std::uint64_t n) noexcept {
return 63 ^ __builtin_clzll(n);
}
int count_digit(std::uint64_t n) noexcept {
return int((digit_count_table.entry[floor_log2(n)] + (n >> (floor_log2(n) / 4))) >> 52);
}
#endif