MCPcopy Create free account
hub / github.com/apache/arrow / Divide

Function Divide

cpp/src/gandiva/precompiled/decimal_ops.cc:350–393  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

348}
349
350BasicDecimal128 Divide(int64_t context, const BasicDecimalScalar128& x,
351 const BasicDecimalScalar128& y, int32_t out_precision,
352 int32_t out_scale, bool* overflow) {
353 if (y.value() == 0) {
354 const char* err_msg = "DIVIDE: divide by zero error (decimal)";
355 gdv_fn_context_set_error_msg(context, err_msg);
356 return 0;
357 }
358
359 // scale up to the output scale, and do an integer division.
360 int32_t delta_scale = out_scale + y.scale() - x.scale();
361 DCHECK_GE(delta_scale, 0);
362
363 BasicDecimal128 result;
364 auto num_bits_required_after_scaling = MaxBitsRequiredAfterScaling(x, delta_scale);
365 if (num_bits_required_after_scaling <= 127) {
366 // fast-path. The dividend fits in 128-bit after scaling too.
367 *overflow = false;
368
369 // do the division.
370 auto x_scaled = CheckAndIncreaseScale(x.value(), delta_scale);
371 BasicDecimal128 remainder;
372 auto status = x_scaled.Divide(y.value(), &result, &remainder);
373 DCHECK_EQ(status, arrow::DecimalStatus::kSuccess);
374
375 // round-up
376 if (BasicDecimal128::Abs(2 * remainder) >= BasicDecimal128::Abs(y.value())) {
377 result += (x.value().Sign() ^ y.value().Sign()) + 1;
378 }
379 } else {
380 // convert to 256-bit and do the divide.
381 *overflow = delta_scale > 38 && num_bits_required_after_scaling > 255;
382 if (!*overflow) {
383 int64_t result_high;
384 uint64_t result_low;
385
386 gdv_xlarge_scale_up_and_divide(x.value().high_bits(), x.value().low_bits(),
387 y.value().high_bits(), y.value().low_bits(),
388 delta_scale, &result_high, &result_low, overflow);
389 result = BasicDecimal128(result_high, result_low);
390 }
391 }
392 return result;
393}
394
395BasicDecimal128 Mod(int64_t context, const BasicDecimalScalar128& x,
396 const BasicDecimalScalar128& y, int32_t out_precision,

Callers 3

VerifyMethod · 0.70
TEST_FFunction · 0.70

Calls 10

CheckAndIncreaseScaleFunction · 0.85
BasicDecimal128Function · 0.85
DivideMethod · 0.80
SignMethod · 0.80
AbsFunction · 0.50
valueMethod · 0.45
scaleMethod · 0.45

Tested by 2

VerifyMethod · 0.56
TEST_FFunction · 0.56