Recent

Author Topic: Pointer Alignment  (Read 2920 times)

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Pointer Alignment
« on: May 27, 2017, 12:04:09 pm »
Hi all,

I'm sure I'm missing something obvious, so please bear with me. What I would like to do is

* get a pointer to a heap segment via getmem() - in advance I can not assume anything about the alignment as this is depending on OS and architecture I understand
* before I hand the pointer over to a function I want to align it to 0, 8, 16, 24 (mod 32)
* I would like to do this without generating a compiler warning (for my own ease of mind) and it should be working regardless if the environment sets pointer sizes to 4 or 8 byte and data sizes to 32 or 64 bit

There are of course zillions of solutions aligning the pointers - but I couldn't come up with one that does not generate a warning. I have looked into PTRUINT and PPTRUINT also but seem to not understand these correct ...

Kind regards,
MathMan

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Pointer Alignment
« Reply #1 on: May 27, 2017, 02:49:04 pm »
Hi all,

I'm sure I'm missing something obvious, so please bear with me. What I would like to do is

* get a pointer to a heap segment via getmem() - in advance I can not assume anything about the alignment as this is depending on OS and architecture I understand
* before I hand the pointer over to a function I want to align it to 0, 8, 16, 24 (mod 32)
* I would like to do this without generating a compiler warning (for my own ease of mind) and it should be working regardless if the environment sets pointer sizes to 4 or 8 byte and data sizes to 32 or 64 bit

There are of course zillions of solutions aligning the pointers - but I couldn't come up with one that does not generate a warning. I have looked into PTRUINT and PPTRUINT also but seem to not understand these correct ...

Kind regards,
MathMan
It is even guaranteed that Getmem will return a pointer aligned to your hearts content. As long as it is even. You may want to read some documentation >:D before asking obvious questions.
 Look at {$ALIGN}, {$CODEALIGN} we have manuals for that.
I -and probably a couple of other users on this forum- have the idea that you read somewhere that aligned code is faster. But you have no clue why that's the case. And it is not necessarily true any more.
« Last Edit: May 27, 2017, 03:20:36 pm by Thaddy »
Specialize a type, not a var.

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Pointer Alignment
« Reply #2 on: May 28, 2017, 12:28:08 pm »
Hi all,

I'm sure I'm missing something obvious, so please bear with me. What I would like to do is

* get a pointer to a heap segment via getmem() - in advance I can not assume anything about the alignment as this is depending on OS and architecture I understand
* before I hand the pointer over to a function I want to align it to 0, 8, 16, 24 (mod 32)
* I would like to do this without generating a compiler warning (for my own ease of mind) and it should be working regardless if the environment sets pointer sizes to 4 or 8 byte and data sizes to 32 or 64 bit

There are of course zillions of solutions aligning the pointers - but I couldn't come up with one that does not generate a warning. I have looked into PTRUINT and PPTRUINT also but seem to not understand these correct ...

Kind regards,
MathMan
It is even guaranteed that Getmem will return a pointer aligned to your hearts content. As long as it is even. You may want to read some documentation >:D before asking obvious questions.
 Look at {$ALIGN}, {$CODEALIGN} we have manuals for that.
I -and probably a couple of other users on this forum- have the idea that you read somewhere that aligned code is faster. But you have no clue why that's the case. And it is not necessarily true any more.

Hi Thaddy - sorry that my question seems to have unnerved you. I put it in the "Beginners" section for a reason.

I do understand that inserting an {$ALIGN xx} will yield a specifically aligned pointer when calling Getmem - unfortunately this is not what I need. I benchmark functions from a command line and would like to define data alignment cases in the command line. I'm not thinking about code alignment - your idea is completely wrong - but about influences of data alignment on code execution speed. Understanding these influences helps me decide if I should spend time on some lead-in code to get to an aligned working set (not always possible, but sometimes). Constant re-compilation with different {$ALIGN xx} settings is not an option for me, neither is maintaining mutiple instances of my benchmark utility.

If you think that data alignment too is of minor concern then feel free to test the attached function with different pointer alignment on entry.

Nonetheless I'll read the docs again regarding {$ALIGN} and {$CODEALIGN}.

Kind regards,
MathMan

 

TinyPortal © 2005-2018