[Skiboot] [RFC PATCH 1/3] libc: Add abs() to stdlib

Shilpasri G Bhat shilpa.bhat at linux.vnet.ibm.com
Wed Sep 14 00:50:37 AEST 2016


Signed-off-by: Shilpasri G Bhat <shilpa.bhat at linux.vnet.ibm.com>
---
 libc/include/stdlib.h    |  1 +
 libc/stdlib/Makefile.inc |  2 +-
 libc/stdlib/abs.c        | 25 +++++++++++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 libc/stdlib/abs.c

diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h
index b4f1c38..d420687 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);
+int __attribute__((const)) abs(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..c6c9138 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 abs.o
 
 STDLIB = $(LIBCDIR)/stdlib/built-in.o
 $(STDLIB): $(STDLIB_OBJS:%=$(LIBCDIR)/stdlib/%)
diff --git a/libc/stdlib/abs.c b/libc/stdlib/abs.c
new file mode 100644
index 0000000..979a666
--- /dev/null
+++ b/libc/stdlib/abs.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>
+
+/**
+ * abs() - Computes the absolute value of integer
+ * @n:	   integer number
+ *
+ * Returns the absolute value of the integer argument
+ */
+
+int abs(int n)
+{
+	return  (n > 0) ? n : -n;
+}
-- 
1.8.3.1



More information about the Skiboot mailing list