Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Alignment of constants



Permlink Replies: 5 - Last Post: Oct 3, 2014 8:03 AM Last Post By: Rudy Velthuis (... Threads: [ Previous | Next ]
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Alignment of constants
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 2, 2014 11:26 PM
Hi,

Over different Delphi versions I could always do something like this:

const cOnes : Array[0..1] of double = (1, 1);

procedure FooAsm(pseudocode param1 to paramx );
asm
movapd xmm0, cOnes;

...
// or something like this:
subpd xmm0, cOnes;
end;

This always worked and I assumed that constants were aligned for SSE
use. Now I checked that with Delphi XE6 and my code gave me
access vioalations on that line above so I needed to use the
unaligned move command

procedure FooAsm(pseudocode param1 to paramx );
asm
movupd xmm0, cOnes;

...
//
movupd xmm1, cOnes;
subpd xmm0, xmm1;

end;

to get rid of it.

Any chance I can force the compiler to align the constants on 16Bytes
boundaries?

kind regards
Mike

Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Alignment of constants
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 3, 2014 2:15 AM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
Michael Rabatscher wrote:

Any chance I can force the compiler to align the constants on 16Bytes
boundaries?

Double is an 8 byte datatype, so it will be aligned on an 8 byte
boundary, but not necessarily on a 16 byte one. I don't think there is
a way to enforce this.
--
Rudy Velthuis http://www.rvelthuis.de

"Imagine if every Thursday your shoes exploded if you tied them
the usual way. This happens to us all the time with computers,
and nobody thinks of complaining." -- Jeff Raskin
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Alignment of constants
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 3, 2014 2:18 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB) wrote:

Michael Rabatscher wrote:

Any chance I can force the compiler to align the constants on
16Bytes boundaries?

Double is an 8 byte datatype, so it will be aligned on an 8 byte
boundary, but not necessarily on a 16 byte one. I don't think there is
a way to enforce this.

But I love to be proven wrong in this case.

--
Rudy Velthuis http://www.rvelthuis.de

"He has the attention span of a lightning bolt."
-- Robert Redford
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Alignment of constants
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 3, 2014 2:34 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB) wrote:

Michael Rabatscher wrote:

Any chance I can force the compiler to align the constants on
16Bytes boundaries?

Double is an 8 byte datatype, so it will be aligned on an 8 byte
boundary, but not necessarily on a 16 byte one. I don't think there is
a way to enforce this.

I guess you can enforce this by creating a record that starts with a
type that has a natural alignment of 16 bytes, followed by your array.
In 32 bit X86 mode, that can be Extended. I would not know what to use
in X64 though. There, Extended is aliased to Double and I don't know
any other Delphi type that has a natural alignment of 16.

In Win32:

  {$ALIGN 16}
 
  ...
  
  type
    Align16Ones = record
      Reserved: Extended;          // will be aligned on 16 bytes.
      Ones: array[0..1] of Double; // so this array too.
    end;
 
  const
    COnes: Align16Ones = (Reserved: 0.0; Ones: (1.0, 1.0));
 
  // Now COnes.Ones is aligned on 16 byte boundary.


--
Rudy Velthuis http://www.rvelthuis.de

"Nothing overshadows truth so much as authority."
-- Leon Battista Alberti
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Re: Alignment of constants
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 3, 2014 4:26 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Double is an 8 byte datatype, so it will be aligned on an 8 byte
boundary, but not necessarily on a 16 byte one. I don't think there is
a way to enforce this.

That's a pitty cause obviously it was different in previous versions
(otherwise my code would have been breaking all along)...
Or eventually it was just coincidence with othr constants and/or code
alignemnts in the project.

It would though make sense to have such an alignment defined since you
may also define various other alighments like code alignment or record
alignment.


I guess you can enforce this by creating a record that starts with a
type that has a natural alignment of 16 bytes, followed by your array.
In 32 bit X86 mode, that can be Extended. I would not know what to use
in X64 though. There, Extended is aliased to Double and I don't know
any other Delphi type that has a natural alignment of 16.

In Win32:

{code}
{$ALIGN 16}

...

type
Align16Ones = record
Reserved: Extended; // will be aligned on 16 bytes.
Ones: array[0..1] of Double; // so this array too.
end;

const
COnes: Align16Ones = (Reserved: 0.0; Ones: (1.0, 1.0));

Thanks for that idea I will check that asop.

kind regards
Mike
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Alignment of constants
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 3, 2014 8:03 AM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
Michael Rabatscher wrote:

Double is an 8 byte datatype, so it will be aligned on an 8 byte
boundary, but not necessarily on a 16 byte one. I don't think
there is >> a way to enforce this.

That's a pitty cause obviously it was different in previous versions
(otherwise my code would have been breaking all along)...
Or eventually it was just coincidence with othr constants and/or code
alignemnts in the project.

I suspect you were just lucky.

--
Rudy Velthuis http://www.rvelthuis.de

"Someday man will imprison the power of the sun, release atomic
power, and harness the rise and fall of the tides. I am proud
of the fact that I never invented weapons to kill."
-- Thomas Edison
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02