ScalarCast

Applicability

Product

Supported/Unsupported

Atlas A3 training products/Atlas A3 inference products

Atlas A2 training products/Atlas A2 inference products

Atlas 200I/500 A2 inference products

x

Atlas inference product's AI Core

Atlas inference product's Vector Core

x

Atlas training products

x

Function Usage

Converts the data type of a scalar.

Prototype

1
2
template <typename T, typename U, RoundMode roundMode>
__aicore__ inline U ScalarCast(T valueIn)

Parameters

Table 1 Parameters in the template

Parameter

Description

T

Data type of valueIn. The value can be float.

U

Data type after conversion. The value can be half or int32_t.

roundMode

Precision conversion mode. The type is RoundMode.

RoundMode is of the enumeration type and is used to control the precision conversion mode. The definition is as follows:

1
2
3
4
5
6
7
8
9
enum class RoundMode {
    CAST_NONE = 0,  // If accuracy drop is involved during conversion, the CAST_RINT mode is used. If accuracy drop is not involved, the value is not rounded.
    CAST_RINT,      // rint, banker's rounding
    CAST_FLOOR,     // floor, rounding towards negative infinity
    CAST_CEIL,      // ceil, rounding towards positive infinity
    CAST_ROUND,     // round, rounding off
    CAST_TRUNC,     // trunc, rounding towards zero
    CAST_ODD,       // Von Neumann rounding, rounding to the nearest odd number
};

For ScalarCast, the conversion can only be float-to-half (f322f16) or float-to-int32_t (f322s32). The supported round modes are as follows:

  • f322f16: CAST_ODD
  • f322s32: CAST_ROUND, CAST_CEIL, CAST_FLOOR, and CAST_RINT

The precision conversion rule of ScalarCast is the same as that of Cast. For details, see Table 1.

Table 2 Parameters

Parameter

Input/Output

Description

valueIn

Input

Scalar of the data type to be converted.

Returns

ValueIn of the U type.

Constraints

None

Example

1
2
3
float valueIn = 2.5;
// Output (valueOut): 3
int32_t valueOut = AscendC::ScalarCast<float, int32_t, AscendC::RoundMode::CAST_ROUND>(valueIn);