按位操作浮点有用 [英] Bitwise operation on a floating point usefulness
问题描述
这个想法给我留下了深刻的印象,如果你按位或者浮点数组的元素,如果它们中的任何一个通过查看结果的符号位而快速消失的话。
对浮点值按位运算还有其他什么用途?
很多。例如,当你只需要在像AVX这样的浮点指令集上进行按位操作时,那么这些操作就变得非常方便了。另外一个应用:制作常量。您可以在表13.10和13.11中的 Agner Fog的x86平台优化指南。一些例子:
pcmpeqd xmm0,xmm0
psrld xmm0,30; 3(32位)
pcmpeqd xmm0,xmm0; -1
pcmpeqw xmm0,xmm0; 1.5f
pslld xmm0,24
psrld xmm0,2
pcmpeqw xmm0,xmm0; -2.0f
pslld xmm0,30
您也可以使用它来检查浮动点值是2的幂。
其他一些应用程序,如哈罗德说:取绝对值和减绝对值,复制符号,muxing ...我'为了便于理解,我们将演示一个单一的数据。 〜(1U <31);
// Muxing
v =(x& mask)| (y&〜mask); // v =掩码? x:y; (mask)= 0或-1
//复制符号
y =(y&〜(1U << 31))| (x&(1U< 31));
I noticed a SSE instruction existed for floating point AND which got me wondering. You can do the same thing with scalars in fp/integer union.
The idea struck me that, if you bitwise OR the components of an array of floats, you can determine if any of them are negative quickly by looking at the sign bit of the result.
What other uses exist for bitwise operations on floating point values?
A lot. For example when you only need to do bitwise operations on a floating-point only instruction set like AVX, then those become very handy.
Another application: making constants. You can see a lot of examples in table 13.10 and 13.11 in Agner Fog's optimization guide for x86 platforms. Some examples:
pcmpeqd xmm0, xmm0
psrld xmm0, 30 ; 3 (32-bit)
pcmpeqd xmm0, xmm0 ; -1
pcmpeqw xmm0, xmm0 ; 1.5f
pslld xmm0, 24
psrld xmm0, 2
pcmpeqw xmm0, xmm0 ; -2.0f
pslld xmm0, 30
You can also use that for checking if the floating-point value is a power of 2 or not.
Some other applications like Harold said: Taking absolute value and minus absolute value, copy sign, muxing... I'll demonstrate in a single data for easier understanding
// Absolute:
abs = x & ~(1U << 31);
// Muxing
v = (x & mask) | (y & ~mask); // v = mask ? x : y; with mask = 0 or -1
// Copy sign
y = (y & ~(1U << 31)) | (x & (1U << 31));
这篇关于按位操作浮点有用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!