flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Main > Shifting and sign

Author
Thread Post new topic Reply to topic
axlucas



Joined: 02 May 2014
Posts: 66
Location: Argentina
Shifting and sign
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
Assembly Artist


Joined: 16 Jun 2003
Posts: 6676
Location: Kraków, Poland
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: 9
Location: Czech republic
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: 3459
Location: Bulgaria

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: 15303
Location: Bigweld Industries

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: 701
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: 3459
Location: Bulgaria

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: 15303
Location: Bigweld Industries

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: 701

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 ediedi
  cmovns eaxedi
  sar eax8
  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: 15303
Location: Bigweld Industries

tthsqe wrote:
For all you non-JS users:


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

f(int):
  lea eax, [rdi+255]
  test ediedi
  cmovns eaxedi
  sar eax8
  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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.