相关疑难解决方法(0)

为什么添加汇编注释导致生成的代码发生如此根本的变化?

所以,我有这个代码:

constexpr unsigned N = 1000;
void f1(char* sum, char* a, char* b) {
    for(int i = 0; i < N; ++i) {
        sum[i] = a[i] + b[i];
    }
}

void f2(char* sum, char* a, char* b) {
    char* end = sum + N;
    while(sum != end) {
        *sum++ = *a++ + *b++;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想看看GCC 4.7.2会产生的代码.所以我跑了g++ -march=native -O3 -masm=intel -S a.c++ -std=c++11并获得了以下输出:

        .file   "a.c++"
        .intel_syntax noprefix
        .text
        .p2align 4,,15
        .globl  _Z2f1PcS_S_
        .type   _Z2f1PcS_S_, @function
_Z2f1PcS_S_:
.LFB0:
        .cfi_startproc …
Run Code Online (Sandbox Code Playgroud)

c++ optimization assembly gcc

80
推荐指数
2
解决办法
6428
查看次数

工作__asm__ __volatile ___("":::"记忆")

ARM架构基本上__asm__ __volatile__ ()做了什么,有什么意义"memory"

c gcc arm volatile embedded-linux

43
推荐指数
3
解决办法
3万
查看次数

使用全局变量的递归函数中的GCC优化差异

前几天我遇到了一个使用GCC和'-Ofast'优化标志的奇怪问题.使用'gcc -Ofast -o fib1 fib1.c'编译以下程序.

#include <stdio.h>

int f1(int n) {
    if (n < 2) {
        return n;
    }
    int a, b;
    a = f1(n - 1);
    b = f1(n - 2);
    return a + b;
}

int main(){
    printf("%d", f1(40));
}
Run Code Online (Sandbox Code Playgroud)

在测量执行时间时,结果是:

peter@host ~ $ time ./fib1
102334155
real    0m0.511s
user    0m0.510s
sys     0m0.000s
Run Code Online (Sandbox Code Playgroud)

现在让我们在程序中引入一个全局变量,然后使用'gcc -Ofast -o fib2 fib2.c'再次编译.

#include <stdio.h>

int global;

int f1(int n) {
    if (n < 2) {
        return n;
    }
    int a, b;
    a …
Run Code Online (Sandbox Code Playgroud)

c gcc

7
推荐指数
1
解决办法
150
查看次数

标签 统计

gcc ×3

c ×2

arm ×1

assembly ×1

c++ ×1

embedded-linux ×1

optimization ×1

volatile ×1