简体   繁体   English

在16位微处理器上,我应该使用数据类型short而不是int吗?

[英]On a 16-bit microprocessor, should I be using datatype short instead of int?

I've read that using short vs int is actually creating an inefficiency for the compiler in that it needs to use the int datatype regardless because of C integer promotion. 我已经读过,使用short vs int实际上会为编译器创建一个低效率,因为它需要使用int数据类型,而不管C整数提升。 Is this true for 16-bit microprocessors? 对16位微处理器来说这是真的吗?

Another question: If I have an array of 1s and 0s, is it most efficient to use uint8_t or the unsigned char in this 16-bit microprocessor? 另一个问题:如果我有一个1和0的数组,在这个16位微处理器中使用uint8_tunsigned char是否最有效? Or is there still an issue with it being converted back to int.. 或者它仍然存在转换回int的问题。

Please help me clear up this muddy issue in my mind. 请帮我澄清这个泥泞的问题。 Thanks! 谢谢!

  1. Is it really an issue? 这真的是一个问题吗? On most 16 bit systems I've heard of, int and short end up being the same size (16 bits), so there shouldn't really be a difference in practice. 在我听说过的大多数16位系统中, intshort最终都是相同的大小(16位),所以在实践中应该没有什么区别。

  2. If uint8_t exists on a system, it's going to be synonymous with unsigned char . 如果uint8_t存在于系统上,它将与unsigned char同义。 unsigned char will be the smallest unsigned type avaliable on the system. unsigned char将是系统上可用的最小无符号类型。 If it's any more than 8 bits, there will be no uint8_t . 如果它超过8位,则不会有uint8_t If it's less than 8 bits, then it's violating the standard. 如果它小于8位,那么它违反了标准。 There will be no efficiency difference since one has to be defined in terms of the other. 没有效率差异,因为必须根据另一个来定义。

Lastly, do you really need to worry about these kind of microscopic differences? 最后,你真的需要担心这些微观差异吗? If you do you'll need to peek at the assembly output or (more likely) profile and see which one is faster. 如果你这样做,你需要查看装配输出或(更可能)配置文件,看看哪个更快。

On a 16-bit or larger processor, if you don't care how much storage things will take, use 'int' instead of 'short' or 'signed char'. 在16位或更大的处理器上,如果您不关心将占用多少存储空间,请使用“int”而不是“short”或“signed char”。 If you don't care about storage requirements or wrapping behavior, use 'unsigned int' instead of 'unsigned short' or 'unsigned char'. 如果您不关心存储要求或包装行为,请使用'unsigned int'而不是'unsigned short'或'unsigned char'。 On an 8-bit processor, 'char' types may be faster than 'int', but on 16-bit and larger processors where 16-bit math is faster than 32-bit math, 'int' is likely to be 16 bits so there's no need to use 'short' or 'char' for speed. 在8位处理器上,'char'类型可能比'int'更快,但在16位和更大的处理器上,16位数学比32位数学更快,'int'可能是16位所以没有必要使用'short'或'char'来提高速度。

BTW, on some processors, 'unsigned short' is much slower than 'unsigned int', because the C standard requires that operations on unsigned types 'wrap'. 顺便说一句,在某些处理器上,'unsigned short'比'unsigned int'要慢得多,因为C标准要求对无符号类型'wrap'进行操作。 If unsigned short variable "foo" is stored in a register, a typical ARM compiler generating code for "foo+=1;" 如果无符号短变量“foo”存储在寄存器中,则典型的ARM编译器生成“foo + = 1;”的代码。 would generate one instruction to do the increment, and two instructions to truncate the value to 65535 [BTW, an optimizing compiler that noticed that 'foo' could never exceed 65536 could shave an instruction, but I don't know if any real compilers would]. 将生成一条指令来执行增量,并将两条指令截断为65535 [BTW,一个优化编译器,注意'foo'永远不会超过65536可能会刮一条指令,但我不知道是否有任何真正的编译器会]。 The signed 'short' would not have to be slower than 'signed int', since no truncation is mandated by the standard; 签名的'short'不必比'signed int'慢,因为标准不强制截断; I'm not sure whether any compilers would skip the truncation for signed types, though. 不过,我不确定是否有任何编译器会跳过对签名类型的截断。

On a Blackfin it is probably not a simple answer whether 32 or 16 bit types will generate higher performance generally since it supports 16, 32 and 64-bit instructions, and has two 16 bit MACs. 在Blackfin上,32位或16位类型通常会产生更高的性能可能不是一个简单的答案,因为它支持16,32和64位指令,并且有两个16位MAC。 It will depend on the operations, but I suggest that you trust your compiler optimiser to make such decisions, it knows more about the processor's instruction timing and scheduling than you probably care to. 这取决于操作,但我建议你相信你的编译器优化器做出这样的决定,它比你可能关心的更多地了解处理器的指令时序和调度。

That said it may be that in your compiler int and short are the same size in any case. 也就是说,在你的编译器中,int和short在任何情况下都可能是相同的大小。 Consult the documentation, ot test with sizeof , or look in the limits.h header for numeric ranges that will infer the widths or the various types. 查阅文档,使用sizeof测试,或者在limits.h标题中查找将推断宽度或各种类型的数值范围。

If you truly want to restrict data type size use the stdint.h types such as int16_t . 如果您确实要限制数据类型大小,请使用stdint.h类型,例如int16_t

stdint.h also defines fastest minimum-width integer types such as int_fast16_t , this will guarantee a minimum width, but will use a larger type if it will be faster on your target. stdint.h还定义了最快的最小宽度整数类型,例如int_fast16_t ,这将保证最小宽度,但如果它在目标上更快,将使用更大的类型。 This is the probably the most portable way of solving your problem, but it relies on the implementer to have made good decisions about the appropriate types to use. 这可能是解决问题的最便携方式,但它依赖于实现者对要使用的适当类型做出了很好的决策。 On most architectures it makes little or no difference, but on RISC and DSP architectures that may not be the case. 在大多数架构上,它几乎没有差别,但在RISC和DSP架构上可能并非如此。 It may also not be the case that a particular size is fastest in all circumstances, and that is probably especially true in the case of Blackfin. 也许情况并非如此,特定尺寸在所有情况下都是最快的,并且在Blackfin的情况下尤其如此。

In some cases (where large amounts of data are transferred to an from external memory), the fastest size is likely to be one that matches the data bus width. 在某些情况下(大量数据从外部存储器传输),最快的大小可能是与数据总线宽度匹配的大小。

I make a point of having a block that looks like this in projects that rely on byte sizes: 我指出在依赖字节大小的项目中有一个看起来像这样的块:

typedef uint8 char
typedef uint16 short
typedef uint32 long

For whatever datatypes are appropriate. 适用于任何适合的数据类型。

Any conversion issues should be figured out at compile time. 任何转换问题都应该在编译时计算出来。 Those will be cpu and compiler dependent. 这些将取决于cpu和编译器。

Of course adding 2 32-bit numbers on a 16-bit CPU will entail some finangling by the compiler. 当然,在16位CPU上添加2个32位数字将需要编译器进行一些修改。 There can also be amusing things when you dink with loading from memory, depending on memory word width and whether you can load from any address, or if bytes have to be loaded from a given boundary 当您从内存加载时,根据内存字宽度以及是否可以从任何地址加载,或者是否必须从给定边界加载字节,也会有一些有趣的事情

In short , YMMV, and optimize after profiling. short ,YMMV,并在分析后进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM