123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include "ftoa_engine.h"
- #include <stdint.h>
- #include <AP_Common/AP_Common.h>
- #include <AP_HAL/AP_HAL.h>
- static const int8_t exponentTable[32] = {
- -36, -33, -31, -29, -26, -24, -21, -19,
- -17, -14, -12, -9, -7, -4, -2, 0,
- 3, 5, 8, 10, 12, 15, 17, 20,
- 22, 24, 27, 29, 32, 34, 36, 39
- };
- static const uint32_t factorTable[32] = {
- 2295887404UL,
- 587747175UL,
- 1504632769UL,
- 3851859889UL,
- 986076132UL,
- 2524354897UL,
- 646234854UL,
- 1654361225UL,
- 4235164736UL,
- 1084202172UL,
- 2775557562UL,
- 710542736UL,
- 1818989404UL,
- 465661287UL,
- 1192092896UL,
- 3051757813UL,
- 781250000UL,
- 2000000000UL,
- 512000000UL,
- 1310720000UL,
- 3355443200UL,
- 858993459UL,
- 2199023256UL,
- 562949953UL,
- 1441151881UL,
- 3689348815UL,
- 944473297UL,
- 2417851639UL,
- 618970020UL,
- 1584563250UL,
- 4056481921UL,
- 1038459372UL
- };
- int16_t ftoa_engine(float val, char *buf, uint8_t precision, uint8_t maxDecimals)
- {
- uint8_t flags;
-
- uint8_t *valbits = (uint8_t*)&val;
- union {
- float v;
- uint32_t u;
- } x;
- x.v = val;
- uint32_t frac = x.u & 0x007fffffUL;
- if (precision>7) precision=7;
-
- if (valbits[3] & (1<<7)) flags = FTOA_MINUS; else flags = 0;
- uint8_t exp = valbits[3]<<1;
- if(valbits[2] & (1<<7)) exp++;
-
- if(exp==0 && frac==0) {
- buf[0] = flags | FTOA_ZERO;
- uint8_t i;
- for(i=0; i<=precision; i++) {
- buf[i+1] = '0';
- }
- return 0;
- }
- if(exp == 0xff) {
- if(frac == 0) flags |= FTOA_INF; else flags |= FTOA_NAN;
- }
-
- if (exp != 0) frac |= (1UL<<23);
- uint8_t idx = exp>>3;
- int8_t exp10 = exponentTable[idx];
-
-
-
- int64_t prod = (int64_t)frac * (int64_t)factorTable[idx];
-
-
-
-
-
-
- prod >>= (15-(exp & 7));
-
- uint8_t hadNonzeroDigit = 0;
- uint8_t outputIdx = 0;
- int64_t decimal = 100000000000000ull;
- do {
- char digit = '0';
- while(1) {
- while ((prod -= decimal) >= 0)
- digit++;
-
-
-
-
- prod += decimal;
- decimal /= 10;
-
- if (hadNonzeroDigit) break;
-
-
- if(digit == '0') {
- exp10--;
- continue;
- }
- hadNonzeroDigit = 1;
-
- if(maxDecimals != 0) {
- int8_t beforeDP = exp10+1;
- if (beforeDP < 1) beforeDP = 1;
-
- maxDecimals = maxDecimals+beforeDP-1;
- if (precision > maxDecimals)
- precision = maxDecimals;
- } else {
- precision++;
- }
- break;
- }
-
- outputIdx++;
- if(digit < '0' + 10)
- buf[outputIdx] = digit;
- else {
-
-
- for(hadNonzeroDigit=outputIdx; hadNonzeroDigit>0; hadNonzeroDigit--)
- buf[hadNonzeroDigit] = '9';
- goto roundup;
- }
- } while (outputIdx<precision);
-
- decimal *= 10;
- if (prod - (decimal >> 1) >= 0) {
- roundup:
-
- while(outputIdx != 0) {
- if(++buf[outputIdx] == '0' + 10) {
- if(outputIdx == 1) {
- buf[outputIdx] = '1';
- exp10++;
- flags |= FTOA_CARRY;
- break;
- } else
- buf[outputIdx--] = '0';
- }
- else break;
- }
- }
- buf[0] = flags;
- return exp10;
- }
|