Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   alignment shame (?) (http://www.velocityreviews.com/forums/t954979-alignment-shame.html)

fir 11-30-2012 09:25 AM

alignment shame (?)
 
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)


Noob 11-30-2012 09:46 AM

Re: alignment shame (?)
 
fir wrote:
> 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_%2...rd_revision%29
_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?


fir 11-30-2012 11:24 AM

Re: alignment shame (?)
 
W dniu piątek, 30 listopada 2012 10:46:23 UTC+1 użytkownik Noob napisał:
> fir wrote:
>
> > 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_%2...rd_revision%29
>
> _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)



Malcolm McLean 11-30-2012 01:22 PM

Re: alignment shame (?)
 
On Friday, November 30, 2012 9:25:27 AM UTC, fir wrote:
> 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.

--
Visit Malcolm's website
http://www.malcolmmclean.site11.com/www




James Kuyper 11-30-2012 01:52 PM

Re: alignment shame (?)
 
On 11/30/2012 08:22 AM, Malcolm McLean wrote:
> On Friday, November 30, 2012 9:25:27 AM UTC, fir wrote:
>> 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.


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.
--
James Kuyper

fir 11-30-2012 02:55 PM

Re: alignment shame (?)
 
W dniu piątek, 30 listopada 2012 14:52:16 UTC+1 użytkownik James Kuyper napisał:
> On 11/30/2012 08:22 AM, Malcolm McLean wrote:
> > On Friday, November 30, 2012 9:25:27 AM UTC, fir wrote:
> >> 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.

>
> 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) ?

ImpalerCore 11-30-2012 02:56 PM

Re: alignment shame (?)
 
On Nov 30, 4:25*am, fir <profesor....@gmail.com> wrote:
> 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

fir 11-30-2012 03:17 PM

Re: alignment shame (?)
 
W dniu piątek, 30 listopada 2012 15:56:48 UTC+1 użytkownik ImpalerCore napisał:
> On Nov 30, 4:25*am, fir <profesor....@gmail.com> wrote:
> > I am not sure how it is in c, but alignment seem
> > to be mess in the world of c and c++

>

(...)

> \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

ImpalerCore 11-30-2012 03:42 PM

Re: alignment shame (?)
 
On Nov 30, 10:17*am, fir <profesor....@gmail.com> wrote:
> W dniu piątek, 30 listopada 2012 15:56:48 UTC+1 użytkownik ImpalerCore napisał:> On Nov 30, 4:25*am, fir <profesor....@gmail.com> wrote:
> > > I am not sure how it is in c, but alignment seem
> > > to be mess in the world of c and c++

>
> *(...)
>
> > \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


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.

James Kuyper 11-30-2012 04:45 PM

Re: alignment shame (?)
 
On 11/30/2012 10:42 AM, ImpalerCore wrote:
....
>> 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.


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.


All times are GMT. The time now is 04:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.