ascii to float (yet another question)
- From: Chewy509@xxxxxxxxxxxxxxxx
- Date: 22 Feb 2007 17:20:39 -0800
Hi Everyone
I've been thinking a lot about this recently (yes, I know I posted
about this back in May06) and have looked at about 30 implementations
of the atof(), however 1 implementation stood out in that it doesn't
use lookup tables for the exponent and to be honest appears really
simple.
It basically is along the lines of:
FILD word [exp] ; st0 = exponent
FLDL2T ; st0 = log2_10, st1 = exponent
FMULP st1; st0 = exp * log2_10
FBLD [sig] ; st0 = sig, st1 = exp
FSCALE ; st0 = st0 * 2 ^ st1
if value is neg then FCHS
done! exp is the exponent biased accordingly for the imported
significant taking into account the movement of the decimal when
converting the significant to BCD.
eg: 100.123456e99, would be sig as bcd when loaded = 100123456 exp =
93 (as the decimal shits right we sub the places from exp, if decimal
shifts left we add to the exp the number of places)
So how does it work? Well since we don't have a x^y instruction, we
need to use:
x^y = 2^ (y * log2_x) or since we are using 10 ^ y
10^y = 2 ^ (y * log2_10) .
The great thing is, is that the x87 FPU contains log2_10 as a
constant! eg FLDL2T!
And since our original form is: sig * 10 ^ exp we can use:
sig * (2 ^ (exp * log2_10))
based on the above knowledge, which in turn converts to x87 asm really
easily.
Can anyone find anything wrong with this approach? Or did I miss
something during my high school math classes?
Randy, I've noticed that you've used a table lookup for the exponent,
what advantages (if any) does that have over the above method?
PS. I've been away from my PC for about 4 months, nothing been done on
any of my projects, I guess life just got in the way! Glad to have
some free time again to take part in one of the hobbies I love. ;)
--
Darran (aka Chewy509) brought to you by Google Groups!
.
- Follow-Ups:
- Re: ascii to float (yet another question)
- From: Wolfgang Kern
- Re: ascii to float (yet another question)
- From: rhyde@xxxxxxxxxx
- Re: ascii to float (yet another question)
- Prev by Date: Re: Advanced Linux Programming
- Next by Date: Re: Advanced Linux Programming
- Previous by thread: Moved to drunkenblog
- Next by thread: Re: ascii to float (yet another question)
- Index(es):