CUDA: Why are bitwise operators sometimes faster than logical operators?

前端 未结 3 1069
一向
一向 2021-01-11 17:46

When I am down to squeezing the last bit of performance out of a kernel, I usually find that replacing the logical operators (&& and

3条回答
  •  北海茫月
    2021-01-11 18:25

    A && B:

    if (!A) {
      return 0;
    }
    if (!B) {
      return 0;
    }
    return 1;
    

    A & B:

    return A & B;
    

    These are the semantics considering that evaluating A and B can have side effects (they can be functions that alter the state of the system when evaluated).

    There are many ways that the compiler can optimize the A && B case, depending on the types of A and B and the context.

提交回复
热议问题