Is there a code that results in 50% branch prediction miss?

前端 未结 3 1824
醉梦人生
醉梦人生 2021-02-04 00:52

The problem:

I\'m trying to figure out how to write a code (C preffered, ASM only if there is no other solution) that would make the branch pred

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 01:17

    If you know how the branch predictor works you can get to 100% misprediction. Just take the expected prediction of the predictor each time and do the opposite. The problem is that we don't know how it is implemented.

    I have read that typical predictors are able to predict patters such as 0,1,0,1 and so on. But I'm sure there is a limit to how long the pattern can be. My suggestion would be to try each and every pattern of a given length (such as 4) and see which one comes closest to your target percentage. You should be able to target both 50% and 100% and come very close. This profiling needs to be done for each platform once or at runtime.

    I doubt that 3% of the total number of branches are in system code like you said. The kernel does not take 3% overhead on purely CPU bound user code. Increase the scheduling priority to the maximum.

    You can take the RNG out of the game by generating random data once and iterating over the same data many times. The branch predictor is unlikely to detect this (although it clearly could).

    I would implement this by filling a bool[1 << 20] with a zero-one pattern like I described. Then, you can run the following loop over it many times:

    int sum0 = 0, sum1 = 0;
    for (...) {
     //unroll this a lot
     if (array[i]) sum0++;
     else sum1++;
    }
    //print both sums here to make sure the computation is not being optimized out
    

    You'll need to examine the disassembly to make sure that the compiler did not do anything clever.

    I don't see why the complicated setup that you have right now is necessary. The RNG can be taken out of the question and I don't see why more than this simple loop is needed. If the compiler is playing tricks you might need to mark the variables as volatile which makes the compiler (better: most compilers) treat them as if they were external function calls.

    Since the RNG now no longer matters since it is almost never called you can even invoke the cryptographic RNG of your OS to get numbers that are indistinguishable (to any human) from true random numbers.

提交回复
热议问题