AVR-LibC  2.3.0git
Standard C library for AVR-GCC
 

AVR-LibC Documen­tation

AVR-LibC Development Pages

Main Page

User Manual

Library Refe­rence

FAQ

Example Projects

File List

Index

Loading...
Searching...
No Matches
stdlib.h
Go to the documentation of this file.
1/* Copyright (c) 2002, Marek Michalkiewicz
2 Copyright (c) 2004,2007 Joerg Wunsch
3 Copyright (c) 2025 Georg-Johann Lay
4
5 Portions of documentation Copyright (c) 1990, 1991, 1993, 1994
6 The Regents of the University of California.
7
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in
18 the documentation and/or other materials provided with the
19 distribution.
20
21 * Neither the name of the copyright holders nor the names of
22 contributors may be used to endorse or promote products derived
23 from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE. */
36
37#ifndef _STDLIB_H_
38#define _STDLIB_H_ 1
39
40#ifndef __ASSEMBLER__
41
42#ifndef __DOXYGEN__
43#define __need_NULL
44#define __need_size_t
45#define __need_wchar_t
46#include <stddef.h>
47
48#ifndef __ptr_t
49#define __ptr_t void *
50#endif
51#endif /* !__DOXYGEN__ */
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/** \file */
58
59/** \defgroup avr_stdlib <stdlib.h>: General utilities
60 \code #include <stdlib.h> \endcode
61
62 This file declares some basic C macros and functions as
63 defined by the ISO standard, plus some AVR-specific extensions.
64
65 For some functions, \ref bench_libc "benchmarks" are available.
66*/
67
68/** \ingroup avr_stdlib */
69/**@{*/
70/** Result type for function div(). */
71typedef struct {
72 int quot; /**< The Quotient. */
73 int rem; /**< The Remainder. */
74} div_t;
75
76/** Result type for function ldiv(). */
77typedef struct {
78 long quot; /**< The Quotient. */
79 long rem; /**< The Remainder. */
80} ldiv_t;
81
82/** Comparision function type for qsort(), just for convenience. */
83typedef int (*__compar_fn_t)(const void *, const void *);
84
85#ifndef __DOXYGEN__
86
87#include <bits/attribs.h>
88
89#endif
90
91/** The abort() function causes abnormal program termination to occur.
92 This realization disables interrupts and jumps to _exit() function
93 with argument equal to 1. In the limited AVR environment, execution is
94 effectively halted by entering an infinite loop. */
95extern void abort(void) __ATTR_NORETURN__;
96
97#ifndef __DOXYGEN__
98static __ATTR_ALWAYS_INLINE__
99int abs (int __i)
100{
101 return __builtin_abs (__i);
102}
103#endif
104/** The abs() function computes the absolute value of the integer \c i.
105 \note The abs() and labs() functions are builtins of gcc.
106*/
107extern int abs(int __i) __ATTR_CONST__;
108
109#ifndef __DOXYGEN__
110static __ATTR_ALWAYS_INLINE__
111long labs (long __i)
112{
113 return __builtin_labs (__i);
114}
115#endif
116/** The labs() function computes the absolute value of the long integer
117 \c i.
118 \note The abs() and labs() functions are builtins of gcc.
119*/
120extern long labs(long __i) __ATTR_CONST__;
121
122/**
123 The bsearch() function searches an array of \c nmemb objects, the
124 initial member of which is pointed to by \c base, for a member
125 that matches the object pointed to by \c key. The size of each
126 member of the array is specified by \c size.
127
128 The contents of the array should be in ascending sorted order
129 according to the comparison function referenced by \c compar.
130 The \c compar routine is expected to have two arguments which
131 point to the key object and to an array member, in that order,
132 and should return an integer less than, equal to, or greater than
133 zero if the key object is found, respectively, to be less than,
134 to match, or be greater than the array member.
135
136 The bsearch() function returns a pointer to a matching member of
137 the array, or a null pointer if no match is found. If two
138 members compare as equal, which member is matched is unspecified.
139*/
140extern void *bsearch(const void *__key, const void *__base, size_t __nmemb,
141 size_t __size, int (*__compar)(const void *, const void *));
142
143/* __divmodhi4 and __divmodsi4 from libgcc.a */
144/**
145 The div() function computes the value \c num/denom and returns
146 the quotient and remainder in a structure named \c div_t that
147 contains two int members named \c quot and \c rem.
148*/
149extern div_t div(int __num, int __denom) __asm__("__divmodhi4") __ATTR_CONST__;
150/**
151 The ldiv() function computes the value \c num/denom and returns
152 the quotient and remainder in a structure named \c ldiv_t that
153 contains two long integer members named \c quot and \c rem.
154*/
155extern ldiv_t ldiv(long __num, long __denom) __asm__("__divmodsi4") __ATTR_CONST__;
156
157/**
158 The qsort() function is a modified partition-exchange sort, or
159 quicksort.
160
161 The qsort() function sorts an array of \c nmemb objects, the
162 initial member of which is pointed to by \c base. The size of
163 each object is specified by \c size. The contents of the array
164 base are sorted in ascending order according to a comparison
165 function pointed to by \c compar, which requires two arguments
166 pointing to the objects being compared.
167
168 The comparison function must return an integer less than, equal
169 to, or greater than zero if the first argument is considered to
170 be respectively less than, equal to, or greater than the second.
171*/
172extern void qsort(void *__base, size_t __nmemb, size_t __size,
173 __compar_fn_t __compar);
174
175/**
176 The strtol() function converts the string in \c nptr to a long
177 value. The conversion is done according to the given base, which
178 must be between 2 and 36 inclusive, or be the special value 0.
179
180 The string may begin with an arbitrary amount of white space (as
181 determined by isspace()) followed by a single optional \c '+' or \c '-'
182 sign. If \c base is zero or 16, the string may then include a
183 \c "0x" or \c "0X" prefix, and the number will be read in base 16;
184 otherwise, a zero base is taken as 10 (decimal) unless the next
185 character is \c '0', in which case it is taken as 8 (octal).
186
187 Similarly, prefixes \c "0b" and \c "0B" signify base 2,
188 and \c "0o" and \c "0O" signify base 8.
189
190 The remainder of the string is converted to a long value in the
191 obvious manner, stopping at the first character which is not a
192 valid digit in the given base. (In bases above 10, the letter \c 'A'
193 in either upper or lower case represents 10, \c 'B' represents 11,
194 and so forth, with \c 'Z' representing 35.)
195
196 If \c endptr is not NULL, strtol() stores the address of the first
197 invalid character in \c *endptr. If there were no digits at all,
198 however, strtol() stores the original value of \c nptr in \c
199 *endptr. (Thus, if \c *nptr is not \c '\\0' but \c **endptr is \c '\\0'
200 on return, the entire string was valid.)
201
202 The strtol() function returns the result of the conversion, unless
203 the value would underflow or overflow. If no conversion could be
204 performed, 0 is returned. If an overflow or underflow occurs, \c
205 errno is set to \ref avr_errno "ERANGE" and the function return value
206 is clamped to \c LONG_MIN or \c LONG_MAX, respectively.
207*/
208extern long strtol(const char *__nptr, char **__endptr, int __base);
209
210/**
211 The strtoll() function converts the string in \c nptr to a long long
212 value. The conversion is done according to the given base, which
213 must be between 2 and 36 inclusive, or be the special value 0.
214
215 The string may begin with an arbitrary amount of white space (as
216 determined by isspace()) followed by a single optional \c '+' or \c '-'
217 sign. If \c base is zero or 16, the string may then include a
218 \c "0x" or \c "0X" prefix, and the number will be read in base 16;
219 otherwise, a zero base is taken as 10 (decimal) unless the next
220 character is \c '0', in which case it is taken as 8 (octal).
221
222 Similarly, prefixes \c "0b" and \c "0B" signify base 2,
223 and \c "0o" and \c "0O" signify base 8.
224
225 The remainder of the string is converted to a long long value in the
226 obvious manner, stopping at the first character which is not a
227 valid digit in the given base. (In bases above 10, the letter \c 'A'
228 in either upper or lower case represents 10, \c 'B' represents 11,
229 and so forth, with \c 'Z' representing 35.)
230
231 If \c endptr is not NULL, strtoll() stores the address of the first
232 invalid character in \c *endptr. If there were no digits at all,
233 however, strtoll() stores the original value of \c nptr in \c
234 *endptr. (Thus, if \c *nptr is not \c '\\0' but \c **endptr is \c '\\0'
235 on return, the entire string was valid.)
236
237 The strtoll() function returns the result of the conversion, unless
238 the value would underflow or overflow. If no conversion could be
239 performed, 0 is returned. If an overflow or underflow occurs, \c
240 errno is set to \ref avr_errno "ERANGE" and the function return value
241 is clamped to \c LLONG_MIN or \c LLONG_MAX, respectively.
242 \since AVR-LibC v2.3
243*/
244extern long long strtoll(const char *__nptr, char **__endptr, int __base);
245
246/**
247 The strtoul() function converts the string in \c nptr to an
248 unsigned long value. The conversion is done according to the
249 given base, which must be between 2 and 36 inclusive, or be the
250 special value 0.
251
252 The string may begin with an arbitrary amount of white space (as
253 determined by isspace()) followed by a single optional \c '+' or \c '-'
254 sign. If \c base is zero or 16, the string may then include a
255 \c "0x" or \c "0X" prefix, and the number will be read in base 16;
256 otherwise, a zero base is taken as 10 (decimal) unless the next
257 character is \c '0', in which case it is taken as 8 (octal).
258
259 Similarly, prefixes \c "0b" and \c "0B" signify base 2,
260 and \c "0o" and \c "0O" signify base 8.
261
262 The remainder of the string is converted to an unsigned long value
263 in the obvious manner, stopping at the first character which is
264 not a valid digit in the given base. (In bases above 10, the
265 letter \c 'A' in either upper or lower case represents 10, \c 'B'
266 represents 11, and so forth, with \c 'Z' representing 35.)
267
268 If \c endptr is not NULL, strtoul() stores the address of the first
269 invalid character in \c *endptr. If there were no digits at all,
270 however, strtoul() stores the original value of \c nptr in \c
271 *endptr. (Thus, if \c *nptr is not \c '\\0' but \c **endptr is \c '\\0'
272 on return, the entire string was valid.)
273
274 The strtoul() function returns either the result of the conversion
275 or, if there was a leading minus sign, the negation of the result
276 of the conversion, unless the original (non-negated) value would
277 overflow; in the latter case, strtoul() returns ULONG_MAX, and \c
278 errno is set to \ref avr_errno "ERANGE". If no conversion could
279 be performed, 0 is returned.
280*/
281extern unsigned long strtoul(const char *__nptr, char **__endptr, int __base);
282
283/**
284 The strtoull() function converts the string in \c nptr to an
285 unsigned long long value. The conversion is done according to the
286 given base, which must be between 2 and 36 inclusive, or be the
287 special value 0.
288
289 The string may begin with an arbitrary amount of white space (as
290 determined by isspace()) followed by a single optional \c '+' or \c '-'
291 sign. If \c base is zero or 16, the string may then include a
292 \c "0x" or \c "0X" prefix, and the number will be read in base 16;
293 otherwise, a zero base is taken as 10 (decimal) unless the next
294 character is \c '0', in which case it is taken as 8 (octal).
295
296 Similarly, prefixes \c "0b" and \c "0B" signify base 2,
297 and \c "0o" and \c "0O" signify base 8.
298
299 The remainder of the string is converted to an unsigned long long value
300 in the obvious manner, stopping at the first character which is
301 not a valid digit in the given base. (In bases above 10, the
302 letter \c 'A' in either upper or lower case represents 10, \c 'B'
303 represents 11, and so forth, with \c 'Z' representing 35.)
304
305 If \c endptr is not NULL, strtoull() stores the address of the first
306 invalid character in \c *endptr. If there were no digits at all,
307 however, strtoull() stores the original value of \c nptr in \c
308 *endptr. (Thus, if \c *nptr is not \c '\\0' but \c **endptr is \c '\\0'
309 on return, the entire string was valid.)
310
311 The strtoull() function returns either the result of the conversion
312 or, if there was a leading minus sign, the negation of the result
313 of the conversion, unless the original (non-negated) value would
314 overflow; in the latter case, strtoull() returns ULLONG_MAX, and \c
315 errno is set to \ref avr_errno "ERANGE". If no conversion could
316 be performed, 0 is returned.
317 \since AVR-LibC v2.3
318*/
319extern unsigned long long strtoull(const char *__nptr, char **__endptr, int __base);
320
321/**
322 The atol() function converts the initial portion of the string
323 pointed to by \p s to long integer representation. In contrast to
324
325 \code strtol(s, (char **)NULL, 10); \endcode
326
327 this function does not detect overflow (\c errno is not changed and
328 the result value is not predictable), uses smaller memory (flash and
329 stack) and works more quickly.
330*/
331extern long atol(const char *__s) __ATTR_PURE__;
332
333/**
334 The atoi() function converts the initial portion of the string
335 pointed to by \p s to integer representation. In contrast to
336
337 \code (int)strtol(s, (char **)NULL, 10); \endcode
338
339 this function does not detect overflow (\c errno is not changed and
340 the result value is not predictable), uses smaller memory (flash and
341 stack) and works more quickly.
342*/
343extern int atoi(const char *__s) __ATTR_PURE__;
344
345/**
346 The exit() function terminates the application. Since there is no
347 environment to return to, \c status is ignored, and code execution
348 will eventually reach an infinite loop, thereby effectively halting
349 all code processing. Before entering the infinite loop, interrupts
350 are globally disabled.
351
352 Global destructors will be called before halting
353 execution, see the \ref sec_dot_fini ".fini" sections.
354*/
355extern void exit(int __status) __ATTR_NORETURN__;
356
357/**
358 The malloc() function allocates \c size bytes of memory.
359 If malloc() fails, a NULL pointer is returned.
360
361 Note that malloc() does \e not initialize the returned memory to
362 zero bytes.
363
364 See the chapter about \ref malloc "malloc() usage" for implementation
365 details.
366*/
367extern void *malloc(size_t __size) __ATTR_MALLOC__;
368
369/**
370 The free() function causes the allocated memory referenced by \c
371 ptr to be made available for future allocations. If \c ptr is
372 NULL, no action occurs.
373*/
374extern void free(void *__ptr);
375
376/**
377 \c malloc() \ref malloc_tunables "tunable".
378*/
379extern size_t __malloc_margin;
380
381/**
382 \c malloc() \ref malloc_tunables "tunable".
383*/
384extern char *__malloc_heap_start;
385
386/**
387 \c malloc() \ref malloc_tunables "tunable".
388*/
389extern char *__malloc_heap_end;
390
391/**
392 Allocate \c nele elements of \c size each. Identical to calling
393 \c malloc() using <tt>nele * size</tt> as argument, except the
394 allocated memory will be cleared to zero.
395*/
396extern void *calloc(size_t __nele, size_t __size) __ATTR_MALLOC__;
397
398/**
399 The realloc() function tries to change the size of the region
400 allocated at \c ptr to the new \c size value. It returns a
401 pointer to the new region. The returned pointer might be the
402 same as the old pointer, or a pointer to a completely different
403 region.
404
405 The contents of the returned region up to either the old or the new
406 size value (whatever is less) will be identical to the contents of
407 the old region, even in case a new region had to be allocated.
408
409 It is acceptable to pass \c ptr as NULL, in which case realloc()
410 will behave identical to malloc().
411
412 If the new memory cannot be allocated, realloc() returns NULL, and
413 the region at \c ptr will not be changed.
414*/
415extern void *realloc(void *__ptr, size_t __size) __ATTR_MALLOC__;
416
417extern float strtof(const char *__nptr, char **__endptr);
418
419/** \ingroup avr_stdlib
420 The strtod() function is similar to strtof(), except that the conversion
421 result is of type \c double instead of \c float.
422
423 strtod() is currently only supported when \c double is a 32-bit type. */
424extern double strtod(const char *__nptr, char **__endptr);
425
426/** \ingroup avr_stdlib
427 The strtold() function is similar to strtof(), except that the conversion
428 result is of type \c long \c double instead of \c float.
429
430 strtold() is currently only supported when \c long \c double is a
431 32-bit type. */
432extern long double strtold(const char *__nptr, char **__endptr);
433
434/**
435 \ingroup avr_stdlib
436 The atexit() function registers function \a func to be run as part of
437 the \c exit() function during \ref sec_dot_fini ".fini8".
438 atexit() calls malloc().
439 */
440extern int atexit(void (*func)(void));
441
442/** \ingroup avr_stdlib
443 \fn float atoff (const char *nptr)
444
445 The atoff() function converts the initial portion of the string pointed
446 to by \a nptr to \c float representation.
447
448 It is equivalent to calling
449 \code strtof(nptr, (char**) 0); \endcode */
450extern float atoff(const char *__nptr);
451
452/** \ingroup avr_stdlib
453 \fn double atof (const char *nptr)
454
455 The atof() function converts the initial portion of the string pointed
456 to by \a nptr to \c double representation.
457
458 It is equivalent to calling
459 \code strtod(nptr, (char**) 0); \endcode */
460extern double atof(const char *__nptr);
461
462/** \ingroup avr_stdlib
463 \fn long double atofl (const char *nptr)
464
465 The atofl() function converts the initial portion of the string pointed
466 to by \a nptr to \c long \c double representation.
467
468 It is equivalent to calling
469 \code strtold(nptr, (char**) 0); \endcode */
470extern long double atofl(const char *__nptr);
471
472/**
473 \ingroup avr_stdlib
474 Successful termination for exit(); evaluates to 0.
475*/
476#define EXIT_SUCCESS 0
477
478/**
479 \ingroup avr_stdlib
480 Unsuccessful termination for exit(); evaluates to a non-zero value.
481*/
482#define EXIT_FAILURE 1
483
484/** Highest number that can be generated by rand(). */
485#define RAND_MAX 0x7FFF
486
487/**
488 The rand() function computes a sequence of pseudo-random integers in the
489 range of 0 to \c RAND_MAX (as defined by the header file <stdlib.h>).
490
491 The srand() function sets its argument \c seed as the seed for a new
492 sequence of pseudo-random numbers to be returned by rand(). These
493 sequences are repeatable by calling srand() with the same seed value.
494
495 If no seed value is provided, the functions are automatically seeded with
496 a value of 1.
497
498 In compliance with the C standard, these functions operate on
499 \c int arguments. Since the underlying algorithm already uses
500 32-bit calculations, this causes a loss of precision. See
501 \c random() for an alternate set of functions that retains full
502 32-bit precision.
503*/
504extern int rand(void);
505/**
506 Pseudo-random number generator seeding; see rand().
507*/
508extern void srand(unsigned int __seed);
509
510/**
511 Variant of rand() that stores the context in the user-supplied
512 variable located at \c ctx instead of a static library variable
513 so the function becomes re-entrant.
514*/
515extern int rand_r(unsigned long *__ctx);
516/**@}*/
517
518/**@{*/
519/** \name Non-standard (i.e. non-ISO C) functions.
520 \ingroup avr_stdlib
521*/
522/**
523 \brief Convert an integer to a string.
524
525 The function itoa() converts the integer value from \c val into an
526 ASCII representation that will be stored under \c s. The caller
527 is responsible for providing sufficient storage in \c s.
528
529 \note The minimal size of the buffer \c s depends on the choice of
530 radix. For example, if the radix is 2 (binary), you need to supply a buffer
531 with a minimal length of 8 * sizeof (int) + 1 characters, i.e. one
532 character for each bit plus one for the string terminator. Using a larger
533 radix will require a smaller minimal buffer size.
534
535 \warning If the buffer is too small, you risk a buffer overflow.
536
537 Conversion is done using the \c radix as base, which may be a
538 number between 2 (binary conversion) and up to 36. If \c radix
539 is greater than 10, the next digit after \c '9' will be the letter
540 \c 'a'.
541
542 If radix is 10 and val is negative, a minus sign will be prepended.
543
544 The itoa() function returns the pointer passed as \c s.
545
546 \note Decimal conversions can be sped up by using the ktoa() function
547 from \ref avr_stdfix "<stdfix.h>" that converts fixed-point values to
548 decimal ASCII, like in <code>ktoa((accum) val, s, FXTOA_TRUNC)</code>
549 that converts \c val to a decimal ASCII representation with zero
550 fractional digits. For example, converting 1000 using itoa() takes
551 around 700 cycles whereas ktoa() does the job in less than 300 cycles.
552*/
553#ifdef __DOXYGEN__
554extern char *itoa(int val, char *s, int radix);
555#else
556extern __ATTR_ALWAYS_INLINE__ __ATTR_GNU_INLINE__
557char *itoa (int __val, char *__s, int __radix)
558{
559 if (!__builtin_constant_p (__radix)) {
560 extern char *__itoa (int, char *, int);
561 return __itoa (__val, __s, __radix);
562 } else if (__radix < 2 || __radix > 36) {
563 *__s = 0;
564 return __s;
565 } else {
566 extern char *__itoa_ncheck (int, char *, unsigned char);
567 return __itoa_ncheck (__val, __s, __radix);
568 }
569}
570#endif
571
572/**
573 \ingroup avr_stdlib
574
575 \brief Convert a long integer to a string.
576
577 The function ltoa() converts the long integer value from \c val into an
578 ASCII representation that will be stored under \c s. The caller
579 is responsible for providing sufficient storage in \c s.
580
581 \note The minimal size of the buffer \c s depends on the choice of
582 radix. For example, if the radix is 2 (binary), you need to supply a buffer
583 with a minimal length of 8 * sizeof (long int) + 1 characters, i.e. one
584 character for each bit plus one for the string terminator. Using a larger
585 radix will require a smaller minimal buffer size.
586
587 \warning If the buffer is too small, you risk a buffer overflow.
588
589 Conversion is done using the \c radix as base, which may be a
590 number between 2 (binary conversion) and up to 36. If \c radix
591 is greater than 10, the next digit after \c '9' will be the letter
592 \c 'a'.
593
594 If radix is 10 and val is negative, a minus sign will be prepended.
595
596 The ltoa() function returns the pointer passed as \c s.
597*/
598#ifdef __DOXYGEN__
599extern char *ltoa(long val, char *s, int radix);
600#else
601extern __ATTR_ALWAYS_INLINE__ __ATTR_GNU_INLINE__
602char *ltoa (long __val, char *__s, int __radix)
603{
604 if (!__builtin_constant_p (__radix))
605 {
606 extern char *__ltoa (long, char *, int);
607 return __ltoa (__val, __s, __radix);
608 }
609 else if (__radix < 2 || __radix > 36)
610 {
611 *__s = 0;
612 return __s;
613 }
614 else
615 {
616 extern char *__ltoa_ncheck (long, char *, unsigned char);
617 return __ltoa_ncheck (__val, __s, __radix);
618 }
619}
620#endif
621
622/**
623 \ingroup avr_stdlib
624
625 \brief Convert an unsigned integer to a string.
626
627 The function utoa() converts the unsigned integer value from \c val into an
628 ASCII representation that will be stored under \c s. The caller
629 is responsible for providing sufficient storage in \c s.
630
631 \note The minimal size of the buffer \c s depends on the choice of
632 radix. For example, if the radix is 2 (binary), you need to supply a buffer
633 with a minimal length of 8 * sizeof (unsigned int) + 1 characters, i.e. one
634 character for each bit plus one for the string terminator. Using a larger
635 radix will require a smaller minimal buffer size.
636
637 \warning If the buffer is too small, you risk a buffer overflow.
638
639 Conversion is done using the \c radix as base, which may be a
640 number between 2 (binary conversion) and up to 36. If \c radix
641 is greater than 10, the next digit after \c '9' will be the letter
642 \c 'a'.
643
644 The utoa() function returns the pointer passed as \c s.
645
646 \note Decimal conversions can be sped up by using the uktoa() function from
647 \ref avr_stdfix "<stdfix.h>" that converts fixed-point values to decimal
648 ASCII, like in <code>uktoa((unsigned accum) val, s, FXTOA_TRUNC)</code>
649 that converts \c val to a decimal ASCII representation with zero
650 fractional digits. For example, converting 1000 using utoa() takes
651 around 700 cycles whereas uktoa() does the job in less than 300 cycles.
652*/
653#ifdef __DOXYGEN__
654extern char *utoa(unsigned int val, char *s, int radix);
655#else
656extern __ATTR_ALWAYS_INLINE__ __ATTR_GNU_INLINE__
657char *utoa (unsigned int __val, char *__s, int __radix)
658{
659 if (!__builtin_constant_p (__radix))
660 {
661 extern char *__utoa (unsigned int, char *, int);
662 return __utoa (__val, __s, __radix);
663 }
664 else if (__radix < 2 || __radix > 36)
665 {
666 *__s = 0;
667 return __s;
668 }
669 else
670 {
671 extern char *__utoa_ncheck (unsigned int, char *, unsigned char);
672 return __utoa_ncheck (__val, __s, __radix);
673 }
674}
675#endif
676
677/**
678 \ingroup avr_stdlib
679 \brief Convert an unsigned long integer to a string.
680
681 The function ultoa() converts the unsigned long integer value from
682 \c val into an ASCII representation that will be stored under \c s.
683 The caller is responsible for providing sufficient storage in \c s.
684
685 \note The minimal size of the buffer \c s depends on the choice of
686 radix. For example, if the radix is 2 (binary), you need to supply a buffer
687 with a minimal length of 8 * sizeof (unsigned long int) + 1 characters,
688 i.e. one character for each bit plus one for the string terminator. Using a
689 larger radix will require a smaller minimal buffer size.
690
691 \warning If the buffer is too small, you risk a buffer overflow.
692
693 Conversion is done using the \c radix as base, which may be a
694 number between 2 (binary conversion) and up to 36. If \c radix
695 is greater than 10, the next digit after \c '9' will be the letter
696 \c 'a'.
697
698 The ultoa() function returns the pointer passed as \c s.
699*/
700#ifdef __DOXYGEN__
701extern char *ultoa(unsigned long val, char *s, int radix);
702#else
703extern __ATTR_ALWAYS_INLINE__ __ATTR_GNU_INLINE__
704char *ultoa (unsigned long __val, char *__s, int __radix)
705{
706 if (!__builtin_constant_p (__radix)) {
707 extern char *__ultoa (unsigned long, char *, int);
708 return __ultoa (__val, __s, __radix);
709 } else if (__radix < 2 || __radix > 36) {
710 *__s = 0;
711 return __s;
712 } else {
713 extern char *__ultoa_ncheck (unsigned long, char *, unsigned char);
714 return __ultoa_ncheck (__val, __s, __radix);
715 }
716}
717#endif
718
719
720/** \ingroup avr_stdlib
721 \brief Convert an unsigned 64-bit integer to a string.
722
723 The function ulltoa() writes the ASCII representation of
724 the unsigned 64-bit integer \c val to a string starting at \c s.
725
726 A very rough estimation of the execution time is
727
728 &nbsp;&nbsp;&nbsp;&nbsp;<i>Cycles</i> &asymp; 950 + 23&middot;<i>N</i> + 8.3&middot;<i>N</i><sup>2</sup>&middot;log(<i>radix</i>) &plusmn; 400
729
730 where <i>N</i> denotes the number of digits in the result,
731 and log stands for the Natural Logarithm.
732 This means a decimal conversion can take up to 9000 cycles,
733 a hexadecimal conversions can take up to 7800 cycles,
734 and a binary conversion can take more than 27000 cycles.
735
736 \param val
737 An unsigned 64-bit integral value for which the ASCII representation
738 is computed.
739
740 \param s
741 The location to which the string representation should be stored.
742 The caller is responsible for providing sufficient storage in \c s.
743 The minimal size of the buffer \c s depends on the choice of the
744 radix. For example, if the radix is 10 (decimal), the function will
745 write at most 21 characters (including the terminating '\\0').
746
747 \param radix
748 The Conversion is done using the \c radix as base, which may be a number
749 between 2 (binary conversion) and up to 36. If \c radix is greater than 10,
750 the next digit after \c '9' will be the letter \c 'a'.
751
752 \return The ulltoa() function returns the pointer passed as \c s.
753
754 \since AVR-LibC v2.3
755*/
756#ifdef __DOXYGEN__
757extern char* ulltoa(unsigned long long val, char *s, int radix);
758#else
759extern char* ulltoa(unsigned long long, char*, int) __asm("__ulltoa");
760#endif /* Doxygen */
761
762/** \ingroup avr_stdlib
763 \brief Convert an unsigned 64-bit integer to a decimal string.
764
765 The function ulltoa_base10() writes the decimal ASCII representation of
766 the unsigned 64-bit integer \c val to a string starting at \c s.
767 The effect is the same like for <tt>ulltoa(val, s, 10)</tt>.
768
769 This function can be used for decimal ASCII conversions when
770 ulltoa() is not fast enough. It consumes no more than 3300 cycles
771 (no more than 2800 cycles with \c MUL),
772 where ulltoa() may consume up to 9000 cycles for a decimal conversion.
773
774 \param val
775 An unsigned 64-bit integral value for which the decimal ASCII
776 representation is computed.
777
778 \param s
779 The location to which the string representation should be stored.
780 The caller is responsible for providing sufficient storage in \c s.
781 The function will write at most 21 characters (including the
782 terminating '\\0').
783
784 \return The ulltoa_base10() function returns the pointer passed as \c s.
785
786 \since AVR-LibC v2.3
787*/
788#ifdef __DOXYGEN__
789extern char* ulltoa_base10(unsigned long long val, char *s);
790#else
791extern char* ulltoa_base10(unsigned long long, char*) __asm("__ulltoa_base10");
792#endif /* Doxygen */
793
794/** \ingroup avr_stdlib
795 \brief Convert a signed 64-bit integer to a string.
796
797 The function lltoa() writes the ASCII representation of
798 the signed 64-bit integer \c val to a string starting at \c s.
799 Except for decimal conversions with a negative \p val,
800 the effect is the same like with \c ulltoa().
801
802 \param val
803 A signed 64-bit integral value for which the ASCII representation
804 is computed.
805
806 \param s
807 The location to which the string representation should be stored.
808 The caller is responsible for providing sufficient storage in \c s.
809 The minimal size of the buffer \c s depends on the choice of the
810 radix. For example, if the radix is 10 (decimal), the function will
811 write at most 21 characters (including the terminating '\\0').
812
813 \param radix
814 The Conversion is done using the \c radix as base, which may be a number
815 between 2 (binary conversion) and up to 36. If \c radix is greater than 10,
816 the next digit after \c '9' will be the letter \c 'a'.
817
818 \return The lltoa() function returns the pointer passed as \c s.
819
820 \since AVR-LibC v2.3
821*/
822#ifdef __DOXYGEN__
823extern char* lltoa(long long val, char *s, int radix);
824#else
825extern char* lltoa(long long, char*, int) __asm("__lltoa");
826#endif /* Doxygen */
827
828/** \ingroup avr_stdlib
829Highest number that can be generated by random(). */
830#define RANDOM_MAX 0x7FFFFFFF
831
832/**
833 \ingroup avr_stdlib
834 The random() function computes a sequence of pseudo-random integers in the
835 range of 0 to \c RANDOM_MAX (as defined by the header file <stdlib.h>).
836
837 The srandom() function sets its argument \c seed as the seed for a new
838 sequence of pseudo-random numbers to be returned by rand(). These
839 sequences are repeatable by calling srandom() with the same seed value.
840
841 If no seed value is provided, the functions are automatically seeded with
842 a value of 1.
843*/
844extern long random(void);
845/**
846 \ingroup avr_stdlib
847 Pseudo-random number generator seeding; see random().
848*/
849extern void srandom(unsigned long __seed);
850
851/**
852 \ingroup avr_stdlib
853 Variant of random() that stores the context in the user-supplied
854 variable located at \c ctx instead of a static library variable
855 so the function becomes re-entrant.
856*/
857extern long random_r(unsigned long *__ctx);
858
859/** \ingroup avr_stdlib
860 \return Returns the square root of the 16-bit value \p radic,
861 rounded down to the next integral value.
862 */
863#ifdef __DOXYGEN__
864extern unsigned char sqrtu16_floor(unsigned int radic);
865#else
866extern unsigned char sqrtu16_floor(unsigned) __asm("__sqrthi");
867extern __ATTR_ALWAYS_INLINE__ __ATTR_GNU_INLINE__
868unsigned char sqrtu16_floor(unsigned __r)
869{
870 if (__builtin_constant_p (__r))
871 {
872 return (unsigned char) __builtin_sqrtf ((float) __r);
873 }
874 else
875 {
876 extern unsigned char __sqrthi (unsigned);
877 return __sqrthi (__r);
878 }
879}
880#endif /* Doxygen */
881
882/** \ingroup avr_stdlib
883 \return Returns the square root of the 32-bit value \p radic,
884 rounded down to the next integral value.
885 */
886#ifdef __DOXYGEN__
887extern unsigned int sqrtu32_floor(unsigned long radic);
888#else
889extern unsigned int sqrtu32_floor(unsigned long radic) __asm("__sqrtsi");
890#endif /* Doxygen */
891
892
893#endif /* __ASSEMBLER */
894/**@}*/
895
896/**@{*/
897/** \name Conversion functions for double arguments. */
898/** \ingroup avr_stdlib
899 Bit value that can be passed in \c flags to ftostre(),
900 dtostre() and ldtostre(). */
901#define DTOSTR_ALWAYS_SIGN 0x01 /* put '+' or ' ' for positives */
902/** \ingroup avr_stdlib
903 Bit value that can be passed in \c flags to ftostre(),
904 dtostre() and ldtostre(). */
905#define DTOSTR_PLUS_SIGN 0x02 /* put '+' rather than ' ' */
906/** \ingroup avr_stdlib
907 Bit value that can be passed in \c flags to ftostre(),
908 dtostre() and ldtostre(). */
909#define DTOSTR_UPPERCASE 0x04 /* put 'E' rather 'e' */
910
911#ifndef __ASSEMBLER__
912
913/**
914 \ingroup avr_stdlib
915 The ftostre() function converts the \c float value passed in \c val into
916 an ASCII representation that will be stored under \c s. The caller
917 is responsible for providing sufficient storage in \c s.
918
919 Conversion is done in the format
920 <tt>&quot;[-]d.ddde&plusmn;dd&quot;</tt> where there is
921 one digit before the decimal-point character and the number of
922 digits after it is equal to the precision \c prec; if the precision
923 is zero, no decimal-point character appears. If \c flags has the
924 #DTOSTR_UPPERCASE bit set, the letter \c 'E' (rather than \c 'e' ) will be
925 used to introduce the exponent. The exponent always contains two
926 digits; if the value is zero, the exponent is \c "00".
927
928 If \c flags has the #DTOSTR_ALWAYS_SIGN bit set, a space character
929 will be placed into the leading position for positive numbers.
930
931 If \c flags has the #DTOSTR_PLUS_SIGN bit set, a plus sign will be
932 used instead of a space character in this case.
933
934 The ftostre() function returns the pointer to the converted string \c s.
935*/
936extern char *ftostre(float __val, char *__s, unsigned char __prec,
937 unsigned char __flags);
938/**
939 \ingroup avr_stdlib
940 The dtostre() function is similar to the ftostre() function, except that
941 it converts a \c double value instead of a \c float value.
942
943 dtostre() is currently only supported when \c double is a 32-bit type. */
944extern char *dtostre(double __val, char *__s, unsigned char __prec,
945 unsigned char __flags);
946/**
947 \ingroup avr_stdlib
948 The ldtostre() function is similar to the ftostre() function, except that
949 it converts a \c long \c double value instead of a \c float value.
950
951 ldtostre() is currently only supported when \c long \c double is a
952 32-bit type. */
953extern char *ldtostre(long double __val, char *__s, unsigned char __prec,
954 unsigned char __flags);
955
956/**
957 \ingroup avr_stdlib
958 The ftostrf() function converts the \c float value passed in \c val into
959 an ASCII representation that will be stored in \c s. The caller
960 is responsible for providing sufficient storage in \c s.
961
962 Conversion is done in the format \c "[-]d.ddd". The minimum field
963 width of the output string (including the possible \c '.' and the possible
964 sign for negative values) is given in \c width, and \c prec determines
965 the number of digits after the decimal sign. \c width is signed value,
966 negative for left adjustment.
967
968 The ftostrf() function returns the pointer to the converted string \c s.
969*/
970extern char *ftostrf(float __val, signed char __width,
971 unsigned char __prec, char *__s);
972/**
973 \ingroup avr_stdlib
974 The dtostrf() function is similar to the ftostrf() function, except that
975 converts a \c double value instead of a \c float value.
976
977 ldtostre() is currently only supported when \c double is a 32-bit type. */
978extern char *dtostrf(double __val, signed char __width,
979 unsigned char __prec, char *__s);
980/**
981 \ingroup avr_stdlib
982 The ldtostrf() function is similar to the ftostrf() function, except that
983 converts a \c long \c double value instead of a \c float value.
984
985 ldtostre() is currently only supported when \c long \c double is a
986 32-bit type. */
987extern char *ldtostrf(long double __val, signed char __width,
988 unsigned char __prec, char *__s);
989
990/**@}*/
991
992#ifndef __DOXYGEN__
993/* dummy declarations for libstdc++ compatibility */
994extern int system (const char *);
995extern char *getenv (const char *);
996#endif /* __DOXYGEN__ */
997
998#ifdef __cplusplus
999}
1000#endif
1001
1002#endif /* __ASSEMBLER */
1003
1004#endif /* _STDLIB_H_ */
void exit(int __status)
char * ftostre(float __val, char *__s, unsigned char __prec, unsigned char __flags)
Definition: dtostre.c:37
char * ldtostrf(long double __val, signed char __width, unsigned char __prec, char *__s)
char * ltoa(long val, char *s, int radix)
Convert a long integer to a string.
long random(void)
Definition: random.c:79
long atol(const char *__s)
Definition: atol.c:37
unsigned long long strtoull(const char *__nptr, char **__endptr, int __base)
long double atofl(const char *__nptr)
char * ulltoa_base10(unsigned long long val, char *s)
Convert an unsigned 64-bit integer to a decimal string.
int(* __compar_fn_t)(const void *, const void *)
Definition: stdlib.h:83
char * dtostre(double __val, char *__s, unsigned char __prec, unsigned char __flags)
char * __malloc_heap_end
Definition: malloc.c:58
void srandom(unsigned long __seed)
Definition: random.c:86
long long strtoll(const char *__nptr, char **__endptr, int __base)
unsigned int sqrtu32_floor(unsigned long radic)
ldiv_t ldiv(long __num, long __denom) __asm__("__divmodsi4")
double strtod(const char *__nptr, char **__endptr)
long double strtold(const char *__nptr, char **__endptr)
void * calloc(size_t __nele, size_t __size)
Definition: calloc.c:36
int atoi(const char *__s)
Definition: atoi.c:34
double atof(const char *__nptr)
float strtof(const char *__nptr, char **__endptr)
Definition: strtod.c:120
div_t div(int __num, int __denom) __asm__("__divmodhi4")
char * dtostrf(double __val, signed char __width, unsigned char __prec, char *__s)
int atexit(void(*func)(void))
Definition: atexit.c:42
char * ldtostre(long double __val, char *__s, unsigned char __prec, unsigned char __flags)
void abort(void)
Definition: abort.c:34
char * __malloc_heap_start
Definition: malloc.c:57
char * itoa(int val, char *s, int radix)
Convert an integer to a string.
long random_r(unsigned long *__ctx)
Definition: random.c:69
char * lltoa(long long val, char *s, int radix)
Convert a signed 64-bit integer to a string.
char * ulltoa(unsigned long long val, char *s, int radix)
Convert an unsigned 64-bit integer to a string.
void srand(unsigned int __seed)
Definition: rand.c:96
unsigned char sqrtu16_floor(unsigned int radic)
int abs(int __i)
char * ftostrf(float __val, signed char __width, unsigned char __prec, char *__s)
Definition: dtostrf.c:49
char * ultoa(unsigned long val, char *s, int radix)
Convert an unsigned long integer to a string.
long labs(long __i)
int rand(void)
Definition: rand.c:89
float atoff(const char *__nptr)
char * utoa(unsigned int val, char *s, int radix)
Convert an unsigned integer to a string.
unsigned long strtoul(const char *__nptr, char **__endptr, int __base)
int rand_r(unsigned long *__ctx)
Definition: rand.c:79
void * bsearch(const void *__key, const void *__base, size_t __nmemb, size_t __size, int(*__compar)(const void *, const void *))
Definition: bsearch.c:55
long strtol(const char *__nptr, char **__endptr, int __base)
void qsort(void *__base, size_t __nmemb, size_t __size, __compar_fn_t __compar)
size_t __malloc_margin
Definition: malloc.c:56
Definition: stdlib.h:71
int quot
Definition: stdlib.h:72
int rem
Definition: stdlib.h:73
Definition: stdlib.h:77
long rem
Definition: stdlib.h:79
long quot
Definition: stdlib.h:78