使用C
example at the bottom here,我正在实现一个必须在单独的源文件中实现的功能。
__restrict__
?如果声明(test.hpp
) __restrict__
,但实施(test.cpp
)不,生成的程序集会将其视为__restrict__
永远不存在。我是否应该将其视为“像const
” - 它直接描述了类型?__restrict__
尚未正式标准化,因此可能无法做到这一点。但是能够捕捉这种情况会很好。我想我发现它有点令人不安,因为不是编译错误。__restrict__
IS声明(test.hpp
),实施__restrict__
中的否test.cpp
(USE_RESTRICT
在下面的测试代码中为0
)< / H3>
__foo:
pushq %rbp
movq %rsp, %rbp
movl $5, (%rdi)
movl $6, (%rsi)
movl (%rdi), %eax
addl $6, %eax
popq %rbp
retq
nopw (%rax,%rax)
__rfoo:
pushq %rbp # v
movq %rsp, %rbp # v
movl $5, (%rdi) # v
movl $6, (%rsi) # v
movl (%rdi), %eax # v
addl $6, %eax ### <<< this one here!
popq %rbp
retq
__restrict__
中test.cpp
USE_RESTRICT
1
下面的测试代码为__foo:
pushq %rbp
movq %rsp, %rbp
movl $5, (%rdi)
movl $6, (%rsi)
movl (%rdi), %eax
addl $6, %eax
popq %rbp
retq
nopw (%rax,%rax)
__rfoo:
pushq %rbp
movq %rsp, %rbp
movl $5, (%rdi)
movl $6, (%rsi)
movl $11, %eax
popq %rbp
retq
test.cpp
在下面的示例代码中,在USE_RESTRICT
中,切换clang
以获得不同的结果(至少对我来说是test.hpp
)。
#pragma once
#if defined(__GNUC__) || defined(__clang__)
#define RESTRICT __restrict__
#elif defined(_MSC_VER)
#define RESTRICT __restrict
#else
#define RESTRICT /* does nothing */
#endif
// http://en.cppreference.com/w/c/language/restrict
extern int foo(int *a, int *b);
extern int rfoo(int * RESTRICT a, int * RESTRICT b);
test.cpp
#include "test.hpp"
// set to 0 to undo restrict
#define USE_RESTRICT 1
#if USE_RESTRICT
#define IMPL_RESTRICT RESTRICT
#else
#define IMPL_RESTRICT /* does nothing */
#endif
// http://en.cppreference.com/w/c/language/restrict
int foo(int *a, int *b) {
*a = 5;
*b = 6;
return *a + *b;
}
int rfoo(int * IMPL_RESTRICT a, int * IMPL_RESTRICT b) {
*a = 5;
*b = 6;
return *a + *b;
}
main.cpp
#include "test.hpp"
int main(void) {
int a, b;
int x = foo(&a, &b);
int y = rfoo(&a, &b);
return x + y;// just making sure the compiler doesn't optimize away
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.1.3 FATAL_ERROR)
project("restrict_test")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "-O3 ${CMAKE_CXX_FLAGS}")
add_executable(restrict-test test.hpp test.cpp main.cpp)
<Route exact path="/champions" render={(props) => <ChampionsLists {...props} />} />
<Route path="/champions/select/:champName" render={(props) => <SelectedChamp {...props} />} />