Page 1 of 1

Force Alignment

Posted: Sun Oct 22, 2006 12:18 am
by eriansa
How does one do this in PB ?

// force 4 byte alignment

#if defined(_MSC_VER) && !defined(__MWERKS__)

#pragma pack(push,4)

#elif PRAGMA_ALIGN_SUPPORTED

#pragma options align = native

#endif

Re: Force Alignment

Posted: Sun Oct 24, 2010 5:56 am
by Maitre_Kanter
Hi,

I have the same problem with Static library compiled with VC2005 using Structure using "#pragma pack(value)"

I wrote a little program in C with the #pragma pack(8) and used OffsetOf and Sizeof to print the Offset and size of each Member.

Then I solved the problem by adding Padding into Structure in PureBasic.

for example :

Code: Select all

;In C
#pragma pack(8)
typedef struct
{
    unsigned char  * X;                         //Offset 0 , Len 4
    unsigned char X_len;                       //Offset 4, Len 1
    unsigned char  * Y;                         //Offset 8, Len 4                
    unsigned char  Y_len;                      //Offset 12, Len 1
    unsigned char TabChar[4];               //Offset 13, len 4  
}S_Example_in_C;

Printf("Size of S_Example_in_C : %d", sizeof(S_Example_in_C) );
//Print 20

;In PureBasic, you have to add padding as below
Structure S_Example_in_PureBasic
  *Coord_P_X.a
  Coord_P_X_len.a
  padding1.a[3]
  *Coord_P_Y.a
  Coord_P_Y_len.a 
  padding2.a[3]
  TabChar.a[4]
  padding3.a[4]
EndStructure 
Arnaud.

Re: Force Alignment

Posted: Sun Oct 24, 2010 10:51 am
by Thorium
I dont know what the code you posted does.
But Maitre_Kanter showed you how to align data in structures.
That shows you how to align data in buffers.

And if you want to align code:

Code: Select all

!align 4
For i = 1 To 1000
  
  ...
  
Next

Re: Force Alignment

Posted: Sun Oct 24, 2010 7:14 pm
by Maitre_Kanter
Hello,

Can you explain : Align the code ?

Thank you

Re: Force Alignment

Posted: Mon Oct 25, 2010 3:38 pm
by Thorium
Maitre_Kanter wrote:Hello,

Can you explain : Align the code ?

Thank you
Well you can align code like you can align any other data in memory.
This is for speed optimization of loops or calls or any other jumps. A jump is fastest if the instruction it jumps to is correctly aligned.
You will not need it for PB code, it was just a example. It's used to optimize asm code.

Re: Force Alignment

Posted: Mon Oct 25, 2010 5:18 pm
by Mistrel
Thorium wrote:

Code: Select all

!align 4
For i = 1 To 1000
  
  ...
  
Next
I ran some loops with and without !align 4 and !align 8, also without, compiled with x86 and x64. It doesn't seem any faster to me. :|

Re: Force Alignment

Posted: Mon Oct 25, 2010 6:21 pm
by Thorium
Mistrel wrote:
Thorium wrote:

Code: Select all

!align 4
For i = 1 To 1000
  
  ...
  
Next
I ran some loops with and without !align 4 and !align 8, also without, compiled with x86 and x64. It doesn't seem any faster to me. :|
It could be that PB aligns loops anyway, so its not needed on pb code, as i wrote.
Also it could be that the loop is by random on a 4/8 byte alignment. So tge assembler dont need to insert nops.
Third it depends on the cpu and how big the loop is. On modern CPUs misaligned memory access are much more optimized than on older ones.
If the loop is very small enough to fit in the instruction cache instructions will be broken down into microops and automaticly optimized by the CPU, if the CPU is not to old.