Truncate

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

x

Atlas A2 training product/Atlas A2 inference product

x

Atlas 200I/500 A2 inference product

x

Atlas inference product AI Core

x

Atlas inference product Vector Core

x

Atlas training product

x

Function Usage

Truncates the float elements of the source operand to their integer parts, while retaining the data type of the source operand.

Prototype

template <typename T = DefaultType, RoundMode roundMode = RoundMode::CAST_NONE, MaskMergeMode mode = MaskMergeMode::ZEROING, typename S>
__simd_callee__ inline void Truncate(S& dstReg, S& srcReg, MaskReg& mask)

Parameters

Table 1 Template parameters

Parameter

Description

T

Operand data type.

For the Atlas 350 Accelerator Card, the supported data types of dstReg/srcReg are half, float, and bfloat16_t.

roundMode

Rounding mode. The options are as follows:

  • RoundMode::CAST_NONE: default value. In scenarios where precision loss occurs during conversion, this value is equivalent to CAST_RINT. In other scenarios, this value has no effect.
  • RoundMode::CAST_RINT: returns the nearest integer. If two integers are equally close, the even integer is returned.
  • RoundMode::CAST_ROUND: round mode, rounding off
  • RoundMode::CAST_FLOOR: floor mode, rounding down
  • RoundMode::CAST_CEIL: ceil mode, rounding up
  • RoundMode::CAST_TRUNC: truncation mode, rounding toward zero and discarding the fractional part

mode

Set it to MERGING or ZEROING.

  • ZEROING: The elements that are not filtered by mask are set to zero in dst.
  • MERGING: not supported currently.

S

srcReg or dstReg type, for example, RegTensor<float>. It is automatically inferred by the compiler and does not need to be specified.

Table 2 Parameters

Parameter

Input/Output

Description

dstReg

Output

Destination operand.

The type is RegTensor.

srcReg

Input

Source operand.

The type is RegTensor.

The source operand must have the same data type as the destination operand.

mask

Input

Valid indication of the source operand element operation. For details, see MaskReg.

Returns

None

Constraints

  • In non-saturation mode, if the input is NaN, the output is NaN. If the input value exceeds the value range of the input type, the output is +/-inf.
  • In saturation mode, if the input is NaN, the output is 0. If the input value exceeds the value range of the input type, the output is saturated to the maximum or minimum value.
  • The float type supports only the non-saturation mode.

Example

template<typename T>
__simd_vf__ inline void TruncVF(__ubuf__ T* dstAddr, __ubuf__ T* srcAddr, uint32_t count, uint32_t oneRepeatSize, uint16_t repeatTimes)
{
    AscendC::Reg::RegTensor<T> srcReg;
    AscendC::Reg::RegTensor<T> dstReg;
    AscendC::Reg::MaskReg mask;
    for (uint16_t i = 0; i < repeatTimes; i++) {
        AscendC::Reg::LoadAlign(srcReg, srcAddr + i * oneRepeatSize);
        mask = AscendC::Reg::UpdateMask<T>(count);
        AscendC::Reg::Truncate<T, AscendC::RoundMode::CAST_FLOOR, AscendC::Reg::MaskMergeMode::ZEROING>(dstReg, srcReg, mask);
        AscendC::Reg::StoreAlign(dstAddr + i * oneRepeatSize, dstReg, mask);
    }
}