C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

'Portable' Measurement of Pointer Alignment in C?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
Brian Gladman
Guest





PostPosted: Thu Mar 29, 2007 6:24 am    Post subject: 'Portable' Measurement of Pointer Alignment in C? Reply with quote



A lot of low level cryptographic code and some hardware cryptographic
accelerators either fail completely or perform very poorly if their input,
output and key storage areas in memory are not aligned on specific memory
boundaries. Moreover, in many situations the cryptographic code does not
itself have any control over the memory alignment of its parameters so the
best it can do is to detect if these aligmments are correct and act
accordingly. This hence rasises the question of the most appropriate way
of determining the aligment of a pointer. Here my interests is less in the
'political' correctness of C code but rather in what method is most likely
to work in practice on the highest proportion of widely deployed processors
and C compilers.

For example, when 'x' is a pointer of some kind, 'n' is a power of two and
'pint' is a pointer sized integer type, on what proportion of systems will:

#define PTR_OFFSET(x,n) (((pint)x) & ((n) - 1))

return the alignment of 'x ' from an 'n' byte boundary in memory? Is:

#define PTR_OFFSET(x,n) (((char*)(x) - (char*)0) & ((n) - 1))

any better (or worse)? Or, on systems that allow for the declaration of
aligned variables, is:

declare_aligned(n) type var;
#define PTR_OFFSET(x,n) (((char*)(x) - (char*)&var) & ((n) - 1))

any better?

I would much appreciate any observations that people here might have on the
most portable (in practice) ways in which pointer alignment might be
determined when the pointer itself is not under the control of the software
that needs to determine this.

Brian Gladman
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Hans-Bernhard Bröker
Guest





PostPosted: Mon Apr 16, 2007 8:17 pm    Post subject: Re: 'Portable' Measurement of Pointer Alignment in C? Reply with quote



Brian Gladman wrote:
Quote:
A lot of low level cryptographic code and some hardware cryptographic
accelerators either fail completely or perform very poorly if their
input, output and key storage areas in memory are not aligned on
specific memory boundaries.

Well, that's far into "Don't do that, then!" country. At least one
instance of either undefined behaviour (bad pointer cast) or decidedly
suboptimal toolchain behaviour (insufficiently aligned heap allocations)
has to occur before this can become a real problem.

Quote:
Here my interests is less in the 'political' correctness of C
code but rather in what method is most likely to work in practice on the
highest proportion of widely deployed processors and C compilers.

Cast the pointer to unsigned long, if you're in C90, or to C99's
intptr_t, and assume that integer is the actual address. If that
doesn't work, nothing else is likely to.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
WillerZ
Guest





PostPosted: Mon Apr 16, 2007 8:18 pm    Post subject: Re: 'Portable' Measurement of Pointer Alignment in C? Reply with quote



Brian Gladman wrote:
Quote:
A lot of low level cryptographic code and some hardware cryptographic
accelerators either fail completely or perform very poorly if their
input, output and key storage areas in memory are not aligned on
specific memory boundaries. Moreover, in many situations the
cryptographic code does not itself have any control over the memory
alignment of its parameters so the best it can do is to detect if these
aligmments are correct and act accordingly. This hence rasises the
question of the most appropriate way of determining the aligment of a
pointer. Here my interests is less in the 'political' correctness of C
code but rather in what method is most likely to work in practice on the
highest proportion of widely deployed processors and C compilers.

For example, when 'x' is a pointer of some kind, 'n' is a power of two
and 'pint' is a pointer sized integer type, on what proportion of
systems will:

#define PTR_OFFSET(x,n) (((pint)x) & ((n) - 1))

return the alignment of 'x ' from an 'n' byte boundary in memory? Is:

#define PTR_OFFSET(x,n) (((char*)(x) - (char*)0) & ((n) - 1))

any better (or worse)? Or, on systems that allow for the declaration of
aligned variables, is:

declare_aligned(n) type var;
#define PTR_OFFSET(x,n) (((char*)(x) - (char*)&var) & ((n) - 1))

any better?

I would much appreciate any observations that people here might have on
the most portable (in practice) ways in which pointer alignment might be
determined when the pointer itself is not under the control of the
software that needs to determine this.

Brian Gladman

In C99 I would prefer:

#include <stdint.h>

inline uintptr_t PRT_OFFSET(void *x, uintptr_t n)
{
return ((uintptr_t)x) & (n-1);
}

Of the three #define statements above, the central one is (IMO) most
likely to work. I would expect difficulties with the top definition on
IBM System i. The third definition is obviously useless without the
option of declaring aligned variables.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Thomas Richter
Guest





PostPosted: Mon Apr 16, 2007 8:18 pm    Post subject: Re: 'Portable' Measurement of Pointer Alignment in C? Reply with quote

Brian Gladman wrote:

Quote:
A lot of low level cryptographic code and some hardware cryptographic
accelerators either fail completely or perform very poorly if their
input, output and key storage areas in memory are not aligned on
specific memory boundaries. Moreover, in many situations the
cryptographic code does not itself have any control over the memory
alignment of its parameters so the best it can do is to detect if these
aligmments are correct and act accordingly. This hence rasises the
question of the most appropriate way of determining the aligment of a
pointer. Here my interests is less in the 'political' correctness of C
code but rather in what method is most likely to work in practice on the
highest proportion of widely deployed processors and C compilers.

For example, when 'x' is a pointer of some kind, 'n' is a power of two
and 'pint' is a pointer sized integer type, on what proportion of
systems will:

#define PTR_OFFSET(x,n) (((pint)x) & ((n) - 1))


The above would work with:

#include <stdint.h>

typedef intptr_t pint;

The "intptr_t" type of stdint.h is an integer large enough to hold a
pointer without loss.

Another candidate (I'm using here) is to cast the pointer to ptrdiff_t,
though (on wierd systems) this might be smaller than an intptr_t, thus
the first option should probably be your best candidate.

So long,
Thomas
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Douglas A. Gwyn
Guest





PostPosted: Thu Apr 19, 2007 7:13 pm    Post subject: Re: 'Portable' Measurement of Pointer Alignment in C? Reply with quote

David Given wrote:
Quote:
However, I don't think there are any platforms where pointers aren't unit
aligned; ...

It's fairly common for some of the wider types (long long and double,
for example) to have a hardware-mandated alignment that is not as
strict as the full width. Some architectures support arbitrary byte
alignment of the built-in types, although it is usually more efficient
to nevertheless use a wider alignment (to match memory subsystem width).
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Keith Thompson
Guest





PostPosted: Thu Apr 19, 2007 7:13 pm    Post subject: Re: 'Portable' Measurement of Pointer Alignment in C? Reply with quote

"Douglas A. Gwyn" <DAGwyn (AT) null (DOT) net> writes:
Quote:
Brian Gladman wrote:
I would much appreciate any observations that people here might have on the
most portable (in practice) ways in which pointer alignment might be
determined when the pointer itself is not under the control of the software
that needs to determine this.

I think a much better approach would be for the library to allocate its own
buffers, ensuring that they are properly aligned.

On the rare occasions when I am trying to ensure such address alignment,
I use the following macros:
#define AlignDown(p) ((uintptr_t)(p)/ALIGN_SIZE*ALIGN_SIZE)
#define AlignUp(p) (((uintptr_t)(p)+(ALIGN_SIZE-1))/ALIGN_SIZE*ALIGN_SIZE)
and let the compiler take care of optimization.

That's likely to work on most systems, but it's not 100% portable. It
depends on assumptions about pointer representations that aren't
guaranteed by the standard. I've worked on systems where this would
fail (because the low-order bits of a pointer don't correspond to byte
addresses).

--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Brian Gladman
Guest





PostPosted: Thu Apr 19, 2007 7:15 pm    Post subject: Re: 'Portable' Measurement of Pointer Alignment in C? Reply with quote

"Douglas A. Gwyn" <DAGwyn (AT) null (DOT) net> wrote in message
news:clcm-20070416-0022 (AT) plethora (DOT) net...
Quote:
Brian Gladman wrote:
I would much appreciate any observations that people here might have on
the
most portable (in practice) ways in which pointer alignment might be
determined when the pointer itself is not under the control of the
software
that needs to determine this.

My thanks to everyone who responded to my post. I should mention that, as
this moderated group didn't appear to work very rapidly, I also aksed this
question comp.lang.c and got quite a few helpful answers.

Quote:
I think a much better approach would be for the library to allocate its
own
buffers, ensuring that they are properly aligned.

I agree and where I have control over buffer allocation, this is what I do.

The situations in which the issue I raised comes up is when I am delivered a
buffer and I don't know its alignment but what I have to do depends on this.

Quote:
On the rare occasions when I am trying to ensure such address alignment,
I use the following macros:
#define AlignDown(p) ((uintptr_t)(p)/ALIGN_SIZE*ALIGN_SIZE)
#define AlignUp(p) (((uintptr_t)(p)+(ALIGN_SIZE-1))/ALIGN_SIZE*ALIGN_SIZE)
and let the compiler take care of optimizations

Thanks for this suggestion.

Right now I am casting to an unsigned char and using an AND mask (I know
that my alignment requirement is a power of 2 not more than 256) because
most people who responded on comp.lang.c seemed to think that this was the
approach most likely to work on most systems. I didn't use uintptr_t since
I am suspicious that this is a C99 invention - was it available in the
earlier C standards?

Thanks again to all who have contributed.

Brian Gladman
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Brian Gladman
Guest





PostPosted: Thu Apr 19, 2007 7:15 pm    Post subject: Re: 'Portable' Measurement of Pointer Alignment in C? Reply with quote

"David Given" <dg (AT) cowlark (DOT) com> wrote in message
news:clcm-20070416-0012 (AT) plethora (DOT) net...
Quote:
Brian Gladman wrote:
[...]
I would much appreciate any observations that people here might have on
the
most portable (in practice) ways in which pointer alignment might be
determined when the pointer itself is not under the control of the
software
that needs to determine this.

I'm not entirely sure what you're asking here; do you want to know the
alignment requirements of a pointer type on your system, or the alignment
of a
specific pointer argument you've been given?

The latter.

Quote:
For (b), you should be able to do this:

intptr_t arg_alignment = (intptr_t)arg & (page_size-1);

...where page_size varies according to the size of the thing you want to
measure alignment against. But that's so trivial that I don't think it was
really what you wanted to know.

Yes, this was what I was after. I was wondering about the proportion of
widely deployed systems this would work on.

I know of some systems where the low end of a pointer won't indicate a
pointer's alignment so I was seeking evidence on whether such systems are
around in sufficient numbers to make this simple aligmnment test dangerous
in practice.

Thanks for your help on this (thanks also to other who have responded).

Brian Gladman
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.