feat(stdlib): rand/div/qsort/bsearch

This commit is contained in:
2026-09-04 06:23:38 -04:00
parent 8bf754419f
commit 99eff26467
5 changed files with 1271 additions and 0 deletions
+166
View File
@@ -201,6 +201,172 @@ strtof(const char *restrict nptr, char **restrict endptr);
long double
strtold(const char *restrict nptr, char **restrict endptr);
/* pseudo-random numbers, search, and integer arithmetic (todo 12) */
/*
* C23 7.22.2/7.22.5/7.22.6: rand/srand, qsort/bsearch, the abs family, and
* the div family are ISO C core and present in every profile. The abs and
* div functions compute a pure function of their arguments, so they are
* declared const (the compiler folds and eliminates the calls in static
* links; GCC's own builtin declarations of abs/labs/llabs are const, and
* the attribute here matches them). rand and srand carry state, and
* qsort/bsearch call a caller-supplied comparator, so none of those four
* carries an intent attribute.
*/
/*
* The largest value rand() returns: 2^31 - 1, the largest int.
*/
#define RAND_MAX 2147483647
/*
* Quotient/remainder pair from div/ldiv/lldiv: quot is the algebraic
* quotient truncated toward zero, rem the remainder of the same sign as
* the dividend, and quot*denom + rem == num.
*/
typedef struct
{
int quot;
int rem;
} div_t;
typedef struct
{
long quot;
long rem;
} ldiv_t;
typedef struct
{
long long quot;
long long rem;
} lldiv_t;
/*
* Absolute value of n. The most-negative value returns itself: the
* negation happens in the unsigned type, which wraps, so the result is
* never undefined behavior and errno is never set.
*/
__attribute__((const)) int
abs(int n);
__attribute__((const)) long
labs(long n);
__attribute__((const)) long long
llabs(long long n);
/*
* Quotient and remainder of num/denom, truncated toward zero.
*/
__attribute__((const)) div_t
div(int num, int denom);
__attribute__((const)) ldiv_t
ldiv(long num, long denom);
__attribute__((const)) lldiv_t
lldiv(long long num, long long denom);
/*
* Pseudo-random integer in [0, RAND_MAX], deterministic for a given
* srand seed.
*/
int
rand(void);
/*
* Seed the rand() sequence with seed.
*/
void
srand(unsigned int seed);
/*
* Sort the array of nmemb elements of size bytes at base into ascending
* order according to compar (C23 7.22.5.2). compar receives two pointers
* to distinct elements and returns negative/zero/positive. The sort is
* not stable.
*/
void
qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
/*
* Binary-search the array of nmemb sorted elements of size bytes at base
* for *key, calling compar(key, element) (C23 7.22.5.1). Returns a
* pointer to the matching element, or NULL when there is none.
*/
void *
bsearch(const void *key, const void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
#if VLIBC_LEVEL_GE(2)
/* Level 2 (muslmimic): XSI and obsolescent extensions. */
/*
* Reentrant rand: the state lives in the caller's *seedp, which is
* updated on every call, so the sequence is independent of rand()'s own
* global state.
*/
int
rand_r(unsigned int *seedp);
/*
* random/srandom family (XSI): a 31-bit pseudo-random sequence in
* [0, 2^31). initstate installs state (size bytes, at least
* sizeof(long)) as the current state buffer, seeds it, and returns the
* previous buffer; setstate installs the buffer and returns the previous
* one. srandom reseeds the current buffer.
*/
long
random(void);
void
srandom(unsigned int seed);
char *
initstate(unsigned int seed, char *state, size_t size);
char *
setstate(char *state);
/*
* drand48 family (XSI): a 48-bit linear congruential sequence carried in
* three unsigned shorts, least-significant first. drand48/erand48 return
* the current value divided by 2^48 as a double in [0, 1); lrand48/
* nrand48 return the high 31 bits; mrand48/jrand48 the high 32 bits
* sign-extended. The erand48/nrand48/jrand48 forms step the caller's
* xsubi in place; srand48/seed48/lcong48 manage the shared state and the
* multiplier/addend.
*/
double
drand48(void);
double
erand48(unsigned short xsubi[3]);
long
lrand48(void);
long
nrand48(unsigned short xsubi[3]);
long
mrand48(void);
long
jrand48(unsigned short xsubi[3]);
void
srand48(long seedval);
unsigned short *
seed48(unsigned short seed16v[3]);
void
lcong48(unsigned short param[7]);
#endif /* VLIBC_LEVEL_GE(2) */
#ifdef __cplusplus
}
#endif