flat assembler
Message board for the users of flat assembler.

Index > Main > [F1 Solved] Precalculated Float

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 29 Jun 2021, 16:34
Hi
I would like to pre-calculate float constants, how to do that?
Code:
.data
struc SOMESTRUC Value,Float
{  .Consatnt1 dd Value#f      ;works ok
   .Constant2 dd Value-1      ;ok as integer
   .Constant3 dd Value-1#f    ;wrong
   .Constant4 dd Float-1f     ;wrong too
 TempValue = Float-1f
   .Constant5 dd TempValue    ;wrong
}

Something  SOMESTRUC  15, 0.15f
    


Last edited by Overclick on 05 Sep 2022, 04:36; edited 1 time in total
Post 29 Jun 2021, 16:34
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18963
Location: In your JS exploiting you and your system
revolution 29 Jun 2021, 16:59
fasm doesn't natively support floating point arithmetic at compile time. Only integer arithmetic is available.
Code:
x = 3.0 - 2.0 ; not valid
x = 3 - 2 ; okay as integers only    
You could probably write a macro, but it would be tricky.
Post 29 Jun 2021, 16:59
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 29 Jun 2021, 18:19
Look at Constant1 that trick is working. But why I cannot precalculate all values as integer and use the same trick on them by #f ? Have I precalculate it on virtual or something?
How a macro will help me to solve this out if "struc" is already some sort of macro? I'm confused, show me the path

p.s.Seems like it's time to separate forum for fasm1 and fasmg.
Post 29 Jun 2021, 18:19
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18963
Location: In your JS exploiting you and your system
revolution 29 Jun 2021, 18:26
Overclick wrote:
But why I cannot precalculate all values as integer and use the same trick on them by #f ?
The hash symbol (#) is processed before the assembler, so the timing is wrong.

The assembler computes integer arithmetic after the preprocessor has finished processing the hash symbol.
Post 29 Jun 2021, 18:26
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 29 Jun 2021, 18:29
How to force calculation or delay that bloody hash?
Post 29 Jun 2021, 18:29
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18963
Location: In your JS exploiting you and your system
revolution 29 Jun 2021, 18:30
Overclick wrote:
How to force calculation or delay that bloody hash?
You can't. The preprocessor runs first, then the assembler later.
Post 29 Jun 2021, 18:30
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 29 Jun 2021, 18:33
What about virtual?
Post 29 Jun 2021, 18:33
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18963
Location: In your JS exploiting you and your system
revolution 29 Jun 2021, 18:33
If you want to go ahead with a macro then it might look like this:
Code:
macro convert_to_float input_integer {
  exponent = bsf input_integer
  ;... more stuff here
  dd sign + exponent + mantissa
}

my_value = 3 - 2
convert_to_float my_value    
Post 29 Jun 2021, 18:33
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 29 Jun 2021, 18:42
Thanks
Post 29 Jun 2021, 18:42
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 29 Jun 2021, 18:50
Much easiest way for me to precalculate them inside the code at initialisation stage Smile
Your mathematics is interesting to me anyway. I was thinking to use it for float as alternative to division, but now I don't use it anymore. I'll meditate on it some day.
Post 29 Jun 2021, 18:50
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18963
Location: In your JS exploiting you and your system
revolution 29 Jun 2021, 18:51
That was the easy part.

It gets tricky when you have fractional parts.
Code:
v1 = 0.15
v2 = 3.0
dd v1 + v2 ; invalid    
So you could make an add macro.
Code:
macro addf float1, float2 {
  ;some code goes here to do the addition
 dd sign + exponent + mantissa
}    
Post 29 Jun 2021, 18:51
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1286
Roman 30 Jun 2021, 08:52
Quote:

macro addf float1, float2 {
;some code goes here to do the addition
dd sign + exponent + mantissa
}

I not understood macro addf get float1 and float2.
But why in macro dd sign + exponent + mantissa ? And not float1 and float2.
Post 30 Jun 2021, 08:52
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1286
Roman 30 Jun 2021, 08:53
I think Fasm need implement some new symbol for this problem topic starter.
I am about float values.
Post 30 Jun 2021, 08:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18963
Location: In your JS exploiting you and your system
revolution 30 Jun 2021, 09:39
Roman wrote:
I not understood macro addf get float1 and float2.
But why in macro dd sign + exponent + mantissa ? And not float1 and float2.
Look at the values:
Code:
a = 2.0 ; 0x4000_0000_0000_0000
b = 3.0 ; 0x4008_0000_0000_0000
c = 5.0 ; 0x4014_0000_0000_0000
d = a + b ; 0x8008_0000_0000_0000    
That last value for d is not the float representation for 5.0. You get a bogus result.
Post 30 Jun 2021, 09:39
View user's profile Send private message Visit poster's website Reply with quote
idle



Joined: 06 Jan 2011
Posts: 410
Location: Ukraine
idle 30 Jun 2021, 14:19
exponent = bsf input_integer

d = a + b ; 0x8008_0000_0000_0000

That last value for d is not the float representation for 5.0. You get a bogus result

bsf (input_integer) ; embrace to let expressions
Also the bias $3fff.
Also sign bit.
And float format.

There are macro to encode 0.etc.
Time loss...
[/b]
Post 30 Jun 2021, 14:19
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 05 Sep 2022, 04:35
Solved, thanks to Manual
Code:
rept 1 num:INT1+INT2 {define SumForMyFloat num#f }    
Post 05 Sep 2022, 04:35
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1286
Roman 05 Sep 2022, 05:58
funny.
Code:
rept 4 num:1 { SumForMyFloat equ num#f }


 irpv ik,SumForMyFloat {        dd SumForMyFloat   } ;all 4.0

 irpv ik,SumForMyFloat {        dd ik   } ;we get 1.0,2.0,3.0,4.0
    


Thats ok. But inverted
Code:
rept 4 num:1 { SumForMyFloat equ num#f }
 irpv ik,SumForMyFloat {
                dd SumForMyFloat
                restore  SumForMyFloat  ;we get 4.0,3.0,2.0,1.0
                } 
    
Post 05 Sep 2022, 05:58
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 05 Sep 2022, 06:07
LOL
My task was to calculate some values and convert the result to float. And I did it after so long time. No need to do it in program any more. What you trying to show? ))
INT1 + INT2 = SumForMyFloat (float)
Post 05 Sep 2022, 06:07
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1286
Roman 05 Sep 2022, 06:15
Overclick wrote:
Solved, thanks to Manual
Code:
rept 1 num:INT1+INT2 {define SumForMyFloat num#f }    


If we need do define SumForMyFloat 2*num#f
How do this ?

I try this
Code:
rept 4 num:1,n:0  { SumForMyFloat equ n#f } ;ok

rept 4 num:1,n: (num*2)  { SumForMyFloat equ n#f } ;but this error
    


Last edited by Roman on 05 Sep 2022, 06:20; edited 2 times in total
Post 05 Sep 2022, 06:15
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 587
Location: Ukraine
Overclick 05 Sep 2022, 06:16
Same way
Code:
rept 1 num:INT*2 {define SumForMyFloat num#f }     
Post 05 Sep 2022, 06:16
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:  
Goto page 1, 2  Next

< 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.