函数地址对齐 __attribute__((aligned(64))) 编译器选项 -falign-functions=4

1, 实验原料 源代码

hello.c

复制代码
#include <stdio.h>

#define  ALI // __attribute__((aligned(64)))

float ALI  adddd(float a, float b, float c, float d){

	a+=b;
	a+=c;
	a+=d;

	return a;
}

float ALI  subbbb(float a, float b, float c, float d)
{
	a-= b;
	a-=c;
	a-=d;

	return a;
}


int main ()
{
	float x,y,z,s;

	x = 77.777;
	y =  1.111;
	z =  2.222;
	s =  3.333;

	printf("sum = %7.3f\n", adddd(x,y,z,s));
	printf("sub = %7.3f\n", subbbb(x,y,z,s));

	return 0;
}

2, no align 要求

#define ALI // attribute((aligned(64)))

ALI 定义为空,相当于不存在;

gcc hello.c

./a.out

objdump -d ./a.out

运气比较好,adddd函数的指令首地址为 0000001149

3,16字节 align

下面分别变成

#define ALI attribute((aligned(16)))

#define ALI attribute((aligned(64)))

来试试效果:

#define ALI attribute((aligned(16)))

效果同预期,adddd 的首指令地址变成了 00000001150,是可以整除16的

#define ALI attribute((aligned(64)))

4, 使用 -falign-functions=N

gcc -falign-functions=1 hello_align.c -o a.16.out

gcc -falign-functions=2 hello_align.c -o a.16.out

gcc -falign-functions=4 hello_align.c -o a.16.out

16:

可以看出来,函数体指令首地址都满足对齐要求。

相关推荐
chao_7891 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
十盒半价1 小时前
从递归到动态规划:手把手教你玩转算法三剑客
javascript·算法·trae
GEEK零零七1 小时前
Leetcode 1070. 产品销售分析 III
sql·算法·leetcode
凌肖战2 小时前
力扣网编程274题:H指数之普通解法(中等)
算法·leetcode
秋说2 小时前
【PTA数据结构 | C语言版】将数组中元素反转存放
c语言·数据结构·算法
WebInfra2 小时前
如何在程序中嵌入有大量字符串的 HashMap
算法·设计模式·架构
森焱森2 小时前
APM与ChibiOS系统
c语言·单片机·算法·架构·无人机
★Orange★3 小时前
Linux Kernel kfifo 实现和巧妙设计
linux·运维·算法
尘世闲鱼3 小时前
解数独(C++版本)
开发语言·c++·算法·解数独
qqxhb3 小时前
零基础数据结构与算法——第四章:基础算法-排序(中)
数据结构·算法·排序算法·归并·快排·堆排