- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以通过使用查找表找到 16 位值的第 n 个设置位,但是对于 32 位值,如果不将其分解并使用多个 LUT,则无法完成此操作。这在How to efficiently find the n-th set bit?中进行了讨论。这表明与此类似的用途
#include <immintrin.h>
//...
unsigned i = 0x6B5; // 11010110101
unsigned n = 4; // ^
unsigned j = _pdep_u32(1u << n, i);
int bitnum = __builtin_ctz(j)); // result is bit 7
我已经介绍了这个建议的循环方法
j = i;
for (k = 0; k < n; k++)
j &= j - 1;
return (__builtin_ctz(j));
针对 Nominal Animal 的答案中的位旋转代码的两种变体。旋转的分支变体是三个代码版本中最快的,但相差不大。然而,在我的实际代码中,使用__builtin_ctz
的上述代码段速度更快。还有其他答案,例如来自 fuz谁也建议了我的发现:位旋转与循环方法花费的时间相似。
所以我现在想尝试使用 _pdep_u32
但无法识别 _pdep_u32
。我读到gcc.gnu.org
The following built-in functions are available when
-mbmi2
is used.
...
unsigned int _pdep_u32 (unsigned int, unsigned int)
unsigned long long _pdep_u64 (unsigned long long, unsigned long long)
...
但是我使用的是在线编译器,无法访问这些选项。
有没有办法启用预处理器的选项?
有很多关于从命令行选项控制预处理器的 Material ,但我找不到如何反过来做。
最终我想使用 64 位版本 _pdep_u64
。
最佳答案
除了内联 asm 之外,gcc/clang 绝不会发出未通过命令行选项、函数属性或编译指示启用的 asm 指令。
如果您无法像普通人一样在命令行上使用 GCC 目标选项(例如 -march=native
启用编译主机支持的所有内容,并调整对于该机器),您可以改为使用 a pragma设置特定于目标的选项。但它的效果并不是很好;看来#pragma target("arch=skylake")
打破了 GCC 的 immintrin.h
(如果您将编译指示放在包含之前),显然是尝试在未启用 AVX512 的情况下编译 AVX512 函数。 The GCC docs do show examples of using "arch=core2"
作为函数属性,甚至 "sse4.1,arch=core2"
您可以设置target("bmi2,tune=skylake")
但不会破坏任何内容(在 #include <immintrin.h>
之前或之后。(与 arch=
不同, tune=
不启用 ISA 扩展,只会影响代码生成选择。)
#include <immintrin.h>
// #pragma GCC target ("arch=haswell") // also broken, disables BMI2??
#pragma GCC target ("bmi2,tune=skylake") // this can be after immintrin.h
int foo(unsigned x) {
// unsigned i = 0x6B5; // 11010110101
unsigned i = x;
unsigned n = 4; // ^
unsigned j = _pdep_u32(1u << n, i);
int bitnum = __builtin_ctz(j); // result is bit 7
return bitnum;
}
int constprop() { // check that GCC can still "understand" the intrinsic
return foo(0x6B5);
}
<强> compiles cleanly on Godbolt 仅 -O3
,否-m
选项。
foo:
mov r8d, edi
mov edi, 16
pdep edi, edi, r8d
bsf eax, edi
ret
constprop:
mov eax, 7
ret
<小时/>
如果您在获取immintrin.h
时遇到困难要定义正确的包装器,您可以查看 GCC 的 header 以了解它们是如何定义的。例如_pdep_u32
是 __builtin_ia32_pdep_si
的包装(单个整数)并且 u64 是 __builtin_ia32_pdep_di
的包装器(双整数)。
这些__builtin
即使没有 header ,函数也始终由 GCC 定义,但这些 ia32
内置函数只能在具有兼容目标选项的函数中使用。没有像 __builtin_popcount
这样的后备方案对于不支持该指令的目标。
但似乎至少对于 BMI2 指令来说,_pdep_u32
和_pdep_u64
由 GCC 的 immintrin.h
定义无论命令行选项如何(这将定义 __BMI2__
CPP 宏)。也许原因是这个用例:具有目标属性的单个函数,或具有编译指示的函数 block 。
_pdep_u64
版本需要 BMI2 + x86-64,而 32 位版本仅需要 BMI2(即也可用于 32 位模式)。 pdep
is a scalar integer instruction因此 64 位操作数大小需要 64 位模式。
Aside: MSVC compiled the instrinsic without fuss
是的,MSVC 和 GCC/clang 对于内在函数有不同的理念。 MSVC 允许您使用任何内容,前提是您将进行运行时调度以确保执行永远不会到达 native 不支持的内部函数。
这可能与 MSVC 本质上根本不优化内在函数有关,通常甚至不通过它们进行简单的常量传播。我猜想,因为它试图确保当 C++ 抽象机中未达到内在函数时,相应的 asm 指令永远不会执行,并采取缓慢而安全的方法。
but the program crashed (I suppose
_pdep_u32
is not supported by my cpu as suggested in the linked question).
是的,并非所有 CPU 都有 BMI2。您对 GCC 所做的任何事情都不会改变这一点。
关于c - 如何从预处理器启用内在函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590790/
我正在阅读哈德利的 Advanced R并尝试一些东西。我正在尝试创建一个 lazy闭包函数返回一个带有提供的函数 data.frame在其环境中以及使用 with并且能够在以后提供额外的函数参数。
我有两个 ViewController。初始 ViewController 是输入和存储 URL 的地方。此初始 ViewController 的 viewDidLoad 还应该在应用程序启动时开始加
你是怎么用的 对于应用程序中的 ListView 项也应该在设备 <11 上运行? 由于 activated_state 在 HC 之前不可用,我只能想到两个肮脏的解决方法: 在您的 Activit
我正在为 android (2.1 > 3.1) 编写一个应用程序,我想使用熟悉的做法,即在 Honeycomb 应用程序中使用应用程序图标来进入家庭 Activity ,但是,当我之前运行该 Act
如果搜索的键不存在,我如何覆盖方法 GET 或编写一个将在服务器端执行的新函数返回另一个键值? 示例: 如果关键字“word_1 word_2 word_3 word_4”不存在则搜索关键字“word
对于我的存储库,我使用的是 Git 和 Stash。在 Stash 端,我限制了(只读)对 master 的访问权限,因此任何用户都可以从 master 分支分支以获取功能/分支,但不能直接 merg
如何配置dgrid及其存储以定义渲染行时是否已经选择了行? 例如,如果我的行数据是这样的: { id: 1, name: 'Item Name', selected: true } 我当前
有没有一种方法可以将变量从一个 BeanShell 前/后处理器引用到另一个 BeanShell 处理器(它们在同一个线程组中)? 如果我在 HTTP 请求下的 BeanShell 预处理器中创建了一
问题 我已尝试添加预操作 shell 脚本,这些脚本会根据我正在构建的内容打开/关闭我的 .pch 文件中的某些定义。 但是,在运行构建时,没有任何反应。我不是一个流利的 shell 脚本编写者,所以
我有一个 HTML 字符串用作 jQuery 输入文档。 // the variable html contains the HTML code jQuery( html ).find( 'p' ).
在 Mercurial 中允许 merge 之前有没有办法进行一些检查? 通过将以下内容添加到 ~/.hg/hgrc,我找到了更新前 Hook ,并拥有一个在允许更新之前运行的脚本: [hooks]
总结: 预 Controller Hook 是否在缓存期间执行?是否有任何 Hook 点可以执行? (系统前?) 我应该强调一个事实,即 Hook 不会影响发送到浏览器的内容。这不是问题。 详细版:
我正在使用适用于 android 的 Skobbler Map API,到目前为止它一直非常好。按照官方的“操作方法”,我已经能够将 map 应用到我的应用程序中。比我可以让应用程序下载 map 并离
当我安装bcrypt时我的 hapi js 项目的模块尚未安装,它显示类似 node-pre-gyp install --fallback-to-build 我尝试通过运行来安装; npm i nod
我试图使用此代码的变体: apply plugin: 'java' apply plugin: 'idea' idea.workspace.iws.withXml { provider ->
假设我们有一个 PHP 项目,其依赖项 A 和 B 分别依赖于 PHP 库 X,但版本不同。 通常,人们会使用诸如 composer 之类的 PHP 依赖管理器,它可以通过在与 A 和 B 兼容的版本
这似乎违背了代码块的目的,但我希望能够在代码块中加粗。例如,如果我想将返回行加粗: int main(void) { **return 0;** } 最佳答案 您必须在 HTML 中执行此操作
我们是否应该使用 Huggingface(预)训练一个 BERT 无框模型的小写输入数据?我查看了 Thomas Wolf ( https://github.com/huggingface/trans
我有两个模式: 技能: var mongoose = require("mongoose"); var SkillSchema = new mongoose.Schema({ skill: {
我这里有问题。这适用于 Chrome,但我无法在 IE11 的 index.html 中使用任何动画。当它不想工作时,我会看到一个静态屏幕。同样在 IE 中,消息不会像它应该的那样消失。如果我将 di
我是一名优秀的程序员,十分优秀!