Can I hint the optimizer by giving the range of an integer?

后端 未结 4 975
一生所求
一生所求 2020-12-02 04:15

I am using an int type to store a value. By the semantics of the program, the value always varies in a very small range (0 - 36), and int (not a

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 04:23

    There is standard support for this. What you should do is to include stdint.h (cstdint) and then use the type uint_fast8_t.

    This tells the compiler that you are only using numbers between 0 - 255, but that it is free to use a larger type if that gives faster code. Similarly, the compiler can assume that the variable will never have a value above 255 and then do optimizations accordingly.

提交回复
热议问题