stdlib.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) mlibc & plct lab
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021/02/17 Bernard first version
  9. */
  10. #ifndef MLIBC_STDLIB_H__
  11. #define MLIBC_STDLIB_H__
  12. #include <stddef.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #ifndef a_ctz_32
  17. #define a_ctz_32 a_ctz_32
  18. static inline int a_ctz_32(uint32_t x)
  19. {
  20. #ifdef a_clz_32
  21. return 31-a_clz_32(x&-x);
  22. #else
  23. static const char debruijn32[32] = {
  24. 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13,
  25. 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14
  26. };
  27. return debruijn32[(x&-x)*0x076be629 >> 27];
  28. #endif
  29. }
  30. #endif
  31. #ifndef a_ctz_64
  32. #define a_ctz_64 a_ctz_64
  33. static inline int a_ctz_64(uint64_t x)
  34. {
  35. static const char debruijn64[64] = {
  36. 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
  37. 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
  38. 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
  39. 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
  40. };
  41. if (sizeof(long) < 8) {
  42. uint32_t y = x;
  43. if (!y) {
  44. y = x>>32;
  45. return 32 + a_ctz_32(y);
  46. }
  47. return a_ctz_32(y);
  48. }
  49. return debruijn64[(x&-x)*0x022fdd63cc95386dull >> 58];
  50. }
  51. #endif
  52. static inline int a_ctz_l(unsigned long x)
  53. {
  54. return (sizeof(long) < 8) ? a_ctz_32(x) : a_ctz_64(x);
  55. }
  56. #define _IMPLEMENT_ABS(type, x) \
  57. { \
  58. type mask = x >> (sizeof(type) * CHAR_BIT - 1); \
  59. return (x + mask) ^ mask; \
  60. }
  61. #define WEXITSTATUS(s) (((s) & 0xff00) >> 8)
  62. #define WTERMSIG(s) ((s) & 0x7f)
  63. #define WSTOPSIG(s) WEXITSTATUS(s)
  64. #define WIFEXITED(s) (!WTERMSIG(s))
  65. #define WIFSTOPPED(s) ((short)((((s)&0xffff)*0x10001U)>>8) > 0x7f00)
  66. #define WIFSIGNALED(s) (((s)&0xffff)-1U < 0xffu)
  67. #define WCOREDUMP(s) ((s) & 0x80)
  68. #define WIFCONTINUED(s) ((s) == 0xffff)
  69. #define EXIT_SUCCESS (0)
  70. #define EXIT_FAILURE (1)
  71. typedef unsigned long long ullong_type;
  72. typedef long long llong_type;
  73. typedef struct
  74. {
  75. int quot;
  76. int rem;
  77. } div_t;
  78. typedef struct
  79. {
  80. long quot;
  81. long rem;
  82. } ldiv_t;
  83. typedef struct
  84. {
  85. long long quot;
  86. long long rem;
  87. } lldiv_t;
  88. int abs(int x);
  89. double atof(const char *str);
  90. int atoi(const char *s);
  91. long atol(const char *s);
  92. long long atoll(const char *s);
  93. char *itoa(int num, char *str,int radix);
  94. div_t div(int num, int den);
  95. long labs(long a);
  96. ldiv_t ldiv(long num, long den);
  97. long long llabs(long long a);
  98. lldiv_t lldiv(long long num, long long den);
  99. int rand_r(unsigned int* seed);
  100. void srand(unsigned int seed);
  101. int rand(void);
  102. void* malloc(size_t size);
  103. void free(void* ptr);
  104. void* calloc(size_t num, size_t size);
  105. void* realloc(void* ptr, size_t size);
  106. void qsort (void *, size_t, size_t, int (*)(const void *, const void *));
  107. void *bsearch (const void *key, const void *base, size_t nelem, size_t elem_size, int (*cmp) (const void *, const void *));
  108. double strtod(char *str, char **ptr);
  109. long strtol(const char *nptr, char **endptr, int base);
  110. llong_type strtoll(const char *nptr, char **endptr, int base);
  111. unsigned long strtoul(const char *nptr, char **endptr, int base);
  112. ullong_type strtoull(const char *nptr, char **endptr, int base);
  113. void abort(void);
  114. void exit(int status);
  115. #endif /*MLIBC_STDLIB_H__*/