From 6bbb700c348150b7f16b185ec63eb0c1fcc92584 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Thu, 27 Nov 2003 19:47:19 +0000 Subject: 2003-11-27 Artem B. Bityuckiy Jeff Johnston * libc/include/stdlib.h (lldiv_t): New type. (atoll, llabs, lldiv): New prototypes. * libc/stdlib/Makefile.am: Add support for atoll, llabs, and lldiv. * libc/stdlib/stdlib.tex: Ditto. * libc/stdlib/Makefile.in: Regenerated. * libc/stdlib/atoll.c: New file. * libc/stdlib/llabs.c: Ditto. * libc/stdlib/lldiv.c: Ditto. --- newlib/libc/include/stdlib.h | 11 ++++ newlib/libc/stdlib/Makefile.am | 10 +++- newlib/libc/stdlib/Makefile.in | 20 ++++--- newlib/libc/stdlib/atoll.c | 73 ++++++++++++++++++++++++++ newlib/libc/stdlib/llabs.c | 66 +++++++++++++++++++++++ newlib/libc/stdlib/lldiv.c | 115 +++++++++++++++++++++++++++++++++++++++++ newlib/libc/stdlib/stdlib.tex | 12 +++++ 7 files changed, 299 insertions(+), 8 deletions(-) create mode 100644 newlib/libc/stdlib/atoll.c create mode 100644 newlib/libc/stdlib/llabs.c create mode 100644 newlib/libc/stdlib/lldiv.c (limited to 'newlib/libc') diff --git a/newlib/libc/include/stdlib.h b/newlib/libc/include/stdlib.h index 5bfe4f712..53dec3ec4 100644 --- a/newlib/libc/include/stdlib.h +++ b/newlib/libc/include/stdlib.h @@ -33,6 +33,14 @@ typedef struct long rem; /* remainder */ } ldiv_t; +#ifndef __STRICT_ANSI__ +typedef struct +{ + long long int quot; /* quotient */ + long long int rem; /* remainder */ +} lldiv_t; +#endif + #ifndef NULL #define NULL 0 #endif @@ -155,6 +163,9 @@ unsigned short * _EXFUN(_seed48_r,(struct _reent *, unsigned short [3])); _VOID _EXFUN(srand48,(long)); _VOID _EXFUN(_srand48_r,(struct _reent *, long)); +long long _EXFUN(atoll,(const char *__nptr)); +long long _EXFUN(llabs,(long long)); +lldiv_t _EXFUN(lldiv,(long long __numer, long long __denom)); long long _EXFUN(strtoll,(const char *__n, char **__end_PTR, int __base)); long long _EXFUN(_strtoll_r,(struct _reent *, const char *__n, char **__end_PTR, int __base)); unsigned long long _EXFUN(strtoull,(const char *__n, char **__end_PTR, int __base)); diff --git a/newlib/libc/stdlib/Makefile.am b/newlib/libc/stdlib/Makefile.am index cdd7987a0..f6ac17cc9 100644 --- a/newlib/libc/stdlib/Makefile.am +++ b/newlib/libc/stdlib/Makefile.am @@ -69,7 +69,10 @@ EXTENDED_SOURCES = \ strtoll.c \ strtoll_r.c \ strtoull.c \ - strtoull_r.c + strtoull_r.c \ + atoll.c \ + llabs.c \ + lldiv.c ELIX_2_OBJS = \ a64l.$(oext) \ @@ -185,16 +188,19 @@ CHEWOUT_FILES= \ assert.def \ atexit.def \ atof.def \ - ecvtbuf.def \ atoi.def \ + atoll.def \ calloc.def \ div.def \ + ecvtbuf.def \ efgcvt.def \ envlock.def \ exit.def \ getenv.def \ labs.def \ ldiv.def \ + llabs.def \ + lldiv.def \ malloc.def \ mallocr.def \ mblen.def \ diff --git a/newlib/libc/stdlib/Makefile.in b/newlib/libc/stdlib/Makefile.in index 6f72debaf..4d0fdf842 100644 --- a/newlib/libc/stdlib/Makefile.in +++ b/newlib/libc/stdlib/Makefile.in @@ -1,6 +1,6 @@ -# Makefile.in generated automatically by automake 1.4 from Makefile.am +# Makefile.in generated automatically by automake 1.4-p5 from Makefile.am -# Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc. +# Copyright (C) 1994, 1995-8, 1999, 2001 Free Software Foundation, Inc. # This Makefile.in is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, # with or without modifications, as long as this notice is preserved. @@ -176,7 +176,10 @@ EXTENDED_SOURCES = \ strtoll.c \ strtoll_r.c \ strtoull.c \ - strtoull_r.c + strtoull_r.c \ + atoll.c \ + llabs.c \ + lldiv.c ELIX_2_OBJS = \ @@ -246,16 +249,19 @@ CHEWOUT_FILES = \ assert.def \ atexit.def \ atof.def \ - ecvtbuf.def \ atoi.def \ + atoll.def \ calloc.def \ div.def \ + ecvtbuf.def \ efgcvt.def \ envlock.def \ exit.def \ getenv.def \ labs.def \ ldiv.def \ + llabs.def \ + lldiv.def \ malloc.def \ mallocr.def \ mblen.def \ @@ -314,7 +320,8 @@ LIBS = @LIBS@ @USE_LIBTOOL_FALSE@mtrim.$(OBJEXT) nrand48.$(OBJEXT) rand48.$(OBJEXT) \ @USE_LIBTOOL_FALSE@seed48.$(OBJEXT) srand48.$(OBJEXT) strtoll.$(OBJEXT) \ @USE_LIBTOOL_FALSE@strtoll_r.$(OBJEXT) strtoull.$(OBJEXT) \ -@USE_LIBTOOL_FALSE@strtoull_r.$(OBJEXT) +@USE_LIBTOOL_FALSE@strtoull_r.$(OBJEXT) atoll.$(OBJEXT) llabs.$(OBJEXT) \ +@USE_LIBTOOL_FALSE@lldiv.$(OBJEXT) LTLIBRARIES = $(noinst_LTLIBRARIES) @USE_LIBTOOL_TRUE@libstdlib_la_OBJECTS = __adjust.lo __exp10.lo \ @@ -330,7 +337,8 @@ LTLIBRARIES = $(noinst_LTLIBRARIES) @USE_LIBTOOL_TRUE@ecvtbuf.lo efgcvt.lo erand48.lo jrand48.lo lcong48.lo \ @USE_LIBTOOL_TRUE@lrand48.lo mrand48.lo msize.lo mtrim.lo nrand48.lo \ @USE_LIBTOOL_TRUE@rand48.lo seed48.lo srand48.lo strtoll.lo \ -@USE_LIBTOOL_TRUE@strtoll_r.lo strtoull.lo strtoull_r.lo +@USE_LIBTOOL_TRUE@strtoll_r.lo strtoull.lo strtoull_r.lo atoll.lo \ +@USE_LIBTOOL_TRUE@llabs.lo lldiv.lo CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) diff --git a/newlib/libc/stdlib/atoll.c b/newlib/libc/stdlib/atoll.c new file mode 100644 index 000000000..1dd4ec39e --- /dev/null +++ b/newlib/libc/stdlib/atoll.c @@ -0,0 +1,73 @@ +/* +FUNCTION +<>---convert a string to a long long integer + +INDEX + atoll + +ANSI_SYNOPSIS + #include + long long atoll(const char *<[str]>); + +TRAD_SYNOPSIS + #include + long long atoll(<[str]>) + const char *<[str]>; + +DESCRIPTION +The function <> converts the initial portion of the string +pointed to by <<*<[str]>>> to a type <>. The call to +atoll(str) should be equivalent to strtoll(str, (char **)NULL, 10) +except that <> doesn't detect errors. + +RETURNS +The converted value. + +PORTABILITY +<> is ISO 9899 (C99) and POSIX 1003.1-2001 compatable. + +No supporting OS subroutines are required. +*/ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +long long +_DEFUN(atoll, (str), + _CONST char *str) +{ + return strtoll(str, (char **)NULL, 10); +} diff --git a/newlib/libc/stdlib/llabs.c b/newlib/libc/stdlib/llabs.c new file mode 100644 index 000000000..11246c354 --- /dev/null +++ b/newlib/libc/stdlib/llabs.c @@ -0,0 +1,66 @@ +/* +FUNCTION +<>---compute the absolute value of an long long integer. + +INDEX + llabs + +ANSI_SYNOPSIS + #include + long long llabs(long long j); + +TRAD_SYNOPSIS + #include + long long llabs(<[j]>) + long long <[j]>; + +DESCRIPTION +The <> function computes the absolute value of the long long integer +argument <[j]> (also called the magnitude of <[j]>). + +The similar function <> uses and returns <> rather than +<> values. + +RETURNS +A nonnegative long long integer. + +PORTABILITY +<> is ISO 9899 (C99) compatable. + +No supporting OS subroutines are required. +*/ + +/*- + * Copyright (c) 2001 Mike Barcroft + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +long long +_DEFUN(llabs, (j), + long long j) +{ + return (j < 0 ? -j : j); +} diff --git a/newlib/libc/stdlib/lldiv.c b/newlib/libc/stdlib/lldiv.c new file mode 100644 index 000000000..6b853e4b6 --- /dev/null +++ b/newlib/libc/stdlib/lldiv.c @@ -0,0 +1,115 @@ +/* +FUNCTION +<>---divide two long long integers + +INDEX + lldiv + +ANSI_SYNOPSIS + #include + lldiv_t lldiv(long long <[n]>, long long <[d]>); + +TRAD_SYNOPSIS + #include + lldiv_t lldiv(<[n]>, <[d]>) + long long <[n]>, <[d]>; + +DESCRIPTION +Divide +@tex +$n/d$, +@end tex +@ifnottex +<[n]>/<[d]>, +@end ifnottex +returning quotient and remainder as two long long integers in a structure +<>. + +RETURNS +The result is represented with the structure + +. typedef struct +. { +. long long quot; +. long long rem; +. } lldiv_t; + +where the <> field represents the quotient, and <> the +remainder. For nonzero <[d]>, if `<<<[r]> = ldiv(<[n]>,<[d]>);>>' then +<[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'. + +To divide <> rather than <> values, use the similar +function <>. + +PORTABILITY +<> is ISO 9899 (C99) compatable. + +No supporting OS subroutines are required. +*/ + +/*- + * Copyright (c) 2001 Mike Barcroft + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +/* + * The ANSI standard says that |r.quot| <= |n/d|, where + * n/d is to be computed in infinite precision. In other + * words, we should always truncate the quotient towards + * 0, never -infinity. + * + * Machine division and remainer may work either way when + * one or both of n or d is negative. If only one is + * negative and r.quot has been truncated towards -inf, + * r.rem will have the same sign as denom and the opposite + * sign of num; if both are negative and r.quot has been + * truncated towards -inf, r.rem will be positive (will + * have the opposite sign of num). These are considered + * `wrong'. + * + * If both are num and denom are positive, r will always + * be positive. + * + * This all boils down to: + * if num >= 0, but r.rem < 0, we got the wrong answer. + * In that case, to get the right answer, add 1 to r.quot and + * subtract denom from r.rem. + */ +lldiv_t +_DEFUN (lldiv, (number, denom), + long long numer _AND long long denom) +{ + lldiv_t retval; + + retval.quot = numer / denom; + retval.rem = numer % denom; + if (numer >= 0 && retval.rem < 0) { + retval.quot++; + retval.rem -= denom; + } + return (retval); +} + diff --git a/newlib/libc/stdlib/stdlib.tex b/newlib/libc/stdlib/stdlib.tex index 5798caa69..39fb915fc 100644 --- a/newlib/libc/stdlib/stdlib.tex +++ b/newlib/libc/stdlib/stdlib.tex @@ -11,6 +11,7 @@ The corresponding declarations are in the header file @file{stdlib.h}. * atexit:: Request execution of functions at program exit * atof:: String to double or float * atoi:: String to integer +* atoll:: String to long long * calloc:: Allocate space for arrays * div:: Divide two integers * ecvtbuf:: Double or float to string of digits @@ -21,6 +22,8 @@ The corresponding declarations are in the header file @file{stdlib.h}. * getenv:: Look up environment variable * labs:: Long integer absolute value (magnitude) * ldiv:: Divide two long integers +* llabs:: Long long integer absolute value (magnitude) +* lldiv:: Divide two long long integers * malloc:: Allocate and manage memory (malloc, realloc, free) * mallinfo:: Get information about allocated memory * __malloc_lock:: Lock memory pool for malloc and free @@ -55,6 +58,9 @@ The corresponding declarations are in the header file @file{stdlib.h}. @page @include stdlib/atoi.def +@page +@include stdlib/atoll.def + @page @include stdlib/calloc.def @@ -82,6 +88,12 @@ The corresponding declarations are in the header file @file{stdlib.h}. @page @include stdlib/ldiv.def +@page +@include stdlib/llabs.def + +@page +@include stdlib/lldiv.def + @page @include stdlib/malloc.def -- cgit v1.2.3