diff options
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/Makefile | 12 | ||||
-rw-r--r-- | stdlib/Versions | 6 | ||||
-rw-r--r-- | stdlib/inttypes.h | 5 | ||||
-rw-r--r-- | stdlib/stdlib.h | 6 | ||||
-rw-r--r-- | stdlib/tst-uabs.c | 45 | ||||
-rw-r--r-- | stdlib/tst-uimaxabs.c | 51 | ||||
-rw-r--r-- | stdlib/tst-ulabs.c | 52 | ||||
-rw-r--r-- | stdlib/tst-ullabs.c | 55 | ||||
-rw-r--r-- | stdlib/uabs.c | 28 | ||||
-rw-r--r-- | stdlib/ulabs.c | 33 | ||||
-rw-r--r-- | stdlib/ullabs.c | 33 |
11 files changed, 326 insertions, 0 deletions
diff --git a/stdlib/Makefile b/stdlib/Makefile index 513445b..1c80e49 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -211,6 +211,9 @@ routines := \ strtoull_l \ swapcontext \ system \ + uabs \ + ulabs \ + ullabs \ wcstombs \ wctomb \ xpg_basename \ @@ -363,6 +366,10 @@ tests := \ tst-swapcontext2 \ tst-thread-quick_exit \ tst-tininess \ + tst-uabs \ + tst-uimaxabs \ + tst-ulabs \ + tst-ullabs \ tst-unsetenv1 \ tst-width \ tst-width-stdint \ @@ -412,6 +419,11 @@ CFLAGS-tst-abs.c += -fno-builtin CFLAGS-tst-labs.c += -fno-builtin CFLAGS-tst-llabs.c += -fno-builtin +CFLAGS-tst-uabs.c += -fno-builtin +CFLAGS-tst-uimaxabs.c += -fno-builtin +CFLAGS-tst-ulabs.c += -fno-builtin +CFLAGS-tst-ullabs.c += -fno-builtin + CFLAGS-tst-stdbit-Wconversion.c += -Wconversion -Werror CFLAGS-tst-stdc_trailing_zeros.c += -fno-builtin CFLAGS-tst-stdc_trailing_ones.c += -fno-builtin diff --git a/stdlib/Versions b/stdlib/Versions index ea2265b..6d02400 100644 --- a/stdlib/Versions +++ b/stdlib/Versions @@ -223,6 +223,12 @@ libc { stdc_bit_ceil_ul; stdc_bit_ceil_ull; } + GLIBC_2.42 { + uabs; + uimaxabs; + ulabs; + ullabs; + } GLIBC_PRIVATE { # functions which have an additional interface since they are # are cancelable. diff --git a/stdlib/inttypes.h b/stdlib/inttypes.h index 95324f0..9726abf 100644 --- a/stdlib/inttypes.h +++ b/stdlib/inttypes.h @@ -350,6 +350,11 @@ typedef struct /* Compute absolute value of N. */ extern intmax_t imaxabs (intmax_t __n) __THROW __attribute__ ((__const__)); + +#if __GLIBC_USE (ISOC2Y) +extern uintmax_t uimaxabs (intmax_t __n) __THROW __attribute__ ((__const__)); +#endif + /* Return the `imaxdiv_t' representation of the value of NUMER over DENOM. */ extern imaxdiv_t imaxdiv (intmax_t __numer, intmax_t __denom) __THROW __attribute__ ((__const__)); diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h index 975f5ae..cd4503c 100644 --- a/stdlib/stdlib.h +++ b/stdlib/stdlib.h @@ -985,6 +985,12 @@ __extension__ extern long long int llabs (long long int __x) __THROW __attribute__ ((__const__)) __wur; #endif +#if __GLIBC_USE (ISOC2Y) +extern unsigned int uabs (int __x) __THROW __attribute__ ((__const__)) __wur; +extern unsigned long int ulabs (long int __x) __THROW __attribute__ ((__const__)) __wur; +__extension__ extern unsigned long long int ullabs (long long int __x) + __THROW __attribute__ ((__const__)) __wur; +#endif /* Return the `div_t', `ldiv_t' or `lldiv_t' representation of the value of NUMER over DENOM. */ diff --git a/stdlib/tst-uabs.c b/stdlib/tst-uabs.c new file mode 100644 index 0000000..13c9f58 --- /dev/null +++ b/stdlib/tst-uabs.c @@ -0,0 +1,45 @@ +/* Basic tests for uabs. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU 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 2.1 of the License, or (at your option) any later version. + + The GNU 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 GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <limits.h> +#include <stdlib.h> + +#include <support/check.h> + +#define LARGE_PRIME 49999 + +static int do_test (void) +{ + int i; + + TEST_COMPARE (uabs (INT_MAX), INT_MAX); + TEST_COMPARE (uabs (INT_MIN), (unsigned int)INT_MAX + 1); + TEST_COMPARE (uabs (-1), 1); + TEST_COMPARE (uabs (0), 0); + TEST_COMPARE (uabs (1), 1); + + for (i = INT_MIN + 1; i < 0; i += LARGE_PRIME) + TEST_COMPARE (uabs (i), -i); + + for (i = 0; i < INT_MAX - LARGE_PRIME; i += LARGE_PRIME) + TEST_COMPARE (uabs (i), i); + + return EXIT_SUCCESS; +} + +#include <support/test-driver.c> diff --git a/stdlib/tst-uimaxabs.c b/stdlib/tst-uimaxabs.c new file mode 100644 index 0000000..4500ec1 --- /dev/null +++ b/stdlib/tst-uimaxabs.c @@ -0,0 +1,51 @@ +/* Basic tests for uimaxabs. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU 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 2.1 of the License, or (at your option) any later version. + + The GNU 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 GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <inttypes.h> +#include <limits.h> +#include <stdlib.h> + +#include <support/check.h> + +#define LARGE_PRIME 49999 + +static int do_test (void) +{ + intmax_t i; + + TEST_COMPARE (uimaxabs (INTMAX_MIN), (uintmax_t)INTMAX_MAX + 1); + TEST_COMPARE (uimaxabs (-1), 1); + TEST_COMPARE (uimaxabs (0), 0); + TEST_COMPARE (uimaxabs (1), 1); + + for (i = INTMAX_MIN + 1; i < INTMAX_MIN + INT_MAX; i += LARGE_PRIME) + TEST_COMPARE (uimaxabs (i), -i); + + for (i = INTMAX_MAX - INT_MAX; i < INTMAX_MAX - LARGE_PRIME; i += LARGE_PRIME) + TEST_COMPARE (uimaxabs (i), i); + + for (i = INT_MIN + 1; i < 0; i += LARGE_PRIME) + TEST_COMPARE (uimaxabs (i), -i); + + for (i = 0; i < INT_MAX; i += LARGE_PRIME) + TEST_COMPARE (uimaxabs (i), i); + + return EXIT_SUCCESS; +} + +#include <support/test-driver.c> diff --git a/stdlib/tst-ulabs.c b/stdlib/tst-ulabs.c new file mode 100644 index 0000000..3b5323e --- /dev/null +++ b/stdlib/tst-ulabs.c @@ -0,0 +1,52 @@ +/* Basic tests for ulabs. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU 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 2.1 of the License, or (at your option) any later version. + + The GNU 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 GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <limits.h> +#include <stdlib.h> + +#include <support/check.h> + +#define LARGE_PRIME 49999 + +static int do_test (void) +{ + long int i; + + TEST_COMPARE (ulabs (LONG_MAX), LONG_MAX); + TEST_COMPARE (ulabs (LONG_MIN), (unsigned long int)LONG_MAX + 1); + TEST_COMPARE (ulabs (-1), 1); + TEST_COMPARE (ulabs (0), 0); + TEST_COMPARE (ulabs (1), 1); + + for (i = LONG_MIN + 1; i < LONG_MIN + INT_MAX; i += LARGE_PRIME) + TEST_COMPARE (ulabs (i), -i); + + for (i = LONG_MAX - INT_MAX; i < LONG_MAX - LARGE_PRIME; + i += LARGE_PRIME) + TEST_COMPARE (ulabs (i), i); + + for (i = INT_MIN + 1; i < 0; i += LARGE_PRIME) + TEST_COMPARE (ulabs (i), -i); + + for (i = 0; i <= INT_MAX - LARGE_PRIME; i += LARGE_PRIME) + TEST_COMPARE (ulabs (i), i); + + return EXIT_SUCCESS; +} + +#include <support/test-driver.c> diff --git a/stdlib/tst-ullabs.c b/stdlib/tst-ullabs.c new file mode 100644 index 0000000..ac34ec7 --- /dev/null +++ b/stdlib/tst-ullabs.c @@ -0,0 +1,55 @@ +/* Basic tests for ullabs. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU 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 2.1 of the License, or (at your option) any later version. + + The GNU 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 GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <limits.h> +#include <stdlib.h> + +#include <support/check.h> + +#define LARGE_PRIME 49999 + +static int do_test (void) +{ + long long int i; + + TEST_COMPARE (ullabs (LLONG_MAX), LLONG_MAX); + TEST_COMPARE (ullabs (LLONG_MIN), (unsigned long long int)LLONG_MAX + 1); + TEST_COMPARE (ullabs (0x00000000ffffffffL), 0x00000000ffffffffL); + TEST_COMPARE (ullabs (0x0000000100000000L), 0x0000000100000000L); + TEST_COMPARE (ullabs (0x80000000ffffffffL), 0x7fffffff00000001L); + TEST_COMPARE (ullabs (0x8000000100000000L), 0x7fffffff00000000L); + TEST_COMPARE (ullabs (-1), 1); + TEST_COMPARE (ullabs (0), 0); + TEST_COMPARE (ullabs (1), 1); + + for (i = LLONG_MIN + 1; i < LLONG_MIN + INT_MAX; i += LARGE_PRIME) + TEST_COMPARE (ullabs (i), -i); + + for (i = LLONG_MAX - INT_MAX; i < LLONG_MAX - LARGE_PRIME; i += LARGE_PRIME) + TEST_COMPARE (ullabs (i), i); + + for (i = INT_MIN + 1; i < 0; i += LARGE_PRIME) + TEST_COMPARE (ullabs (i), -i); + + for (i = 0; i < INT_MAX; i += LARGE_PRIME) + TEST_COMPARE (ullabs (i), i); + + return EXIT_SUCCESS; +} + +#include <support/test-driver.c> diff --git a/stdlib/uabs.c b/stdlib/uabs.c new file mode 100644 index 0000000..72aea5f --- /dev/null +++ b/stdlib/uabs.c @@ -0,0 +1,28 @@ +/* Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU 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 2.1 of the License, or (at your option) any later version. + + The GNU 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 GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <stdlib.h> + +#undef uabs + +/* Return the absolute value of I. */ +unsigned int +uabs (int i) +{ + unsigned int j = i; + return i < 0 ? -j : i; +} diff --git a/stdlib/ulabs.c b/stdlib/ulabs.c new file mode 100644 index 0000000..c2ccfb8 --- /dev/null +++ b/stdlib/ulabs.c @@ -0,0 +1,33 @@ +/* Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU 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 2.1 of the License, or (at your option) any later version. + + The GNU 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 GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <limits.h> +#include <stdlib.h> + +#undef ulabs + +/* Return the absolute value of I. */ +unsigned long int +ulabs (long int i) +{ + unsigned long int j = i; + return i < 0 ? -j : i; +} + +#if ULONG_MAX != UINT_MAX +weak_alias (ulabs, uimaxabs) +#endif diff --git a/stdlib/ullabs.c b/stdlib/ullabs.c new file mode 100644 index 0000000..11268c2 --- /dev/null +++ b/stdlib/ullabs.c @@ -0,0 +1,33 @@ +/* Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU 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 2.1 of the License, or (at your option) any later version. + + The GNU 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 GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <limits.h> +#include <stdlib.h> + +#undef ullabs + +/* Return the absolute value of I. */ +unsigned long long int +ullabs (long long int i) +{ + unsigned long long int j = i; + return i < 0 ? -j : i; +} + +#if ULONG_MAX == UINT_MAX +weak_alias (ullabs, uimaxabs) +#endif |