Force Alignment

Everything else that doesn't fall into one of the other PB categories.
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Force Alignment

Post 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
Maitre_Kanter
User
User
Posts: 84
Joined: Mon Sep 06, 2010 3:05 pm

Re: Force Alignment

Post 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.
Thorium
Addict
Addict
Posts: 1314
Joined: Sat Aug 15, 2009 6:59 pm

Re: Force Alignment

Post 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
Maitre_Kanter
User
User
Posts: 84
Joined: Mon Sep 06, 2010 3:05 pm

Re: Force Alignment

Post by Maitre_Kanter »

Hello,

Can you explain : Align the code ?

Thank you
Thorium
Addict
Addict
Posts: 1314
Joined: Sat Aug 15, 2009 6:59 pm

Re: Force Alignment

Post 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.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Force Alignment

Post 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. :|
Thorium
Addict
Addict
Posts: 1314
Joined: Sat Aug 15, 2009 6:59 pm

Re: Force Alignment

Post 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.
Post Reply