Author Topic: C runtime function question.  (Read 310 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5944
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
C runtime function question.
« on: August 11, 2018, 01:31:43 AM »
I am looking for a pair of floating point conversions from the C runtime library and the two I have found are as follows.

    double atof( 
       const char *str   
    );

    char *_fcvt(   
       double value, 
       int count, 
       int *dec, 
       int *sign   
    ); 

I don't claim to be fully up to date with C runtime functions and while I can probably get these to work, I would be interested in any viable suggestions if there are any that are better suited to do the two way conversions.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 8891
  • Assembler is fun ;-)
    • MasmBasic
Re: C runtime function question.
« Reply #1 on: August 11, 2018, 02:30:16 AM »
There is really no lack of atof alternatives :lol:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/atodbl-atodbl-l-atoldbl-atoldbl-l-atoflt-atoflt-l
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/atof-atof-l-wtof-wtof-l

https://stackoverflow.com/questions/98586/where-can-i-find-the-worlds-fastest-atof-implementation

Of course, there are faster and more precise solutions around:
Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)

152829  cycles for 100 * CRT atof
23178   cycles for 100 * MB Val()

151248  cycles for 100 * CRT atof
23024   cycles for 100 * MB Val()

151656  cycles for 100 * CRT atof
24375   cycles for 100 * MB Val()

152207  cycles for 100 * CRT atof
23248   cycles for 100 * MB Val()

151453  cycles for 100 * CRT atof
23178   cycles for 100 * MB Val()

36      bytes for CRT atof
34      bytes for MB Val()

ST0     1234567890.123456716
ST0     1234567890.123456789

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5944
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: C runtime function question.
« Reply #2 on: August 11, 2018, 02:53:33 AM »
atof already works, I got it from the MSDN links before you posted them.

I think I have the two functions working, just doing the text formatting from fcvt.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

zedd151

  • Member
  • ****
  • Posts: 850
Re: C runtime function question.
« Reply #3 on: August 11, 2018, 03:02:29 AM »
 :biggrin:

Code: [Select]
AMD A6-9220e RADEON R4, 5 COMPUTE CORES 2C+3G   (SSE4)

127115  cycles for 100 * CRT atof
21107   cycles for 100 * MB Val()

126984  cycles for 100 * CRT atof
20389   cycles for 100 * MB Val()

126807  cycles for 100 * CRT atof
21040   cycles for 100 * MB Val()

127579  cycles for 100 * CRT atof
21366   cycles for 100 * MB Val()

127227  cycles for 100 * CRT atof
20325   cycles for 100 * MB Val()

36      bytes for CRT atof
34      bytes for MB Val()

ST0     1234567890.123456716
ST0     1234567890.123456789

--- ok ---
I'm not always the sharpest knife in the drawer, but I have my moments.  :P

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5944
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: C runtime function question.
« Reply #4 on: August 11, 2018, 03:12:42 AM »
Like most of the suggestions, they are not much use to me at the moment.

None the less, with a change to the floating point to ascii conversion, all is well and up and running reliably as a general purpose pair of conversions.  :P
« Last Edit: August 11, 2018, 04:19:07 AM by hutch-- »
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: