Add
产品支持情况
产品 |
是否支持 |
|---|---|
Atlas 350 加速卡 |
√ |
x |
|
x |
|
x |
|
x |
|
x |
|
x |
功能说明
Add指令根据mask对源操作数srcReg0、srcReg1进行按元素求和操作,将结果写入目的操作数dstReg。计算公式如下:

若srcReg0,srcReg1输入转换为uint32_t类型相加时超出uint32_t最大值,在carry(存放进位的MaskReg寄存器)中对应位置每4bit设置1,否则写0。
函数原型
- 计算结果不保留进位
template <typename T = DefaultType, MaskMergeMode mode = MaskMergeMode::ZEROING, typename U> __simd_callee__ inline void Add(U& dstReg, U& srcReg0, U& srcReg1, MaskReg& mask)
- 计算结果保留进位
template <typename T = DefaultType, typename U> __simd_callee__ inline void Add(MaskReg& carry, U& dstReg, U& srcReg0, U& srcReg1, MaskReg& mask)
参数说明
参数名 |
描述 |
|---|---|
T |
操作数数据类型。 Atlas 350 加速卡,支持的数据类型为:uint8_t/int8_t/uint16_t/int16_t/uint32_t/int32_t/half/float/bfloat16_t/uint64_t/int64_t/complex32/complex64 |
mode |
选择MERGING模式或ZEROING模式。
|
U |
目的操作数的RegTensor类型,例如RegTensor<half>,由编译器自动推导,用户不需要填写。 |
返回值说明
无
约束说明
dstReg和srcReg0/srcReg1可以为同一个RegTensor。
调用示例
- 计算结果不保留进位
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); } }
- 计算结果保留进位
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); } }
父主题: 基础算术