Author Topic: FPU use, C vs MASM  (Read 20312 times)

KeepingRealBusy

  • Member
  • ***
  • Posts: 426
Re: FPU use, C vs MASM
« Reply #15 on: December 28, 2012, 02:43:04 PM »
Here is the result of the new output looking for rounding errors:

Code: [Select]
------------------------------------------------------------------ 17
Chi square distribution for 2048 samples is 6.13 (6.1250), and randomly
-------------------------------------------------------------- 17
Chi square distribution for 2048 samples is 6.12 (6.1250), and randomly

Now the question is, which one is correct? Should it round at .5 or at .5+.?
Note that my FPU output is via a slightly modified  FPULIB PROC FpuFLtoA, but because it is an embedded PROC in my code and not a library routine, I can modify it, but should I, or is ent wrong?

Dave.

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1196
Re: FPU use, C vs MASM
« Reply #16 on: December 28, 2012, 06:28:46 PM »
The CRT could be changing the rounding mode inside some function and then restoring it before the function returns, but I can’t see any ready way to detect this other than a debugger.

I have now tried optimizations up through /O2 /G7, and the output still matches that of the original EXE.
Well Microsoft, here’s another nice mess you’ve gotten us into.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: FPU use, C vs MASM
« Reply #17 on: December 28, 2012, 10:58:35 PM »
Here is the result of the new output looking for rounding errors:

Code: [Select]
------------------------------------------------------------------ 17
Chi square distribution for 2048 samples is 6.13 (6.1250), and randomly
-------------------------------------------------------------- 17
Chi square distribution for 2048 samples is 6.12 (6.1250), and randomly

Now the question is, which one is correct? Should it round at .5 or at .5+.?
Note that my FPU output is via a slightly modified  FPULIB PROC FpuFLtoA, but because it is an embedded PROC in my code and not a library routine, I can modify it, but should I, or is ent wrong?

Dave.

macht nichts
rounding up to 6.13 is technically correct
but, 6.12 is just as close - lol
wouldn't hurt to add another decimal digit   :P

KeepingRealBusy

  • Member
  • ***
  • Posts: 426
Re: FPU use, C vs MASM
« Reply #18 on: December 29, 2012, 11:48:27 AM »
Here are a couple of  links I picked up at the CodeProject:

http://linuxgazette.net/144/misc/lg/a_question_of_rounding_in_issue_143.html
http://msdn.microsoft.com/en-us/library/ty9hx077(VS.100).aspx
http://www.stroustrup.com/

I could not find too much in the msdn documentation about rounding, other than they round up the last digit, but nothing about which rounding method. I did find that if you cast a float to a (short) then you get truncation.

So far, in ENT, I have found truncation, round to an even , and normal rounding (just add a 0.5 and truncate).

Dave.

FORTRANS

  • Member
  • *****
  • Posts: 1226
Re: FPU use, C vs MASM
« Reply #19 on: December 30, 2012, 01:02:13 AM »
Hi Dave,

   That first link was interesting, thanks.

   In "The Art of Compuer Programming" by Donald E. Knuth,
there is a discussion on rounding.  He says that rounding up,
(rounding down), and truncation can cause drift in some cases.
Rounding to nearest or even (round to even) or round to odd
don't drift.  He says for base 2 or base 10 you should round
to even.  Drift is an accumulating error caused by the bias in
rounding in some repetitive algorithms.  (This is a bit simplfied.)

Regards,

Steve

Gunther

  • Member
  • *****
  • Posts: 4149
  • Forgive your enemies, but never forget their names
Re: FPU use, C vs MASM
« Reply #20 on: December 30, 2012, 03:47:21 AM »
You have to know the facts before you can distort them.

KeepingRealBusy

  • Member
  • ***
  • Posts: 426
Re: FPU use, C vs MASM
« Reply #21 on: December 30, 2012, 03:54:52 AM »
Steve,

I thought it was a very interesting  article also, even it it was talking about Linux. It did reference the  IEEE spec which identified the fact that rounding is implementation dependent.

I decided to just accept the differences I have found in the output when it is a difference of 1 in the last digit, and let it go at that. I removed the forced rounding for the cast in my modified ENT (since that could round an even to an odd). My MASMENT always uses round to nearest even, and it appears that the FpuLib FpuFLtoA does also.

Dave.

KeepingRealBusy

  • Member
  • ***
  • Posts: 426
Re: FPU use, C vs MASM
« Reply #22 on: December 30, 2012, 03:56:07 AM »
Gunther,

You responded while I was typing.

I agree.

Dave.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: FPU use, C vs MASM
« Reply #23 on: December 30, 2012, 03:56:25 AM »
it would be so much easier if CPU's spoke decimal as a native tongue - lol
if humans had 1 finger on each hand, we'd all be speaking binary
but, then, the computer may never have come about
it's kind of a zen parable thing

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: FPU use, C vs MASM
« Reply #24 on: December 30, 2012, 03:59:05 AM »
found it
reminds me of a discussion Jochen and i were having a while back...
http://www.masmforum.com/board/index.php?topic=11781.0

FORTRANS

  • Member
  • *****
  • Posts: 1226
Re: FPU use, C vs MASM
« Reply #25 on: December 30, 2012, 07:51:50 AM »
it would be so much easier if CPU's spoke decimal as a native tongue - lol

Hi Dave,

   That's what BCD is for.  But it would be a bit of a project
to implement IEEE floating point operations.

Cheers,

Steve N.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: FPU use, C vs MASM
« Reply #26 on: December 30, 2012, 07:55:26 AM »
BCD is a second language, with a poor translater   :P

il tuo asino sembra delizioso, oggi

lol
that turned out even worse than i expected