Two floats on same page?

Hi guys, I have a C and operating systems question.

I was recently asked how to determine if two floats reside within the same page, where page sizes are 10KB large, and they start at 0. Basically, implement the following method:

bool areOnSamePage( float * x , float * y ) {

}

To start, I believe this is done by checking how much memory space resides between both pointers? Is this correct? If so, how could I proceed?
Who is Participating?

Commented:
Hi,

Based on the info provided by the author, i figure out that the following algorithm may solve the problem.
Hope its useful.
``````#define PAGE_SEZE 10240 // 10kb

bool areOnSamePage( float * x , float * y ) {

// discover in which page x and y are
int xPage = x / PAGE_SIZE;
int yPage = y / PAGE_SIZE;

return xPage == yPage;
}
``````
0

Commented:
>> To start, I believe this is done by checking how much memory space resides between both pointers?

If you subtract one pointer from the other, what value do you get ?
0

Commented:
If the pointers are not elements of the same array object or one past the last element of the array object, the result of subtracting one pointer from the other is undefined.
0

Commented:
ozo, this seems to be a platform dependent question, so I think we can assume that subtracting pointers is well defined on the platform in question (otherwise the question wouldn't make much sense).
0

Commented:
Your C implementation may support a non portable, non strictly conforming method of extracting the page from a pointer.
If so, you may then be able to compare them.
Even if subtracting pointers that are not elements of the same array object produces a sensible result, the it may still be difficult to distinguish between  pointers at the end of one page and the beginning of the next page
from pointers at the beginning and end of the same page.
0

Author Commented:
I assume this could be done in two steps.

1. Get difference of pointers, and if it's greater than 10KB, then they must reside on separate pages.
2. If difference < 10KB, somehow figure out the page bounds for each pointer and see if they're equal.

I think?
0

Commented:
What information do you have on the platform ?
Do pages start at memory addresses that are multiples of 10kB ?
0

Commented:
Subtracting the two pointers is not even a viable solution.
Suppose the object 'x'  points to resides at the very  end of a 10kb page
and the object 'y'  resides at the very beginning of the following page.

And even though the results of your pointer subtraction are undefined according to the C language definition,  you do it anyways

and you find that  with

ptrdiff_t  val = y - x;

val  is equal to   -sizeof(float)

what then?
If  you conclude that the two pointers are on the same page,  because the  integer representation of the subtraction is less than 10240
then your conclusion is in error.

You need an algorithm to  determine which page 'x'  is in,  or to at least find the boundary of the page x is in.

Your 10kb  page size has a base 2 representation of:   10100000000000b

What you really need to determine is if     x  > y     or  x < y

And if  x > y,  then does x   reside beyond the next multiple of your page size above y or not?

Or if  y > x,  then does y  reside beyond the next multiple of your page size above x or not?

Some pseudocode

ptrdiff_t  difference = x - y;

if ( difference < 0 ) {
/*  y > x  */
/*  now check   if   (int)y    is beyond  the next multiple of your page size *
*  after x or not  */
}
else if ( difference > 0 ) {
/* x > y */
}
else {
/* x == y */
}

0

Author Commented:
Infinity08: No info on the platform. And yeah, the pages start at addr 0 and keep going up in multiples of 10KB.
0

Commented:
>> And yeah, the pages start at addr 0 and keep going up in multiples of 10KB.

Then it's easyt o know what page a certain address is on. The modulo operator will be of great assistance ;)
0

Commented:
the shift or divide or bitwise and operator may be of greater assistance
0

Commented:
>> the shift or divide or bitwise and operator may be of greater assistance

I meant the division operator obviously lol.

re. the binary operators : afaik, 10kB is not a power of 2 ;) Which complicates things slightly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.