本文介绍了标准C ++ 11代码相当于PEXT Haswell指令(可能由编译器优化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Haswell架构提供了几个新指令。其中一个是 PEXT ( 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: PEXTtemplate <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指令(可能由编译器优化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 05:34