diff options
Diffstat (limited to '')
-rw-r--r-- | include/bits/types.h | 95 | ||||
-rw-r--r-- | include/inttypes.h | 45 | ||||
-rw-r--r-- | include/stdlib.h | 41 | ||||
-rw-r--r-- | src/inttypes/div.c | 38 | ||||
-rw-r--r-- | src/stdlib/div.c | 68 |
5 files changed, 284 insertions, 3 deletions
diff --git a/include/bits/types.h b/include/bits/types.h index 41967f2..5d99e4f 100644 --- a/include/bits/types.h +++ b/include/bits/types.h @@ -25,6 +25,13 @@ #include <bits/intconf.h> +/* Ensure that dependencies for type are defined. */ +#if defined(__NEED_imaxdiv_t) && !defined(__NEED_intmax_t) +# define __NEED_intmax_t +#endif + + + /** * Returns the maximum value of an unsigned integer type, * in that type. @@ -267,3 +274,91 @@ typedef int sig_atomic_t; # define __SIG_ATOMIC_BIT __INT_BIT #endif + +/** + * A structure than holds both the quotient and + * the remainer in an integer division, of + * `int` type. + */ +#if defined(__NEED_div_t) && !defined(__DEFINED_div_t) +# define __DEFINED_div_t +typedef struct { + /** + * Quotient. + */ + int quot; + + /** + * Remainder. + */ + int rem; + +} div_t; +#endif + + +/** + * A structure than holds both the quotient and + * the remainer in an integer division, of + * `long int` type. + */ +#if defined(__NEED_ldiv_t) && !defined(__DEFINED_ldiv_t) +# define __DEFINED_ldiv_t +typedef struct { + /** + * Quotient. + */ + long int quot; + + /** + * Remainder. + */ + long int rem; + +} ldiv_t; +#endif + + +/** + * A structure than holds both the quotient and + * the remainer in an integer division, of + * `long long int` type. + */ +#if defined(__NEED_lldiv_t) && !defined(__DEFINED_lldiv_t) +# define __DEFINED_lldiv_t +typedef struct { + /** + * Quotient. + */ + long long int quot; + + /** + * Remainder. + */ + long long int rem; + +} lldiv_t; +#endif + + +/** + * A structure than holds both the quotient and + * the remainer in an integer division, of + * `intmax_t` type. + */ +#if defined(__NEED_imaxdiv_t) && !defined(__DEFINED_imaxdiv_t) +# define __DEFINED_imaxdiv_t +typedef struct { + /** + * Quotient. + */ + intmax_t quot; + + /** + * Remainder. + */ + intmax_t rem; + +} imaxdiv_t; +#endif + diff --git a/include/inttypes.h b/include/inttypes.h new file mode 100644 index 0000000..dac5e27 --- /dev/null +++ b/include/inttypes.h @@ -0,0 +1,45 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef _STDLIB_H +#define _STDLIB_H +#include <slibc/version.h> +#include <slibc/features.h> + + + +#define __NEED_intmax_t +#define __NEED_imaxdiv_t + +#include <bits/types.h> + + +/** + * Perform an integer division and return + * both the quotient and the remainder. + * + * @param numerator The numerator. + * @param denominator The denominator. + * @return The quotient in `.quot`, and + * the remainder in `.rem`. + */ +imaxdiv_t imaxdiv(intmax_t, intmax_t); + + + +#endif + diff --git a/include/stdlib.h b/include/stdlib.h index f24fbf3..fd78d27 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -27,9 +27,9 @@ #define __NEED_size_t #define __NEED_wchar_t -#define __NEED_div_t /* TODO not defined */ -#define __NEED_ldiv_t /* TODO not defined */ -#define __NEED_lldiv_t /* TODO not defined */ +#define __NEED_div_t +#define __NEED_ldiv_t +#define __NEED_lldiv_t #include <bits/types.h> @@ -56,5 +56,40 @@ +/** + * Perform an integer division and return + * both the quotient and the remainder. + * + * @param numerator The numerator. + * @param denominator The denominator. + * @return The quotient in `.quot`, and + * the remainder in `.rem`. + */ +div_t div(int, int); + +/** + * Perform an integer division and return + * both the quotient and the remainder. + * + * @param numerator The numerator. + * @param denominator The denominator. + * @return The quotient in `.quot`, and + * the remainder in `.rem`. + */ +ldiv_t ldiv(long, long); + +/** + * Perform an integer division and return + * both the quotient and the remainder. + * + * @param numerator The numerator. + * @param denominator The denominator. + * @return The quotient in `.quot`, and + * the remainder in `.rem`. + */ +lldiv_t lldiv(long long, long long); + + + #endif diff --git a/src/inttypes/div.c b/src/inttypes/div.c new file mode 100644 index 0000000..e0330ec --- /dev/null +++ b/src/inttypes/div.c @@ -0,0 +1,38 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <inttypes.h> + + +# pragma GCC diagnostic ignored "-Waggregate-return" + + + +/** + * Perform an integer division and return + * both the quotient and the remainder. + * + * @param numerator The numerator. + * @param denominator The denominator. + * @return The quotient in `.quot`, and + * the remainder in `.rem`. + */ +imaxdiv_t imaxdiv(intmax_t numerator, intmax_t denominator) +{ + return (imaxdiv_t){ numerator / denominator, numerator % denominator }; +} + diff --git a/src/stdlib/div.c b/src/stdlib/div.c new file mode 100644 index 0000000..fb32d53 --- /dev/null +++ b/src/stdlib/div.c @@ -0,0 +1,68 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdlib.h> + + +# pragma GCC diagnostic ignored "-Waggregate-return" + + + +/** + * Perform an integer division and return + * both the quotient and the remainder. + * + * @param numerator The numerator. + * @param denominator The denominator. + * @return The quotient in `.quot`, and + * the remainder in `.rem`. + */ +div_t div(int numerator, int denominator) +{ + return (div_t){ numerator / denominator, numerator % denominator }; +} + + +/** + * Perform an integer division and return + * both the quotient and the remainder. + * + * @param numerator The numerator. + * @param denominator The denominator. + * @return The quotient in `.quot`, and + * the remainder in `.rem`. + */ +ldiv_t ldiv(long numerator, long denominator) +{ + return (ldiv_t){ numerator / denominator, numerator % denominator }; +} + + +/** + * Perform an integer division and return + * both the quotient and the remainder. + * + * @param numerator The numerator. + * @param denominator The denominator. + * @return The quotient in `.quot`, and + * the remainder in `.rem`. + */ +lldiv_t lldiv(long long numerator, long long denominator) +{ + return (lldiv_t){ numerator / denominator, numerator % denominator }; +} + |