Author Topic: Floating point arithmetic question  (Read 2468 times)

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Floating point arithmetic question
« Reply #15 on: April 18, 2018, 04:04:04 AM »
fstp  st should be used, it is faster. It removes the current st(0) when we dont need it.

Btw there is also fincstp, which at first sight has the same effect. But try a simple fldpi afterwards, and you'll see the difference. Olly has a section with the FPU regs, you must scroll down a little bit the upper right pane to see it.
            Ok Jochen. I dont know where i have Olly in this computer. I rarely use it. LoneWolff may do this to learn.
            What i want to say to LoneWolff is this: generally, we use fstp st to remove st(0) when we dont need it.

Ascended

  • Member
  • ***
  • Posts: 332
Re: Floating point arithmetic question
« Reply #16 on: April 18, 2018, 07:47:06 AM »
Yeah, the fstp resultX call was just to get rid of the value. I'll swap out the command now that that I know about it.

Thanks guys! Awesome information as always  8)

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Floating point arithmetic question
« Reply #17 on: April 18, 2018, 08:03:43 AM »
Yeah, the fstp resultX call was just to get rid of the value. I'll swap out the command now that that I know about it.

Thanks guys! Awesome information as always  8)
Good luck  :t

Ascended

  • Member
  • ***
  • Posts: 332
Re: Floating point arithmetic question
« Reply #18 on: April 18, 2018, 08:17:31 AM »
Good luck  :t

Thanks Rui  :biggrin:

There was a measurable performance increase with the fstp st method.

Code: [Select]
fld number
fptan
fstp st
fstp result


But fincstp gave me a NaN result.

Code: [Select]
fld number
fptan
fincstp
fstp result

Siekmanski

  • Member
  • *****
  • Posts: 1684
Re: Floating point arithmetic question
« Reply #19 on: April 18, 2018, 08:22:52 AM »
Try this one,
ffree   st(0)
Creative coders use backward thinking techniques as a strategy.

Ascended

  • Member
  • ***
  • Posts: 332
Re: Floating point arithmetic question
« Reply #20 on: April 18, 2018, 08:26:21 AM »
Code: [Select]
fld number
fptan
ffree st(0)
fstp result

That one gives me a Nan result also  :(

Ascended

  • Member
  • ***
  • Posts: 332
Re: Floating point arithmetic question
« Reply #21 on: April 18, 2018, 08:34:38 AM »
Was just reading about the fincstp method.

http://qcd.phys.cmu.edu/QCDcluster/intel/vtune/reference/vc98.htm

Code: [Select]
This operation is not equivalent to popping the stack, because the tag for the previous top-of-stack register is not marked empty.

raymond

  • Member
  • **
  • Posts: 218
    • Raymond's page
Re: Floating point arithmetic question
« Reply #22 on: April 18, 2018, 10:18:38 AM »
Code: [Select]
fld number
fptan
ffree st(0)
fstp result

That one gives me a Nan result also  :(

Let's see if you can use your skills to find out in the tutorial why you are getting that result. ;)
Whenever you assume something, you risk being wrong half the time.
http://www.ray.masmcode.com/

Ascended

  • Member
  • ***
  • Posts: 332
Re: Floating point arithmetic question
« Reply #23 on: April 18, 2018, 10:31:29 AM »
Code: [Select]
fld number
fptan
ffree st(0)
fstp result

That one gives me a Nan result also  :(

Let's see if you can use your skills to find out in the tutorial why you are getting that result. ;)

Ha! Got me again.

It's that double pop thing again. Had only just woken up when I tried it out.  :t

jj2007

  • Member
  • *****
  • Posts: 8840
  • Assembler is fun ;-)
    • MasmBasic
Re: Floating point arithmetic question
« Reply #24 on: April 18, 2018, 11:21:32 AM »
Try this one,
ffree   st(0)

That instruction is about as rare as fincstp, and for the same reason :P

This operation is not equivalent to popping the stack, because the tag for the previous top-of-stack register is not marked empty.

Exactly :t

The point here is: fincstp rotates ST(0) into ST(7). If ST(7) is not empty, i.e. it carries a value, then any attempt to load ST(0) will fail.

In practice, you will never see ffree ST(0) or fincstp, but you will often see
Code: [Select]
fstp st ; pop ST, clear ST(7)
ffree ST(7) ; clear ST(7)
fxch ; to get ST(1), exchange ST(0), ST(1)

Ascended

  • Member
  • ***
  • Posts: 332
Re: Floating point arithmetic question
« Reply #25 on: April 18, 2018, 11:24:55 AM »
Try this one,
ffree   st(0)

That instruction is about as rare as fincstp, and for the same reason :P

I found that using that instruction, I had to fincstp anyway to get the right result. Which negated any performance gains.

Seems that fstp st has given the best performance so far.

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Floating point arithmetic question
« Reply #26 on: April 19, 2018, 04:42:17 AM »
Hi LoneWolff,
                   fstp    st  is the same as fstp  st(0).
                   But there are cases where we want to remove st(1) ...
                   In these cases we use  fstp  st(1) ...

                   In some cases, after one FPU instruction, we add this code to detect an error:

                   fstsw      ax         ; store Status Word register to AX register
                   fwait
                   shr         ax, 1     ; move bit 0 to carry flag
                   jc           _iserror
                   ; go on, here not error
                   ...
                   ; exit here without error


 _iserror:     fstp         st     ; remove st(0)
                  fclex                ; clear all bits in the status word register -> new instruction, new error ?
                  ; exit here with an error message

Ascended

  • Member
  • ***
  • Posts: 332
Re: Floating point arithmetic question
« Reply #27 on: April 24, 2018, 09:23:29 AM »
Hey guys,

I thought I'd ask this in this topic rather than create a new one, as it is still a FP question.

Is there a more efficient way of writing this? I regards to the final 'fstp' call.

This code works fine, just wondering if it is optimal.

Code: [Select]
fld fP ; farPlane / (farPlane - nearPlane)
fld nP
fsub
fld fP
fdiv st(0),st(1)
fstp _res
mov ecx, _res
mov [eax+40], ecx ; Store result in _33
fstp _res ; Clear the last value from the FP stack

If I don't call the final line, a value gets left on the FP stack. I could call 'fpinit' but that is a very slow call.

Just wondering if I am going about this the right way.

Thanks again  8)


[edit]
Actually, thinking about it, I probably just need to play with the order of operation.


[edit2]
Yep worked well. And one line less.

Code: [Select]
fld fP ; farPlane / (farPlane - nearPlane)
fld fP
fld nP
fsub
fdiv
fstp _res
mov ecx, _res
mov [eax+40], ecx ; Store result in _33

Siekmanski

  • Member
  • *****
  • Posts: 1684
Re: Floating point arithmetic question
« Reply #28 on: April 24, 2018, 10:03:12 AM »
   fld fp            ; farPlane / (farPlane - nearPlane)
   fld st
   fld rp
   fsubp
   fdivp
   fstp real4 ptr [eax+40]

 :biggrin:
Creative coders use backward thinking techniques as a strategy.

Ascended

  • Member
  • ***
  • Posts: 332
Re: Floating point arithmetic question
« Reply #29 on: April 24, 2018, 10:05:52 AM »
Nice!

Even better  :eusa_clap:

[edit]
Hang on. It says invalid operands on fsubp and fdivp.  :P

Were those two lines a typo?