简体   繁体   English

用gcc生成BZHI指令

[英]generate BZHI instruction with gcc

I'm trying to make gcc generate the bzhi instruction , part of BMI2, without using intrinsics, in order to create a portable code. 我试图让gcc生成bzhi指令 ,BMI2的一部分, 使用内在函数,以便创建一个可移植的代码。

Given the outcome of bzhi , I expected that objective to be relatively accessible. 鉴于bzhi的结果,我预计这个目标是相对容易获得的。 The following SO answer provides a code example, simplified below : 以下SO答案提供了一个代码示例,简化如下:

unsigned bzhi32(unsigned value, int nbBits)
{
    return value & ((1u << nbBits) - 1);
}

clang has no problem generating bzhi instruction with it, while I haven't found any similar outcome for gcc so far : https://godbolt.org/g/jYrh8F clang使用它生成bzhi指令没有问题,而到目前为止我还没有找到任何类似的gcc结果: https//godbolt.org/g/jYrh8F

I was wondering if this was possible. 我想知道这是否可行。 This capability was at least requested , but not sure if it was completed. 至少要求此功能,但不确定它是否已完成。 If it was, maybe there are just some subtle issues in the code snippet, for example with type or properties, which could be fixed to succeed this transformation with gcc . 如果是,可能在代码片段中只有一些微妙的问题,例如类型或属性,可以修复以使用gcc成功完成此转换。

edit : added u for constant, as suggested by @chux. 编辑 :按照@chux的建议添加u作为常量。 It marginally changes the outcome for gcc , though it's still a 4-instructions function without bzhi . 它略微改变了gcc的结果,尽管它仍然是一个没有bzhi的4指令函数。

This optimization is not implemented in gcc as of January 2018 (there is a feature request ). 自2018年1月起,gcc中未实现此优化(有功能请求 )。 You can get the instruction by using intrinsics: 您可以使用内在函数获取指令:

#include <x86intrin.h>

unsigned bzhi32(unsigned value, int nbBits) {
   return _bzhi_u32(value, nbBits);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM