MulOverflow

Function Usage

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

Prototype

template<typename TLhs, typename TRhs, typename TRet>

bool MulOverflow(TLhs lhs, TRhs rhs, TRet &ret)

Parameters

Table 1 Parameters in the template

Parameter

Description

TLhs

Data type of the left operand for the multiplication operation.

TRhs

Data type of the right operand for the multiplication operation.

TRet

Data type of the multiplication operation result.

Table 2 Parameters

Parameter

Input/Output

Description

lhs

Input

Left operand of the multiplication operation.

rhs

Input

Right operand of the multiplication operation.

ret

Input

Multiplication result 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
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
}