Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created June 6, 2017 08:06
Show Gist options
  • Save kaityo256/858c7c243cd0828aa36197bd44532024 to your computer and use it in GitHub Desktop.
Save kaityo256/858c7c243cd0828aa36197bd44532024 to your computer and use it in GitHub Desktop.
x87 test
#include <math.h>
double
func(void){
const int N = 1000000;
const double s = 2.0*M_PI/static_cast<double>(N);
double x = 0.0;
double y = 0.0;
for(int i=0;i<N*100;i++){
y += sin(x);
x += s;
}
return y;
}
@kaityo256
Copy link
Author

$ g++ -O2 -mfpmath=387 -ffast-math -S test.cpp

will give you the following asm.

func():
.LFB3:
        .cfi_startproc
        fldz
        movl    $100000000, %eax
        fld     %st(0)
        fld     %st(1)
        fldl    .LC1(%rip)
        fxch    %st(1)
        jmp     .L3
        .p2align 4,,10
        .p2align 3
.L5:
        fld     %st(2)
        fsin
        fxch    %st(2)
.L3:
        faddp   %st, %st(2)
        subl    $1, %eax
        fadd    %st, %st(2)
        jne     .L5
        fstp    %st(0)
        fstp    %st(1)
        fstpl   -8(%rsp)
        movsd   -8(%rsp), %xmm0
        ret

Thanks fujitanozomu for the comment on this article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment