读取(简单)程序集以查找使用C++模板产生的编译时计算

Esc*_*alo 2 c++ assembly fortran90

我正在比较Fortran 90和C++进行演示.我的一个比较依赖于通过简单的程序产生的装配g++gfortran.

一个例子如下:

#include<cstdio> // quick and dirty number formatting

template<int N>
double dot(double x[], double y[]){
  return x[N-1] * y[N-1] + dot<N-1>(x, y);
}

template<>
double dot<1>(double x[], double y[]){
  return x[0] * y[0];
}

int main(){
  double x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  double y[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  printf("x.y = %23.16E\n", dot<10>(x, y));
}
Run Code Online (Sandbox Code Playgroud)

g++ -S -O3 myprogram.cpp在OS X 10.7.4 x86_64-apple-darwin11.4.2上使用g ++ 4.7.2 的命令生成以下汇编程序:

    .text
    .align 4,0x90
    .globl __Z3dotILi1EEdPdS0_
__Z3dotILi1EEdPdS0_:
LFB2:
    movsd   (%rdi), %xmm0
    mulsd   (%rsi), %xmm0
    ret
LFE2:
    .cstring
LC1:
    .ascii "x.y = %23.16E\12\0"
    .section __TEXT,__text_startup,regular,pure_instructions
    .align 4
    .globl _main
_main:
LFB3:
    leaq    LC1(%rip), %rdi
    subq    $8, %rsp
LCFI0:
    movl    $1, %eax
    movsd   LC0(%rip), %xmm0
    call    _printf
    xorl    %eax, %eax
    addq    $8, %rsp
LCFI1:
    ret
LFE3:
    .literal8
    .align 3
LC0:
    .long   0
    .long   1081610240
    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
    .set L$set$0,LECIE1-LSCIE1
    .long L$set$0
LSCIE1:
    .long   0
    .byte   0x1
    .ascii "zR\0"
    .byte   0x1
    .byte   0x78
    .byte   0x10
    .byte   0x1
    .byte   0x10
    .byte   0xc
    .byte   0x7
    .byte   0x8
    .byte   0x90
    .byte   0x1
    .align 3
LECIE1:
LSFDE1:
    .set L$set$1,LEFDE1-LASFDE1
    .long L$set$1
LASFDE1:
    .long   LASFDE1-EH_frame1
    .quad   LFB2-.
    .set L$set$2,LFE2-LFB2
    .quad L$set$2
    .byte   0
    .align 3
LEFDE1:
LSFDE3:
    .set L$set$3,LEFDE3-LASFDE3
    .long L$set$3
LASFDE3:
    .long   LASFDE3-EH_frame1
    .quad   LFB3-.
    .set L$set$4,LFE3-LFB3
    .quad L$set$4
    .byte   0
    .byte   0x4
    .set L$set$5,LCFI0-LFB3
    .long L$set$5
    .byte   0xe
    .byte   0x10
    .byte   0x4
    .set L$set$6,LCFI1-LCFI0
    .long L$set$6
    .byte   0xe
    .byte   0x8
    .align 3
LEFDE3:
    .constructor
    .destructor
    .align 1
    .subsections_via_symbols
Run Code Online (Sandbox Code Playgroud)

点积是385,它似乎是在编译时计算的,但我似乎无法找到确切的位置.我怀疑它在以下汇编程序段中的某个位置:

    movl    $1, %eax
    movsd   LC0(%rip), %xmm0
    call    _printf
    xorl    %eax, %eax
    addq    $8, %rsp
LCFI1:
    ret
LFE3:
    .literal8
    .align 3
LC0:
    .long   0
    .long   1081610240
    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
Run Code Online (Sandbox Code Playgroud)

我(非常非常有限)对装配的理解会告诉我,点积是由编译器计算并放在寄存器(LC0)中.然后该指令movsd LC0(%rip), %xmm0将值放在一个字符串中,并调用printf生成的格式化字符串.

是这样的吗?实际数字385是否包含在此输出中的某个位置,或者是否计算在其中?

谢谢!

编辑:

如果有人想知道gfortran生产的组件是怎么样的,我会在下面附上它.请注意,即使在编译时已知,并且我正在使用Fortran的内部dot_product运算符,生成的程序集也要大得多(130行与C++版本中的90行),似乎优化程序无法减少操作.

程序(注意我使用的是内在的内置dot_product运算符):

PROGRAM MAIN
  REAL(8), DIMENSION(10):: X = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)
  REAL(8), DIMENSION(10):: Y = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)
  PRINT "(A, E23.16)", "x.y = ", DOT_PRODUCT(X, Y)
ENDPROGRAM MAIN
Run Code Online (Sandbox Code Playgroud)

程序集(在OS X 10.7.4 x86_64-apple-darwin11.4.2上使用gcc 4.7.2的gfortran -S -O3 myprogram.cpp)

    .cstring
LC0:
    .ascii "dotproduct-intrinsic.f90\0"
    .const
LC1:
    .ascii "(A, E23.16)"
LC2:
    .ascii "x.y = "
    .text
    .align 4,0x90
_MAIN__:
LFB0:
    leaq    LC0(%rip), %rax
    subq    $504, %rsp
LCFI0:
    movq    %rax, 24(%rsp)
    leaq    16(%rsp), %rdi
    leaq    LC1(%rip), %rax
    movl    $5, 32(%rsp)
    movq    %rax, 88(%rsp)
    movl    $11, 96(%rsp)
    movl    $4096, 16(%rsp)
    movl    $6, 20(%rsp)
    call    __gfortran_st_write
    leaq    16(%rsp), %rdi
    movl    $6, %edx
    leaq    LC2(%rip), %rsi
    call    __gfortran_transfer_character_write
    leaq    8(%rsp), %rsi
    movl    $8, %edx
    movabsq $4645480607818711040, %rax
    leaq    16(%rsp), %rdi
    movq    %rax, 8(%rsp)
    call    __gfortran_transfer_real_write
    leaq    16(%rsp), %rdi
    call    __gfortran_st_write_done
    addq    $504, %rsp
LCFI1:
    ret
LFE0:
    .section __TEXT,__text_startup,regular,pure_instructions
    .align 4
    .globl _main
_main:
LFB1:
    subq    $8, %rsp
LCFI2:
    call    __gfortran_set_args
    leaq    _options.3.1864(%rip), %rsi
    movl    $8, %edi
    call    __gfortran_set_options
    call    _MAIN__
    xorl    %eax, %eax
    addq    $8, %rsp
LCFI3:
    ret
LFE1:
    .const
    .align 5
_options.3.1864:
    .long   68
    .long   1023
    .long   0
    .long   0
    .long   1
    .long   1
    .long   0
    .long   1
    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
    .set L$set$0,LECIE1-LSCIE1
    .long L$set$0
LSCIE1:
    .long   0
    .byte   0x1
    .ascii "zR\0"
    .byte   0x1
    .byte   0x78
    .byte   0x10
    .byte   0x1
    .byte   0x10
    .byte   0xc
    .byte   0x7
    .byte   0x8
    .byte   0x90
    .byte   0x1
    .align 3
LECIE1:
LSFDE1:
    .set L$set$1,LEFDE1-LASFDE1
    .long L$set$1
LASFDE1:
    .long   LASFDE1-EH_frame1
    .quad   LFB0-.
    .set L$set$2,LFE0-LFB0
    .quad L$set$2
    .byte   0
    .byte   0x4
    .set L$set$3,LCFI0-LFB0
    .long L$set$3
    .byte   0xe
    .byte   0x80,0x4
    .byte   0x4
    .set L$set$4,LCFI1-LCFI0
    .long L$set$4
    .byte   0xe
    .byte   0x8
    .align 3
LEFDE1:
LSFDE3:
    .set L$set$5,LEFDE3-LASFDE3
    .long L$set$5
LASFDE3:
    .long   LASFDE3-EH_frame1
    .quad   LFB1-.
    .set L$set$6,LFE1-LFB1
    .quad L$set$6
    .byte   0
    .byte   0x4
    .set L$set$7,LCFI2-LFB1
    .long L$set$7
    .byte   0xe
    .byte   0x10
    .byte   0x4
    .set L$set$8,LCFI3-LCFI2
    .long L$set$8
    .byte   0xe
    .byte   0x8
    .align 3
LEFDE3:
    .subsections_via_symbols
Run Code Online (Sandbox Code Playgroud)

编辑2

感谢@JerryCoffin的回答,我确实能够验证位模式

LC0:
    .long   0
    .long   1081610240
Run Code Online (Sandbox Code Playgroud)

在汇编输出中找到对应的数字385.

我使用了@JerryCoffin提供的完全相同的程序,即:

#include <stdio.h>


    #pragma pack(1)
    struct x {
        long x, y;
    };    

    int main() { 
        x v = {0, 1081610240};

        printf("%f\n", *(double *)&v);
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

唯一需要注意的是我必须使用32位目标编译它:g++ verification.cpp -m32.

Jer*_*fin 7

是的 - 这个:

LC0:
    .long   0
    .long   1081610240
Run Code Online (Sandbox Code Playgroud)

双精度的位模式是值385 1.那么正在发生的是:

LCFI0:
    movl    $1, %eax
    movsd   LC0(%rip), %xmm0
    call    _printf
Run Code Online (Sandbox Code Playgroud)

...正在将该值加载到XMM寄存器中,然后调用printf将其打印出来.我不能肯定地说,但如果我不得不猜测,我会说它1告诉它它正在通过一个参数打印出来.


1如果您需要验证,请尝试以下方法:

#include <stdio.h>

#pragma pack(1)
struct x {
    long x, y;
};    

int main() { 
    x v = {0, 1081610240};

    printf("%f\n", *(double *)&v);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当然,这是官方的,这是不可移植的等等,但是在同一台机器上使用相同的编译器,你可能获得相同输出的概率大约为99% - 385.