Alignment of vector types

  • From: Jeff Slutter <jeff@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 10 Sep 2012 01:28:18 -0400

From what I can tell it appears that vector types are getting an
alignment of 64, but to the best of my knowledge it should be 16. Below
is some sample code using gcc style mode and vector_size. It correctly
reports size as 16 bytes, but the alignment is 64.

This affects passing arrays containing vector data to C functions. If I
make a matrix struct containing an array of 4 vectors it is 256 bytes,
when I believe it should be 64 bytes (which it would be if I just made 4
individual members within the struct). Even then I can't do an array of
matrix structs :)

ffi = require('ffi')
ffi.cdef([[
typedef int v4sf __attribute__ ((mode(V4SF)));
typedef float v4sf2 __attribute__ ((vector_size(16)));
struct a
{
  v4sf asArray[4];
};
struct b
{
  v4sf x, y, z, w;
};
]])
v4sf = ffi.typeof('v4sf')
v4sf2 = ffi.typeof('v4sf2')
a = ffi.typeof('struct a')
b = ffi.typeof('struct b')
print('align(v4sf)',ffi.alignof(v4sf))
print('align(v4sf2)',ffi.alignof(v4sf2))
print('sizeof(v4sf)',ffi.sizeof(v4sf))
print('sizeof(v4sf2)',ffi.sizeof(v4sf2))
print('align(a)',ffi.alignof(a))
print('align(b)',ffi.alignof(b))
print('sizeof(a)',ffi.sizeof(a))
print('sizeof(b)',ffi.sizeof(b))


align(v4sf)     64
align(v4sf2)    64
sizeof(v4sf)    16
sizeof(v4sf2)   16
align(a)        64
align(b)        64
sizeof(a)       64
sizeof(b)       256



Other related posts: