您如何使用clang的新自定义大小int功能? [英] How do you use clang's new custom size int feature?

查看:129
本文介绍了您如何使用clang的新自定义大小int功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我听说clang提供了一个新功能_ExtInt.我知道它可以让您指定整数的大小(奇数,甚至是13位整数),但是如何使用呢?

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将用作常规说明符.例如:

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

_ExtInt(13) foo;

在这里,您声明foo为13位.记住不要将shortlong类型的关键字放在前面(因为这实际上没有意义),尽管您可以放置​​signedunsigned(默认为signed). 请注意,您不允许做类似的事情; _ExtInt(5) + _ExtInt(6).根据网站,是因为:

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), though you can put signed or unsigned (signed is default). Note that you aren't allowed to do things like; _ExtInt(5) + _ExtInt(6). According to this website, that is because:

WG14论文提出将整数提升为最大的类型(即,添加_ExtInt(5)和_ExtInt(6)将导致_ExtInt(6)),但是实现不允许这样做,并且_ExtInt (5)+ _ExtInt(6)将导致编译器错误.这样做是为了在WG14更改论文设计的情况下,我们能够在不破坏现有程序的情况下实现它.

The WG14 paper proposes integer promotion to the largest of the types (that is, adding an _ExtInt(5) and an _ExtInt(6) would result in an _ExtInt(6)), however the implementation does not permit that and _ExtInt(5) + _ExtInt(6) would result in a compiler error. This was done so that in the event that WG14 changes the design of the paper, we will be able to implement it without breaking existing programs.

这可以通过使用强制转换来解决:

This can be worked around by using casts:

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

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

Not only that, but if you use c++ you can do some really crazy stuff:

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.

其他笔记:

  • 添加到_ExtInt(32)int将是一个整数.
  • 您的int大小可以增加116,777,215位.
  • An int added to an _ExtInt(32) will be an int.
  • Your int size can go up 1 to 16,777,215 bits.

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

Note: In order to use this feature, you will need the latest version of clang, as the change was made on 4/21/2020.

这篇关于您如何使用clang的新自定义大小int功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆