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

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

问题描述

Haswell架构提供了几个新指令。其中一个是 PEXT ( < a href =https://github.com/fmatthew5876/stdcxx-bitops =nofollow> stdcxx-bitops GitHub repo 浮动到 std-proposals mailinglist作为初步建议,为C ++添加 constexpr 位运算库。

  #ifndef HAS_CXX14_CONSTEXPR 
#define HAS_CXX14_CONSTEXPR 0
#endif

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

//平行位提取
// x HGFEDCBA
//掩码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;
}


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):

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

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.

解决方案

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代码相当于PEXT Haswell指令(可能由编译器优化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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