Velocity Reviews > Alignment

# Alignment

Peter J. Holzer
Guest
Posts: n/a

 08-20-2007
On 2007-08-20 03:47, Ark Khasin <(E-Mail Removed)> wrote:
> And, taking off my assembler motley, may I ask again what is alignment?
> Is there some sort of conspiracy and addresses in fact /are/ some sort
> of numbers?

(if it didn't, pointer arithmetic would not be possible). In a flat
address space (the usual case today), the address consists only of a
single number. In a segmented address space, it contains (at least) two
numbers: A segment number and a segment offset, where arithmetic is
often only meaningful on the offset. An address can contain other
information (type information, flags, etc.).

Of course you can interpret any collection of bits as a number ...

hp

--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | http://www.velocityreviews.com/forums/(E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"

Peter J. Holzer
Guest
Posts: n/a

 08-20-2007
On 2007-08-19 22:07, Malcolm McLean <(E-Mail Removed)> wrote:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "Malcolm McLean" <(E-Mail Removed)> writes:
>>> <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> How to declare a variable guaranteed to have the strictest possible
>>>> alignment?
>>> In practise, align to a double.

[...]
>> What if long double has stricter alignment requirements than double?
>>

> Computer like things in units of powers of two.
> doubles are almost always 64 bits, in fact have to be if IEEE. long doubles
> are 80.

No. long doubles are 80 on Intel x86. I've seen 96 bit and 128 bit long
doubles. It is entirely possible that a 128 bit long double needs to be
aligned on a 128 bit boundary.

hp

--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"

fishpond@invalid.com
Guest
Posts: n/a

 08-20-2007
On 19 Aug 2007 at 21:38, Harald van Dĳk wrote:
> (E-Mail Removed) wrote:
>> On 19 Aug 2007 at 21:22, Keith Thompson wrote:
>>> If you want something guaranteed to be aligned for any type, use
>>> malloc(). Or check your system's documentation.

>>
>> It's hard to believe that something this simple can't be achieved in
>> Standard C. I mean, if that's true, how would you ever get type punning
>> going?

>
> Type punning isn't something you should do in code that's meant to be
> portable, and if the code isn't meant to be portable, it shouldn't matter
> as much that the behaviour is defined by your implementation rather than by
> the standard.

But some type punning can be done in completely Standard C. For example,
deciding whether a float is positive or negative by treating it as an
int and extracting the sign bit uses only bit-shifting which is a
Standard C operator.

--
"Irrationality is the square root of all evil"

Ian Collins
Guest
Posts: n/a

 08-20-2007
(E-Mail Removed) wrote:
> On 19 Aug 2007 at 21:38, Harald van Dĳk wrote:
>> (E-Mail Removed) wrote:
>>> On 19 Aug 2007 at 21:22, Keith Thompson wrote:
>>>> If you want something guaranteed to be aligned for any type, use
>>>> malloc(). Or check your system's documentation.
>>> It's hard to believe that something this simple can't be achieved in
>>> Standard C. I mean, if that's true, how would you ever get type punning
>>> going?

>> Type punning isn't something you should do in code that's meant to be
>> portable, and if the code isn't meant to be portable, it shouldn't matter
>> as much that the behaviour is defined by your implementation rather than by
>> the standard.

>
> But some type punning can be done in completely Standard C. For example,
> deciding whether a float is positive or negative by treating it as an
> int and extracting the sign bit uses only bit-shifting which is a
> Standard C operator.
>

What if float and int are different sizes?

--
Ian Collins.

Peter J. Holzer
Guest
Posts: n/a

 08-20-2007
On 2007-08-20 09:51, (E-Mail Removed) <(E-Mail Removed)> wrote:
> On 19 Aug 2007 at 21:38, Harald van Dĳk wrote:
>> (E-Mail Removed) wrote:
>>> On 19 Aug 2007 at 21:22, Keith Thompson wrote:
>>>> If you want something guaranteed to be aligned for any type, use
>>>> malloc(). Or check your system's documentation.
>>>
>>> It's hard to believe that something this simple can't be achieved in
>>> Standard C. I mean, if that's true, how would you ever get type punning
>>> going?

>>
>> Type punning isn't something you should do in code that's meant to be
>> portable, and if the code isn't meant to be portable, it shouldn't matter
>> as much that the behaviour is defined by your implementation rather than by
>> the standard.

>
> But some type punning can be done in completely Standard C. For example,
> deciding whether a float is positive or negative by treating it as an
> int and extracting the sign bit uses only bit-shifting which is a
> Standard C operator.

You can't use >> on a float. You would need to do something like this:

union {
float f;
unsigned u;
} u;

assert(sizeof float == sizeof unsigned);
u.f = 42.23;
sign = u.u >> (CHAR_BIT * sizeof unsigned - 1);

The actual type punning is done via accesses to the fields of the union.

And I think this answers the question why C doesn't provide a way to
declare a variable which can hold any type: You have to explicitely
program all accesses with different types, so there can only be a finite
number (or your program would be infinitely big), so you can just create
a union with the appropriate members and the compiler will make sure it
is correctly aligned and large enough.

Apart from that, malloc exists, as Keith already noted.

hp

--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"

Ivar Rosquist
Guest
Posts: n/a

 08-20-2007
On Sun, 19 Aug 2007 23:07:43 +0100, Malcolm McLean wrote:

> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "Malcolm McLean" <(E-Mail Removed)> writes:
>>> <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> How to declare a variable guaranteed to have the strictest possible
>>>> alignment?
>>>>
>>> In practise, align to a double.
>>> eg
>>>
>>> union alignedint
>>> {
>>> double dummy;
>>> int x;
>>> }
>>>
>>> x will now have double alignement.

>>
>> What if long double has stricter alignment requirements than double?
>>

> Computer like things in units of powers of two. doubles are almost
> always 64 bits, in fact have to be if IEEE. long doubles are 80. So the
> alignment requirements for double are likely to be stricter. However you
> are right, in practise isn't the same as "by the standard". It might
> even be storing up trouble for the future to advise such a thing. I

go back to your specialty - C is NOT it.

CBFalconer
Guest
Posts: n/a

 08-20-2007
(E-Mail Removed) wrote:
>

.... snip ...
>
> But some type punning can be done in completely Standard C. For
> example, deciding whether a float is positive or negative by
> treating it as an int and extracting the sign bit uses only
> bit-shifting which is a Standard C operator.

But the number of bits in an int isn't standard. What's wrong in:

if (value < 0) return 1;
else return 0;

or the equivalent in other terms?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Eric Sosman
Guest
Posts: n/a

 08-20-2007
Ark Khasin wrote:
> Eric Sosman wrote:
>> Ark Khasin wrote:
>>>
>>> I cannot imagine why one needs a strictest-aligned variable of static
>>> duration, but the following would work
>>> typedef union {
>>> char c;
>>> short s;
>>> int i;
>>> long l;
>>> float f;
>>> double d;
>>> long double ld;
>>> long long ll;
>>> //and whatever native types I missed

>>
>> "Aye: There's the rub." -- H, P of D
>>
>>> } strict_align_t;
>>> strict_align_t myvar;

>>

> Of course. Will the addition of "void *v; const void *cv;" do it?

It can't hurt (but it's not necessary to add both; the
qualifiers don't affect the alignment requirement). Include
a function pointer while you're about it, and some struct
types (a struct might require stricter alignment than its
members), and size_t, and intmax_t, and ... The "rub" is
that the list of possible types is potentially infinite.

<off-topic>

There's another "rub," too, which is that some alignment
requirements of the platform may not correspond to any C type
at all. "Page alignment," for example, is likely to be hard
to achieve with this approach. On systems that support multiple
page sizes, testing for page alignment uses a divisor specific to
the "arena" containing the address. A static, location-independent
scheme like the above cannot work on such systems, even if you can
find a page-sized C data type.

</off-topic>

> And, taking off my assembler motley, may I ask again what is alignment?
> Is there some sort of conspiracy and addresses in fact /are/ some sort
> of numbers?

Peter J. Holzer's explanation elsethread is good.

--
Eric Sosman
(E-Mail Removed)lid

Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a

 08-20-2007
Ian Collins wrote:
> (E-Mail Removed) wrote:
>> On 19 Aug 2007 at 21:38, Harald van Dĳk wrote:
>>> (E-Mail Removed) wrote:
>>>> On 19 Aug 2007 at 21:22, Keith Thompson wrote:
>>>>> If you want something guaranteed to be aligned for any type, use
>>>>> malloc(). Or check your system's documentation.
>>>> It's hard to believe that something this simple can't be achieved in
>>>> Standard C. I mean, if that's true, how would you ever get type punning
>>>> going?
>>> Type punning isn't something you should do in code that's meant to be
>>> portable, and if the code isn't meant to be portable, it shouldn't
>>> matter as much that the behaviour is defined by your implementation
>>> rather than by the standard.

>>
>> But some type punning can be done in completely Standard C. For example,
>> deciding whether a float is positive or negative by treating it as an
>> int and extracting the sign bit uses only bit-shifting which is a
>> Standard C operator.
>>

> What if float and int are different sizes?

And even if they are the same size, how can you portably find which bit is
the sign bit?

CBFalconer
Guest
Posts: n/a

 08-20-2007
Ark Khasin wrote:
> Eric Sosman wrote:
>> Ark Khasin wrote:
>>>
>>> I cannot imagine why one needs a strictest-aligned variable of
>>> static duration, but the following would work
>>> typedef union {
>>> char c;
>>> short s;
>>> int i;
>>> long l;
>>> float f;
>>> double d;
>>> long double ld;
>>> long long ll;
>>> //and whatever native types I missed

>>
>> "Aye: There's the rub." -- H, P of D
>>
>>> } strict_align_t;
>>> strict_align_t myvar;

>
> Of course. Will the addition of "void *v; const void *cv;" do it?
>
> And, taking off my assembler motley, may I ask again what is
> alignment? Is there some sort of conspiracy and addresses in
> fact /are/ some sort of numbers?

Even all this is suspect. Nothing forbids saying, for example,
that chars must be aligned on odd addresses, short on multiple of
2, double on multiple of 4, ints on even multiples of 8, pointers
on multiples of 3. Granted the result is peculiar. It is up to
the implementor to satisfy all these for malloc/realloc.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
n

--
Posted via a free Usenet account from http://www.teranews.com