From 2630c7cb4e4c17ae4f570963d4a43137fe575b07 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 19 Mar 2013 08:59:26 +0100 Subject: [PATCH] Add div(3), ldiv(3) and lldiv(3). --- libc/Makefile | 3 +++ libc/div.cpp | 33 +++++++++++++++++++++++++++++++++ libc/include/stdlib.h | 25 ++++++++++++++++++++----- libc/ldiv.cpp | 33 +++++++++++++++++++++++++++++++++ libc/lldiv.cpp | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 libc/div.cpp create mode 100644 libc/ldiv.cpp create mode 100644 libc/lldiv.cpp diff --git a/libc/Makefile b/libc/Makefile index 42c62ebe..e02c8ba1 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -30,6 +30,7 @@ c++.o \ ctime.o \ ctype.o \ dir.o \ +div.o \ errno.o \ fabs.o \ fbufsize.o \ @@ -69,6 +70,8 @@ gmtime.o \ gmtime_r.o \ heap.o \ integer.o \ +ldiv.o \ +lldiv.o \ localtime.o \ localtime_r.o \ mbrtowc.o \ diff --git a/libc/div.cpp b/libc/div.cpp new file mode 100644 index 00000000..e5dc79c8 --- /dev/null +++ b/libc/div.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013 + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + div.cpp + Compute quotient and remainder of integer division. + +*******************************************************************************/ + +#include + +extern "C" div_t div(int numer, int denom) +{ + div_t ret; + ret.quot = numer / denom; + ret.rem = numer % denom; + return ret; +} diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 01dc6eb5..91003106 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -39,8 +39,23 @@ __BEGIN_DECLS /* TODO: This is just a value. It's not a compile time constant! */ #define MB_CUR_MAX 16 -/* TODO: div_t, ldiv_t, and lldiv_t is missing here */ -typedef int div_t, ldiv_t, lldiv_t; +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; @include(NULL.h) @include(size_t.h) @@ -54,11 +69,14 @@ long atol(const char*); long long atoll(const char*); void* bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*)); void* calloc(size_t, size_t); +div_t div(int, int); void exit(int) __attribute__ ((noreturn)); void _Exit(int status) __attribute__ ((noreturn)); void free(void*); long labs(long); +ldiv_t ldiv(long, long); long long llabs(long long); +lldiv_t lldiv(long long, long long); void* malloc(size_t); int mbtowc(wchar_t *restrict, const char* restrict, size_t); #if !defined(_SORTIX_SOURCE) @@ -99,7 +117,6 @@ int clearenv(void); #if defined(__SORTIX_SHOW_UNIMPLEMENTED) long a64l(const char* s); double atof(const char* value); -div_t div(int, int); double drand48(void); double erand48(unsigned short [3]); int getsubopt(char**, char* const *, char**); @@ -108,8 +125,6 @@ char* initstate(unsigned, char*, size_t); long jrand48(unsigned short [3]); char* l64a(long); void lcong48(unsigned short [7]); -ldiv_t ldiv(long, long); -lldiv_t lldiv(long long, long long); long lrand48(void); int mblen(const char*, size_t); size_t mbstowcs(wchar_t *restrict, const char* restrict, size_t); diff --git a/libc/ldiv.cpp b/libc/ldiv.cpp new file mode 100644 index 00000000..5fd5d544 --- /dev/null +++ b/libc/ldiv.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013 + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ldiv.cpp + Compute quotient and remainder of integer division. + +*******************************************************************************/ + +#include + +extern "C" ldiv_t ldiv(long numer, long denom) +{ + ldiv_t ret; + ret.quot = numer / denom; + ret.rem = numer % denom; + return ret; +} diff --git a/libc/lldiv.cpp b/libc/lldiv.cpp new file mode 100644 index 00000000..5567a6ba --- /dev/null +++ b/libc/lldiv.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013 + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + lldiv.cpp + Compute quotient and remainder of integer division. + +*******************************************************************************/ + +#include + +extern "C" lldiv_t lldiv(long long numer, long long denom) +{ + lldiv_t ret; + ret.quot = numer / denom; + ret.rem = numer % denom; + return ret; +}