blob: 1c990c162956c18067dc408c29412756cf769616 [file] [log] [blame]
Richard Smith5ef06d12015-10-09 01:41:45 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Richard Smith5ef06d12015-10-09 01:41:45 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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 Smith5ef06d12015-10-09 01:41:45 +00007//
8//===----------------------------------------------------------------------===//
9
Louis Dionne463596e2022-08-08 14:06:40 -040010#ifndef _LIBCPP_STDLIB_H
Richard Smith5ef06d12015-10-09 01:41:45 +000011#define _LIBCPP_STDLIB_H
12
13/*
14 stdlib.h synopsis
15
16Macros:
17
18 EXIT_FAILURE
19 EXIT_SUCCESS
20 MB_CUR_MAX
21 NULL
22 RAND_MAX
23
24Types:
25
26 size_t
27 div_t
28 ldiv_t
29 lldiv_t // C99
30
31double atof (const char* nptr);
32int atoi (const char* nptr);
33long atol (const char* nptr);
34long long atoll(const char* nptr); // C99
35double strtod (const char* restrict nptr, char** restrict endptr);
36float strtof (const char* restrict nptr, char** restrict endptr); // C99
37long double strtold (const char* restrict nptr, char** restrict endptr); // C99
38long strtol (const char* restrict nptr, char** restrict endptr, int base);
39long long strtoll (const char* restrict nptr, char** restrict endptr, int base); // C99
40unsigned long strtoul (const char* restrict nptr, char** restrict endptr, int base);
41unsigned long long strtoull(const char* restrict nptr, char** restrict endptr, int base); // C99
42int rand(void);
43void srand(unsigned int seed);
44void* calloc(size_t nmemb, size_t size);
45void free(void* ptr);
46void* malloc(size_t size);
47void* realloc(void* ptr, size_t size);
48void abort(void);
49int atexit(void (*func)(void));
50void exit(int status);
51void _Exit(int status);
52char* getenv(const char* name);
53int system(const char* string);
54void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
55 int (*compar)(const void *, const void *));
56void qsort(void* base, size_t nmemb, size_t size,
57 int (*compar)(const void *, const void *));
58int abs( int j);
59long abs( long j);
60long long abs(long long j); // C++0X
61long labs( long j);
62long long llabs(long long j); // C99
63div_t div( int numer, int denom);
64ldiv_t div( long numer, long denom);
65lldiv_t div(long long numer, long long denom); // C++0X
66ldiv_t ldiv( long numer, long denom);
67lldiv_t lldiv(long long numer, long long denom); // C99
68int mblen(const char* s, size_t n);
69int mbtowc(wchar_t* restrict pwc, const char* restrict s, size_t n);
70int wctomb(char* s, wchar_t wchar);
71size_t mbstowcs(wchar_t* restrict pwcs, const char* restrict s, size_t n);
72size_t wcstombs(char* restrict s, const wchar_t* restrict pwcs, size_t n);
73int at_quick_exit(void (*func)(void)) // C++11
74void quick_exit(int status); // C++11
75void *aligned_alloc(size_t alignment, size_t size); // C11
76
77*/
78
79#include <__config>
80
81#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050082# pragma GCC system_header
Richard Smith5ef06d12015-10-09 01:41:45 +000083#endif
84
Louis Dionnebe1aa9e2022-11-15 17:08:01 -050085# if __has_include_next(<stdlib.h>)
86# include_next <stdlib.h>
87# endif
Richard Smith5ef06d12015-10-09 01:41:45 +000088
89#ifdef __cplusplus
Eric Fiselier6c9e1a72020-02-15 18:55:07 -050090extern "C++" {
91// abs
92
Louis Dionne787b52c2021-08-24 10:30:39 -040093#ifdef abs
94# undef abs
95#endif
96#ifdef labs
97# undef labs
98#endif
99#ifdef llabs
100# undef llabs
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500101#endif
102
103// MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
jasonliudadd6962021-04-08 21:48:42 +0000104#if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500105inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
106 return __builtin_labs(__x);
107}
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500108inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {
109 return __builtin_llabs(__x);
110}
jasonliudadd6962021-04-08 21:48:42 +0000111#endif // !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500112
jasonliudadd6962021-04-08 21:48:42 +0000113#if !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500114inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {
115 return __builtin_fabsf(__lcpp_x); // Use builtins to prevent needing math.h
116}
117
118inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
119 return __builtin_fabs(__lcpp_x);
120}
121
122inline _LIBCPP_INLINE_VISIBILITY long double
123abs(long double __lcpp_x) _NOEXCEPT {
124 return __builtin_fabsl(__lcpp_x);
125}
jasonliudadd6962021-04-08 21:48:42 +0000126#endif // !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500127
128// div
129
Louis Dionne787b52c2021-08-24 10:30:39 -0400130#ifdef div
131# undef div
132#endif
133#ifdef ldiv
134# undef ldiv
135#endif
136#ifdef lldiv
137# undef lldiv
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500138#endif
139
140// MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
jasonliudadd6962021-04-08 21:48:42 +0000141#if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500142inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT {
143 return ::ldiv(__x, __y);
144}
Louis Dionne787b52c2021-08-24 10:30:39 -0400145#if !(defined(__FreeBSD__) && !defined(__LONG_LONG_SUPPORTED))
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500146inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x,
147 long long __y) _NOEXCEPT {
148 return ::lldiv(__x, __y);
149}
Louis Dionne787b52c2021-08-24 10:30:39 -0400150#endif
jasonliudadd6962021-04-08 21:48:42 +0000151#endif // _LIBCPP_MSVCRT / __sun__
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500152} // extern "C++"
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400153#endif // __cplusplus
Richard Smith5ef06d12015-10-09 01:41:45 +0000154
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400155#endif // _LIBCPP_STDLIB_H