flat assembler
Message board for the users of flat assembler.

Index > Main > fast float to string and back?

Author
Thread Post new topic Reply to topic
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Hello assembly guru's.
I wish to find the fastest way to convert a float into a string and back.
Commonly used C functions are _gcvt/sprintf and strod/sscanf.
So i read online that sprintf profiles faster than _gcvt?
There must be room for improvement here?
Maybe we can put our minds together and come up with something really fast.
Post 20 Oct 2008, 19:56
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
doing nothing is faster.
Post 20 Oct 2008, 20:10
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
edfed wrote:
doing nothing is faster.

I don't mean to convert float to string then right back to float.
These should be two seperate statements.
Sorry if my post was that unclear...
Post 20 Oct 2008, 20:18
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
bitshifter,

Do you need some specific functionality (e.g. convert normalized 32/64/80-bit IEEE-754 float to decimal scientific representation) or generic one (like "%g" for printf/scanf family)?

By the way, I/O is much slower than preparation for it... (I hope that you don't involve strings in computations, but your mileage may vary Wink) Optimize where it worth that.
Post 20 Oct 2008, 20:52
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
Check here
Or here
Or here

I haven't really checked the posts, but they may have some stuff to work with.

_________________
----> * <---- My star, won HERE
Post 20 Oct 2008, 20:55
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
I am using it for dynamic 32 bit float to ASCII text output in game engine.
Generic sprintf/sscanf using "%f" is all i need to do.
Since im updating strings with new values at hundreds of frames per second this becomes the slowest part of this process, the output code is plenty fast enough.
Post 21 Oct 2008, 04:27
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
bitshifter,

It's still unclear: do you need to convert float to/from fixed format string (e.g. "%+.3f" for output) or more versatile and generic one (e.g. handle too big values with exponent, etc.)…

If your value expected to be in well defined interval (e.g. [0…1000) for frames per second), you can simply scale and fist it, then use your preferred itoa() and insert separator.

Or, may be, USAGE IS DISPLAY (contrary to COMPUTATIONAL -- COBOL'esque Wink)? Then store them as packed BCD, though it may be slower (do Intel's engineers still remember daa & das?)

Anyway, how many pixels you have to touch to display one-bit value? Eight-bit? Unless your GPU/API have simple-to-execute "display glyph X" command, your code is I/O-bound.
Post 21 Oct 2008, 17:56
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
In C, i would use:
Code:
float value = -123.006;
char buffer[32];
sprintf(buffer,"value is: %f",value);    

As for the width and precision, i am unsure how to determine what can fit in a dword.
Post 21 Oct 2008, 22:01
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3056
Location: vpcmipstrm
bitRAKE
baldr wrote:
Unless your GPU/API have simple-to-execute "display glyph X" command, your code is I/O-bound.
Maybe it could be done with a shader - just send value and GPU does the conversion / texture lookup for glyphs. Laughing

_________________
¯\(°_o)/¯ unlicense.org
Post 22 Oct 2008, 02:00
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
I am using a texture to define my own font and the strings characters will index into the texture, which are to be mapped onto a surface.
The rastering part is not what i need help with.
I only need to know a fast way to convert float to string.
How is a dword arranged as a floating point value?
If i could find that information, im sure i could make a string out of it.
Post 22 Oct 2008, 12:51
View user's profile Send private message Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
Ciao Smile
There is a FBSTP instruction.
This instruction rounds the value of ST register converts it to the packed BCD format and store it
Note that you must do a multiplication before to convert it, to desidered number of digit after the decimal point.
Example:
to have 2 digit after decimal point multiplicate for 100 then use FBSTP
to have 4 digit after decimal point multiplicate for 10000 then use FBSTP
and so on....
This is not faster way but is a start point.
To load BCD number use the FPU instruction FBLD
Wink

_________________
Nil Volentibus Arduum Razz
Post 22 Oct 2008, 14:01
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3056
Location: vpcmipstrm
bitRAKE
bitshifter wrote:
How is a dword arranged as a floating point value?
If i could find that information, im sure i could make a string out of it.
Intel Manual, Volume 1, Chapter 4
Best to look at the manual, but most numbers follow a 1:8:23 partitioning of the bits: high bit is sign; eight bits for biased exponent (need to subtract 127 for correct exponent); and twenty-three bits for fraction (bit 24 is implied set).

Starting with FBSTP/FBLD is a very good idea, imho.

_________________
¯\(°_o)/¯ unlicense.org
Post 23 Oct 2008, 05:19
View user's profile Send private message Visit poster's website Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
bitshifter wrote:
I only need to know a fast way to convert float to string.
How is a dword arranged as a floating point value?
If i could find that information, im sure i could make a string out of it.

I wrote a little test program using the fpu FILD (load integer) and FSTP (store short real & pop) instructions and here's what I found in short real dwords:

integer.................short real
word......................dword

FFF8 = -8.0 = C1000000h

FFFD = -3.0 = C0400000h
FFFE = -2.0 = C0000000h
FFFF = -1.0 = BF800000h
0000 = 0 = 00000000h
0001 = 1.0 = 3F800000h
0002 = 2.0 = 40000000h
0003 = 3.0 = 40400000h


0008 = 8.0 = 41000000h

0010 = 16.0 = 41800000h

0020 = 32.0 = 42000000h


Then I just used fasm to the assemble source as below which generated dwords as shown and confirmed my results above:

Code:
   DD -2.0         C0000000h
   DD -1.0         BF800000h
        DD -0.75   BF400000h
   DD -0.5         BF000000h
   DD -0.25        BE800000h
   DD -0.1         BDCCCCCDh
   DD -0.075       BD99999Ah
   DD -0.05        BD4CCCCDh
   DD -0.025       BCCCCCCDh
        DD -0.01   BC23D70Ah

       DD 0.0          00000000h
   DD 0.01         3C23D70Ah
   DD 0.025        3CCCCCCDh
   DD 0.05         3D4CCCCDh
   DD 0.075        3D99999Ah
   DD 0.1          3DCCCCCDh
   DD 0.25         3E800000h
   DD 0.5          3F000000h
   DD 0.75         3F400000h
   DD 1.0          3F800000h
   DD 2.0          40000000h    


I hope you can figure out the string conversion... Let us know how you go!

Maybe look in the fasm sources, or ask Tomasz how he does it in the reverse direction from the text string after the directive?

_________________
FAMOS - the first memory operating system
Post 23 Oct 2008, 23:44
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Yes, i have been reading EXPRESSI.INC from the fasm sources and also some of the work Randy Hyde had done within his stdlib sources.
It looks like using the FPU might be easier to do, but in the long run it would be slower than if done without FPU.
I guess since speed is my goal here, it will be worth doing it the hard way.
Since i have only been punching assembly code for a short time this will probably take forever for me to fully grasp this concept.
Post 24 Oct 2008, 01:59
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
you could use the functions (atof, sprintf) in 'crtdll.dll or msvcrt.dll' libraries. Just remember to use 'cinvoke' to call these functions.
Post 25 Oct 2008, 09:36
View user's profile Send private message Reply with quote
LowLevelMahn



Joined: 30 Sep 2008
Posts: 12
LowLevelMahn
http://code.google.com/p/stringencoders/source/browse/trunk/src/modp_numtoa.c

modp_dtoa

much faster than sprintf

and you can make this even faster if you use the same precision all the time or if you don't need the sprintf rounding style
Post 27 Oct 2008, 08:17
View user's profile Send private message Reply with quote
codelab



Joined: 03 Apr 2006
Posts: 16
Location: Denmark
codelab
bitshifter wrote:

Since im updating strings with new values at hundreds of frames per second this becomes the slowest part of this process, the output code is plenty fast enough.


Hi bitsh, if these values are for displaying the eye will never catch it at this rate, go for 25-30 updates/second, calculate&update changed values only.
sincerely C.
Post 29 Oct 2008, 09:54
View user's profile Send private message 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


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.