stdlib: add abs, labs, llabs, div, ldiv, lldiv (TEST)

This commit is contained in:
Lephenixnoir 2021-05-18 21:25:45 +02:00
parent e8aaadf2e7
commit 73b536bbb6
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
9 changed files with 109 additions and 4 deletions

View File

@ -58,7 +58,9 @@ endif()
# Building
set(SOURCES
# assert
src/libc/assert/assert.c
# ctype
src/libc/ctype/isalnum.c
src/libc/ctype/isalpha.c
src/libc/ctype/isblank.c
@ -73,9 +75,12 @@ set(SOURCES
src/libc/ctype/isxdigit.c
src/libc/ctype/tolower.c
src/libc/ctype/toupper.c
# errno
src/libc/errno/errno.c
# locale
src/libc/locale/setlocale.c
src/libc/locale/localeconv.c
# stdio
src/libc/stdio/vsnprintf.c
src/libc/stdio/sprintf.c
src/libc/stdio/dprintf.c
@ -89,8 +94,16 @@ set(SOURCES
src/libc/stdio/internal/printf.h
src/libc/stdio/vdprintf.c
src/libc/stdio/printf.c
# stdlib
src/libc/stdlib/abs.c
src/libc/stdlib/calloc.c
src/libc/stdlib/div.c
src/libc/stdlib/labs.c
src/libc/stdlib/ldiv.c
src/libc/stdlib/llabs.c
src/libc/stdlib/lldiv.c
src/libc/stdlib/reallocarray.c
# string
src/libc/string/strchr.c
src/libc/string/strcpy.c
src/libc/string/memcpy.c

10
STATUS
View File

@ -95,7 +95,15 @@ DONE: Function/symbol/macro is defined, builds, links, and is tested
TODO (will give the full list later on)
7.20 <stdlib.h>
TODO (will give the full list later on)
7.20.1 Numeric conversion functions: TODO
7.20.2 Pseudo-random sequence generation functions: TODO
7.20.3 Memory management functions: TODO (check existing code first)
7.20.4 Communication with the environment: TODO
7.20.5 Searching and sorting utilities: TODO
7.20.6.1 abs, labs, llabs: TEST
7.20.6.2 div, ldiv, lldiv: TEST
7.20.7 Multibyte/wide character conversion functions: TODO
7.20.8 Multibyte/wide string conversion functions: TODO
7.21 <string.h>
TODO (will give the full list later on)

View File

@ -4,9 +4,8 @@
#include <stddef.h>
#include <stdint.h>
//---
// Dynamic memory management
//---
/* Dynamic memory management. */
/* Allocate SIZE bytes of memory. */
extern void *malloc(size_t size);
@ -28,4 +27,40 @@ extern void *reallocarray(void *ptr, size_t nmemb, size_t size);
/* Free a block allocated by `malloc', `realloc' or `calloc'. */
extern void free(void *ptr);
/* Integer arithmetic functions. */
extern int abs(int __j);
#define abs(j) ({ \
int __j = (j); \
(__j >= 0) ? __j : -(__j); \
})
extern long int labs(long int __j);
#define labs(j) ({ \
long int __j = (j); \
(__j >= 0) ? __j : -(__j); \
})
extern long long int llabs(long long int __j);
#define llabs(j) ({ \
long long int __j = (j); \
(__j >= 0) ? __j : -(__j); \
})
typedef struct {
int quot, rem;
} div_t;
typedef struct {
long int quot, rem;
} ldiv_t;
typedef struct {
long long int quot, rem;
} lldiv_t;
div_t div(int __num, int __denom);
ldiv_t ldiv(long int __num, long int __denom);
lldiv_t lldiv(long long int __num, long long int __denom);
#endif /*__STDLIB_H__*/

7
src/libc/stdlib/abs.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdlib.h>
#undef abs
int abs(int j)
{
return (j >= 0) ? j : -j;
}

12
src/libc/stdlib/div.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdlib.h>
div_t div(int num, int denom)
{
/*
** On SuperH, division and modulus are both very slow, so it's more
** efficient to perform only one division.
*/
int q = num / denom;
int r = num - q * denom;
return (div_t){ q, r };
}

7
src/libc/stdlib/labs.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdlib.h>
#undef labs
long int labs(long int j)
{
return (j >= 0) ? j : -j;
}

8
src/libc/stdlib/ldiv.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdlib.h>
ldiv_t ldiv(long int num, long int denom)
{
long int q = num / denom;
long int r = num - q * denom;
return (ldiv_t){ q, r };
}

7
src/libc/stdlib/llabs.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdlib.h>
#undef llabs
long long int llabs(long long int j)
{
return (j >= 0) ? j : -j;
}

8
src/libc/stdlib/lldiv.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdlib.h>
lldiv_t lldiv(long long int num, long long int denom)
{
long long int q = num / denom;
long long int r = num - q * denom;
return (lldiv_t){ q, r };
}