关于数据对齐(About Data Alignment)

关于数据对齐(About Data Alignment

原文出自 MSDN ,链接为 http://msdn2.microsoft.com/en-us/library/ms253949.aspx

       很多 CPU ,如基于 Alpha, IA-64, MIPS, SuperH 体系的,拒绝读取未对齐数据。当一个程序要求其中之一的 CPU 读取未对齐数据时,这时 CPU 会进入异常处理状态并且通知程序不能继续执行。举个例子,在 ARM, MIPS, SH 硬件平台上,当操作系统被要求存取一个未对齐数据时默认通知应用程序一个异常。

对齐性
        对齐性是一种内存地址的特性,表现为内存地址模上 2 的幂。例如,内存地址 0x 0001103F 4 结果为 3 ;这个地址就叫做与 4n 3 对齐, 4 指出了所选用的 2 的幂的值。内存地址的对齐性取决于所选择的关于 2 的幂值。同样的地址模 8 结果为 7

       一个内存地址符合表达式 Xn 0 ,那么就说该地址对齐于 X

       CPU 执行指令就是对存储于内存上的数据进行操作,这些数据在内存上是以地址为标记的。对于地址来说,单独的数据会有其占用内存的字节数。如果它的地址对齐于它的字节数,那么就称作该数据自然对齐,否则称为未对齐。例如,一个标记 8 字节的浮点数据的地址对齐于 8 ,那么这个数据就自然对齐。

数据对齐性的编译处理
       设备编译器以一种防止造成数据未对齐的方式来对数据进行地址分配。

       对于单个的数据类型,编译器为其分配的地址是数据类型字节数的倍数。因此,编译器分配给 long 型变量的地址为 4 的倍数,就是说以 2 进制表示地址的话,最后两位为 0

       另外,编译器以一种自然对齐每个结构成员的方式来填充结构体。参看下面的代码里面的结构体 struct x_

    
struct x_
      {
            char a;     // 1 byte
            int b;      // 4 bytes
            short c;    // 2 bytes
            char d;     // 1 byte
      } MyStruct;

     
编译器填充这个结构以使其自然对齐。

例如
      下面的代码说明了编译器是如何在内存中填充的。

      // Shows the actual memory layout
      struct x_
     {
           char a;            // 1 byte
           char _pad0[3];     // padding to put 'b' on 4-byte boundary
           int b;            // 4 bytes
           short c;          // 2 bytes
           char d;           // 1 byte
           char _pad1[1];    // padding to make sizeof(x_) multiple of 4
     }

    
两种定义在作sizeof(struct x_)运算都会返回12字节。

      
第二种定义含有两种填充成分:

             *char _pad0[3]
使得int4字节边界上对齐

             *
char _pad1[1]使得结构数组struct _x  bar[3]对齐。
             
填充使得bar[3]各个变量能够自然对齐。

            
下面的代码显示了bar[3]的内存结构:

             
adr
              offset   element
------   -------
0x0000   char a;         // bar[0]
0x0001   char pad0[3];
0x0004   int b;
0x0008   short c;
0x 000a    char d;
0x000b   char _pad1[1];

0x 000c    char a;         // bar[1]
0x000d   char _pad0[3];
0x0010   int b;
0x0014   short c;
0x0016   char d;
0x0017   char _pad1[1];

0x0018   char a;         // bar[2]
0x0019   char _pad0[3];
0x 001c     int b;
0x0020   short c;
0x0022   char d;
0x0023   char _pad1[1];

alignment   对齐; Quadword 8 byte Doubleword 4 byte
对齐.BMP
                                                                                                                                                                                             ---- saga. constantine

### 3D Volume Rotation and Coordinate System Alignment In the context of computer graphics or medical imaging software development, performing a 3D volume rotation involves transforming volumetric data from one orientation to another while ensuring that the new orientation aligns with a specified coordinate system. This process typically requires understanding both linear algebra transformations and spatial relationships between different axes. To achieve this transformation effectively: - Define an initial set of coordinates representing points within the original volume. - Apply rotational matrices corresponding to desired angles around each axis (X, Y, Z). - Translate these transformed points into their respective positions under the target coordinate system[^1]. For practical implementation using Python alongside libraries such as NumPy for matrix operations and SciPy for interpolation during resampling after rotation, consider following code snippet demonstrating how to perform rotations on a simple cubic grid: ```python import numpy as np from scipy.ndimage import affine_transform def rotate_volume(volume, angle_x=0., angle_y=0., angle_z=0.): """Rotate a 3D volume by given degrees along X, Y, Z axes.""" # Convert degree angles to radians rx = np.radians(angle_x) ry = np.radians(angle_y) rz = np.radians(angle_z) # Create individual rotation matrices Rx = np.array([[1, 0, 0], [0, np.cos(rx), -np.sin(rx)], [0, np.sin(rx), np.cos(rx)]]) Ry = np.array([[ np.cos(ry), 0, np.sin(ry)], [ 0 , 1, 0], [-np.sin(ry), 0, np.cos(ry)]]) Rz = np.array([[np.cos(rz), -np.sin(rz), 0], [np.sin(rz), np.cos(rz), 0], [ 0, 0, 1]]) # Combine all three rotations into single transform M = Rz @ Ry @ Rx # Perform actual rotation via affine transformation rotated_vol = affine_transform(volume, np.linalg.inv(M), mode='constant', cval=-1000.) return rotated_vol ``` This function applies successive rotations about the principal axes based upon provided Euler angles before finally executing the operation through `affine_transform`. Note here `-1000.` serves merely as padding value outside valid voxel range; adjust according specific application needs accordingly. After applying necessary geometric manipulations like above, ensure proper validation steps are taken including visual inspection tools available within chosen programming environment or external visualization packages specialized towards handling multi-dimensional datasets efficiently.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值