flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > [feature request] abs operator

Author
Thread Post new topic Reply to topic
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 20 Jul 2015, 11:50
I would like fasm to have an 'abs' (absolute value) unary operator, with the same priority as 'not' (btw, fasm manual does not state the priority of unary minus, I suppose it is also the same as 'not').
Post 20 Jul 2015, 11:50
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4330
Location: Now
edfed 20 Jul 2015, 12:00
abs is not an operator.
mathematically, abs(x) = (x²)^1/2
logically, it is just a

cmp x,0
jge @f
neg x
@@:

but if it is for the inside of expressions, why not.
Post 20 Jul 2015, 12:00
View user's profile Send private message Visit poster's website Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 20 Jul 2015, 13:54
Abs, norm, absolute value or whatever you want to call it...
abs x = abs (x) = |x| = sqr(x^2)

If it is a function or an operator I think its just semantics. It can be done in an endless number of equivalent ways.

and/or/test x,x
jns @f
neg x
@@:

And yes, for use in numerical expressions.
Post 20 Jul 2015, 13:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 20 Jul 2015, 15:22
There are other common mathematical operators/functions that are missing also. As examples: sine, cosine, tangent (and their inverses and reciprocals), signum, ceiling, floor, factorial, square root, etc..

Is there really a need? Can your problem be solved another way?


Last edited by revolution on 20 Jul 2015, 21:56; edited 1 time in total
Post 20 Jul 2015, 15:22
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 20 Jul 2015, 17:10
El Tangas
Code:
x xor x shr 64 - x shr 64    

_________________
Faith is a superposition of knowledge and fallacy
Post 20 Jul 2015, 17:10
View user's profile Send private message Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 20 Jul 2015, 22:31
l_inc wrote:
El Tangas
Code:
x xor x shr 64 - x shr 64    


I know that trick.
Code:
cdq
xor eax,edx
sub eax,edx
    

See? But tnx anyway Wink

revolution wrote:
There are other common mathematical operators/functions that are missing also. As examples: sine, cosine, tangent (and their inverses and reciprocals), signum, ceiling, floor, factorial, square root, etc..

Is there really a need? Can your problem be solved another way?


Of course it can be solved in another way. In fact I already had solved it. I'm not asking for transcendental trig functions, abs is very basic Rolling Eyes in that list only signum is also basic.

Presenting alternative code is like saying "Why do we have 'not x', we can just do '-(x+1)'". Not the point.
I was just asking because I imagined it would not be difficult and would generate cleaner and shorter code in some situations.
Post 20 Jul 2015, 22:31
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 20 Jul 2015, 22:50
El Tangas
Quote:
Presenting alternative code is like saying "Why do we have 'not x', we can just do '-(x+1)'"

This wouldn't work for (-1) shl 64 xor (-1) Smile, so I'd better use x xor (-1). But I certainly do agree with your point.

_________________
Faith is a superposition of knowledge and fallacy
Post 20 Jul 2015, 22:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 21 Jul 2015, 13:13
l_inc wrote:
Code:
x xor x shr 64 - x shr 64    
This code relies upon fasm using 64/65 bit arithmetic. This is not guaranteed to be true for either past, or future, versions of fasm. If you were to use this code in fasmg then it would fail in certain circumstances.
Post 21 Jul 2015, 13:13
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 21 Jul 2015, 15:00
revolution
If I was to use this code in VB it wouldn't work at all. As for the arithmetic bit length in fasm the author mentioned he would undertake no big changes anymore. Though I hope this does not include some of the previously planned improvements such as access to the relocation info.

_________________
Faith is a superposition of knowledge and fallacy
Post 21 Jul 2015, 15:00
View user's profile Send private message Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 22 Jul 2015, 09:55
revolution wrote:
l_inc wrote:
Code:
x xor x shr 64 - x shr 64    
This code relies upon fasm using 64/65 bit arithmetic. This is not guaranteed to be true for either past, or future, versions of fasm. If you were to use this code in fasmg then it would fail in certain circumstances.


No, this code must always work, because, according to FASM documentation

Quote:
arithmetical and bit–logical calculations are usually processed as if they operated on infinite precision 2–adic numbers


This means any negative number must behave as if it was (1)nnnnn..nnb, in other words, the 'shr' operator in FASM is actually 'sar'.

So this code must be equivalent to abs x for any 'arbitrarily large' value of _shift:
Code:
x xor x shr _shift - x shr _shift    
Post 22 Jul 2015, 09:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 22 Jul 2015, 11:35
El Tangas wrote:
So this code must be equivalent to abs x for any 'arbitrarily large' value of _shift:
Code:
x xor x shr _shift - x shr _shift    
Not if the absolute value of "x" is larger than 2^_shift. For fasmg there is no guarantee that "x" has less bits than any particular value for _shift.
Post 22 Jul 2015, 11:35
View user's profile Send private message Visit poster's website Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 22 Jul 2015, 12:56
So, there you have it. If there is no absolutely reliable way to get the sign of a numeric value in a way that can be operated upon, I think we need 'abs' and/or 'sgn' to be implemented.
Post 22 Jul 2015, 12:56
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 25 Jul 2015, 09:55
This could only work if everything is typed otherwise it's ambiguous. This in itself is not a problem but has implications on everything else, in particular previously equivalent statements such as "-1" vs "not 0" vs "$F..F".

As adding this information is going to be long anyway, defining abs/sgn/... via macros seems acceptable, plus it allows for greater controls on edge cases such as -2^(N-1).
Post 25 Jul 2015, 09:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 25 Jul 2015, 16:12
cod3b453 wrote:
This could only work if everything is typed otherwise it's ambiguous. This in itself is not a problem but has implications on everything else, in particular previously equivalent statements such as "-1" vs "not 0" vs "$F..F".
In the current version with 65 bit arithmetic -1 does not equal 0xfff...fff.
Post 25 Jul 2015, 16:12
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


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

Website powered by rwasa.