对齐,对齐的内存访问? [英] Aligned and unaligned memory accesses?

查看:168
本文介绍了对齐,对齐的内存访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对齐,对齐的内存访问之间的区别是什么?

What is the difference between aligned and unaligned memory access?

我工作在一个的TMS320C64x DSP,我想用内部函数(C函数汇编指令),它有

I work on an TMS320C64x DSP, and I want to use the intrinsic functions (C functions for assembly instructions) and it has

ushort & _amem2(void *ptr);
ushort & _mem2(void *ptr);

其中, _amem2 做的2个字节对​​齐访问和 _mem2 不对齐访问。

where _amem2 does an aligned access of 2 bytes and _mem2 does unaligned access.

当我应该使用哪一个?

推荐答案

这是对准的存储器访问指指针(作为整数)是一类特定的值的倍数称为对准。对齐是自然地址的多个地方类型必须是,或应存放(如出于性能的考虑),在CPU上。例如,一个CPU可以要求所有两字节加载或存储通过是二的倍数的地址完成的。对于小的原始类型(下4个字节),对准几乎总是类型的大小。对于结构的调整通常是任何成员的最大对齐。

C编译器总是把你在声明满足了正确定位地址变量。因此,如果ptr指向例如一个uint16_t变量,它会对齐,你可以使用_amem2。您需要使用_mem2只有当您正在访问如通过I / O,或字节的字符串中间收到一个压缩字节数组。

An aligned memory access means that the pointer (as an integer) is a multiple of a type-specific value called the alignment. The alignment is the natural address multiple where the type must be, or should be stored (e.g. for performance reasons) on a CPU. For example, a CPU might require that all two-byte loads or stores are done through addresses that are multiples of two. For small primitive types (under 4 bytes), the alignment is almost always the size of the type. For structs, the alignment is usually the maximum alignment of any member.

The C compiler always puts variables that you declare at addresses which satisfy the "correct" alignment. So if ptr points to e.g. a uint16_t variable, it will be aligned and you can use _amem2. You need to use _mem2 only if you are accessing e.g. a packed byte array received via I/O, or bytes in the middle of a string.

这篇关于对齐,对齐的内存访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆