Effectiveness of GCC optmization on bit operations

后端 未结 5 1428
暗喜
暗喜 2021-02-19 14:25

Here are two ways to set an individual bit in C on x86-64:

inline void SetBitC(long *array, int bit) {
   //Pure C version
   *array |= 1<

        
5条回答
  •  爱一瞬间的悲伤
    2021-02-19 14:44

    Why does GCC optimize so poorly for such a common operation?

    Prelude: Since the late 1980s, focus on compiler optimization has moved away from microbenchmarks which focus on individual operations and toward macrobenchmarks which focus on applications whose speed people care about. These days most compiler writers are focused on macrobenchmarks, and developing good benchmark suites is something that is taken seriously.

    Answer: Nobody on the gcc is using a benchmark where the difference between or and bts matters to the execution time of a real program. If you can produce such a program, you might be able to get the attention of people in gcc-land.

    Am I doing something wrong with the C version?

    No, this is perfectly good standard C. Very readable and idiomatic, in fact.

提交回复
热议问题