2005-09-15 09:58:31 -04:00
|
|
|
#ifndef EL__UTIL_MATH_H
|
|
|
|
#define EL__UTIL_MATH_H
|
|
|
|
|
2020-10-05 14:14:55 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* It's evil to include this directly, elinks.h includes it for you
|
|
|
|
* at the right time. */
|
|
|
|
|
|
|
|
|
|
|
|
/* These macros will evaluate twice their arguments.
|
|
|
|
* Ie. MIN(a+b, c+d) will do 3 additions...
|
|
|
|
* Please prefer to use int_min() and int_max() if possible. */
|
|
|
|
|
|
|
|
/* FreeBSD needs this. */
|
|
|
|
#ifdef MIN
|
|
|
|
#undef MIN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MAX
|
|
|
|
#undef MAX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
|
|
|
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
|
|
|
|
|
|
|
|
|
|
|
static inline int
|
2021-12-03 06:34:00 -05:00
|
|
|
int_min(int x, int y)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
if (x < y) return x;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2021-12-03 06:34:00 -05:00
|
|
|
int_max(int x, int y)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
if (x > y) return x;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** Limit @a what pointed value to upper bound @a limit. */
|
2005-09-15 09:58:31 -04:00
|
|
|
static inline void
|
2021-12-03 06:34:00 -05:00
|
|
|
int_upper_bound(int *what, int limit)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
if (*what > limit) *what = limit;
|
|
|
|
}
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** Limit @a what pointed value to lower bound @a limit. */
|
2005-09-15 09:58:31 -04:00
|
|
|
static inline void
|
2021-12-03 06:34:00 -05:00
|
|
|
int_lower_bound(int *what, int limit)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
if (*what < limit) *what = limit;
|
|
|
|
}
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** Limit @a what pointed value to lower bound @a lower_limit and to
|
|
|
|
* upper bound @a upper_limit. */
|
2005-09-15 09:58:31 -04:00
|
|
|
static inline void
|
2021-12-03 06:34:00 -05:00
|
|
|
int_bounds(int *what, int lower_limit,
|
|
|
|
int upper_limit)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
if (*what < lower_limit)
|
|
|
|
*what = lower_limit;
|
|
|
|
else if (*what > upper_limit)
|
|
|
|
*what = upper_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** Swap values @a a and @a b, both of type @a type.
|
|
|
|
* This is supposed to evaluate at compile time, giving no performance hit. */
|
2005-09-15 09:58:31 -04:00
|
|
|
#define swap_values(type, a, b) \
|
|
|
|
do { \
|
|
|
|
type swap_register_ = (a); \
|
|
|
|
(a) = (b); \
|
|
|
|
(b) = (swap_register_); \
|
|
|
|
} while (0)
|
|
|
|
|
2020-10-05 14:14:55 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
#endif
|