flat assembler
Message board for the users of flat assembler.

 flat assembler > Main > Shifting and sign
Author
 Thread
axlucas

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

Joined: 16 Jun 2003
Posts: 6981
Location: Kraków, Poland
Take a look at SAR instruction.
19 Oct 2017, 07:12
Hrstka

Joined: 05 May 2008
Posts: 12
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
27 Oct 2017, 13:43
JohnFound

Joined: 16 Jun 2003
Posts: 3475
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

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

Joined: 24 Aug 2004
Posts: 16098
Location: Squiddler's Patch
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: 718
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: 3475
Location: Bulgaria
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: 16098
Location: Squiddler's Patch
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: 718
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: 16098
Location: Squiddler's Patch
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----------------Blog General----------------MainDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsCompiler InternalsIDE DevelopmentOS ConstructionNon-x86 architecturesHigh Level LanguagesProgramming Language DesignProjects and IdeasExamples and Tutorials 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-2018, Tomasz Grysztar.

Powered by rwasa.