flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Main > fpu accuracy

Author
Thread Post new topic Reply to topic
vivik



Joined: 29 Oct 2016
Posts: 88
fpu accuracy
Is that true that different processors have different default fpu accuracy, and you better manually set your accuracy with fldcw before each fpu usage? Or maybe setting it at the start of program is enough?

Do most computers currently in use support all three accuracy modes (24- 53- 64-bit)?

Is using highest accuracy much slower than using lowest?

Which accuracy would you recommend?

Also, which rounding method you would recommend: up, down, to zero, to nearest?

Extra info:
This guy warned me about different default accuracy on different computers: http://mauve.mizuumi.net/2013/06/16/desyncs-and-fpu-synchronization/

This tutorial says 64-bit is default:
http://www.website.masmforum.com/tutorials/fptute/fpuchap1.htm
>11 = 64 bits (REAL10) (this is the initialized state)

And, on my computer, my program starts with 53-bit accuracy. I guess that kind of answers half of first question.
Post 30 Dec 2016, 13:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14802
Location: Lost in translation
Within the same class of CPU (e.g. x86) the FPU accuracy is the same.

And the OS will decide which default precision (note, this is different from accuracy) to give each process. Different OSes might decide different precision values.

There can be differences in execution speed with a precision change. Faster results for lower precision. This may or may not affect your program. It depends upon what you are doing. There is no one best answer, you need to decide the trade-off that makes sense in your application. The timing difference usually only affects long running instructions like FDIV and FSIN etc. But different CPUs will give different results.

Same for rounding, you need to see what your algorithms need for rounding. Most common usage would probably be "nearest".
Post 30 Dec 2016, 16:25
View user's profile Send private message Visit poster's website Reply with quote
Xorpd!



Joined: 21 Dec 2006
Posts: 161
The precision bits IIRC only affect addition, subtraction, multiplication, division, and square root. Only division and square root have variable speeds depending on the precision. For normal math round to nearest is the most useful, probably you will only want the other modes for isolated carefully thought out instructions.

What is your goal, BTW? Given current (< 2 years old) hardware, AVX2 has the potential to blast the socks off FPU code in terms of performance for IEEE-754 double or single precision computation. Even using the GPU might be considered, although I don't know of anyone that uses assembly language to handle that particular beast.
Post 31 Dec 2016, 03:03
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 88
@revolution
What's the difference between accuracy and precision in this context?

@Xorpd!
My goal is gamedev. Precise results are not too important (unless i'll go into 3d i guess), speed is probably important, and working without problems on as many computers as possible is very important.

Thanks for info, hopefully it will help me avoid some problems in the future, and save some time from checking things by myself. Finally took down to learning fpu commands, i know very little about them.
Post 31 Dec 2016, 07:50
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
vivik, your problem is in terminologies. Accuracy, precision and rounding are different things. You're mixing them up. Setting the default precision and rounding are done by the OS. Linux and Windows have different default precision and rounding. Then the compilers knock in - offering their own unconventional precision. There is also 16-bit precision for low-powered devices and CPUs, NVIDIA does offer 16-bit FP precision for smaller data. They call it half-precision.

Rounding to nearest may be the choice for scientific and graphic apps but not so in financial applications where rounding is set by the central bank's rounding policy. Trust me, you don't want to use "round to nearest" at Point-of-Sale terminals Very Happy
Post 31 Dec 2016, 07:58
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 88
I probably should've used words 24- 53- 64-bit mantissa instead of both precision and accuracy.
Post 31 Dec 2016, 08:05
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 88
Is it possible for some external function to change fpu control word? Or there is some kind of convention that forbids that?

Also, what exactly finit does? And does it do the same thing on all oses and cpus?
Post 31 Dec 2016, 08:08
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
it is possible and not prohibited to change the control word of the FPU.


Code:
aPlace dw 0

fstcw word[aPlace]   ;store current control word
;set/reset the precision/rounding bits
fldcw word[aplace]  ;load the new modified control word



You can find things like this in Example section. Lookup fasmnewbie's posts.
Post 31 Dec 2016, 08:26
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 88
@system error
just wandering how often i should do that, before each fpu session or just once at the start of program.
Post 31 Dec 2016, 08:37
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
Just set it once... until it encounters a finit or you manually reset it to something else as the requirement changes. It's sticky.
Post 31 Dec 2016, 08:44
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14802
Location: Lost in translation

vivik wrote:
What's the difference between accuracy and precision in this context?

Take pi (3.141592653589...) for example.

6 digit precision and 6 digit accuracy = 3.14159
6 digit precision and 5 digit accuracy = 3.14158
6 digit precision and 4 digit accuracy = 3.14182

Or take the age old approximation for pi: 22/7

15 digit precision and 3 digit accuracy: 3.142857142857
We can specify this approximation to as many digits of precision as we want, but we can never improve on the accuracy no matter how many digits we list. Think of it this way: We can specify a precise value of 22/7, but it is not particularly accurate if we use it for pi.

Digits of extra precision is often not required if the accuracy is much less.
Post 31 Dec 2016, 10:32
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 88
Hopefully last question: where and when to put the FWAIT instruction? What will happen if I'll never use it, CPU will never raise an exception if I divide by zero for example?

Here are some explanations, but it's still not very clear to me: http://www.website.masmforum.com/tutorials/fptute/fpuchap3.htm
Post 31 Dec 2016, 13:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14802
Location: Lost in translation
FWAIT is not required unless you have a very old 8086 & 8087 system.

The CPU won't raise an exception for any FPU error unless you enable the specific exception bits for each error. This happens regardless of whether FWAIT is there or not.
Post 31 Dec 2016, 13:33
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 88
Always using FNINIT instead of FINIT is ok too?
Post 31 Dec 2016, 15:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14802
Location: Lost in translation
For all instructions you won't need FWAIT. Most of the old advice on the Internet about FWAIT is grossly outdated. You won't need it for any modern system. But if you want to use it then go ahead. No harm will happen. And for FINIT vs FNINIT you'll never see any difference if you use either one.
Post 31 Dec 2016, 16:57
View user's profile Send private message Visit poster's website Reply with quote
Xorpd!



Joined: 21 Dec 2006
Posts: 161
If you're interested in game development don't count on using the FPU in real life. A brand new computer that sells for about $100 has at least SSE4 and a GPU, e.g. a Z8330. So I would concentrate on programming SSEx or even AVX and OpenCL or something to program the GPU.

That's not to say that learning how to use the FPU isn't an excellent exercise in low level programming, but once you are conversant with it I suggest moving on to more powerful mechanisms that are already available in your computer.
Post 31 Dec 2016, 18:26
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 88
Yeah, I think I'll need to learn mmx/sse/sse2... I just cut open one old game, and seems like they didn't use anything beyond sse2, so I guess I'll do so too. I'm targeting 10 years old computers, sure, why not.

I created a new thread about mmx/sse/sse2, further questions about them will be there:
https://board.flatassembler.net/topic.php?t=19642
Post 01 Jan 2017, 05:54
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 88
Just stumbled upon this quote from fasm manual:

>frndint rounds it to the nearest integral value, depending on the current rounding mode.

Question: rounding mode affects results of (for example) multiplication when fpu runs out of mantissa bits, or it only affects result of frndint instruction?
Post 04 Jan 2017, 09:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14802
Location: Lost in translation
The rounding mode affects all operations where excess bits can be generated. The Intel and AMD manuals explain precisely what happens in the various cases that it applies to.
Post 04 Jan 2017, 09:35
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.