[Skiboot] [PATCH V3 1/3] libc: Add labs() to stdlib

Shilpasri G Bhat shilpa.bhat at linux.vnet.ibm.com
Mon Oct 3 05:02:53 AEDT 2016


Signed-off-by: Shilpasri G Bhat <shilpa.bhat at linux.vnet.ibm.com>
---
Changes from v2:
- Implemented labs() instead of abs()
- Added test case for labs()

 libc/include/stdlib.h    |  1 +
 libc/stdlib/Makefile.inc |  2 +-
 libc/stdlib/labs.c       | 25 +++++++++++++++++++++++++
 libc/test/run-stdlib.c   |  7 +++++++
 4 files changed, 34 insertions(+), 1 deletion(-)
 create mode 100644 libc/stdlib/labs.c

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..46f396a
--- /dev/null
+++ b/libc/stdlib/labs.c
@@ -0,0 +1,25 @@
+/******************************************************************************
+ * Copyright (c) 2016 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;
 }
-- 
1.8.3.1



More information about the Skiboot mailing list