@@ -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__ \
@@ -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/%)
new file mode 100644
@@ -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;
+}
@@ -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;
}