flat assembler
Message board for the users of flat assembler.

Index > Main > Shifting and sign

Author
Thread Post new topic Reply to topic
axlucas



Joined: 02 May 2014
Posts: 70
Location: Argentina
axlucas 19 Oct 2017, 07:03
I need to divide by 256, but I want to do it fast. I thought of shifting right 8 bits, but the problem is I'm using signed integers. Then suddenly, I wonder if shr actually preserves the higher bit or not or whether there's a signed and an unsigned version of this mnemonic. Funny how I never put attention to this before. Is this possible?
Post 19 Oct 2017, 07:03
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 19 Oct 2017, 07:12
Take a look at SAR instruction.
Post 19 Oct 2017, 07:12
View user's profile Send private message Visit poster's website Reply with quote
Hrstka



Joined: 05 May 2008
Posts: 56
Location: Czech republic
Hrstka 27 Oct 2017, 13:43
Keep in mind that div and sar don't produce the same result for negative values.

-257 idiv 256 = -1
-257 sar 8 = -2
Post 27 Oct 2017, 13:43
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 27 Oct 2017, 13:56
Hrstka wrote:
Keep in mind that div and sar don't produce the same result for negative values.

-257 div 256 = -1
-257 sar 8 = -2


-257 div 256 = -1 and remainder -1

And it is ok because:
-1 * 256 + (-1) = -257

-257 sar 8 = -2 and remainder 255.

Which is OK as well, because -2 * 256 + 255 = -257 Wink

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 27 Oct 2017, 13:56
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 27 Oct 2017, 13:59
JohnFound wrote:
-257 sar 8 = -2 and remainder 255.
Me is wondering where the remainder is found after using SAR? Confused
Post 27 Oct 2017, 13:59
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 28 Oct 2017, 09:57
The compiler folks have had this one figured out for a long time.
https://godbolt.org/g/rhpn7y
Post 28 Oct 2017, 09:57
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 28 Oct 2017, 10:45
revolution wrote:
JohnFound wrote:
-257 sar 8 = -2 and remainder 255.
Me is wondering where the remainder is found after using SAR? Confused


The remainder is there before using SAR, because, you know, most instructions return only one result, not two like DIV.

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 28 Oct 2017, 10:45
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 28 Oct 2017, 14:21
tthsqe wrote:
The compiler folks have had this one figured out for a long time.
https://godbolt.org/g/rhpn7y
All I see is a mostly blank page with a small dark grey header at the top. Question
Post 28 Oct 2017, 14:21
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 28 Oct 2017, 16:43
Quote:
All I see is a mostly blank page with a small dark grey header at the top.


For all you non-JS users:

Code:
int f(int a){return a/256;}

f(int):
  lea eax, [rdi+255]
  test edi, edi
  cmovns eax, edi
  sar eax, 8
  ret    
Post 28 Oct 2017, 16:43
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 29 Oct 2017, 13:35
tthsqe wrote:
For all you non-JS users:

Code:
int f(int a){return a/256;}

f(int):
  lea eax, [rdi+255]
  test edi, edi
  cmovns eax, edi
  sar eax, 8
  ret    
Thanks.

That code is good, but it assumes the existence of CMOVcc. For 64-bit code that is fine, but there are still some 32-bit CPUs that don't have it.
Post 29 Oct 2017, 13:35
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.