flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > BSR operator with negative constants

Author
Thread Post new topic Reply to topic
CandyMan



Joined: 04 Sep 2009
Posts: 382
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 14 Dec 2022, 10:16
Code:
A = bsr(-1)
processed: A=bsr(-1)
error: value out of range.    
Code:
macro IsPower V
{
  A = bsf(V)
  B = bsr(V)
if A=B
  display 'IsPowerOf2'
end if
}

V = -1

IsPower V    

_________________
smaller is better
Post 14 Dec 2022, 10:16
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8105
Location: Kraków, Poland
Tomasz Grysztar 14 Dec 2022, 11:26
CandyMan wrote:
Code:
A = bsr(-1)
processed: A=bsr(-1)
error: value out of range.    
What is the highest bit of -1 representation depends on how many bits you have. For the sake of universality, fasm gives results of calculations that are consistent with an unbounded number of bits (in the manual I also define this as computation on 2-adic numbers, see my video series for detailed explanation), and BSR of -1 is then meaningless, because infinite series of ones does not have the highest one.

If you know your maximum size, just mask it out before the calculation:
Code:
A = bsr((-1) and 0FFFFFFFFFFFFFFFFh)    

In case of fasmg, you can use mask of any length (making bsr(-1) larger an larger).

CandyMan wrote:
Code:
macro IsPower V
{
  A = bsf(V)
  B = bsr(V)
if A=B
  display 'IsPowerOf2'
end if
}

V = -1

IsPower V    
There are other options:
Code:
macro IsPower V
{
if V=1 shl bsf(V)
  display 'IsPowerOf2'
end if
}    
Post 14 Dec 2022, 11:26
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 382
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 14 Dec 2022, 11:49
I understood. Thanks for the explanation.
Post 14 Dec 2022, 11:49
View user's profile Send private message 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-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.