diff options
author | Shilpasri G Bhat <shilpa.bhat@linux.vnet.ibm.com> | 2017-05-26 13:58:57 +0530 |
---|---|---|
committer | Stewart Smith <stewart@linux.vnet.ibm.com> | 2017-05-31 16:58:05 +1000 |
commit | 41bb3e7cd4eda5371d7cd0310e31357550279ccd (patch) | |
tree | 62e6d7dfea5e419444162a8dee2e9abe72bb7776 | |
parent | 1d4a0a4374f69c890b64f5463930775d94139ded (diff) | |
download | skiboot-41bb3e7cd4eda5371d7cd0310e31357550279ccd.zip skiboot-41bb3e7cd4eda5371d7cd0310e31357550279ccd.tar.gz skiboot-41bb3e7cd4eda5371d7cd0310e31357550279ccd.tar.bz2 |
libc: Add labs() to stdlib
Signed-off-by: Shilpasri G Bhat <shilpa.bhat@linux.vnet.ibm.com>
Acked-by: Balbir Singh <bsingharora@gmail.com>
Signed-off-by: Stewart Smith <stewart@linux.vnet.ibm.com>
-rw-r--r-- | libc/include/stdlib.h | 1 | ||||
-rw-r--r-- | libc/stdlib/Makefile.inc | 2 | ||||
-rw-r--r-- | libc/stdlib/labs.c | 25 | ||||
-rw-r--r-- | libc/test/run-stdlib.c | 7 |
4 files changed, 34 insertions, 1 deletions
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index b4f1c38..234bd11 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -25,6 +25,7 @@ unsigned long int strtoul(const char *nptr, char **endptr, int base); long int strtol(const char *nptr, char **endptr, int base); int rand(void); +long int __attribute__((const)) labs(long int n); void __attribute__((noreturn)) _abort(const char *msg); #define abort() do { \ _abort("abort():" __FILE__ \ diff --git a/libc/stdlib/Makefile.inc b/libc/stdlib/Makefile.inc index 6d1123e..22417df 100644 --- a/libc/stdlib/Makefile.inc +++ b/libc/stdlib/Makefile.inc @@ -13,7 +13,7 @@ SUBDIRS += $(LIBCDIR)/stdlib STDLIB_OBJS = error.o atoi.o atol.o strtol.o strtoul.o \ - rand.o + rand.o labs.o STDLIB = $(LIBCDIR)/stdlib/built-in.o $(STDLIB): $(STDLIB_OBJS:%=$(LIBCDIR)/stdlib/%) diff --git a/libc/stdlib/labs.c b/libc/stdlib/labs.c new file mode 100644 index 0000000..9b68bb2 --- /dev/null +++ b/libc/stdlib/labs.c @@ -0,0 +1,25 @@ +/****************************************************************************** + * Copyright (c) 2017 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stdlib.h> + +/** + * labs() - Computes the absolute value of long integer + * @n: long integer number + * + * Returns the absolute value of the long integer argument + */ + +long int labs(long int n) +{ + return (n > 0) ? n : -n; +} diff --git a/libc/test/run-stdlib.c b/libc/test/run-stdlib.c index 1f3a2e1..bf17604 100644 --- a/libc/test/run-stdlib.c +++ b/libc/test/run-stdlib.c @@ -85,5 +85,12 @@ int main(void) assert(strtoul("z", NULL, 36) == 35); assert(strtoul("Y", NULL, 36) == 34); + /* labs - ensure it returns absolute value */ + assert(labs(0) == 0); + assert(labs(2147483647) == 2147483647); + assert(labs(-2147483647) == 2147483647); + assert(labs(9223372036854775807) == 9223372036854775807); + assert(labs(-9223372036854775807) == 9223372036854775807); + return 0; } |