Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC7.1 C++ ASM
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! Smile | :)
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? Smile | :)
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 | :laugh:
I even tried to debug sprintf() itself looking into its disassembly but the code is way too messy for me. Smile | :)
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! Smile | :)
Posted 13-Jul-10 4:45am
Edited 13-Jul-10 5:03am
v4

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
Ivan Ivanov 83 at 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 at 13-Jul-10 11:16am
   
Reason for my vote of 5
Worth 5!
Sauro Viti at 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
0 OriginalGriff 436
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 170
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 13 Jul 2010
Copyright © CodeProject, 1999-2014
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