views:

136

answers:

4
+3  Q: 

{$IFOPT A4} ?

In Delphi 2009 (or older versions), how do you check the "Align" compile option in the code?

The IFOPT directive seems to work only with pure switches ( {$IFOPT A4} does not compile ).

I couldn't find an equivalent constant or such defined ( {$IF Align = 4} or such )

A: 

I believe there is no way to do this :(

gabr
I agree. I don't know of any way to do this either. (Not sure why you'd need to; I can't think of any reason you'd need to know this at compile time, or what you'd do differently in code if you *did* know.
Ken White
Certain tasks only work on properly-aligned values. A lot of SIMD instructions are like that, for example...
Mason Wheeler
@Mason: That's right, but why not simply *demand* or force proper alignment, and do the same thing the processor does when misaligning, i.e. throw?
Mihai Limbășan
Trying to implement allen bauer's idea to create generic multicast events, we found several bugs, among others in ObjAuto.pas.For example, his solution implies creating at runtime a TMethodPointer from a method (and its TypeInfo), using ObjAuto.CreateMethodPointer.This function incorrectly assumes (through nested calls : line 726) that local variables and arguments are not aligned on the stack. To fix this, for example, we need to know the alignment size.Allen Bauer's original post : http://blogs.embarcadero.com/abauer/2008/08/15/38865#comment-11698
LeGEC
I'm using smart records to generate 4-aligned and 8-aligned values. See TGp4AlignedInt and TGp8AlignedInt64 in my GpStuff unit (most recent version is at http://code.google.com/p/omnithreadlibrary/source/browse/trunk/src/GpStuff.pas).
gabr
+1  A: 

There is {$IFOPT A+} directive, but it doesn't tell you alignment value.

Alexander
+2  A: 

Write code to test the actual runtime behavior. Only way I can think of.

Craig Stuntz
+7  A: 

You can do this by defining a record with known packing rules and check it using SizeOf. Tested in Delphi 2009:

type
  TTestRec = record
    A: Byte;
    B: Int64;
  end;

{$IF SIZEOF(TTestRec) = 9}
  {$MESSAGE HINT '$A1'}
{$ELSEIF SIZEOF(TTestRec) = 10}
  {$MESSAGE HINT '$A2'}
{$ELSEIF SIZEOF(TTestRec) = 12}
  {$MESSAGE HINT '$A4'}
{$ELSEIF SIZEOF(TTestRec) = 16}
  {$MESSAGE HINT '$A8'}
{$ELSE}
  {$MESSAGE HINT 'Unknown alignment'}
{$IFEND}
Craig Peterson
+1. creative. It works in Delphi 7 too.
Wouter van Nifterick
Thanks for the answer.Do we have a rock-solid guaranty that the compiler treats in the exact same way non packed records and local variables placed on the stack? (I certainly do hope so, but I have had several surprises when testing the compiler behaviour on other features...)
LeGEC