structures align

A

alariq

is it correct to use this define

typedef<typeame T>
struct my_helper {
char c;
T data;
};

#define aligned_at(C) ( sizeof(my_hepler<C>) - sizeof(C) )

to ensure that if we know that T is aligned on some value this define
will return this value
For example, for type T that is aligned on 16 bytes will this macro
always return 16 also?
I saw this but it does not work in my case.

E.g. i have
struct aligned16 {
char data __attribute__ aligned(16);
};

and aligned_at(aligned16) does not return 16 as it intended

is there any other way? thanks
 
N

Noah Roberts

is it correct to use this define

typedef<typeame T>
struct my_helper {
char c;
T data;
};

#define aligned_at(C) ( sizeof(my_hepler<C>) - sizeof(C) )

to ensure that if we know that T is aligned on some value this define
will return this value
For example, for type T that is aligned on 16 bytes will this macro
always return 16 also?
I saw this but it does not work in my case.

E.g. i have
struct aligned16 {
char data __attribute__ aligned(16);
};

and aligned_at(aligned16) does not return 16 as it intended

Why do you think it would? Let's assume everything is lined up exactly
as the data structures' sizes. That being the case, sizeof(my_helper
<C>) == sizeof(C) + sizeof(char). Thus aligned_at(C) would return
sizeof(char), or 1. In fact it would return 1 for any data type you
passed it.

It might seem that you should be able to do something like so:

my_helper<T> helper;
T * t = (&helper + aligned_at(T));

If things worked as I was just working with then it would. However,
reality is different and the compiler is free to pad. There could be
unused bytes of data used after your "aligned_at".

There's a commonly used, but I believe UB generating, construct that
does what I think you want:

#define member_at(X,Y) (&((X*)(NULL))->Y) - ((X*)(NULL))

Not sure if that's exactly right since it's been a few years since I've
used it. In C++ you don't need it since there's such a thing as member
pointers. In C there is not so you have stuff like that to work around
it.
 
C

Chris M. Thomasson

alariq said:
is it correct to use this define

typedef<typeame T>
struct my_helper {
char c;
T data;
};

#define aligned_at(C) ( sizeof(my_hepler<C>) - sizeof(C) )

to ensure that if we know that T is aligned on some value this define
will return this value
For example, for type T that is aligned on 16 bytes will this macro
always return 16 also?
I saw this but it does not work in my case.

E.g. i have
struct aligned16 {
char data __attribute__ aligned(16);
};

and aligned_at(aligned16) does not return 16 as it intended

is there any other way? thanks

I am getting an output of 16 and 4 using G++ with the following code:
___________________________________________________________________
template<typename T>
struct alignof_impl
{
struct holder
{
T m_state;
};


struct aligner
{
char m_pad;
holder m_holder;
};
};


#define ALIGNOF(T) \
(sizeof(alignof_impl<T>::aligner) - \
sizeof(alignof_impl<T>::holder))


#define ATTR_ALIGN(V) __attribute__((aligned(V)))




#include <cstdio>


struct my_struct
{
char ATTR_ALIGN(16) buffer;
};


int
main()
{
{
std::printf("my_struct = %lu\n"
"my_struct& = %lu\n",
(unsigned long)ALIGNOF(my_struct),
(unsigned long)ALIGNOF(my_struct&));
}

return 0;
}

___________________________________________________________________




What output are you getting?
 
A

alariq

"alariq" <[email protected]> wrote in message
What output are you getting?
16 on PC
and still 1 on Wii

2 Noah Roberts:
thanks for the answer but it is not that i really want. what was the
point is to get align value in compile time for arbitrary datatype.
the problem is that shown "solution" should not necessarily give right
results by my opinion, but as far as i saw it and encountered problems
using it i asked this question.
 
S

SG

alariq said:
is it correct to use this define

typedef<typeame T>
struct my_helper {
char c;
T data;
};

#define aligned_at(C) ( sizeof(my_hepler<C>) - sizeof(C) )
Neat!

to ensure that if we know that T is aligned on some value this define
will return this value For example, for type T that is aligned on 16
bytes will this macro always return 16 also?

I would have thought so. Maybe it's not guaranteed by the standard but
it surely makes a lot of sense to me.
I saw this but it does not work in my case.

E.g. i have
struct aligned16 {
    char data __attribute__ aligned(16);
};

and aligned_at(aligned16) does not return 16 as it intended

Maybe your compiler just doesn't honor this attribute?
What does sizeof(aligned16) return?

Cheers,
SG
 
C

Chris M. Thomasson

16 on PC
and still 1 on Wii
2 Noah Roberts:
thanks for the answer but it is not that i really want. what was the
point is to get align value in compile time for arbitrary datatype.
the problem is that shown "solution" should not necessarily give right
results by my opinion, but as far as i saw it and encountered problems
using it i asked this question.

Does the compiler give any warnings about align directives being ignored?
 
C

Chris M. Thomasson


FWIW, the construct above goes not give correct answer when you pass it a
reference. Here is an example:
______________________________________________________________
template<typename T>
struct my_helper {
char c;
T data;
};

#define aligned_at(C) ( sizeof(my_helper<C>) - sizeof(C) )




#include <cstdio>


#define ATTR_ALIGN(V) __attribute__((aligned(V)))


struct my_struct
{
char ATTR_ALIGN(16) buffer;
};


int
main()
{
{
std::printf("my_struct = %lu\n"
"my_struct& = %lu\n",
(unsigned long)aligned_at(my_struct),
(unsigned long)aligned_at(my_struct&));
}

return 0;
}

______________________________________________________________




I am getting the following output:

my_struct = 16
my_struct& = 4294967288




<corrected align hack>
______________________________________________________________
template<typename T>
struct alignof_impl
{
struct holder
{
T m_state;
};


struct aligner
{
char m_pad;
holder m_holder;
};
};


#define ALIGNOF(T) \
(sizeof(alignof_impl<T>::aligner) - \
sizeof(alignof_impl<T>::holder))


#define ATTR_ALIGN(V) __attribute__((aligned(V)))




#include <cstdio>


struct my_struct
{
char ATTR_ALIGN(16) buffer;
};


int
main()
{
{
std::printf("my_struct = %lu\n"
"my_struct& = %lu\n",
(unsigned long)ALIGNOF(my_struct),
(unsigned long)ALIGNOF(my_struct&));
}

return 0;
}

______________________________________________________________




I get:


my_struct = 16
my_struct& = 4


[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,158
Messages
2,570,881
Members
47,414
Latest member
djangoframe

Latest Threads

Top