gpt4 book ai didi

c - gcc 用 memcpy 和 memset 替换循环

转载 作者:太空狗 更新时间:2023-10-29 14:56:57 25 4
gpt4 key购买 nike

我有以下简单程序:

#define N 20
long c[N];
long a[N + N];

void f(void)
{
long *s = c;
long *p = a;
while (p != a + N) *p++ = *s++;
while (p != a + N + N) *p++ = 0;
}

我编译它:

/usr/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gcc -mthumb -O3 -o main.o -c main.c

gcc 分别用 memcpymemset 方便地替换了循环:

00000000 <f>:
0: b570 push {r4, r5, r6, lr}
2: 4d07 ldr r5, [pc, #28] ; (20 <f+0x20>)
4: 4c07 ldr r4, [pc, #28] ; (24 <f+0x24>)
6: 002a movs r2, r5
8: 4907 ldr r1, [pc, #28] ; (28 <f+0x28>)
a: 0020 movs r0, r4
c: f7ff fffe bl 0 <memcpy>
10: 1960 adds r0, r4, r5
12: 002a movs r2, r5
14: 2100 movs r1, #0
16: f7ff fffe bl 0 <memset>
1a: bc70 pop {r4, r5, r6}
1c: bc01 pop {r0}
1e: 4700 bx r0

显然,gcc 很聪明,它决定库实现更有效,在每种特定情况下可能是这样,也可能不是这样。我想知道如何避免这种行为,例如,当速度不重要并且不需要库调用时。

最佳答案

好的,搜索https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html显示以下选项:

-ftree-loop-distribute-patterns

Perform loop distribution of patterns that can be code generated with calls to a library. This flag is enabled by default at -O3.

指定 -fno-tree-loop-distribute-patterns 避免触及标准库,而似乎不会影响其他优化。

关于c - gcc 用 memcpy 和 memset 替换循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46996893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com