Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions cpp/src/gandiva/decimal_ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,23 @@ DecimalIR::ValueWithOverflow DecimalIR::AddWithOverflowCheck(const ValueFull& x,
// This is pretty complex, so use CPP fns.
llvm::Value* DecimalIR::AddLarge(const ValueFull& x, const ValueFull& y,
const ValueFull& out) {
std::vector<llvm::Value*> args;

auto block = ir_builder()->GetInsertBlock();
auto out_high_ptr = new llvm::AllocaInst(types()->i64_type(), 0, "out_hi", block);
auto out_low_ptr = new llvm::AllocaInst(types()->i64_type(), 0, "out_low", block);
auto x_split = ValueSplit::MakeFromInt128(this, x.value());
args.push_back(x_split.high());
args.push_back(x_split.low());
args.push_back(x.precision());
args.push_back(x.scale());

auto y_split = ValueSplit::MakeFromInt128(this, y.value());
args.push_back(y_split.high());
args.push_back(y_split.low());
args.push_back(y.precision());
args.push_back(y.scale());

args.push_back(out.precision());
args.push_back(out.scale());

auto split = ir_builder()->CreateCall(
module()->getFunction("add_large_decimal128_decimal128"), args);
std::vector<llvm::Value*> args = {
x_split.high(), x_split.low(), x.precision(), x.scale(),
y_split.high(), y_split.low(), y.precision(), y.scale(),
out.precision(), out.scale(), out_high_ptr, out_low_ptr,
};
ir_builder()->CreateCall(module()->getFunction("add_large_decimal128_decimal128"),
args);

auto sum = ValueSplit::MakeFromStruct(this, split).AsInt128(this);
auto out_high = ir_builder()->CreateLoad(out_high_ptr);
auto out_low = ir_builder()->CreateLoad(out_low_ptr);
auto sum = ValueSplit(out_high, out_low).AsInt128(this);
ADD_TRACE_128("AddLarge : sum", sum);
return sum;
}
Expand Down
20 changes: 7 additions & 13 deletions cpp/src/gandiva/precompiled/decimal_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,18 @@

extern "C" {

/// TODO : Passing around structs in IR can be fragile due to c-abi compatibility issues.
/// This seems to work for now, but will need to revisit if we hit issues.
struct DecimalSplit {
int64_t high_bits;
uint64_t low_bits;
};

FORCE_INLINE
DecimalSplit add_large_decimal128_decimal128(int64_t x_high, uint64_t x_low,
int32_t x_precision, int32_t x_scale,
int64_t y_high, uint64_t y_low,
int32_t y_precision, int32_t y_scale,
int32_t out_precision, int32_t out_scale) {
void add_large_decimal128_decimal128(int64_t x_high, uint64_t x_low, int32_t x_precision,
int32_t x_scale, int64_t y_high, uint64_t y_low,
int32_t y_precision, int32_t y_scale,
int32_t out_precision, int32_t out_scale,
int64_t* out_high, uint64_t* out_low) {
gandiva::Decimal128Full x(x_high, x_low, x_precision, x_scale);
gandiva::Decimal128Full y(y_high, y_low, y_precision, y_scale);

arrow::Decimal128 out = gandiva::decimalops::Add(x, y, out_precision, out_scale);
return DecimalSplit{out.high_bits(), out.low_bits()};
*out_high = out.high_bits();
*out_low = out.low_bits();
}

} // extern "C"