flat assembler
Message board for the users of flat assembler.
Index
> Main > fast float to string and back? |
Author |
|
edfed 20 Oct 2008, 20:10
doing nothing is faster.
|
|||
20 Oct 2008, 20:10 |
|
bitshifter 20 Oct 2008, 20:18
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... |
|||
20 Oct 2008, 20:18 |
|
baldr 20 Oct 2008, 20:52
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 ) Optimize where it worth that. |
|||
20 Oct 2008, 20:52 |
|
windwakr 20 Oct 2008, 20:55
Check here
Or here Or here I haven't really checked the posts, but they may have some stuff to work with. |
|||
20 Oct 2008, 20:55 |
|
bitshifter 21 Oct 2008, 04:27
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. |
|||
21 Oct 2008, 04:27 |
|
baldr 21 Oct 2008, 17:56
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 )? 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. |
|||
21 Oct 2008, 17:56 |
|
bitshifter 21 Oct 2008, 22:01
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. |
|||
21 Oct 2008, 22:01 |
|
bitRAKE 22 Oct 2008, 02:00
baldr wrote: Unless your GPU/API have simple-to-execute "display glyph X" command, your code is I/O-bound. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
22 Oct 2008, 02:00 |
|
bitshifter 22 Oct 2008, 12:51
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. |
|||
22 Oct 2008, 12:51 |
|
DJ Mauretto 22 Oct 2008, 14:01
Ciao
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 _________________ Nil Volentibus Arduum |
|||
22 Oct 2008, 14:01 |
|
bitRAKE 23 Oct 2008, 05:19
bitshifter wrote: How is a dword arranged as a floating point value? 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)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
23 Oct 2008, 05:19 |
|
neville 23 Oct 2008, 23:44
bitshifter wrote: I only need to know a fast way to convert float to string. 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 |
|||
23 Oct 2008, 23:44 |
|
bitshifter 24 Oct 2008, 01:59
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. |
|||
24 Oct 2008, 01:59 |
|
madmatt 25 Oct 2008, 09:36
you could use the functions (atof, sprintf) in 'crtdll.dll or msvcrt.dll' libraries. Just remember to use 'cinvoke' to call these functions.
|
|||
25 Oct 2008, 09:36 |
|
LowLevelMahn 27 Oct 2008, 08:17
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 |
|||
27 Oct 2008, 08:17 |
|
codelab 29 Oct 2008, 09:54
bitshifter wrote:
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. |
|||
29 Oct 2008, 09:54 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.