在函数的实现签名中是否需要__restrict__?

时间:2018-01-23 09:31:02

标签: c++11 disassembly

使用C example at the bottom here,我正在实现一个必须在单独的源文件中实现的功能。

  1. 为什么我需要在实施文件的签名中使用__restrict__如果声明test.hpp __restrict__,但实施(test.cpp,生成的程序集会将其视为__restrict__永远不存在。我是否应该将其视为“像const” - 它直接描述了类型?
  2. 有没有办法让这个编译错误?我知道__restrict__尚未正式标准化,因此可能无法做到这一点。但是能够捕捉这种情况会很好。我想我发现它有点令人不安,因为不是编译错误。
  3. 生成的程序集

    __restrict__ IS声明(test.hpp),实施__restrict__中的否test.cppUSE_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} />} />
    

0 个答案:

没有答案