Add
Applicability
Product |
Supported |
|---|---|
Atlas 350 Accelerator Card |
√ |
x |
|
x |
|
x |
|
x |
|
x |
|
x |
Function Usage
The Add instruction performs element-wise addition on the source operands srcReg0 and srcReg1 based on the mask and writes the result to the destination operand dstReg. The formula is as follows:

If the sum of srcReg0 and srcReg1 (converted to uint32_t) exceeds the maximum value of uint32_t, set the corresponding 4-bit field in carry (the MaskReg register storing the carry) to 1; otherwise, write 0.
Prototype
- The computation result does not retain the carry.
template <typename T = DefaultType, MaskMergeMode mode = MaskMergeMode::ZEROING, typename U> __simd_callee__ inline void Add(U& dstReg, U& srcReg0, U& srcReg1, MaskReg& mask)
- The computation result retains the carry.
template <typename T = DefaultType, typename U> __simd_callee__ inline void Add(MaskReg& carry, U& dstReg, U& srcReg0, U& srcReg1, MaskReg& mask)
Parameters
Parameter |
Description |
|---|---|
T |
Operand data type. For the Atlas 350 Accelerator Card, the supported data types are uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, half, float, bfloat16_t, uint64_t, int64_t, complex32, and complex64. |
mode |
Set it to MERGING or ZEROING.
|
U |
RegTensor type of the destination operand, for example, RegTensor<half>. It is automatically inferred by the compiler and does not need to be specified. |
Parameter |
Input/Output |
Description |
|---|---|---|
dstReg |
Output |
Destination operand. The type is RegTensor. |
srcReg0 |
Input |
Source operand. The type is RegTensor. The source and destination operands must have the same data type. |
srcReg1 |
Input |
Source operand. The type is RegTensor. The source and destination operands must have the same data type. |
carry |
Output |
Destination operand. MaskReg register for storing the carry. The type is MaskReg. |
mask |
Input |
Valid indication of the source operand element operation. For details, see MaskReg. |
Returns
None
Restrictions
dstReg and srcReg0/srcReg1 can be the same RegTensor.
Examples
- The computation result does not retain the carry.
template <typename T> __simd_vf__ inline void AddVF(__ubuf__ T* dstAddr, __ubuf__ T* src0Addr, __ubuf__ T* src1Addr, uint32_t count, uint32_t oneRepeatSize, uint16_t repeatTimes) { AscendC::Reg::RegTensor<T> srcReg0; AscendC::Reg::RegTensor<T> srcReg1; AscendC::Reg::RegTensor<T> dstReg; AscendC::Reg::MaskReg mask; for (uint16_t i = 0; i < repeatTimes; i++) { mask = AscendC::Reg::UpdateMask<T>(count); AscendC::Reg::LoadAlign(srcReg0, src0Addr + i * oneRepeatSize); AscendC::Reg::LoadAlign(srcReg1, src1Addr + i * oneRepeatSize); AscendC::Reg::Add(dstReg, srcReg0, srcReg1, mask); AscendC::Reg::StoreAlign(dstAddr + i * oneRepeatSize, dstReg, mask); } }
- The computation result retains the carry.
template <typename T> static __simd_vf__ inline void AddVF(__ubuf__ T* dst0Addr, __ubuf__ T* dst1Addr, __ubuf__ T* src0Addr, __ubuf__ T* src1Addr, uint32_t count, uint16_t repeatTimes, uint32_t oneRepeatSize){ AscendC::Reg::RegTensor<T> srcReg0; AscendC::Reg::RegTensor<T> srcReg1; AscendC::Reg::RegTensor<T> dstReg0; AscendC::Reg::MaskReg mask; AscendC::Reg::MaskReg carry = AscendC::Reg::CreateMask<uint8_t>(); for (uint16_t i = 0; i < repeatTimes; i++) { mask = AscendC::Reg::UpdateMask<T>(count); AscendC::Reg::LoadAlign(srcReg0, src0Addr + i * oneRepeatSize); AscendC::Reg::LoadAlign(srcReg1, src1Addr + i * oneRepeatSize); AscendC::Reg::Add(carry, dstReg0, srcReg0, srcReg1, mask); // 8*4B=32B align AscendC::Reg::StoreAlign<uint32_t, AscendC::Reg::MaskDist::DIST_NORM>(dst1Addr + i * 8, carry); AscendC::Reg::StoreAlign(dst0Addr + i * oneRepeatSize, dstReg0, mask); } }