ftoa_engine.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Copyright (c) 2005, Dmitry Xmelkov
  2. All rights reserved.
  3. Rewritten in C by Soren Kuula
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of the copyright holders nor the names of
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE. */
  26. #include "ftoa_engine.h"
  27. #include <stdint.h>
  28. #include <AP_Common/AP_Common.h>
  29. #include <AP_HAL/AP_HAL.h>
  30. /*
  31. * 2^b ~= f * r * 10^e
  32. * where
  33. * i = b div 8
  34. * r = 2^(b mod 8)
  35. * f = factorTable[i]
  36. * e = exponentTable[i]
  37. */
  38. static const int8_t exponentTable[32] = {
  39. -36, -33, -31, -29, -26, -24, -21, -19,
  40. -17, -14, -12, -9, -7, -4, -2, 0,
  41. 3, 5, 8, 10, 12, 15, 17, 20,
  42. 22, 24, 27, 29, 32, 34, 36, 39
  43. };
  44. static const uint32_t factorTable[32] = {
  45. 2295887404UL,
  46. 587747175UL,
  47. 1504632769UL,
  48. 3851859889UL,
  49. 986076132UL,
  50. 2524354897UL,
  51. 646234854UL,
  52. 1654361225UL,
  53. 4235164736UL,
  54. 1084202172UL,
  55. 2775557562UL,
  56. 710542736UL,
  57. 1818989404UL,
  58. 465661287UL,
  59. 1192092896UL,
  60. 3051757813UL,
  61. 781250000UL,
  62. 2000000000UL,
  63. 512000000UL,
  64. 1310720000UL,
  65. 3355443200UL,
  66. 858993459UL,
  67. 2199023256UL,
  68. 562949953UL,
  69. 1441151881UL,
  70. 3689348815UL,
  71. 944473297UL,
  72. 2417851639UL,
  73. 618970020UL,
  74. 1584563250UL,
  75. 4056481921UL,
  76. 1038459372UL
  77. };
  78. int16_t ftoa_engine(float val, char *buf, uint8_t precision, uint8_t maxDecimals)
  79. {
  80. uint8_t flags;
  81. // Bit reinterpretation hacks. This will ONLY work on little endian machines.
  82. uint8_t *valbits = (uint8_t*)&val;
  83. union {
  84. float v;
  85. uint32_t u;
  86. } x;
  87. x.v = val;
  88. uint32_t frac = x.u & 0x007fffffUL;
  89. if (precision>7) precision=7;
  90. // Read the sign, shift the exponent in place and delete it from frac.
  91. if (valbits[3] & (1<<7)) flags = FTOA_MINUS; else flags = 0;
  92. uint8_t exp = valbits[3]<<1;
  93. if(valbits[2] & (1<<7)) exp++; // TODO possible but in case of subnormal
  94. // Test for easy cases, zero and NaN
  95. if(exp==0 && frac==0) {
  96. buf[0] = flags | FTOA_ZERO;
  97. uint8_t i;
  98. for(i=0; i<=precision; i++) {
  99. buf[i+1] = '0';
  100. }
  101. return 0;
  102. }
  103. if(exp == 0xff) {
  104. if(frac == 0) flags |= FTOA_INF; else flags |= FTOA_NAN;
  105. }
  106. // The implicit leading 1 is made explicit, except if value subnormal.
  107. if (exp != 0) frac |= (1UL<<23);
  108. uint8_t idx = exp>>3;
  109. int8_t exp10 = exponentTable[idx];
  110. // We COULD try making the multiplication in situ, where we make
  111. // frac and a 64 bit int overlap in memory and select/weigh the
  112. // upper 32 bits that way. For starters, this is less risky:
  113. int64_t prod = (int64_t)frac * (int64_t)factorTable[idx];
  114. // The expConvFactorTable are factor are correct iff the lower 3 exponent
  115. // bits are 1 (=7). Else we need to compensate by divding frac.
  116. // If the lower 3 bits are 7 we are right.
  117. // If the lower 3 bits are 6 we right-shift once
  118. // ..
  119. // If the lower 3 bits are 0 we right-shift 7x
  120. prod >>= (15-(exp & 7));
  121. // Now convert to decimal.
  122. uint8_t hadNonzeroDigit = 0; // a flag
  123. uint8_t outputIdx = 0;
  124. int64_t decimal = 100000000000000ull;
  125. do {
  126. char digit = '0';
  127. while(1) {// find the first nonzero digit or any of the next digits.
  128. while ((prod -= decimal) >= 0)
  129. digit++;
  130. // Now we got too low. Fix it by adding again, once.
  131. // it might appear more efficient to check before subtract, or
  132. // to save and restore last nonnegative value - but in fact
  133. // they take as long time and more space.
  134. prod += decimal;
  135. decimal /= 10;
  136. // If already found a leading nonzero digit, accept zeros.
  137. if (hadNonzeroDigit) break;
  138. // Else, don't return results with a leading zero! Instead
  139. // skip those and decrement exp10 accordingly.
  140. if(digit == '0') {
  141. exp10--;
  142. continue;
  143. }
  144. hadNonzeroDigit = 1;
  145. // Compute how many digits N to output.
  146. if(maxDecimals != 0) { // If limiting decimals...
  147. int8_t beforeDP = exp10+1; // Digits before point
  148. if (beforeDP < 1) beforeDP = 1; // Numbers < 1 should also output at least 1 digit.
  149. /*
  150. * Below a simpler version of this:
  151. int8_t afterDP = outputNum - beforeDP;
  152. if (afterDP > maxDecimals-1)
  153. afterDP = maxDecimals-1;
  154. outputNum = beforeDP + afterDP;
  155. */
  156. maxDecimals = maxDecimals+beforeDP-1;
  157. if (precision > maxDecimals)
  158. precision = maxDecimals;
  159. } else {
  160. precision++; // Output one more digit than the param value.
  161. }
  162. break;
  163. }
  164. // Now have a digit.
  165. outputIdx++;
  166. if(digit < '0' + 10) // normal case.
  167. buf[outputIdx] = digit;
  168. else {
  169. // Abnormal case, write 9s and bail.
  170. // We might as well abuse hadNonzeroDigit as counter, it will not be used again.
  171. for(hadNonzeroDigit=outputIdx; hadNonzeroDigit>0; hadNonzeroDigit--)
  172. buf[hadNonzeroDigit] = '9';
  173. goto roundup; // this is ugly but it _is_ code derived from assembler :)
  174. }
  175. } while (outputIdx<precision);
  176. // Rounding:
  177. decimal *= 10;
  178. if (prod - (decimal >> 1) >= 0) {
  179. roundup:
  180. // Increment digit, cascade
  181. while(outputIdx != 0) {
  182. if(++buf[outputIdx] == '0' + 10) {
  183. if(outputIdx == 1) {
  184. buf[outputIdx] = '1';
  185. exp10++;
  186. flags |= FTOA_CARRY;
  187. break;
  188. } else
  189. buf[outputIdx--] = '0'; // and the loop continues, carrying to next digit.
  190. }
  191. else break;
  192. }
  193. }
  194. buf[0] = flags;
  195. return exp10;
  196. }