的的CryENGINE SDK頭的這部分引起了我的注意:在CryENGINE 3中,分支掩膜如何工作?
branchmask.h
#ifndef __BRANCHLESS_MASK__
#define __BRANCHLESS_MASK__
///////////////////////////////////////////
// helper functions for branch elimination
//
// msb/lsb - most/less significant byte
//
// mask - 0xFFFFFFFF
// nz - not zero
// zr - is zero
ILINE const uint32 nz2msb(const uint32 x)
{
return -(int32)x | x;
}
ILINE const uint32 msb2mask(const uint32 x)
{
return (int32)(x) >> 31;
}
ILINE const uint32 nz2one(const uint32 x)
{
return nz2msb(x) >> 31; // int((bool)x);
}
ILINE const uint32 nz2mask(const uint32 x)
{
return (int32)msb2mask(nz2msb(x)); // -(int32)(bool)x;
}
ILINE const uint32 iselmask(const uint32 mask, uint32 x, const uint32 y)// select integer with mask (0xFFFFFFFF or 0x0 only!!!)
{
return (x & mask) | (y & ~mask);
}
ILINE const uint32 mask_nz_nz(const uint32 x, const uint32 y)// mask if(x != 0 && y != 0)
{
return msb2mask(nz2msb(x) & nz2msb(y));
}
ILINE const uint32 mask_nz_zr(const uint32 x, const uint32 y)// mask if(x != 0 && y == 0)
{
return msb2mask(nz2msb(x) & ~nz2msb(y));
}
ILINE const uint32 mask_zr_zr(const uint32 x, const uint32 y)// mask if(x == 0 && y == 0)
{
return ~nz2mask(x | y);
}
#endif//__BRANCHLESS_MASK__
可能有人扔了簡短的解釋究竟如何打算用來減少分支這些功能呢? ILINE我想是預先定義的內聯或類似的東西。我向Google搜索了這個消息,但是我發現的所有消息都是在不同網站上傳的CryENGINE頭文件的副本,但沒有討論這個具體問題。
與更好的分支預測我猜 – user1849534
@didierc或者更確切地說,這些例子完全刪除分支。 – Mysticial