flat assembler
Message board for the users of flat assembler.
Index
> Main > Shifting and sign |
Author |
|
Tomasz Grysztar 19 Oct 2017, 07:12
Take a look at SAR instruction.
|
|||
19 Oct 2017, 07:12 |
|
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 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 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 27 Oct 2017, 13:59
JohnFound wrote: -257 sar 8 = -2 and remainder 255. |
|||
27 Oct 2017, 13:59 |
|
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 28 Oct 2017, 10:45
revolution wrote:
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 28 Oct 2017, 14:21
tthsqe wrote: The compiler folks have had this one figured out for a long time. |
|||
28 Oct 2017, 14:21 |
|
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 29 Oct 2017, 13:35
tthsqe wrote: For all you non-JS users: 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 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.