scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 610a73fa975fbb6981dc1aa383b2d92567060a98
parent 1ef01b62fa5d1651c190dd95fb9806830cb92d4f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  6 Dec 2017 23:16:21 +0100

[lib/c] Add abs(), labs() and llabs()

Diffstat:
Mlib/c/include/stdlib.h | 10++++++++++
Mlib/c/src/Makefile | 1+
Alib/c/src/__abs.c | 5+++++
Alib/c/src/__labs.c | 5+++++
Alib/c/src/__llabs.c | 5+++++
Alib/c/src/abs.c | 9+++++++++
Alib/c/src/labs.c | 9+++++++++
Alib/c/src/llabs.c | 9+++++++++
8 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/lib/c/include/stdlib.h b/lib/c/include/stdlib.h @@ -66,4 +66,14 @@ extern int wctomb(char *s, wchar_t wchar); extern size_t mbstowcs(wchar_t * restrict pwcs, const char * restrict s, size_t n); extern size_t wcstombs(char * restrict s, const wchar_t * restrict pwcs, size_t n); +#ifdef __USE_MACROS +extern int __abs; +extern long __labs; +extern long long __llabs; + +#define abs(x) (__abs = (x), (__abs) < 0 ? -__abs : __abs) +#define labs(x) (__labs = (x), (__labs) < 0 ? -__labs : __labs) +#define llabs(x) (__llabs = (x), (__llabs) < 0 ? -__llabs : __llabs) +#endif + #endif diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile @@ -3,6 +3,7 @@ include ../../../config.mk OBJ = bsearch.o \ + abs.o __abs.o labs.o __labs.o llabs.o __labs.o \ printf.o fprintf.o vfprintf.o \ fgets.o gets.of fgetc.o fputc.o getchar.o putchar.o \ fputs.o puts.o fread.o fwrite.o \ diff --git a/lib/c/src/__abs.c b/lib/c/src/__abs.c @@ -0,0 +1,5 @@ + +#define __USE_MACROS +#include <stdlib.h> + +int __abs; diff --git a/lib/c/src/__labs.c b/lib/c/src/__labs.c @@ -0,0 +1,5 @@ + +#define __USE_MACROS +#include <stdlib.h> + +long __labs; diff --git a/lib/c/src/__llabs.c b/lib/c/src/__llabs.c @@ -0,0 +1,5 @@ + +#define __USE_MACROS +#include <stdlib.h> + +long long __llabs; diff --git a/lib/c/src/abs.c b/lib/c/src/abs.c @@ -0,0 +1,9 @@ + +#include <stdlib.h> +#undef abs + +int +abs(int n) +{ + return (n < 0) ? -n : n; +} diff --git a/lib/c/src/labs.c b/lib/c/src/labs.c @@ -0,0 +1,9 @@ + +#include <stdlib.h> +#undef labs + +long +labs(long n) +{ + return (n < 0) ? -n : n; +} diff --git a/lib/c/src/llabs.c b/lib/c/src/llabs.c @@ -0,0 +1,9 @@ + +#include <stdlib.h> +#undef llabs + +long long +llabs(long long n) +{ + return (n < 0) ? -n : n; +}