flat assembler
Message board for the users of flat assembler.

Index > Main > Yet another floating point thread...

Author
Thread Post new topic Reply to topic
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
When working on my AFS project, i figured it'd be easiest to point the reader to an instruction reference, explain how the fpu works, and then write a printing function using the fpu... However, for some reason this doesn't work...

Code:
;Prints st0 to the usual 8 digits beyond the "."
printfpubuffer dd 0
printfpupreserve dd 0
printfpudot db '.', 0
printfpu:
  fst dword [printfpupreserve]
        fist dword [printfpubuffer]
 printnum dword [printfpubuffer]
     printtext printfpudot
       fninit ;we MUST make sure it's ready for us.
       fld dword [printfpupreserve]
        fisub dword [printfpubuffer]
        mov dword [printfpubuffer], 100000000
       fimul dword [printfpubuffer]
        fist dword [printfpubuffer]
 printnum dword [printfpubuffer]
     ret    


ok, the printtext is just a macro to hide "printf <"%s", 0>, text" and printnum is the same thing, only with %i instead of %s. Now, with that out of the way, there isn't a problem with the stuff to the left of the decimal point. The problem comes in if it is equal or larger than 5 tenths, because it gets negged for some reason. So what the heck is going on there?
Post 05 Dec 2009, 07:26
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
maybe a better way would be convert fpu value directlly in long integer

for example, a 64 bit value, 32 for the int part, 32 for the frac part.

for this, you should store integer part, sub integer
multiply the rest by 2^32
store as integer.
ptr => str
do the num2str convertion of int part
put '.' char at ptr, inc ptr
do the num2str convertion of frac part
print str
Post 05 Dec 2009, 08:33
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
That's basically what i'm doing, no? I'm using fist to get the left part, i'm then printing it (but not before preserving it and the floating point registers). Then i throw the preserved st0 back in, subtract the int part from it, multiply what's left by 1 and eight 0s for 8 places of precision, then i fist that part to print it out.
Post 05 Dec 2009, 08:43
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
Laughing

excuze me, i didn't understood the code you wrote.
i am noob in fpu.
Post 05 Dec 2009, 09:01
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
It's ok, the FPU is a horrible device, and i made sure the reader knew that too. The only thing worse than the device itself is the format of floating point numbers which produces amazing results so people who aren't as knowledgeable tend to trust them more than they should. Can you imagine how many stupid scientific theories there may be out there that exist all because the accuracy is "good enough" when applied to models? Once you have an error due to the representation of the number in floating point format (whole numbers are notoriously strange) the errors just keep increasing and increasing.

I remember one time when taking VB last year that I spent a week or two arguing with the teacher about why my output would either match the book's sample or the output generated by the running program that came with the book. Needless to say, the argument finally ended when he did what i told him and checked the output in the book to see if the program he was running came up with the same numbers. Considering all i had to do to get my program to match one or the other was to switch from "float" to "double" and back, the outputs didn't match and i once again managed to get him to shut up when it came to problems we students were having. He notoriously seeks perfection and blames his students when they find that the book and/or test programs provided with it don't work. I still remember arguing with him that 10/2 is 5, not 8...
Post 05 Dec 2009, 09:10
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
What's your code supposed to do? Extract the mantissa & exponent? Sorry, I don't get it.
Post 05 Dec 2009, 17:06
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
Borsuc wrote:
What's your code supposed to do? Extract the mantissa & exponent? Sorry, I don't get it.


He extracts the whole and fractional part of the number and prints them.

He makes a backup and stores the thing as an integer, then prints that. Then he prints a "." and subtracts the integer part off the backup, multiplies that by 100,000,000, and prints that.


Kohlrak, I recommend that you change the rounding to truncate so rounding doesn't mess you up. With the default rounding, you will end up with the first digit after the decimal point wrong. And if it rounded up, the thing would be COMPLETELY wrong.

http://www.website.masmforum.com/tutorials/fptute/fpuchap1.htm#cword
http://www.website.masmforum.com/tutorials/fptute/fpuchap3.htm#fstcw
http://www.website.masmforum.com/tutorials/fptute/fpuchap3.htm#fldcw


Something like this(I put comments on what I added):
Code:
printfpubuffer dd 0
printfpupreserve dd 0
printfpudot db '.', 0
printfpucw dw 0 ;Holder for the Control Word
printfpu:
  fstcw [printfpucw] ;Store Control Word
      or [printfpucw],0000110000000000 ;Enable Truncate
   fldcw [printfpucw] ;Load Control Word to the FPU
        fst dword [printfpupreserve]
        fist dword [printfpubuffer]
        printnum dword [printfpubuffer]
        printtext printfpudot
        fninit ;resets the Control Word, which is fine. You only needed it for the fist.
        fld dword [printfpupreserve]
        fisub dword [printfpubuffer]
        mov dword [printfpubuffer], 100000000
        fimul dword [printfpubuffer]
        fist dword [printfpubuffer]
        printnum dword [printfpubuffer]
        ret
    

_________________
----> * <---- My star, won HERE


Last edited by windwakr on 05 Dec 2009, 20:28; edited 1 time in total
Post 05 Dec 2009, 18:28
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
oh I thought he wanted to display it in scientific notation, sorry.
Post 05 Dec 2009, 20:08
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
Odd, i thought the FIs truncated, and you had to frndint to get anything else. If that's the case, all i have to do is fabs and the result is good, and just add a comment about rounding mode. Thanks for your help, all.

Edit: what was i thinking? fld1, some sort of fcompare, and fadd. That way, the reader doesn't have to look up the uglyness of the FPU, just a handful of instructions.

Plus, shouldn't the fninit to be used to prevent the fpu from flipping out if it somehow gets overflowed or mmx is used behind the scenes? (Since we dont' know what printf does.)

Edit: I shouldn't code or make comments shortly after waking up... Laughing

Looks like i'll have to do that, despite how much i don't want to.
Post 05 Dec 2009, 23:31
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger 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-2020, Tomasz Grysztar.

Powered by rwasa.