MSP430x54x學(xué)習(xí)筆記--硬件乘法器MPY
·16位乘法器使用方法:
本文引用地址:http://m.butianyuan.cn/article/201611/322783.htm1.無(wú)符號(hào)數(shù)相乘 result="n1"*n2。第一操作數(shù)n1(16位無(wú)符號(hào))賦給MPY寄存器,第二操作數(shù)n2(16位無(wú)符號(hào))賦給OP2寄存器,賦值完成后,結(jié)果result(32位無(wú)符號(hào))就可以在RESLO(低16位)和RESHI(高165位)中讀取。
代碼如下:
MPY = n1; // Load first operand -unsigned mult
OP2 = n2; // Load second operand
result = RESHI; // Load RESHI word result
result = (result<<16)|RESLO; // Shift RESHI left and concat with
2.無(wú)符號(hào)數(shù)乘加操作 result="n1"*n2+n3*n4。第一操作數(shù)n1(16位無(wú)符號(hào))賦給MPY寄存器,第二操作數(shù)n2(16位無(wú)符號(hào))賦給OP2寄存器,第三操作數(shù)n3(16位無(wú)符號(hào))賦給MAC寄存器,第四操作數(shù)n4(16位無(wú)符號(hào))賦給OP2寄存器,結(jié)果result(32位無(wú)符號(hào))就可以在RESLO(低16位)和RESHI(高165位)中讀取。SUMEXT寄存器中保存進(jìn)位標(biāo)志。
代碼如下:
MPY = n1; // Load first operand -unsigned mult
OP2 = n2; // Load second operand
MAC = n3;// Load 3rd operand -unsigned mult
OP2 = n4;// Load 4rd operand
result = RESHI; // Load RESHI word result
result = (result<<16)|RESLO; // Shift RESHI left and concat with
3.有符號(hào)數(shù)相乘 result="n1"*n2。第一操作數(shù)n1(16位有符號(hào))賦給MPYS寄存器,第二操作數(shù)n2(16位有符號(hào))賦給OP2寄存器,賦值完成后,結(jié)果result(32位有符號(hào))就可以在RESLO(低16位)、RESHI(高165位)和SUMEXT(符號(hào))中讀取。SUMEXT=0xffff則結(jié)果為負(fù),SUMEXT=0則結(jié)果為正。
代碼如下:
MPYS = n1; // Load first operand -signed mult
OP2 = n2; // Load second operand
result = RESHI; // Load RESHI word result
result = (result<<16)|RESLO; // Shift RESHI left and concat with
4.有符號(hào)乘加操作 result="n1"*n2+n3*n4。第一操作數(shù)n1(16位無(wú)符號(hào))賦給MPY寄存器,第二操作數(shù)n2(16位無(wú)符號(hào))賦給OP2寄存器,第三操作數(shù)n3(16位有符號(hào))賦給MAC寄存器,第四操作數(shù)n4(16位有符號(hào))賦給OP2寄存器,結(jié)果result(32位有符號(hào))就可以在RESLO(低16位)和RESHI(高165位)中讀取。SUMEXT寄存器中保存進(jìn)位和正負(fù)標(biāo)志。
代碼如下:
MPY = n1; // Load first operand -unsigned mult
OP2 = n2; // Load second operand
MACS = n3;
OP2 = n4;
result = RESHI; // Load RESHI word result
result = (result<<16)|RESLO; // Shift RESHI left and concat with
·32位乘法器使用方法:
1.無(wú)符號(hào)相乘 result="n1"*n2。n1(32位無(wú)符號(hào))低16位賦給MPY32L寄存器,n1高16位賦給MPY32H寄存器,n2(32位無(wú)符號(hào))低16位賦給OP2L寄存器,n2高16位賦給OP2H寄存器,賦值完成后,結(jié)果result(64位無(wú)符號(hào))就可以在RES0(低位), RES1, RES2 和 RES3(高位)中讀取。
代碼如下:
MPY32L = 0x1234; // Load lower 16 bits of operand 1
MPY32H = 0x1234; // Load upper 16 bits of operand 1
OP2L = 0x5678; // Load lower 16 bits of operand 2
OP2H = 0x5678; // Load upper 16 bits of operand 2
result_L = RES1;
result_L = (result_L<<16)|RES0;
result_H = RES3;
result_H = (result_H<<16)|RES2;
其他情況依次類(lèi)推,與16位情況類(lèi)似。
定點(diǎn)小數(shù)部分還有待研究~~
評(píng)論