Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nonstd.c 5.0KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "avr_functions.h"
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <math.h>
  34. char * ultoa(unsigned long val, char *buf, int radix)
  35. {
  36. unsigned digit;
  37. int i=0, j;
  38. char t;
  39. while (1) {
  40. digit = val % radix;
  41. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  42. val /= radix;
  43. if (val == 0) break;
  44. i++;
  45. }
  46. buf[i + 1] = 0;
  47. for (j=0; j < i; j++, i--) {
  48. t = buf[j];
  49. buf[j] = buf[i];
  50. buf[i] = t;
  51. }
  52. return buf;
  53. }
  54. char * ltoa(long val, char *buf, int radix)
  55. {
  56. if (val >= 0) {
  57. return ultoa(val, buf, radix);
  58. } else {
  59. buf[0] = '-';
  60. ultoa(-val, buf + 1, radix);
  61. return buf;
  62. }
  63. }
  64. #define DTOA_UPPER 0x04
  65. char * fcvtf(float, int, int *, int *);
  66. int isnanf (float x);
  67. int isinff (float x);
  68. char * dtostrf(float val, int width, unsigned int precision, char *buf)
  69. {
  70. int decpt, sign, reqd, pad;
  71. const char *s, *e;
  72. char *p;
  73. int awidth = abs(width);
  74. if (isnanf(val)) {
  75. int ndigs = (val<0) ? 4 : 3;
  76. awidth = (awidth > ndigs) ? awidth - ndigs : 0;
  77. if (width<0) {
  78. while (awidth) {
  79. *buf++ = ' ';
  80. awidth--;
  81. }
  82. }
  83. if (copysignf(1.0f, val)<0) *buf++ = '-';
  84. if (DTOA_UPPER) {
  85. *buf++ = 'N'; *buf++ = 'A'; *buf++ = 'N';
  86. } else {
  87. *buf++ = 'n'; *buf++ = 'a'; *buf++ = 'n';
  88. }
  89. while (awidth) {
  90. *buf++ = ' ';
  91. awidth--;
  92. }
  93. *buf = 0;
  94. return buf;
  95. }
  96. if (isinff(val)) {
  97. int ndigs = (val<0) ? 4 : 3;
  98. awidth = (awidth > ndigs) ? awidth - ndigs : 0;
  99. if (width<0) {
  100. while (awidth) {
  101. *buf++ = ' ';
  102. awidth--;
  103. }
  104. }
  105. if (val<0) *buf++ = '-';
  106. if (DTOA_UPPER) {
  107. *buf++ = 'I'; *buf++ = 'N'; *buf++ = 'F';
  108. } else {
  109. *buf++ = 'i'; *buf++ = 'n'; *buf++ = 'f';
  110. }
  111. while (awidth) {
  112. *buf++ = ' ';
  113. awidth--;
  114. }
  115. *buf = 0;
  116. return buf;
  117. }
  118. s = fcvtf(val, precision, &decpt, &sign);
  119. // if only 1 digit in output
  120. if (precision == 0 && decpt == 0) {
  121. // round and move decimal point
  122. s = (*s < '5') ? "0" : "1";
  123. decpt++;
  124. }
  125. // if all zeros, limit to precision
  126. if (-decpt > (int)precision) {
  127. s = "0";
  128. decpt = -precision;
  129. }
  130. reqd = strlen(s);
  131. // add 1 for decimal point
  132. if (reqd > decpt) reqd++;
  133. // add 1 for zero in front of decimal point
  134. if (decpt == 0) reqd++;
  135. // if leading zeros after decimal point
  136. if (decpt < 0 && precision > 0) {
  137. // ensure enough trailing zeros, add 2 for '0.'
  138. reqd = precision + 2;
  139. if (strlen(s) > precision + decpt) {
  140. // bug in fcvtf. e.g. 0.012, precision 2 should return 1 instead of 12.
  141. // However, 1.2, precision 0 returns correct value. So shift values so
  142. // that decimal point is after the first digit, then convert again
  143. int newPrecision = precision;
  144. int newDecimalPoint;
  145. // shift decimal point
  146. while (newPrecision > 0) {
  147. val *= 10.0;
  148. newPrecision--;
  149. }
  150. // round after accounting for leading 0's
  151. s = fcvtf(val, newPrecision, &newDecimalPoint, &sign);
  152. // if rounded up to new digit (e.g. 0.09 to 0.1), move decimal point
  153. if (newDecimalPoint - decpt == precision + 1) decpt++;
  154. }
  155. }
  156. // add 1 for sign if negative
  157. if (sign) reqd++;
  158. p = buf;
  159. e = p + reqd;
  160. pad = width - reqd;
  161. if (pad > 0) {
  162. e += pad;
  163. while (pad-- > 0) *p++ = ' ';
  164. }
  165. if (sign) *p++ = '-';
  166. if (decpt == 0 && precision > 0) {
  167. *p++ = '0';
  168. *p++ = '.';
  169. }
  170. else if (decpt < 0 && precision > 0) {
  171. *p++ = '0';
  172. *p++ = '.';
  173. // print leading zeros
  174. while ( decpt < 0 ) {
  175. decpt++;
  176. *p++ = '0';
  177. }
  178. }
  179. // print digits
  180. while (p < e) {
  181. *p++ = *s++;
  182. if (p == e) break;
  183. if (--decpt == 0) *p++ = '.';
  184. }
  185. if (width < 0) {
  186. pad = (reqd + width) * -1;
  187. while (pad-- > 0) *p++ = ' ';
  188. }
  189. *p = 0;
  190. //char format[20];
  191. //sprintf(format, "%%%d.%df", width, precision);
  192. //sprintf(buf, format, val);
  193. return buf;
  194. }