c - 嵌入式编程

标签 c embedded

我在网上找到了这个问答:

Q: Which is better a char, short or int type for optimization?

A: Where possible, it is best to avoid using char and short as local variables. For the types char and short the compiler needs to reduce the size of the local variable to 8 or 16 bits after each assignment. This is called sign-extending for signed variables and zeroextending for unsigned variables. It is implemented by shifting the register left by 24 or 16 bits, followed by a signed or unsigned shift right by the same amount, taking two instructions (zero-extension of an unsigned char takes one instruction). These shifts can be avoided by using int and unsigned int for local variables. This is particularly important for calculations which first load data into local variables and then process the data inside the local variables. Even if data is input and output as 8- or 16-bit quantities, it is worth considering processing them as 32-bit quantities.

这是正确的吗?我认为最好避免使用 char 和 short,因为它们会进行算术转换(它们很可能会被转换为 int 或 long,这会导致编译器生成额外的指令)。

Q: How to reduce function call overhead in ARM based systems?

A: Avoid functions with a parameter that is passed partially in a register and partially on the stack (split-argument). This is not handled efficiently by the current compilers: all register arguments are pushed on the stack.

· Avoid functions with a variable number of parameters. Varargs functions. ...

关于“varargs”——这是因为参数将通过堆栈传递吗? 什么是参数部分通过寄存器传递,部分通过堆栈传递的函数,您能提供示例吗?

我们可以说,函数参数的传递方式(通过寄存器或堆栈)在很大程度上取决于体系结构吗?

谢谢!

最佳答案

简单地说:关于优化的建议具有误导性。不一定是错误的,但不完整。

看来您的来源是 CodeProject .他说他主要是在谈论针对 ARM 的优化。

首先,char 和 short 的处理方式高度依赖于处理器。根据体系结构,转换可能是零成本或最低成本,具体取决于它们发生的时间和方式——在加载时、操作类型、哪些指令可以并行运行并且实际上可能是免费的,具体取决于其余部分代码 - 例如在 TI DSP c64 架构上,每个周期可以运行 8 个操作。通常最有效的使用是“ native ”整数大小,但它也取决于数据的来源——加载、修改和存储回 char/short 数据可能比加载和转换为 int 更有效,修改并存储回 char/short。也可能不是——这取决于架构和正在执行的操作。编译器通常可以更好地了解是否为您执行此操作。

其次,在许多架构中,char 和 short 与 int 一样快,尤其是在计算避免隐式转换为 int 的情况下。注意:这在 C 中很容易搞砸,比如“x = y + 1”——强制转换为 int(假设 x 和 y 是 char 或 short),但好消息是几乎所有编译器都足够聪明为您优化转换。许多其他使用本地字符/短字符的情况将导致编译器根据以后的使用方式优化任何转换。这是因为在典型的处理器中,char/short 的溢出/环绕与将其计算为 int 并在存储中转换的结果相同(或者通过在稍后将该寄存器简单地寻址为 char/short操作 - 获得“免费”转换)。

在他们的例子中:

int wordinc (int a)
{ 
   return a + 1;
}
short shortinc (short a)
{ 
    return a + 1;
}
char charinc (char a)
{ 
    return a + 1;
}

在许多架构/编译器中,它们在实践中运行得同样快。

第三,在某些架构中,char/short 比 int 快。自然大小为 8 位或 16 位的嵌入式架构(不可否认,这不是您现在想到的那种开发)就是一个例子。

第四,虽然在现代大量使用 ram、大缓存的处理器环境中通常不是一个大问题,但保持本地堆栈存储大小较小(假设编译器不将其提升到寄存器)可能有助于提高缓存访问的效率,尤其是一级缓存。

另一方面,如果编译器不够聪明,无法向您隐藏它,则本地字符/短裤作为参数传递给其他函数(尤其是文件本地“静态”函数)may entail up-conversions to int .同样,根据上述内容,编译器可能足够聪明以隐藏转换。

确实同意您引用的网站开头的声明:

Although a number of guidelines are available for C code optimization, there is no substitute for having a thorough knowledge of the compiler and machine for which you are programming.

关于c - 嵌入式编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4654552/

相关文章:

C:匹配scanf中的单个空格

c - 在 Switch-Case 构造中使用 'return' 与 'break' 有什么好处?

c - 如何将-o生成的C程序输出添加到$PATH?

c - 在 C 中传递链表时如何解决这些冲突类型错误?

c - 为什么频率阵列保存垃圾值?

android - 在 Android NDK - C/embed 中映射有关传感器的信号量内存值

c - ARM 汇编程序 - 如何使用 CMP、BLT 和 BGT?

c - 是否可以指示 C 不对全局数组进行零初始化?

linux - 如何使用 oob 正确地 nandwrite 一个 nanddump 的转储?

c++ - 由于从客户端启动守护程序应用程序,服务器端口无法返回监听状态并处于 close_wait 状态