Intel 的编译器有一个编译指示,可用于生成非临时存储。例如,我可以写
void square(const double* x, double* y, int n) {
#pragma vector nontemporal
for (int i=0; i<n; ++i) {
y[i] = x[i] * x[i];
}
}
Run Code Online (Sandbox Code Playgroud)
和ICC会产生这样的指令这样(编译器,资源管理器)
...
vmovntpd %ymm1, (%rsi,%r9,8) #4.5
...
Run Code Online (Sandbox Code Playgroud)
gcc 和 clang 有什么类似的吗?(内在函数除外)
非临时存储使代码更快。使用这个基准
...
vmovntpd %ymm1, (%rsi,%r9,8) #4.5
...
Run Code Online (Sandbox Code Playgroud)
非时间代码在我的机器上运行速度几乎是它的两倍。以下是完整结果:
国际刑事法院:
> icc -O3 -march=native -std=c++11 benchmark.cpp -lbenchmark -lbenchmark_main
> ./a.out
------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------
BM_Square/1000000 430889 ns 430889 ns 1372
Run Code Online (Sandbox Code Playgroud)
铛:
> clang++ -O3 -march=native -std=c++11 benchmark.cpp …Run Code Online (Sandbox Code Playgroud) 以下是合法的表达式吗?
template <std::size_t N, std::size_t... Ix>
bool in_range(std::index_sequence<Ix...>) {
return ((Ix < N) && ...);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将一般要求子句应用于lambda仿函数的参数?
假设我有两个约束条件C1,并C2说我要对参数进行检查.我希望以下内容能够正常工作,因为函数允许使用类似的语法:
[](auto x) requires C1<decltype(x)> && C2<decltype(x)> {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我希望可以编写这样的类
template <class T>
struct A {
T& operator*()
requires (!std::is_void_v<T>)
{
return *ptr;
}
T* ptr;
};
Run Code Online (Sandbox Code Playgroud)
但是如果我写
A<void> a;
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误
prog.cc: In instantiation of 'struct A<void>':
prog.cc:16:13: required from here
prog.cc:5:8: error: forming reference to void
5 | T& operator*()
| ^~~~~~~~
Run Code Online (Sandbox Code Playgroud)
即使 requires 子句禁用了该功能。
有没有办法编写类,以便编译器可以接受禁用的方法?
(我知道我可以将 A 部分专用于 void ,但这不太方便)。
例如,我可以定义一个概念
template <class Iter>
concept bool Iterator =
requires(Iter i, typename std::iterator_traits<Iter>::value_type val,
typename std::iterator_traits<Iter>::reference ref) {
++i;
// other implementation
};
Run Code Online (Sandbox Code Playgroud)
与海湾合作委员会6这段代码编译,而是要像Iterator<int>也将导致对true即使val和ref会替换故障.这是它的假设吗?
考虑以下代码:
#include <type_traits>
#include <iostream>
template <class T> concept bool C1 = std::is_same<T, int>::value;
template <class T> concept bool C2 =
C1<decltype(std::declval<T>() + std::declval<T>())>;
struct A {};
int main() {
std::cout << C2<int>;
std::cout << C2<A>;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC编译得很好并打印10.
但§14.10.1.2谓词约束[temp.constr.pred]的N4553说
谓词约束是一个约束,用于计算常量表达式E(5.19).
然后
替换后,E应具有bool类型.
由于C1<decltype(std::declval<A>() + std::declval<A>())>替换失败,而不是类型bool,这是否意味着该程序应该是格式错误的?
你怎么能告诉 bazel 在 OS X 上使用不同的 C++ 编译器?
bazel build --action_env CC=/path/to/compiler //:target
Run Code Online (Sandbox Code Playgroud)
在 linux 上工作。
但是-s表明external/local_config_cc/wrapped_clang无论是什么,bazel 总是在 OSX 上运行(clang) CC。
Bazel 不直接支持模块(请参阅问题 #4005)。
但是,可以为 bazel 提供定制的 CROSSTOOL。
来自https://docs.bazel.build/versions/0.22.0/crosstool-reference.html:
默认情况下,Bazel 会自动为您的构建配置 CROSSTOOL,但您可以选择手动配置它。
并且可以使用自定义规则扩展 bazel。
来自https://docs.bazel.build/versions/master/skylark/rules.html:
Bazel 本身内置了一些规则。这些原生规则,例如 cc_library 和 java_binary,为某些语言提供了一些核心支持。通过定义自己的规则,您可以为 Bazel 本身不支持的语言和工具添加类似的支持。
关于Bazel 模块问题的评论表明,即使没有本机支持,您也可以使用自定义 CROSSTOOL 来支持模块:
有关模块(仅适用于 clang)的所有内容都已经开源。唯一缺少的部分是使用它们并提供所有必要功能的 CROSSTOOL。
任何人都可以展示如何为 clang 编写自定义 CROSSTOOL 以及如何使用它为模块编写自定义 C++ 规则(例如cc_module),以便您可以执行以下操作:
编写一个基本模块
// helloworld.cc
module;
#include <stdio.h>
export module helloworld;
export void hello();
module :private;
void hello() { puts("Hello world!"); }
Run Code Online (Sandbox Code Playgroud)
使用模块
// main.cc
import helloworld;
int main() { hello(); }
Run Code Online (Sandbox Code Playgroud)
将零件集成到构建系统中
cc_module(
name = "helloworld",
srcs …Run Code Online (Sandbox Code Playgroud) 像这样有效的 C++20 代码吗?
#include <iostream>
template <typename T>
concept impls_decrement = requires(T it) { it.decrement(); };
template <class Derived>
struct iterator_facade {
Derived& operator--()
requires impls_decrement<Derived>
{
auto& self = static_cast<Derived&>(*this);
self.decrement();
return self;
}
};
struct my_iterator : iterator_facade<my_iterator> {
void decrement() {
std::cout << "decrement" << std::endl;
}
};
int main() {
my_iterator iter;
--iter;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
改编自vector-of-bool博客文章。
使用最新版本的 gcc,代码工作正常,但最新版本的 clang 出现此错误:
prog.cc:25:4: error: cannot decrement value of type 'my_iterator'
--iter; …Run Code Online (Sandbox Code Playgroud) 给定一个模块
// a-m.cc
export module A;
import B;
import C;
import "D.h";
...
Run Code Online (Sandbox Code Playgroud)
有没有一种调用 gcc 的方法(类似于 -M 对标头所做的操作)来列出对其他模块和导入标头(即 B、C 和“Dh”)的直接依赖项?