From 105af75dcd05f6b15b51db0ca8db21b5c03d0560 Mon Sep 17 00:00:00 2001 From: kugel Date: Mon, 3 Aug 2009 01:38:58 +0000 Subject: [PATCH] Remove various ABS() definitions with a single one using typeof (if using gcc) to avoid multiple evaluations of the input expressions. Speex still uses its own as I didn't want to change this imported code too much. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22129 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/lib/codeclib.h | 3 --- apps/codecs/libspeex/arch.h | 1 + apps/gui/pitchscreen.c | 3 +-- apps/plugins/calculator.c | 1 - apps/plugins/chessbox/gnuchess.c | 2 +- apps/plugins/invadrox.c | 5 ----- apps/plugins/lib/buflib.c | 7 ------- apps/plugins/spacerocks.c | 2 -- firmware/include/stdlib.h | 10 +++++++++- 9 files changed, 12 insertions(+), 22 deletions(-) diff --git a/apps/codecs/lib/codeclib.h b/apps/codecs/lib/codeclib.h index 52bf72f7d..e7f45d357 100644 --- a/apps/codecs/lib/codeclib.h +++ b/apps/codecs/lib/codeclib.h @@ -58,9 +58,6 @@ int strcmp(const char *, const char *); void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); -#define abs(x) ((x)>0?(x):-(x)) -#define labs(x) abs(x) - /*MDCT library functions*/ extern void mdct_backward(int n, int32_t *in, int32_t *out); diff --git a/apps/codecs/libspeex/arch.h b/apps/codecs/libspeex/arch.h index 9f81e0c51..35b536383 100644 --- a/apps/codecs/libspeex/arch.h +++ b/apps/codecs/libspeex/arch.h @@ -80,6 +80,7 @@ #include "speex/speex_types.h" #endif +#undef ABS #define ABS(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute integer value. */ #define ABS16(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute 16-bit value. */ #define MIN16(a,b) ((a) < (b) ? (a) : (b)) /**< Maximum 16-bit value. */ diff --git a/apps/gui/pitchscreen.c b/apps/gui/pitchscreen.c index 8215da2a9..78d049be1 100644 --- a/apps/gui/pitchscreen.c +++ b/apps/gui/pitchscreen.c @@ -23,6 +23,7 @@ #include #include #include +#include /* for ABS() */ #include "config.h" #include "sprintf.h" #include "action.h" @@ -42,8 +43,6 @@ #include "tdspeed.h" #endif -#define ABS(x) ((x) > 0 ? (x) : -(x)) - #define ICON_BORDER 12 /* icons are currently 7x8, so add ~2 pixels */ /* on both sides when drawing */ diff --git a/apps/plugins/calculator.c b/apps/plugins/calculator.c index 079a6e500..3d0105e33 100644 --- a/apps/plugins/calculator.c +++ b/apps/plugins/calculator.c @@ -102,7 +102,6 @@ PLUGIN_HEADER #define X_5_POS (X_4_POS + REC_WIDTH) /* x5 = 110, column 111 left blank */ #define SIGN(x) ((x)<0?-1:1) -#define ABS(x) ((x)<0?-(x):(x)) /* variable button definitions */ #if CONFIG_KEYPAD == RECORDER_PAD diff --git a/apps/plugins/chessbox/gnuchess.c b/apps/plugins/chessbox/gnuchess.c index b8fef724f..5c30c6a6d 100644 --- a/apps/plugins/chessbox/gnuchess.c +++ b/apps/plugins/chessbox/gnuchess.c @@ -60,7 +60,7 @@ #define maxdepth 30 #define true 1 #define false 0 -#define absv(x) ((x) < 0 ? -(x) : (x)) +#define absv(x) (ABS(x)) #define taxicab(a,b) (abs(column[a]-column[b]) + abs(row[a]-row[b])) /* ---- Chess datatypes and variables ---- */ diff --git a/apps/plugins/invadrox.c b/apps/plugins/invadrox.c index d07bf2878..d424f1f4c 100644 --- a/apps/plugins/invadrox.c +++ b/apps/plugins/invadrox.c @@ -167,11 +167,6 @@ PLUGIN_HEADER #define UNUSED __attribute__ ((unused)) #endif -#ifndef ABS -#define ABS(x) (((x) < 0) ? (-(x)) : (x)) -#endif - - /* Defines common to all models */ #define UFO_Y (SCORENUM_Y + FONT_HEIGHT + ALIEN_HEIGHT) #define PLAYFIELD_Y (LCD_HEIGHT - SHIP_HEIGHT - 2) diff --git a/apps/plugins/lib/buflib.c b/apps/plugins/lib/buflib.c index ddfc82c52..5d03ca4bb 100644 --- a/apps/plugins/lib/buflib.c +++ b/apps/plugins/lib/buflib.c @@ -40,13 +40,6 @@ * case that use a predefined context. */ -#define ABS(x) \ -({ \ - typeof(x) xtmp_abs_ = x; \ - xtmp_abs_ = xtmp_abs_ < 0 ? -xtmp_abs_ : xtmp_abs_; \ - xtmp_abs_; \ -}) - /* Initialize buffer manager */ void buflib_init(struct buflib_context *ctx, void *buf, size_t size) diff --git a/apps/plugins/spacerocks.c b/apps/plugins/spacerocks.c index 7ceec1637..32d48862a 100644 --- a/apps/plugins/spacerocks.c +++ b/apps/plugins/spacerocks.c @@ -246,8 +246,6 @@ PLUGIN_HEADER #endif #endif -#define ABS(x) ((x)>0?(x):-(x)) - #define RES MAX(LCD_WIDTH, LCD_HEIGHT) #define LARGE_LCD RES >= 200 #define ENEMY_MISSILE_SURVIVAL_LENGTH RES/2 diff --git a/firmware/include/stdlib.h b/firmware/include/stdlib.h index 504b59247..a287889c6 100644 --- a/firmware/include/stdlib.h +++ b/firmware/include/stdlib.h @@ -35,7 +35,15 @@ void *realloc(void *, size_t); void srand(unsigned int seed); int rand(void); -#define abs(x) ((x)>0?(x):-(x)) +#ifndef ABS +#if defined(__GNUC__) +#define ABS(a) ({typeof (a) ___a = (a); ___a < 0 ? -___a: ___a; }) +#else +#define ABS(a) (((a) < 0) ? -(a) : (a)) +#endif /* __GNUC__ */ +#endif + +#define abs(x) (ABS(x)) #define labs(x) abs(x) #ifdef SIMULATOR -- 2.11.4.GIT