Richard Smith | 5ef06d1 | 2015-10-09 01:41:45 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- stdlib.h ---------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Richard Smith | 5ef06d1 | 2015-10-09 01:41:45 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 10 | #if defined(__need_malloc_and_calloc) |
Richard Smith | 5ef06d1 | 2015-10-09 01:41:45 +0000 | [diff] [blame] | 11 | |
| 12 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 13 | #pragma GCC system_header |
| 14 | #endif |
| 15 | |
| 16 | #include_next <stdlib.h> |
| 17 | |
| 18 | #elif !defined(_LIBCPP_STDLIB_H) |
| 19 | #define _LIBCPP_STDLIB_H |
| 20 | |
| 21 | /* |
| 22 | stdlib.h synopsis |
| 23 | |
| 24 | Macros: |
| 25 | |
| 26 | EXIT_FAILURE |
| 27 | EXIT_SUCCESS |
| 28 | MB_CUR_MAX |
| 29 | NULL |
| 30 | RAND_MAX |
| 31 | |
| 32 | Types: |
| 33 | |
| 34 | size_t |
| 35 | div_t |
| 36 | ldiv_t |
| 37 | lldiv_t // C99 |
| 38 | |
| 39 | double atof (const char* nptr); |
| 40 | int atoi (const char* nptr); |
| 41 | long atol (const char* nptr); |
| 42 | long long atoll(const char* nptr); // C99 |
| 43 | double strtod (const char* restrict nptr, char** restrict endptr); |
| 44 | float strtof (const char* restrict nptr, char** restrict endptr); // C99 |
| 45 | long double strtold (const char* restrict nptr, char** restrict endptr); // C99 |
| 46 | long strtol (const char* restrict nptr, char** restrict endptr, int base); |
| 47 | long long strtoll (const char* restrict nptr, char** restrict endptr, int base); // C99 |
| 48 | unsigned long strtoul (const char* restrict nptr, char** restrict endptr, int base); |
| 49 | unsigned long long strtoull(const char* restrict nptr, char** restrict endptr, int base); // C99 |
| 50 | int rand(void); |
| 51 | void srand(unsigned int seed); |
| 52 | void* calloc(size_t nmemb, size_t size); |
| 53 | void free(void* ptr); |
| 54 | void* malloc(size_t size); |
| 55 | void* realloc(void* ptr, size_t size); |
| 56 | void abort(void); |
| 57 | int atexit(void (*func)(void)); |
| 58 | void exit(int status); |
| 59 | void _Exit(int status); |
| 60 | char* getenv(const char* name); |
| 61 | int system(const char* string); |
| 62 | void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, |
| 63 | int (*compar)(const void *, const void *)); |
| 64 | void qsort(void* base, size_t nmemb, size_t size, |
| 65 | int (*compar)(const void *, const void *)); |
| 66 | int abs( int j); |
| 67 | long abs( long j); |
| 68 | long long abs(long long j); // C++0X |
| 69 | long labs( long j); |
| 70 | long long llabs(long long j); // C99 |
| 71 | div_t div( int numer, int denom); |
| 72 | ldiv_t div( long numer, long denom); |
| 73 | lldiv_t div(long long numer, long long denom); // C++0X |
| 74 | ldiv_t ldiv( long numer, long denom); |
| 75 | lldiv_t lldiv(long long numer, long long denom); // C99 |
| 76 | int mblen(const char* s, size_t n); |
| 77 | int mbtowc(wchar_t* restrict pwc, const char* restrict s, size_t n); |
| 78 | int wctomb(char* s, wchar_t wchar); |
| 79 | size_t mbstowcs(wchar_t* restrict pwcs, const char* restrict s, size_t n); |
| 80 | size_t wcstombs(char* restrict s, const wchar_t* restrict pwcs, size_t n); |
| 81 | int at_quick_exit(void (*func)(void)) // C++11 |
| 82 | void quick_exit(int status); // C++11 |
| 83 | void *aligned_alloc(size_t alignment, size_t size); // C11 |
| 84 | |
| 85 | */ |
| 86 | |
| 87 | #include <__config> |
| 88 | |
| 89 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 90 | #pragma GCC system_header |
| 91 | #endif |
| 92 | |
| 93 | #include_next <stdlib.h> |
| 94 | |
| 95 | #ifdef __cplusplus |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 96 | extern "C++" { |
| 97 | // abs |
| 98 | |
Louis Dionne | 787b52c | 2021-08-24 10:30:39 -0400 | [diff] [blame^] | 99 | #ifdef abs |
| 100 | # undef abs |
| 101 | #endif |
| 102 | #ifdef labs |
| 103 | # undef labs |
| 104 | #endif |
| 105 | #ifdef llabs |
| 106 | # undef llabs |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined |
jasonliu | dadd696 | 2021-04-08 21:48:42 +0000 | [diff] [blame] | 110 | #if !defined(_LIBCPP_MSVCRT) && !defined(__sun__) |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 111 | inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT { |
| 112 | return __builtin_labs(__x); |
| 113 | } |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 114 | inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT { |
| 115 | return __builtin_llabs(__x); |
| 116 | } |
jasonliu | dadd696 | 2021-04-08 21:48:42 +0000 | [diff] [blame] | 117 | #endif // !defined(_LIBCPP_MSVCRT) && !defined(__sun__) |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 118 | |
jasonliu | dadd696 | 2021-04-08 21:48:42 +0000 | [diff] [blame] | 119 | #if !defined(__sun__) |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 120 | inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT { |
| 121 | return __builtin_fabsf(__lcpp_x); // Use builtins to prevent needing math.h |
| 122 | } |
| 123 | |
| 124 | inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT { |
| 125 | return __builtin_fabs(__lcpp_x); |
| 126 | } |
| 127 | |
| 128 | inline _LIBCPP_INLINE_VISIBILITY long double |
| 129 | abs(long double __lcpp_x) _NOEXCEPT { |
| 130 | return __builtin_fabsl(__lcpp_x); |
| 131 | } |
jasonliu | dadd696 | 2021-04-08 21:48:42 +0000 | [diff] [blame] | 132 | #endif // !defined(__sun__) |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 133 | |
| 134 | // div |
| 135 | |
Louis Dionne | 787b52c | 2021-08-24 10:30:39 -0400 | [diff] [blame^] | 136 | #ifdef div |
| 137 | # undef div |
| 138 | #endif |
| 139 | #ifdef ldiv |
| 140 | # undef ldiv |
| 141 | #endif |
| 142 | #ifdef lldiv |
| 143 | # undef lldiv |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 144 | #endif |
| 145 | |
| 146 | // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined |
jasonliu | dadd696 | 2021-04-08 21:48:42 +0000 | [diff] [blame] | 147 | #if !defined(_LIBCPP_MSVCRT) && !defined(__sun__) |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 148 | inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT { |
| 149 | return ::ldiv(__x, __y); |
| 150 | } |
Louis Dionne | 787b52c | 2021-08-24 10:30:39 -0400 | [diff] [blame^] | 151 | #if !(defined(__FreeBSD__) && !defined(__LONG_LONG_SUPPORTED)) |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 152 | inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x, |
| 153 | long long __y) _NOEXCEPT { |
| 154 | return ::lldiv(__x, __y); |
| 155 | } |
Louis Dionne | 787b52c | 2021-08-24 10:30:39 -0400 | [diff] [blame^] | 156 | #endif |
jasonliu | dadd696 | 2021-04-08 21:48:42 +0000 | [diff] [blame] | 157 | #endif // _LIBCPP_MSVCRT / __sun__ |
Eric Fiselier | 6c9e1a7 | 2020-02-15 18:55:07 -0500 | [diff] [blame] | 158 | } // extern "C++" |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 159 | #endif // __cplusplus |
Richard Smith | 5ef06d1 | 2015-10-09 01:41:45 +0000 | [diff] [blame] | 160 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 161 | #endif // _LIBCPP_STDLIB_H |