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

Method BuildAdd

cpp/src/gandiva/decimal_ir.cc:273–336  ·  view source on GitHub ↗

The output scale/precision cannot be arbitrary values. The algo here depends on them to be the same as computed in DecimalTypeSql. TODO: enforce this.

Source from the content-addressed store, hash-verified

271/// to be the same as computed in DecimalTypeSql.
272/// TODO: enforce this.
273Status DecimalIR::BuildAdd() {
274 // Create fn prototype :
275 // int128_t
276 // add_decimal128_decimal128(int128_t x_value, int32_t x_precision, int32_t x_scale,
277 // int128_t y_value, int32_t y_precision, int32_t y_scale
278 // int32_t out_precision, int32_t out_scale)
279 auto i32 = types()->i32_type();
280 auto i128 = types()->i128_type();
281 auto function = BuildFunction(kAddFunction, i128,
282 {
283 {"x_value", i128},
284 {"x_precision", i32},
285 {"x_scale", i32},
286 {"y_value", i128},
287 {"y_precision", i32},
288 {"y_scale", i32},
289 {"out_precision", i32},
290 {"out_scale", i32},
291 });
292
293 auto arg_iter = function->arg_begin();
294 ValueFull x(&arg_iter[0], &arg_iter[1], &arg_iter[2]);
295 ValueFull y(&arg_iter[3], &arg_iter[4], &arg_iter[5]);
296 ValueFull out(nullptr, &arg_iter[6], &arg_iter[7]);
297
298 auto entry = llvm::BasicBlock::Create(*context(), "entry", function);
299 ir_builder()->SetInsertPoint(entry);
300
301 // CPP :
302 // if (out_precision < 38) {
303 // return AddFastPath(x, y)
304 // } else {
305 // ret = AddWithOverflowCheck(x, y)
306 // if (ret.overflow)
307 // return AddLarge(x, y)
308 // else
309 // return ret.value;
310 // }
311 llvm::Value* lt_max_precision = ir_builder()->CreateICmpSLT(
312 out.precision(), types()->i32_constant(DecimalTypeUtil::kMaxPrecision));
313 auto then_lambda = [&] {
314 // fast-path add
315 return AddFastPath(x, y);
316 };
317 auto else_lambda = [&] {
318 if (kUseOverflowIntrinsics) {
319 // do the add and check if there was overflow
320 auto ret = AddWithOverflowCheck(x, y, out);
321
322 // if there is an overflow, switch to the AddLarge codepath.
323 return BuildIfElse(
324 ret.overflow(), types()->i128_type(), [&] { return AddLarge(x, y, out); },
325 [&] { return ret.value(); });
326 } else {
327 return AddLarge(x, y, out);
328 }
329 };
330 auto value =

Callers 1

AddFunctionsMethod · 0.80

Calls 9

AddFastPathFunction · 0.85
AddLargeFunction · 0.85
overflowMethod · 0.80
typesFunction · 0.70
contextFunction · 0.70
ir_builderFunction · 0.70
OKFunction · 0.50
precisionMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected