ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
mathio.c
Go to the documentation of this file.
1 
28 #ifdef USER_CODFIG
29 #include "user_config.h"
30 #endif
31 
32 #ifdef PRINTF_TEST
33 #include <stdio.h>
34 #include <stdlib.h>
35 #endif
36 
37 #include <stdint.h>
38 #include <stdarg.h>
39 #include <string.h>
40 
41 #include "mathio.h"
42 
43 #include <math.h>
44 #undef atof
45 
46 // =============================================
47 // MATH/IO
48 // =============================================
49 
50 // =============================================
56 MEMSPACE
57 int
58 atodigit(int c,int radix)
59 {
60  int ret = -1;
61  if(c >= '0' && c <= '9')
62  ret = c - '0';
63  else if(c >= 'A' && c <= 'F')
64  ret = c - 'A' + 10;
65  else if(c >= 'a' && c <= 'f')
66  ret = c - 'a' + 10;
67  else return (-1);
68  return((ret >= radix) ? -1 : ret);
69 }
70 
71 // =============================================
79 long atoh(const char *p)
80 {
81  return strtol(p, (char **) NULL, 16);
82 }
83 
84 
85 // =============================================
92 long
93 aton(char *str, int base)
94 {
95  unsigned long num;
96  char *endptr;
97 
98  num = strtol(str, &endptr, base);
99  return(num);
100 }
101 
102 // =============================================
107 MEMSPACE
108 int mul10str(uint8_t *str, int size)
109 {
110 
111  uint16_t d;
112  uint16_t carry = 0;
113  while(size--)
114  {
115  d = *str;
116  d <<= 1;
117  d <<= 1;
118  d += *str;
119  d <<= 1;
120  d += carry;
121  *str = d & 0xff;
122  carry = d >> 8;
123  ++str;
124  }
125  return(carry);
126 }
127 
128 // =============================================
134 MEMSPACE
135 long
136 strtol(const char *nptr, char **endptr, int base)
137 {
138  unsigned long num;
139  int sign;
140  int d;
141 
142  while(*nptr == ' ' || *nptr == '\t')
143  ++nptr;
144  sign = 0;
145  if(*nptr == '-' )
146  {
147  sign = 1;
148  ++nptr;
149  }
150  else if(*nptr == '+' )
151  {
152  ++nptr;
153  }
154  // skip leading zeros
155  while(*nptr == '0')
156  ++nptr;
157  num = 0;
158  while(*nptr)
159  {
160  d = atodigit(*nptr,base);
161  if(d < 0)
162  break;
163  num = num*base;
164  num += d;
165  ++nptr;
166  }
167 
168  if(sign)
169  num = -num;
170  if(endptr)
171  *endptr = (char *) nptr;
172  return(num);
173 }
174 
175 // =============================================
181 MEMSPACE
182 long long
183 strtoll(const char *nptr, char **endptr, int base)
184 {
185  unsigned long long num;
186  int sign;
187  int d;
188 
189  while(*nptr == ' ' || *nptr == '\t')
190  ++nptr;
191  sign = 0;
192  if(*nptr == '-' )
193  {
194  sign = 1;
195  ++nptr;
196  }
197  else if(*nptr == '+' )
198  {
199  ++nptr;
200  }
201  // skip leading zeros
202  while(*nptr == '0')
203  ++nptr;
204 
205  num = 0;
206  while(*nptr)
207  {
208  d = atodigit(*nptr,base);
209  if(d < 0)
210  break;
211  num = num*base;
212  num += d;
213  ++nptr;
214  }
215  if(sign)
216  num = -num;
217  if(endptr)
218  *endptr = (char *) nptr;
219  return(num);
220 }
221 
222 
223 #ifdef __SIZEOF_INT128__
224 // =============================================
230 MEMSPACE
231 __uint128_t
232 strto128(const char *nptr, char **endptr, int base)
233 {
234  __uint128_t num;
235  int sign;
236  int d;
237 
238  while(*nptr == ' ' || *nptr == '\t')
239  ++nptr;
240  sign = 0;
241  if(*nptr == '-' )
242  {
243  sign = 1;
244  ++nptr;
245  }
246  else if(*nptr == '+' )
247  {
248  ++nptr;
249  }
250 
251  // skip leading zeros
252  while(*nptr == '0')
253  ++nptr;
254 
255  num = 0;
256  while(*nptr)
257  {
258  d = atodigit(*nptr,base);
259  if(d < 0)
260  break;
261  num = num*base;
262  num += d;
263  ++nptr;
264  }
265  if(sign)
266  num = -num;
267  if(endptr)
268  *endptr = (char *) nptr;
269  return(num);
270 }
271 
272 #endif
273 
274 // =============================================
279 MEMSPACE
280 int
281 atoi(const char *str)
282 {
283  unsigned long num;
284  num = strtol(str, NULL, 10);
285  return((int)num);
286 }
287 
288 // =============================================
293 MEMSPACE
294 long
295 atol(const char *str)
296 {
297  unsigned long num;
298  num = strtol(str, NULL, 10);
299  return(num);
300 }
301 
302 // =============================================
303 // Floating point I/O helper functions
304 // =============================================
305 #ifdef FLOATIO
306 MEMSPACE
312 double
313 iexp(double num, int exp)
314 {
315  double a;
316  if(exp==0)
317  return(1.0);
318  if(exp <0)
319  {
320  a = 1.0 / num;
321  exp = 1 - exp;
322  }
323  else
324  {
325  exp = exp - 1;
326  a = num;
327  }
328  while(exp)
329  {
330  if(exp & 0x01)
331  num *= a;
332  if(exp >>= 1)
333  a *= a;
334  }
335  return(num);
336 }
337 
338 // =============================================
343 MEMSPACE
344 double
345 scale10(double num, int *exp)
346 {
347  int exp10,exp2;
348  int sign;
349 
350  double scale;
351 
352  if(!num)
353  {
354  *exp = 0;
355  return(0.0);
356  }
357 
358  sign = 0;
359  if(num < 0)
360  {
361  num = -num;
362  sign = 1;
363  }
364 
365  // extract exponent
366  frexp(num, &exp2);
367  // aproximate exponent in base 10
368  exp10 = ((double) exp2) / (double) 3.321928095;
369 
370  // convert scale to 10.0**exp10
371  scale = iexp((double)10.0, exp10);
372 
373  // remove scale
374  num /= scale;
375 
376  // correct for over/under
377  while(num >= (double)10.0)
378  {
379  num /= (double) 10.0;
380  ++exp10;
381  }
382  while(num < (double) 1.0)
383  {
384  num *= (double) 10.0;
385  --exp10;
386  }
387 
388  *exp = exp10;
389  if(sign)
390  return(-num);
391  return(num);
392 }
393 
394 // =============================================
399 MEMSPACE
400 double
401 strtod(const char *nptr, char **endptr)
402 {
403  double num;
404  double frac;
405  double tmp;
406 
407  int digit, power,sign;
408 
409  while(*nptr == ' ' || *nptr == '\t' || *nptr == '\n')
410  ++nptr;
411  sign = 1;
412  if(*nptr == '-')
413  {
414  ++nptr;
415  sign = -1;
416  }
417  else if(*nptr == '+')
418  {
419  ++nptr;
420  }
421  // skip leading zeros
422  while(*nptr == '0')
423  ++nptr;
424  num = 0.0;
425  while(*nptr && isdigit(*nptr))
426  {
427  num *= 10.0; // make room for new digit
428  digit = (*nptr - '0');
429  num += (double) digit;
430  nptr++;
431  }
432 
433  if(*nptr == '.')
434  {
435  ++nptr;
436  frac = 0.1;
437  while(*nptr && isdigit(*nptr))
438  {
439  digit = (*nptr - '0');
440  tmp = (double) digit;
441  num += tmp * frac;
442  frac *= 0.1;
443  nptr++;
444  }
445  }
446  if(sign == -1)
447  num = -num;
448 
449  if(*nptr == 'E' || *nptr == 'e')
450  {
451  nptr++;
452  sign = (*nptr == '-') ? -1 : 1;
453  if(sign == -1 || *nptr == '+')
454  nptr++;
455  power=0;
456  while(isdigit(*nptr))
457  {
458  power *= 10.0;
459  digit = (*nptr - '0');
460  power += (double) digit;
461  nptr++;
462  }
463  if(num)
464  {
465  if(sign<0)
466  power = -power;
467  // iexp - number to integer power
468  num *= iexp(10.0, power);
469  }
470  }
471  if(endptr)
472  *endptr = (char *) nptr;
473  return(num);
474 }
475 
476 // =============================================
480 MEMSPACE
481 double
482 atof(const char *str)
483 {
484  double num;
485  num = strtod(str, NULL);
486  return(num);
487 }
488 
489 #endif // ifdef FLOATIO
490 
MEMSPACE int atodigit(int c, int radix)
Convert ASCII character to radix based digit , or -1.
Definition: mathio.c:58
unsigned short uint16_t
Definition: send.c:18
Master include file for project Includes all project includes and defines here.
MEMSPACE double scale10(double num, int *exp)
MEMSPACE int mul10str(uint8_t *str, int size)
Fast multiply number of any size by 10.
Definition: mathio.c:108
MEMSPACE double atof(const char *str)
#define NULL
Definition: cpu.h:55
MEMSPACE double strtod(const char *nptr, char **endptr)
MEMSPACE long atol(const char *str)
Convert ASCII string to number in base 10.
Definition: mathio.c:295
Math IO functions, and verious conversion code with floating point support.
MEMSPACE long strtol(const char *nptr, char **endptr, int base)
Convert ASCII string to number in a given base.
Definition: mathio.c:136
MEMSPACE double iexp(double num, int exp)
MEMSPACE long long strtoll(const char *nptr, char **endptr, int base)
Convert ASCII string to number in a given base.
Definition: mathio.c:183
#define MEMSPACE
Definition: cpu.h:25
MEMSPACE int WEAK_ATR isdigit(int c)
test if a character is a digit
Definition: stringsup.c:48
unsigned char uint8_t
Definition: send.c:17
MEMSPACE int atoi(const char *str)
Convert ASCII string to number in base 10.
Definition: mathio.c:281
MEMSPACE long aton(char *str, int base)
Convert ASCII string to number in a given base.
Definition: mathio.c:93
MEMSPACE long atoh(const char *p)
Convert ASCII hex string to long integer.
Definition: mathio.c:79