Click here to Skip to main content
13,044,467 members (58,917 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All

I'm trying to write my own "ftoa" function, as a part of my performance CString class (CFString). Since I aim performance I can't use sprintf() or any other standard method to do the job. Actually I'm supposed to match the performance of my function against sprintf() and others standard solutions like CString::Format().

I'm actually almost done, but the last push seems to be the hardest! :)
The IEEE-754 standard imposes a few cases which require different approach in order to extract the value into string, according to the binary exponent's value. They are:

if(exp > 31)
    //NO solution
else if(exp < -31)
    //Solved. Works twise more slowlly than sprintf().
    //Don't know how to imrove it.
else if(exp < 31 && exp >= 23)
    //Solved. Works twise faster than sprintf().
else if(exp < 23 && exp >= 0)
    //Solved. twise faster.
else if(exp < 0 && exp >= -23)
    //Solved. twise faster.

This is the overall structure of my ftoa (although it's body is entirely implemened in assembly).

Now my question is how to solve the case with exp > 31? :)
I've done a lot of research on IEEE-754 standard but some details never got quite clear to me.
I've found just two useful examples which helped me but their goal is to merely do the job, no performance issues taken into account, and very limited functionality. For example, the one doesn't process the cases (exp > 31) and (exp < -31) at all, the other is even worse! :laugh:
I even tried to debug sprintf() itself looking into its disassembly but the code is way too messy for me. :)
And of course if you google "How to convert float to string" almost all the answers will be -> use sprintf()

So considering the amount of time I've spent any help will be greatly appreciated! :)
Posted 13-Jul-10 3:45am
Updated 13-Jul-10 4:03am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try having a look to the _ecvt (CRT)[^] and _fcvt (CRT)[^] functions: they make exactly the job you are working on.

They are faster than sprintf because they don't need to parse the format string.

You can also get their disassembly to get an idea on how to do (use the RELEASE version): the code of sprintf is complicated because that function has variable number of arguments, a format string to parse, and a lot of data-types and formats to handle. Probably the _ecvt and _fcvt disassembly is easier.
Ivan Ivanov 83 13-Jul-10 11:05am
Thank you veery much :) That's indeed exactly what I needed!
As you mentioned "variable number of arguments, a format string to parse, and a lot of data-types" it puzzles me how sprintf and others like it tell the difference between float and double? I mean we type "%f" in both cases but when it's double two dwords are pushed in the stack, so how can that be distinguished from within the function? I'm just curious :)
Nishant Sivakumar 13-Jul-10 11:16am
Reason for my vote of 5
Worth 5!
Sauro Viti 13-Jul-10 12:01pm
The compiler automatically cast float values to double and then pushes two dwords in the stack: each float can be represented with a double and the cast overhead is about one CPU clock cycle in the worst case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 13 Jul 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100