flat assembler
Message board for the users of flat assembler.

 Index > Main > Shifting and sign
Author
 Thread
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?
19 Oct 2017, 07:03
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8347
Location: Kraków, Poland
Tomasz Grysztar 19 Oct 2017, 07:12
Take a look at SAR instruction.
19 Oct 2017, 07:12
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
27 Oct 2017, 13:43
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

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
27 Oct 2017, 13:56
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20248
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?
27 Oct 2017, 13:59
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
28 Oct 2017, 09:57
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?

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

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
28 Oct 2017, 10:45
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20248
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.
28 Oct 2017, 14:21
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
28 Oct 2017, 16:43
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20248
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.
29 Oct 2017, 13:35
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum

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

Website powered by rwasa.