Posts

Showing posts with the label assembly

Comparing Fortran & C++ assembler for int = floor(sqrt(…))

37 6 I have implemented a function in Fortran and C++ each: #include <math.h> void dbl_sqrt_c(double *x, double *y){ *y = sqrt(*x - 1.0); return; } pure subroutine my_dbl_sqrt(x,y) bind(c, name="dbl_sqrt_fort") USE, INTRINSIC :: ISO_C_BINDING implicit none real(kind=c_double), intent(in) :: x real(kind=c_double), intent(out) :: y y = sqrt(x - 1d0) end subroutine my_dbl_sqrt I compared them in the compiler explorer: Fortran: https://godbolt.org/z/froz4rx97 C++: https://godbolt.org/z/45aex99Yz And the way I read the assembler, they do basically the same thing, but C++ checks whether the argument of the sqrt is negative, which Fortran doesn't. I compared their performance using googles benchmark, but they are pretty evenly...

Include of iostream leads to different binary

Compiling the following code int main() { return 0; } gives the assembly main: xorl %eax, %eax ret https://gcc.godbolt.org/z/oQvRDd If now iostream is included #include <iostream> int main(){ return 0; } this assembly is created. main: xorl %eax, %eax ret _GLOBAL__sub_I_main: subq $8, %rsp movl $_ZStL8__ioinit, %edi call std::ios_base::Init::Init() [complete object constructor] movl $__dso_handle, %edx movl $_ZStL8__ioinit, %esi movl $_ZNSt8ios_base4InitD1Ev, %edi addq $8, %rsp jmp __cxa_atexit Full optimization is turned on (-O3). https://gcc.godbolt.org/z/EtrEX8 Can someone explain, why including an unused header changes the binary. What is _GLOBAL__sub_I_main:? Each translation unit that includes <iostream> contains a copy of ios_base::Init object: static ios_base::Init __ioinit; This object is used to initialize the standard stre...