alignment shame (?)

F

fir

I am not sure how it is in c, but alignment seem
to be mess in the world of c and c++
Is it standarized as to today state of these
languages?

(to include such solution to language seem to be
easy for me, just some thing like

align(4096) int table[4096];

or some better looking modifier that could be
used for any thing needed to be define aligned)
 
N

Noob

fir said:
I am not sure how it is in c, but alignment seem
to be mess in the world of c and c++

You're not sure for C, but you know for C?
Is it standarized as to today state of these languages?

As far as C is concerned, yes.
https://en.wikipedia.org/wiki/C11_(C_standard_revision)
_Alignas specifier,
alignof operator,
aligned_alloc function,
<stdalign.h> header file

C++ is obviously off-topic in comp.lang.c
(to include such solution to language seem to be
easy for me, just some thing like

align(4096) int table[4096];

or some better looking modifier that could be
used for any thing needed to be define aligned)

Why don't you define your own language, or write your own
compiler with extensions, and stop trolling this newsgroup,
then?
 
F

fir

W dniu piątek, 30 listopada 2012 10:46:23 UTC+1 użytkownik Noob napisał:
fir said:
I am not sure how it is in c, but alignment seem
to be mess in the world of c and c++



You're not sure for C, but you know for C?


Is it standarized as to today state of these languages?



As far as C is concerned, yes.

https://en.wikipedia.org/wiki/C11_(C_standard_revision)

_Alignas specifier,

alignof operator,

aligned_alloc function,

<stdalign.h> header file



C++ is obviously off-topic in comp.lang.c


(to include such solution to language seem to be
easy for me, just some thing like
align(4096) int table[4096];
or some better looking modifier that could be
used for any thing needed to be define aligned)



Why don't you define your own language, or write your own

compiler with extensions, and stop trolling this newsgroup,

then?

man youre agressive, if it is your psychical
need youre welcome to do it if you must, but not to much (for my personal taste it do not
qualify to talk on such level)

personally I do not know to much about present standards, though it do not exclude some deep
'involveness' on some other area of thinking
about c language (I am involved in spirit of
it as I call it and also do some amount of
'theory' of some paradigms; standards are less
known to me so I found it is some place to ask about it here, maybe)
 
M

Malcolm McLean

I am not sure how it is in c, but alignment seem
to be mess in the world of c and c++
Is it standarized as to today state of these
languages?
I think something was broguht in in C99.

But the problem is that if you want to do this

unsigned char pool[1000000];

/* allocate memory from pool */
void *mymalloc(int size)

there's no fully portable way in c89 of ensuring that the meory blocks are
suitably aligned for any purpose. For virtually all real platforms you can
achieve this by aligning to double, but nothing guarantees it.

It's a minor glitch. It means that a few routines may need care in porting,
in sanely written code.
 
J

James Kuyper

I think something was broguht in in C99.

No, that didn't happen until C2011
But the problem is that if you want to do this

unsigned char pool[1000000];

/* allocate memory from pool */
void *mymalloc(int size)

there's no fully portable way in c89 of ensuring that the meory blocks are
suitably aligned for any purpose. For virtually all real platforms you can
achieve this by aligning to double, but nothing guarantees it.

Whereas, in C2011, it can be done using

#include <stddef.h>
_Alignas(max_align_t) unsigned char pool[1000000];

You'd probably want to use _Alignof(max_align_t) somewhere in the
computation of the pool's size.
 
F

fir

W dniu piątek, 30 listopada 2012 14:52:16 UTC+1 użytkownik James Kuyper napisał:
I think something was broguht in in C99.

No, that didn't happen until C2011
But the problem is that if you want to do this
unsigned char pool[1000000];
/* allocate memory from pool */
void *mymalloc(int size)
there's no fully portable way in c89 of ensuring that the meory blocks are
suitably aligned for any purpose. For virtually all real platforms you can
achieve this by aligning to double, but nothing guarantees it.



Whereas, in C2011, it can be done using
#include <stddef.h>
_Alignas(max_align_t) unsigned char pool[1000000];

You'd probably want to use _Alignof(max_align_t) somewhere in the
computation of the pool's size.

can I use it to align 2-dim array such way? :

_Alignas(4096) char table[100][4096];

(I would expect here that each row will fit one 4KB ram page (in quest for searching for
some speed maybe) ?
 
I

ImpalerCore

I am not sure how it is in c, but alignment seem
to be mess in the world of c and c++
Is it standarized as to today state of these
languages?

(to include such solution to language seem to be
easy for me, just some thing like

align(4096) int table[4096];

or some better looking modifier that could be
used for any thing needed to be define aligned)

You can create a macro that leverages either C11 alignas or a compiler
extension to align an array. This is an excerpt from the library I'm
working on.

\code
/* The symbol 'stdalign_alignof_defined' is temporarily used to
record whether or not the 'alignof' macro is available. */
#undef stdalign_alignof_defined

/* The symbol 'stdalign_alignas_defined' is temporarily used to
record whether or not the 'alignas' macro is available. */
#undef stdalign_alignas_defined

/*
* If C11 standard support is available, then 'alignof' is directly
* available, and the declaration syntax for a typed variable with
* aligned storage can be defined directly in terms of 'alignas'.
*/
#if defined(__STDC__) && defined(__STDC_VERSION__)
# if (__STDC__ && __STDC_VERSION__ >= 201112L)
# include <stdalign.h>
# if defined(__alignof_is_defined)
# define stdalign_alignof_defined
# endif
# if defined(__alignas_is_defined)
# define stdalign_alignas_defined
# endif
# endif
#endif

/*!
* \brief Determine the alignment of the type in bytes.
* \param type A type.
*
* The number of bytes returned is not guaranteed to be the smallest
* alignment allowed by the platform if \c alignof is not natively
* available from the compiler. Without \c alignof, the \c offsetof
* operator can determine a compatible alignment for the given type.
*
* \usage
* \include align/alignof_example.c
*
* The example above should display something similar to the
* following.
*
* \code
* alignof example
* -------------------------------------
* | type | sizeof | alignof |
* -------------------------------------
* char 1 1
* short int 2 2
* int 4 4
* long int 4 4
* long long int 8 8
* float 4 4
* double 8 8
* long double 12 4
* size_t 4 4
* ptrdiff_t 4 4
* void* 4 4
* enum align_e 4 4
* struct align_s 16 8
* align_func 4 4
*
* ALIGN_MAX = 8
* \endcode
*/
#if !defined(stdalign_alignof_defined)
# define alignof(type) (offsetof (struct { char c; type t; }, t))
#endif

#undef stdalign_alignof_defined

/*!
* \brief Declare a variable with aligned storage.
* \param type The type.
* \param alignment The alignment constraint.
*
* This macro depends on the availability of \c alignas in C11 or
* compiler extensions such as \c __attribute__ or \c __declspec to
* specify an alignment constraint. An alignment constraint can be
* specified when declaring variables with static or automatic
* storage or for variables declared in \c struct definitions.
*
* \usage
* \include align/c_aligned_storage_example.c
*
* The example above should display something similar to the
* following.
*
* \code
* fv1 0022ff60 = [1,2,3,4]
* fv2 0022ff50 = [5,6,7,8]
*
* dot product = 70
* \endcode
*
* The key to using the aligned intrinsic \c _mm_load_ps in the
* dot product function is to ensure that the vectors \c fv1 and
* \c fv2 are aligned to an address on a 16-byte boundary.
*
* The alignment of variables with static storage is dependent on
* the capabilities of the linker. If the linker is only able to
* align variables up to a maximum of 16 bytes, then requesting a
* buffer with a stricter alignment is not possible. Often the
* compiler will generate a warning if an alignment constraint
* cannot be met.
*/
#if defined(stdalign_alignas_defined)
# define c_aligned_storage(type, alignment) alignas(alignment) type
#else
# if defined(__GNUC__)
# define c_aligned_storage(type, alignment) type
__attribute__((aligned (alignment)))
# elif defined(_MSC_VER) && (_MSC_VER >= 1310)
# define c_aligned_storage(type, alignment) __declspec(align
(alignment)) type
# else
# define c_aligned_storage(type, alignment) type
# endif
#endif

#undef stdalign_alignas_defined
\endcode

Then you can use it like the following

\code
#include <stdio.h>
#include <xmmintrin.h>
#include <clover/constraint.h>
#include <clover/align.h>

/* SSE Intrinsics support must be enabled by adding '-msse' to
gcc or '/arch:SSE' to cl. */

static inline void sse_v4sf_dot( float* dot,
const float* v1,
const float* v2 )
{
__m128 mv1, mv2, mbuf1, mbuf2;
mv1 = _mm_load_ps( v1 );
mv2 = _mm_load_ps( v2 );
mbuf1 = _mm_mul_ps( mv1, mv2 );
mbuf2 = _mm_shuffle_ps( mbuf1, mbuf1, _MM_SHUFFLE( 3, 2, 3, 2 ) );
mbuf1 = _mm_add_ps( mbuf1, mbuf2 );
mbuf2 = _mm_shuffle_ps( mbuf1, mbuf1, _MM_SHUFFLE( 0, 1, 0, 1 ) );
mbuf1 = _mm_add_ss( mbuf1, mbuf2 );
_mm_store_ss( dot, mbuf1 );
}

int main( void )
{
float dot_product;

c_aligned_storage(float, 16) fv1[4] = { 1, 2, 3, 4 };
c_aligned_storage(float, 16) fv2[4] = { 5, 6, 7, 8 };

printf( "fv1 %p = [%.0f,%.0f,%.0f,%.0f]\n",
fv1, fv1[0], fv1[1], fv1[2], fv1[3] );
printf( "fv2 %p = [%.0f,%.0f,%.0f,%.0f]\n",
fv2, fv2[0], fv2[1], fv2[2], fv2[3] );
printf( "\n" );

c_exit_if_fail( c_is_aligned_ptr( fv1, 16 ),
"Vector not properly aligned for SSE" );
c_exit_if_fail( c_is_aligned_ptr( fv2, 16 ),
"Vector not properly aligned for SSE" );

sse_v4sf_dot( &dot_product, fv1, fv2 );
printf( "dot product = %.0f\n", dot_product );

return 0;
}
\endcode

Best regards,
John D.

P.S. I have not actually tested it on a C11 compiler yet ... will get
there eventually
 
F

fir

W dniu piątek, 30 listopada 2012 15:56:48 UTC+1 użytkownik ImpalerCore napisał:
(...)

\endcode

John D.

This is much trouble. I need simple
modifier to align anything to any
boundary, Standarized one would be
the best. Simple Alignas_(value) would be
good if it will work in broad scope and good
 
I

ImpalerCore

This is much trouble. I need simple
modifier to align anything to any
boundary, Standarized one would be
the best. Simple Alignas_(value) would be
good if it will work in broad scope and good

If you want simple, you'll have to restrict yourself to a single
platform and compiler that does what you want, which is often good
enough for applications, but less so for libraries. If you want to
support multiple platforms/compilers with varying feature support,
simple goes out the door. Even so, although defining
'c_aligned_storage' is not simple, using it is.

c_aligned_storage(int, 4096) table[4096 / sizeof (int)];

Not all linkers support page sized alignments. An alternative to
using automatic storage is that most platforms do have some kind of
'aligned_malloc' that can allocate and return page-aligned blocks of
memory if you really need it.

Windows: _aligned_malloc, _aligned_free
POSIX: posix_memalign, free

Best regards,
John D.
 
J

James Kuyper

On 11/30/2012 10:42 AM, ImpalerCore wrote:
....
If you want simple, you'll have to restrict yourself to a single
platform and compiler that does what you want, which is often good
enough for applications, but less so for libraries.

Isn't _Alignas() adequate? It's a brand new feature of C2011, but it
already works on far more than one platform and one compiler, and will
probably be much more widely implemented in the near future.
 
F

fir

W dniu piątek, 30 listopada 2012 16:42:54 UTC+1 użytkownik ImpalerCore napisał:
This is much trouble. I need simple
modifier to align anything to any
boundary, Standarized one would be
the best. Simple Alignas_(value) would be
good if it will work in broad scope and good



If you want simple, you'll have to restrict yourself to a single

platform and compiler that does what you want, which is often good

enough for applications, but less so for libraries. If you want to

support multiple platforms/compilers with varying feature support,

simple goes out the door. Even so, although defining

'c_aligned_storage' is not simple, using it is.



c_aligned_storage(int, 4096) table[4096 / sizeof (int)];



Not all linkers support page sized alignments. An alternative to

using automatic storage is that most platforms do have some kind of

'aligned_malloc' that can allocate and return page-aligned blocks of

memory if you really need it.



Windows: _aligned_malloc, _aligned_free

POSIX: posix_memalign, free
alright,

does you or any other person know which
are the linkers that do not support such
4k-aligns and why? (it is interesting)

I complain here to the mess in this alignment
area (so I even called it a shame, couse
alignment is a topic for over 10 years and
it is still such unstandarized mess with
that :( )
 
F

fir

W dniu piątek, 30 listopada 2012 17:45:52 UTC+1 użytkownik James Kuyper napisał:
On 11/30/2012 10:42 AM, ImpalerCore wrote:

...




Isn't _Alignas() adequate? It's a brand new feature of C2011, but it
already works on far more than one platform and one compiler, and will
probably be much more widely implemented in the near future.


If it works ok, it would be quite good and
important (as to solution without judging
the syntax subtlities) (but does it working
good? can I align anything with any boundary
with that ?) What with 'padded' bytes are they
filled with zeroes?
 
I

ImpalerCore

On 11/30/2012 10:42 AM, ImpalerCore wrote:
...



Isn't _Alignas() adequate? It's a brand new feature of C2011, but it
already works on far more than one platform and one compiler, and will
probably be much more widely implemented in the near future.

From your post on Sept. 20, 2012:

My project is still governed by a 1997 agreement with our client which
requires us to write code which "conforms" to C90 plus the two
Technical
Corrigenda.

I'm curious how you would apply _Alignas in your scenario if you had a
need to use it.

I'm not saying that using _Alignas is wrong. It's just unfortunate
that the C landscape is so varied, and projects tend to become
entrenched within their language and coding standard choices.

I've had to write my own <stdint.h> wrappers because the embedded
compiler I was using didn't support them. I would love to use "zu"
instead of my own PRIuSIZE modifier but Windows compatibility is
important to me.

Best regards,
John D.
 
J

James Kuyper

From your post on Sept. 20, 2012:

My project is still governed by a 1997 agreement with our client which
requires us to write code which "conforms" to C90 plus the two
Technical
Corrigenda.

I'm curious how you would apply _Alignas in your scenario if you had a
need to use it.

I can't - but the OP only said that he would prefer a standardized
feature, which _Alignas() is. He didn't specify a particular version of
the standard that had to support the feature. The fact that he's willing
to even consider a non-standardized feature implies he's much less
strictly bound by coding standards than I am.
 
F

fir

W dniu piątek, 30 listopada 2012 20:07:34 UTC+1 użytkownik James Kuyper napisał:
(...)

I can't - but the OP only said that he would prefer a standardized
feature, which _Alignas() is. He didn't specify a particular version of
the standard that had to support the feature. The fact that he's willing
to even consider a non-standardized feature implies he's much less
strictly bound by coding standards than I am.


I would like to try some alignments searching
for possible speedups. By now I do not used
other than defaults, but would like to try if
4096-alignments of arrays would speed up and so. I do not even know by now how this
alignments things in distinct compilers look like and also how standarisation of it looks like also *. (Now I know it partially: must
check up compiler specyfic opts or possibly
search for _Alignas implementations (if it
can allign to as much as 4096)
 

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

Similar Threads

Alignment problems 20
struct alignment 14
Alignment problems 22
malloc and alignment question 8
gcc alignment options 19
Alignment of a structure. 6
Alignment, Cast 27
Power of two alignment (bit masks) 1

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top