1 /* (C) Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005 Stijn van Dongen 2 * (C) Copyright 2006, 2007, 2008, 2009 Stijn van Dongen 3 * 4 * This file is part of tingea. You can redistribute and/or modify tingea 5 * under the terms of the GNU General Public License; either version 3 of the 6 * License or (at your option) any later version. You should have received a 7 * copy of the GPL along with tingea, in the file COPYING. 8 */ 9 10 #ifndef minmax_h 11 #define minmax_h 12 13 #include "compile.h" 14 15 #if MCX_GNUC_OK && TINGEA__TYPED_MINMAX 16 /* these buggers do not nest, which I dislike */ 17 # define MCX_MAX(x,y) \ 18 ( { const typeof(x) _x = x; \ 19 const typeof(y) _y = y; \ 20 (void) (&_x == &_y); \ 21 _x > _y ? _x : _y; \ 22 } ) 23 # define MCX_MIN(x,y) \ 24 ( { const typeof(x) _x = x; \ 25 const typeof(y) _y = y; \ 26 (void) (&_x == &_y); \ 27 _x < _y ? _x : _y; \ 28 } ) 29 #else 30 /* The usual brain-damaged min and max, which do nest though. */ 31 # define MCX_MAX(a,b) ((a)>(b) ? (a) : (b)) 32 # define MCX_MIN(a,b) ((a)<(b) ? (a) : (b)) 33 #endif 34 35 36 #define ABS(x) ((x) > 0 ? (x) : (x) < 0 ? (-(x)) : 0) 37 38 39 40 /* The first version cannot be used recursively. 41 * I don't like this at all I think, which is why I turned it off. 42 */ 43 44 #if 0 && MCX_GNUC_OK 45 # define MCX_SIGN(a) \ 46 __extension__ \ 47 ( { typedef _ta = (a) \ 48 ; _ta _a = (a) \ 49 ; _a > 0 \ 50 ? 1 \ 51 : _a < 0 \ 52 ? -1 \ 53 : 0 \ 54 ; } \ 55 ) 56 #else 57 # define MCX_SIGN(a) \ 58 ((a) > 0 ? 1 : !(a) ? 0 : -1) 59 #endif 60 61 #define MCX_RESTRICT(x,a,b) \ 62 do { if (x < a) x = a; else if (x > b) x = b; } while (0) 63 64 #endif 65 66 67 68