I want to make a small experiment, the purpose of which is to clarify the question of the need to get rid of branches in such functions as min, max, sign and abs. Everyone should be able to take this cpp file (GitHub), compile, run and look at the output in stdout. The program produces two columns of numbers: the time the function works with and without branching. The program can work for a long time, several minutes (do not worry, it does not hang).

Now the program is compiled only in VC ++ 2015 and in GCC 4.8.1 (from MinGW), and I would like it to work in other compilers. Who can advise what to do? I cannot get rid of std::chrono , time needs to be measured more or less universally, int32_t too, since I donโ€™t know, all of a sudden someone will have an int of 64 bits (little). Perhaps I do not follow the Standard in everything or are there any other mistakes?

The important thing is this : I am aware that the code depends on the presence of a sign shift and an additional code in the representation of negative numbers. So it was intended .

  • it compiles normally gcc 4.9.3 and clang 3.7 (with the -std=c++11 option -std=c++11 ) in Linux. And even there are no warnings from the compiler. - KoVadim

1 answer 1

The above code is compiled "only in VC ++ 2015 and in GCC 4.8.1", because it uses the capabilities of the C ++ 11 standard (namely, std::chrono and the std::chrono header file). In order for a program to be built on compilers that support exclusively earlier standards, it is necessary:

  1. Use the clock function from the ctime header file. This function returns the time in processor cycles, and therefore is ideal for solving the task.
  2. Replace the inclusion of <cstdint> with <stdint.h> . Both headers have almost identical content, but the latter was introduced back in C99.
  • one
    There is also auto , which had a different meaning before C ++ 11 - int3