您如何使用clang的新自定义大小int功能?

Recently, I heard that clang got a new feature, _ExtInt. I know that it lets you specify the size of an integer (odd or even like 13-bit int), but how do you use it?

评论
  • 黑色爱情
    黑色爱情 回复

    _ExtInt is to be used as a normal specifier. For example:

    _ExtInt(13) foo;
    

    Here you declared foo to be of 13 bits. Remember to not put short or long type of keywords before it (cause it wouldn't really make sense). You can also do casts:

    (_ExtInt(6))AnExtInt5 + AnExtInt6 or static_cast<ExtInt(6)>(AnExtInt5) + AnExtInt6
    

    不仅如此,如果您使用c ++,还可以做一些非常疯狂的事情:

    template<size_t WidthA, size_t WidthB>
      _ExtInt(WidthA + WidthB) lossless_mul(_ExtInt(WidthA) a, _ExtInt(WidthB) b) {
      return static_cast<_ExtInt(WidthA + WidthB)>(a) 
           * static_cast<_ExtInt(WidthA + WidthB)>(b);
    } 
    

    Look here for some more details.

    注意:要使用此功能,您将需要最新版本的clang,因为更改是在2020年4月21日进行的。