stdlib.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 EXIT_SUCCESS (0)
  62. #define EXIT_FAILURE (1)
  63. typedef unsigned long long ullong_type;
  64. typedef long long llong_type;
  65. typedef struct
  66. {
  67. int quot;
  68. int rem;
  69. } div_t;
  70. typedef struct
  71. {
  72. long quot;
  73. long rem;
  74. } ldiv_t;
  75. typedef struct
  76. {
  77. long long quot;
  78. long long rem;
  79. } lldiv_t;
  80. int abs(int x);
  81. double atof(const char *str);
  82. int atoi(const char *s);
  83. long atol(const char *s);
  84. long long atoll(const char *s);
  85. char *itoa(int num, char *str,int radix);
  86. div_t div(int num, int den);
  87. long labs(long a);
  88. ldiv_t ldiv(long num, long den);
  89. long long llabs(long long a);
  90. lldiv_t lldiv(long long num, long long den);
  91. int rand_r(unsigned int* seed);
  92. void srand(unsigned int seed);
  93. int rand(void);
  94. void* malloc(size_t size);
  95. void free(void* ptr);
  96. void* calloc(size_t num, size_t size);
  97. void* realloc(void* ptr, size_t size);
  98. void qsort (void *, size_t, size_t, int (*)(const void *, const void *));
  99. void *bsearch (const void *key, const void *base, size_t nelem, size_t elem_size, int (*cmp) (const void *, const void *));
  100. double strtod(char *str, char **ptr);
  101. long strtol(const char *nptr, char **endptr, int base);
  102. llong_type strtoll(const char *nptr, char **endptr, int base);
  103. unsigned long strtoul(const char *nptr, char **endptr, int base);
  104. ullong_type strtoull(const char *nptr, char **endptr, int base);
  105. void abort(void);
  106. void exit(int status);
  107. #endif /*MLIBC_STDLIB_H__*/