flat assembler
Message board for the users of flat assembler.

 Index > Compiler Internals > [feature request] abs operator
Author
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').
20 Jul 2015, 11:50
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.
20 Jul 2015, 12:00
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.
20 Jul 2015, 13:54
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20180
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
20 Jul 2015, 15:22
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
20 Jul 2015, 17:10
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

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 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.
20 Jul 2015, 22:31
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) , so I'd better use x xor (-1). But I certainly do agree with your point.

_________________
Faith is a superposition of knowledge and fallacy
20 Jul 2015, 22:50
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20180
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.
21 Jul 2015, 13:13
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
21 Jul 2015, 15:00
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    `
22 Jul 2015, 09:55
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20180
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.
22 Jul 2015, 11:35
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.
22 Jul 2015, 12:56
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).
25 Jul 2015, 09:55
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20180
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.
25 Jul 2015, 16:12
 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