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
Force Alignment
-
Maitre_Kanter
- User

- Posts: 84
- Joined: Mon Sep 06, 2010 3:05 pm
Re: Force Alignment
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 :
Arnaud.
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
Re: Force Alignment
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:
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

- Posts: 84
- Joined: Mon Sep 06, 2010 3:05 pm
Re: Force Alignment
Hello,
Can you explain : Align the code ?
Thank you
Can you explain : Align the code ?
Thank you
Re: Force Alignment
Well you can align code like you can align any other data in memory.Maitre_Kanter wrote:Hello,
Can you explain : Align the code ?
Thank you
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
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 wrote:Code: Select all
!align 4 For i = 1 To 1000 ... Next
Re: Force Alignment
It could be that PB aligns loops anyway, so its not needed on pb code, as i wrote.Mistrel wrote: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 wrote:Code: Select all
!align 4 For i = 1 To 1000 ... Next
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.

