NO

Author Topic: Computing number of Pages to alloc usin VirtualAlloc  (Read 490 times)

Offline HellOfMice

  • Member
  • *
  • Posts: 105
  • Never be pleased, always improve
Computing number of Pages to alloc usin VirtualAlloc
« on: November 14, 2024, 01:19:04 PM »
My formula is
_dwSize = ((__dwSize + 4095) >> 12) << 12 ;

is equivalent to (((__dwSize  + 4095) / 4096) * 4096)
Suppose __dwSize = 3456 bytes to alloc

(3456 + 4095) = 7551 bytes
7551 / 4096 = 1 (number of pages)
1 * 4096 = 4096 bytes really allocated

For me 4096 is not the same than 7551 or I must return to school. to old...
I proceed like this because I want a quantity representing full memory pages.
But Pelle does not understand

Code: [Select]
Building Utils.obj.
C:\Users\51966\Documents\#@# Programmation\DIF\Utils.c(111): warning #2287: Dead code removed (around here).
Done

My solution is
Code: [Select]
   _dwSize = (__dwSize + 4095) >> 12 ;
   _dwSize = _dwSize << 12 ;

I don't like

The project is in Release mode. Is it a bug?
« Last Edit: November 14, 2024, 01:24:47 PM by HellOfMice »
--------------------------------
Kenavo

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #1 on: November 14, 2024, 03:23:37 PM »
test:
Code: [Select]
#include <stdio.h>

int __cdecl main(void)
{
unsigned long _dwSize, __dwSize;
__dwSize = 3456;
_dwSize = ((__dwSize + 4095) >> 12) << 12 ;
printf("%d\n", _dwSize);
return 0;
}
returns 4096
from podump
Code: [Select]
1: #include <stdio.h>
2:
3: int __cdecl main(void)

_main:
  [00000000] 55                     push              ebp
  [00000001] 89E5                   mov               ebp,esp
  [00000003] 83EC08                 sub               esp,8
4: {
5: unsigned long _dwSize, __dwSize;
6: __dwSize = 3456;
  [00000006] C745F8800D0000         mov               dword ptr [ebp-8],D80
7: _dwSize = ((__dwSize + 4095) >> 12) << 12 ;
  [0000000D] 8B45F8                 mov               eax,dword ptr [ebp-8]
  [00000010] 8D80FF0F0000           lea               eax,[eax+FFF]
  [00000016] C1E80C                 shr               eax,C
  [00000019] C1E00C                 shl               eax,C
  [0000001C] 8945FC                 mov               dword ptr [ebp-4],eax
8: printf("%d\n", _dwSize);
  [0000001F] FF75FC                 push              dword ptr [ebp-4]
  [00000022] 6800000000             push              @9
  [00000027] E800000000             call              _printf
  [0000002C] 83C408                 add               esp,8
9: return 0;
  [0000002F] B800000000             mov               eax,0
  [00000034] 89EC                   mov               esp,ebp
  [00000036] 5D                     pop               ebp
  [00000037] C3                     ret               
Code: [Select]
1: #include <stdio.h>
2:
3: int __cdecl main(void)

main:
  [0000000000000000] 4883EC28                     sub               rsp,28
4: {
5: unsigned long _dwSize, __dwSize;
6: __dwSize = 3456;
  [0000000000000004] C7442420800D0000             mov               dword ptr [rsp+20],D80
7: _dwSize = ((__dwSize + 4095) >> 12) << 12 ;
  [000000000000000C] 8B442420                     mov               eax,dword ptr [rsp+20]
  [0000000000000010] 678D80FF0F0000               lea               eax,[eax+FFF]
  [0000000000000017] C1E80C                       shr               eax,C
  [000000000000001A] C1E00C                       shl               eax,C
  [000000000000001D] 89442424                     mov               dword ptr [rsp+24],eax
8: printf("%d\n", _dwSize);
  [0000000000000021] 488D0D00000000               lea               rcx,[@9]
  [0000000000000028] 8B542424                     mov               edx,dword ptr [rsp+24]
  [000000000000002C] E800000000                   call              printf
9: return 0;
  [0000000000000031] B800000000                   mov               eax,0
  [0000000000000036] 4883C428                     add               rsp,28
  [000000000000003A] C3                           ret               
May the source be with you

Offline HellOfMice

  • Member
  • *
  • Posts: 105
  • Never be pleased, always improve
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #2 on: November 14, 2024, 03:27:33 PM »
Thank you Timo. I was afraid.



--------------------------------
Kenavo

Offline John Z

  • Member
  • *
  • Posts: 858
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #3 on: November 15, 2024, 12:53:57 PM »
Hi,

Integer math != 'real' math except in limited circumstances
Integer math
dwSize = ((dwSize + 4095) >> 12) << 12 ;

can't be equivalent to the look alike 'real' math

'Real' math
fSize = ((fSize + 4095.0) / 4096) *4096;

To get close you need to preserve at least a few of the decimals from >>12

so
dwSize = ((((dwSize + 4096)*10000) >> 12) << 12)/10000 ; // = 7551
but if need *10000 and then /10000 one might as well use float... unless
no float lib available.

John Z
« Last Edit: November 15, 2024, 04:39:06 PM by John Z »

Offline HellOfMice

  • Member
  • *
  • Posts: 105
  • Never be pleased, always improve
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #4 on: November 15, 2024, 12:59:56 PM »
Ok John but it is slow that is the reason I use shifts
With
Quote
dwSize = ((((dwSize + 4096)*10000) >> 12) << 12)/10000 ; // = 7551but if need *10000 and then /1000 one might as well use float... unlessno float lib available

you add another page if the user wants to alloc 4096 byte you give him 8192 bytes
Change with dwSize + 4095

dwSize = ((((dwSize + 4095)*10000) >> 12) << 12)/10000 ; // = 7551

V1 = (dwSize + 4095)  ;
V2 = V1 << 10 ;
V3 = V1 << 4 ;
V4 = V1 << 3 ;
V5 = V3 + V4 ;
V6 = V2 - V5 ; ==> (V1 * 1024) - (V1 * 24)

dwSize  = ((V6 >> 12) << 12) / 1000 ;


or

dwSize = (((V1 << 10) - (V1 << 4) - (V1 << 3)) >> 12) << 12) / 1000 ;

:P
 
« Last Edit: November 15, 2024, 01:15:54 PM by HellOfMice »
--------------------------------
Kenavo

Offline John Z

  • Member
  • *
  • Posts: 858
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #5 on: November 16, 2024, 10:49:23 AM »
you add another page if the user wants to alloc 4096 byte you give him 8192 bytes
Change with dwSize + 4095

dwSize = ((((dwSize + 4095)*10000) >> 12) << 12)/10000 ; // = 7551
oops you are right


V1 = (dwSize + 4095)  ;
V2 = V1 << 10 ;
V3 = V1 << 4 ;
V4 = V1 << 3 ;
V5 = V3 + V4 ;
V6 = V2 - V5 ; ==> (V1 * 1024) - (V1 * 24)

dwSize  = ((V6 >> 12) << 12) / 1000 ;[/size][/font]

or

dwSize = (((V1 << 10) - (V1 << 4) - (V1 << 3)) >> 12) << 12) / 1000 ;


Very innovative!

John Z

Offline HellOfMice

  • Member
  • *
  • Posts: 105
  • Never be pleased, always improve
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #6 on: November 16, 2024, 10:51:47 AM »
I like to be an original
--------------------------------
Kenavo

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #7 on: November 17, 2024, 09:44:04 AM »
I like to be an original
unique  ;)
just to avoid imul from code, but your example confuse normal coder, as in code speed actually wasn't real reason ?
Code: [Select]
unsigned long dwSize2 = ((dwSize + 4095) / 4096) * 4096;
May the source be with you

Offline HellOfMice

  • Member
  • *
  • Posts: 105
  • Never be pleased, always improve
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #8 on: November 17, 2024, 09:47:46 AM »
Hello Timo.


What I use is your formula, the other one was just for the fun.


Here is a link that could interest you [size=78%]https://github.com/WojciechMula/toys[/size]


Now my program works well for its first part but I would like to improve avg, mean and stddev so I am searching SSE instruction to speed it up.


Thank you for your message.


Philippe
--------------------------------
Kenavo

Offline HellOfMice

  • Member
  • *
  • Posts: 105
  • Never be pleased, always improve
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #9 on: November 17, 2024, 09:49:23 AM »
Hello Timo.

What I use is your formula, the other one was just for the fun.
Here is a link that could interest you https://github.com/WojciechMula/toys
Now my program works well for its first part but I would like to improve avg, mean and stddev so I am searching SSE instruction to speed it up.

Thank you for your message.

Philippe
--------------------------------
Kenavo

Offline Vortex

  • Member
  • *
  • Posts: 864
    • http://www.vortex.masmcode.com
Code it... That's all...

Offline HellOfMice

  • Member
  • *
  • Posts: 105
  • Never be pleased, always improve
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #11 on: November 17, 2024, 01:44:12 PM »
Many words for doing what Timo does in one line
--------------------------------
Kenavo

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #12 on: November 19, 2024, 09:20:16 AM »
As Vortex hinted, this should tested:
Code: [Select]
#include <stdio.h>

int __cdecl main(void)
{
unsigned long dwSize = 3456;
unsigned long dwSize2;
for (dwSize = 4090; dwSize < 4100; dwSize++) {
dwSize2 = ((dwSize + 4095) & 0x0000F000);
printf("%d %d\n",dwSize, dwSize2);
}
for (dwSize = 0xFFF5; dwSize < 0x10005; dwSize++) {
dwSize2 = ((dwSize + 4095) & 0xFFFFF000);
printf("%d %d\n",dwSize, dwSize2);
}

return 0;
}
May the source be with you

Offline HellOfMice

  • Member
  • *
  • Posts: 105
  • Never be pleased, always improve
Re: Computing number of Pages to alloc usin VirtualAlloc
« Reply #13 on: November 19, 2024, 09:38:43 AM »
Thank You


I will test


Philippe
--------------------------------
Kenavo