If you have a full 32-bit number and you need to divide, you can simply do a multiply and take the top 32-bit half as the result. This is faster because multiplication is faster than division. ( thanks to pdixon for the tip). 如果你有一個(gè)滿32-bit(full 32-bit)的數(shù)要做除法,你可以簡單地做乘法,然后取高32-bit部分作為結(jié)果。這樣會快些,因?yàn)槌朔ū瘸欤?感謝pdixon提供了這個(gè)tip)(譯者注:由于水平有限,此tip翻譯尚待商討,而且不能給出一個(gè)例子。還仰仗各位幫忙。)
<12>被常數(shù)除
這兒有一些很好的信息----怎樣被常數(shù)除(在Agner Fog的pentopt.pdf中)。我(Mark Larson)寫了一個(gè)小程序:可以根據(jù)你輸入的除數(shù)自動產(chǎn)生匯編代碼序列。我會繼續(xù)探究探究,然后貼出來。這是Agner的文檔的鏈接。Agner's Pentopt PDF (http://www.agner.org/assem/)
在P4上,MMS/SSE/SSE2指令的延遲那么長以至于我總是每個(gè)循環(huán)處理2個(gè)事件或者提前讀取一個(gè)循環(huán)。如果你有足夠的寄存器,可以多于2個(gè)事件。所有的各種各樣的MOVE(包括MOVD)指令在P4上的速度都慢。所以2個(gè)32-bit的數(shù)字?jǐn)?shù)組相加運(yùn)算在P4上比P3上還慢。一個(gè)快點(diǎn)兒的方法可能就是每個(gè)循環(huán)(這個(gè)循環(huán)在FRED標(biāo)號之前預(yù)讀循環(huán)初始值MM0和MM1)處理兩個(gè)事件。你必須做的只是在數(shù)組元素個(gè)數(shù)為奇時(shí)進(jìn)行特殊的處理;在最后檢查一下,如果為奇數(shù),加一個(gè)額外的dword。這兒有個(gè)并沒有提前讀取值的代碼段。我想,把它改為提前讀取值是很容易的,所以我沒有兩個(gè)都貼出。下面的代碼可以:在P4機(jī)上避免ADC這個(gè)速度慢的指令來把兩個(gè)數(shù)組相加。 pxor mm7,mm7 ; the previous loops carry stays in here fred: movd mm0,[esi] ; esi points to src1 movd mm1,[edi] ; edi points to src2, also to be used as the destination paddq mm0,mm1 ; add both values together paddq mm0,mm7 ; add in remainder from last add movd [edi],mm0 ; save value to memory movq mm7,mm0 psrlq mm7,32 ; shift the carry over to bit 0 add esi,8 add edi,8 sub ecx,1 jnz fred movd [edi],mm7 ; save carry