【发布时间】:2020-11-01 23:06:57
【问题描述】:
stdatomic.h appears to containatomic_uint_least16_t和atomic_uint_fast16_t,它们是_Atomic版本的stdint.h typesuint_least16_t和uint_fast16_t,但它不包含atomic_uint16_t。 为什么?
来自N1548 draft的一些背景信息:
7.18.1.1 精确宽度整数类型
1 typedef 名称
intN_t指定宽度为 N、无填充位和二进制补码表示的有符号整数类型。因此,int8_t表示这样一个宽度正好为 8 位的有符号整数类型。2 typedef 名称
uintN_t指定宽度为 N 且无填充位的无符号整数类型。因此,uint24_t表示这样一种无符号整数类型,其宽度正好为 24 位。3 这些类型是可选的。但是,如果实现提供整数类型 宽度为 8、16、32 或 64 位,无填充位,并且(对于有符号类型)具有 二进制补码表示,它应该定义相应的typedef名称。
7.18.1.2 最小宽度整数类型
1 typedef 名称
int_leastN_t指定宽度至少为 N 的有符号整数类型,使得没有更小尺寸的有符号整数类型至少具有指定的宽度。因此,int_least32_t表示宽度至少为 32 位的有符号整数类型。2 typedef 名称
uint_leastN_t指定一个宽度至少为 N 的无符号整数类型,因此没有更小尺寸的无符号整数类型至少具有指定的宽度。因此,uint_least16_t表示一个无符号整数类型,其宽度至少为 16 位。3 需要以下类型:
int_least8_t int_least16_t int_least32_t int_least64_t uint_least8_t uint_least16_t uint_least32_t uint_least64_t此表单的所有其他类型都是可选的。
(等等,包括int_fastN_t / uint_fastN_t 类型等)
第 3 段值得强调:
但是,如果实现提供整数类型 宽度为 8、16、32 或 64 位,无填充位,并且(对于有符号类型)具有二进制补码表示,它应定义相应的 typedef 名称。
这意味着,例如,如果我有一个像 int 或 short 这样的类型,它被实现为具有二进制补码表示的 16 位整数,那么实现 shall 定义 @ 987654352@.
<stdatomic.h> 的atomic_ 类型也在N1548 中列出(转载如下),但它没有做出相应的要求,如果实现有int16_t,那么就有atomic_int16_t ---这就是我的问题的本质。
7.17.6 原子整数和地址类型
1 对于下表中的每一行,原子类型名称声明为 对应的直接类型。
Atomic type name Direct type ---------------- ----------- atomic_char _Atomic char atomic_schar _Atomic signed char atomic_uchar _Atomic unsigned char atomic_short _Atomic short atomic_ushort _Atomic unsigned short atomic_int _Atomic int atomic_uint _Atomic unsigned int atomic_long _Atomic long atomic_ulong _Atomic unsigned long atomic_llong _Atomic long long atomic_ullong _Atomic unsigned long long atomic_char16_t _Atomic char16_t atomic_char32_t _Atomic char32_t atomic_wchar_t _Atomic wchar_t atomic_int_least8_t _Atomic int_least8_t atomic_uint_least8_t _Atomic uint_least8_t atomic_int_least16_t _Atomic int_least16_t atomic_uint_least16_t _Atomic uint_least16_t atomic_int_least32_t _Atomic int_least32_t atomic_uint_least32_t _Atomic uint_least32_t atomic_int_least64_t _Atomic int_least64_t atomic_uint_least64_t _Atomic uint_least64_t atomic_int_fast8_t _Atomic int_fast8_t atomic_uint_fast8_t _Atomic uint_fast8_t atomic_int_fast16_t _Atomic int_fast16_t atomic_uint_fast16_t _Atomic uint_fast16_t atomic_int_fast32_t _Atomic int_fast32_t atomic_uint_fast32_t _Atomic uint_fast32_t atomic_int_fast64_t _Atomic int_fast64_t atomic_uint_fast64_t _Atomic uint_fast64_t atomic_intptr_t _Atomic intptr_t atomic_uintptr_t _Atomic uintptr_t atomic_size_t _Atomic size_t atomic_ptrdiff_t _Atomic ptrdiff_t atomic_intmax_t _Atomic intmax_t atomic_uintmax_t _Atomic uintmax_t2 对这些类型的操作的语义在 7.17.7 中定义。
3
atomic_bool类型提供原子布尔值。4
atomic_address类型提供原子 void * 操作。单位 加/减应该是一个字节。5 注意原子整数和地址类型的表示不需要与它们对应的常规类型具有相同的大小。它们应尽可能具有相同的大小,因为它可以减轻所需的工作量 移植现有代码。
【问题讨论】:
标签: c language-lawyer c11 stdatomic stdint