3166|0

2618

帖子

0

TA的资源

纯净的硅(高级)

楼主
 

C6678:malloc函数如何做字节对齐 [复制链接]

矩阵的大小是函数形参,想要申请动态缓存作为动态数组,但是之后的很多向量运算都必须字节对齐才能够计算,如DSPF_sp_vecrecip函数,malloc函数在CCS编译器中虽然没有自动字节对齐;但是用memalign函数Ordinary dynamic memory allocation does not guarantee that the address of the buffer is aligned可以。具体介绍如下:



Dynamic Memory Allocation
www.ti.com Interfacing C and C++ With Assembly Language
An argument such as ptr is most commonly passed the base address of an array, for example:
short buffer[100];
...
f(buffer);
When compiling for C6400, C6400+, and C6740 devices, such an array is automatically aligned to an 8-byte boundary. When compiling for C6200 or C6700, such an array is automatically aligned to 4-byte boundary, or, if the base type requires it, an 8-byte boundary. This is true whether the array is global, static, or local. This automatic alignment is all that is required to achieve SIMD optimization on those respective devices. You still need to include the _nassert because, in the general case, the compiler cannot guarantee that ptr holds the address of a properly aligned array.
If you always pass the base address of an array to pointers like ptr, then you can use the following macro to reflect that fact.
#if defined(_TMS320C6400) #define ALIGNED_ARRAY(ptr) _nassert((int) ptr % 8 == 0) #elif defined(_TMS320C6200) || defined(_TMS320C6700) #define ALIGNED_ARRAY(ptr) _nassert((int) ptr % 4 == 0) #else #define ALIGNED_ARRAY(ptr) /* empty */ #endif
void f(short *ptr) { ALIGNED_ARRAY(ptr);
; a loop operating on data accessed by ptr
}
The macro works regardless of which C6x device you build for, or if you port the code to another target.
A more rare case is to pass the address of an offset from an array, for example:
f(&buffer[3]);
This code passes an unaligned address to ptr, thus violating the presumption coded in the _nassert(). There is no direct remedy for this case. Avoid this practice whenever possible.
Ordinary dynamic memory allocation does not guarantee that the address of the buffer is aligned, for example:
buffer = calloc(100 * sizeof(short));
You should use memalign() with an alignment of 8 instead, for example:
buffer = memalign(8, 100 * sizeof(short);
If you are using BIOS memory allocation routines, be sure to pass the alignment factor as the last argument using the syntax that follows:
buffer = MEM_alloc(segid, 100 * sizeof(short), 8);
See the TMS320C6000 DSP/BIOS Help for more information about BIOS memory allocation routines and the segid parameter in particular.



 
点赞 关注

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表