avx浮点按位逻辑运算的原因是什么? [英] Which is the reason for avx floating point bitwise logical operations?

查看:242
本文介绍了avx浮点按位逻辑运算的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AVX允许对诸如和/或对浮点数据类型__m256和__m256d进行按位逻辑运算.

AVX allow for bitwise logical operations such as and/or on floating point data-type __m256 and __m256d.

但是,C ++合理地不允许对浮点数和双精度数进行按位运算.如果我是正确的话,就不能保证浮点数的内部表示形式,也不能保证编译器是否使用IEEE754,因此程序员无法确定浮点数的样子.

However, C++ doesn't allow for bitwise operations on floats and doubles, reasonably. If I'm right, there's no guarantee on the internal representation of floats, whether the compiler will use IEEE754 or not, hence a programmer can't be sure about how the bits of a float will look like.

请考虑以下示例:

#include <immintrin.h>
#include <iostream>
#include <limits>
#include <cassert>

int main() {

    float x[8] = {1,2,3,4,5,6,7,8};
    float mask[8] = {-1,0,0,-1,0,-1,0,0};
    float x_masked[8];

    assert(std::numeric_limits<float>::is_iec559);

    __m256 x_ = _mm256_load_ps(x);
    __m256 mask_ = _mm256_load_ps(mask);

    __m256 x_masked_ = _mm256_and_ps(x_,mask_);

    _mm256_store_ps(x_masked,x_masked_);

    for(int i = 0; i < 8; i++)
        std::cout << x_masked[i] << " ";

    return 0;
}

假设使用IEEE754,因为-1的表示形式为0xffffffff,所以我希望输出为

Assuming that IEEE754 is used, as the representation of -1 is 0xffffffff, I would expect the output to be

1,0,0,4,0,6,0,0

相反,

1 0 0 1.17549e-38 0 1.17549e-38 0 0

因此,我对内部表示形式的假设可能是错误的(或者我犯了一些愚蠢的错误).

Hence my assumption about the internal representation was probably wrong (or I made some silly mistake).

所以问题是:有没有一种方法可以使用浮点逻辑并且对结果有意义的事实感到安全?

So the question is: is there a way in which I can use floating point logical and be safe about the fact that the result will make sense?

推荐答案

如果您使用的是AVX内在函数,那么您就知道您正在使用IEEE754浮点数,因为这就是AVX的作用.

If you're using AVX intrinsics, then you know you're using IEEE754 floats, because that's what AVX does.

对浮点数有意义的一些按位运算是

Some of the bitwise operations on floats that make sense are

  • 选择,就像Jens的回答一样,尽管从SSE4.1开始,我们有blendvps及其亲戚在一条指令中做到这一点
  • 绝对值(遮盖符号)
  • 取反(与-0.0f进行异或运算)
  • 转让标志
  • 提取指数(稀有)
  • selecting, as in Jens' answer, though as of SSE4.1 we have blendvps and its relatives to do that in one instruction
  • absolute value (mask away the sign)
  • negate (xor with -0.0f)
  • transfer sign
  • extracting the exponent (rare)

主要用于操纵符号,或有选择地将整个浮点数清零,而不是为了处理指数或有效位数的单个部分而已-您可以做到这一点,但这很少有用.

Mostly it's for manipulating the sign, or to selectively zero out whole floats, not so much for mucking about with individual bits of the exponent or significand - you can do it, but it's rarely useful.

这篇关于avx浮点按位逻辑运算的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆