AddOverflow

Function Usage

A template function to determine whether overflow occurs when two numbers are added. If no overflow occurs, the correct computation result is returned.

Prototype

1
2
template<typename TLhs, typename TRhs, typename TRet>
bool AddOverflow(TLhs lhs, TRhs rhs, TRet &ret)

Parameters

Table 1 Parameters in the template

Parameter

Description

TLhs

Data type of the left operand for the addition operation.

TRhs

Data type of the right operand for the addition operation.

TRet

Data type of the addition operation result.

Table 2 Parameters

Parameter

Input/Output

Description

lhs

Input

Left operand of the addition operation.

rhs

Input

Right operand of the addition operation.

ret

Output

Sum of the left and right operands. The result is valid only when the function return value is true.

Returns

The value true indicates that the computation fails and the return value of ret is invalid. The value false indicates that the computation is successful and the return value of ret is valid.

Constraints

None

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// ...
ge::DataType out_data_type = ge::DT_FLOAT;
GE_ASSERT_GRAPH_SUCCESS(GetOutputDataType(context, out_data_type), "get data type failed");
GE_ASSERT_TRUE(out_data_type == ge::DataType::DT_INT32 || out_data_type == ge::DataType::DT_INT64,
               "only support DT_INT32 and DT_INT64, but out_data_type is %s",
               ge::TypeUtils::DataTypeToSerialString(out_data_type).c_str());
const auto is_malloc = (out_data_type == ge::DataType::DT_INT32);
const auto data_type_size = ge::GetSizeByDataType(out_data_type);
if (data_type_size <= 0) {
   // Error reported
}
size_t malloc_buffer_size = 0U;
if (ge::MulOverflow(static_cast<size_t>(data_type_size), Shape::kMaxDimNum, malloc_buffer_size)) {
   // Error reported
}
if (ge::AddOverflow(malloc_buffer_size, sizeof(GertTensorData), malloc_buffer_size)) {
   // Error reported
}