AVR-LibC  2.3.0
Standard C library for AVR-GCC
 

AVR-LibC Manual

AVR-LibC Sources

Main Page

User Manual

Lib­rary Refe­rence

FAQ

Exam­ple Pro­jects

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