标准C ++ 11 code相当于PEXT的Haswell指令(并可能被编译器优化) [英] Standard C++11 code equivalent to the PEXT Haswell instruction (and likely to be optimized by compiler)

查看:304
本文介绍了标准C ++ 11 code相当于PEXT的Haswell指令(并可能被编译器优化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Haswell的结构出现了几个新的指令。其中之一是 PEXT 并行位提取),其功能通过这一形象(来源这里)解释说:

The Haswell architectures comes up with several new instructions. One of them is PEXT (parallel bits extract) whose functionality is explained by this image (source here):

这需要一个值 R2 和掩码 R3 ,并把的提取位R2 R1

It takes a value r2 and a mask r3 and puts the extracted bits of r2 into r1.

我的问题是:这将是一个优化的模板功能的纯标准 C ++ 11,这很可能会进行优化,以通过编译器该指令相当于code在未来的。

My question is the following: what would be the equivalent code of an optimized templated function in pure standard C++11, that would be likely to be optimized to this instruction by compilers in the future.

推荐答案

下面是的马太福音Fioravante的 href=\"https://github.com/fmatthew5876/stdcxx-bitops\" rel=\"nofollow\"> stdcxx-bitops GitHub库这是浮动到 STD-建议 邮件列表为preliminary建议增加一个 constexpr 位运算库,用于C ++。

Here is some code from Matthew Fioravante's stdcxx-bitops GitHub repo that was floated to the std-proposals mailinglist as a preliminary proposal to add a constexpr bitwise operations library for C++.

#ifndef HAS_CXX14_CONSTEXPR
#define HAS_CXX14_CONSTEXPR 0
#endif

#if HAS_CXX14_CONSTEXPR
#define constexpr14 constexpr
#else
#define constexpr14
#endif

//Parallel Bits Extract
//x    HGFEDCBA
//mask 01100100
//res  00000GFC
//x86_64 BMI2: PEXT
template <typename Integral>
constexpr14 Integral extract_bits(Integral x, Integral mask) {
  Integral res = 0;
  for(Integral bb = 1; mask != 0; bb += bb) {
    if(x & mask & -mask) {
      res |= bb;
    }
    mask &= (mask - 1);
  }
  return res;
}

这篇关于标准C ++ 11 code相当于PEXT的Haswell指令(并可能被编译器优化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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