1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* Copyright (C) 2001-2004 Michael H. Schimek
* Copyright (C) 2000-2003 Iaki Garca Etxebarria
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* $Id: misc.h,v 1.12 2006/04/12 01:48:15 mschimek Exp $ */
#ifndef __ZTV_MISC_H__
#define __ZTV_MISC_H__
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stddef.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include "macros.h"
#define N_ELEMENTS(array) (sizeof (array) / sizeof (*(array)))
#ifdef __GNUC__
#undef likely
#undef unlikely
#if __GNUC__ < 3
/* Expect expression usually true/false, schedule accordingly. */
# define likely(expr) (expr)
# define unlikely(expr) (expr)
#else
# define likely(expr) __builtin_expect(expr, 1)
# define unlikely(expr) __builtin_expect(expr, 0)
#endif
#undef __i386__
#undef __i686__
#if #cpu (i386)
# define __i386__ 1
#endif
#if #cpu (i686)
# define __i686__ 1
#endif
/* &x == PARENT (&x.tm_min, struct tm, tm_min),
safer than &x == (struct tm *) &x.tm_min. A NULL _ptr is safe and
will return NULL, not -offsetof(_member). */
#undef PARENT
#define PARENT(_ptr, _type, _member) ({ \
__typeof__ (&((_type *) 0)->_member) _p = (_ptr); \
(_p != 0) ? (_type *)(((char *) _p) - offsetof (_type, \
_member)) : (_type *) 0; \
})
/* Like PARENT(), to be used with const _ptr. */
#undef CONST_PARENT
#define CONST_PARENT(_ptr, _type, _member) ({ \
__typeof__ (&((const _type *) 0)->_member) _p = (_ptr); \
(_p != 0) ? (const _type *)(((const char *) _p) - offsetof \
(const _type, _member)) : (const _type *) 0; \
})
/* Note the following macros have no side effects only when you
compile with GCC, so don't expect this. */
/* Absolute value of int without a branch.
Note ABS (INT_MIN) == INT_MAX + 1. */
#undef ABS
#define ABS(n) ({ \
register int _n = (n), _t = _n; \
_t >>= sizeof (_t) * 8 - 1; /* assumes signed shift, safe? */ \
_n ^= _t; \
_n -= _t; \
})
#undef MIN
#define MIN(x, y) ({ \
__typeof__ (x) _x = (x); \
__typeof__ (y) _y = (y); \
(void)(&_x == &_y); /* alert when type mismatch */ \
(_x < _y) ? _x : _y; \
})
#undef MAX
#define MAX(x, y) ({ \
__typeof__ (x) _x = (x); \
__typeof__ (y) _y = (y); \
(void)(&_x == &_y); /* alert when type mismatch */ \
(_x > _y) ? _x : _y; \
})
#define SWAP(x, y) \
do { \
__typeof__ (x) _x = x; \
x = y; \
y = _x; \
} while (0)
#undef SATURATE
#ifdef __i686__ /* has conditional move. */
#define SATURATE(n, min, max) ({ \
__typeof__ (n) _n = (n); \
__typeof__ (n) _min = (min); \
__typeof__ (n) _max = (max); \
(void)(&_n == &_min); /* alert when type mismatch */ \
(void)(&_n == &_max); \
if (_n < _min) \
_n = _min; \
if (_n > _max) \
_n = _max; \
_n; \
})
#else
#define SATURATE(n, min, max) ({ \
__typeof__ (n) _n = (n); \
__typeof__ (n) _min = (min); \
__typeof__ (n) _max = (max); \
(void)(&_n == &_min); /* alert when type mismatch */ \
(void)(&_n == &_max); \
if (_n < _min) \
_n = _min; \
else if (_n > _max) \
_n = _max; \
_n; \
})
#endif
#define SWAB16(n) ({ \
__typeof__ (n) _n = (n); \
((_n & 0xFF) << 8) | ((_n >> 8) & 0xFF); \
})
#else /* !__GNUC__ */
#define likely(expr) (expr)
#define unlikely(expr) (expr)
#undef __i386__
#undef __i686__
#define __attribute__(args...)
static char *
PARENT_HELPER (char *p, unsigned int offset)
{ return (p == 0) ? 0 : p - offset; }
static const char *
CONST_PARENT_HELPER (const char *p, unsigned int offset)
{ return (p == 0) ? 0 : p - offset; }
#undef PARENT
#define PARENT(_ptr, _type, _member) \
((offsetof (_type, _member) == 0) ? (_type *)(_ptr) \
: (_type *) PARENT_HELPER ((char *)(_ptr), offsetof (_type, _member)))
#undef CONST_PARENT
#define CONST_PARENT(_ptr, _type, _member) \
((offsetof (const _type, _member) == 0) ? (const _type *)(_ptr) \
: (const _type *) CONST_PARENT_HELPER ((const char *)(_ptr), \
offsetof (const _type, _member)))
#undef ABS
#define ABS(n) (((n) < 0) ? -(n) : (n))
#undef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#undef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define SWAP(x, y) \
do { \
long _x = x; \
x = y; \
y = _x; \
} while (0)
#undef SATURATE
#define SATURATE(n, min, max) MIN (MAX (n, min), max)
#define SWAB16(n) ((((n) & 0xFF) << 8) | (((n) >> 8) & 0xFF))
#endif /* !__GNUC__ */
/* Find first set bit in 32 bit constant, see man 3 ffs. */
#define FFS2(m) ((m) & 0x2 ? 2 : 1)
#define FFS4(m) ((m) & 0xC ? 2 + FFS2 ((m) >> 2) : FFS2 (m))
#define FFS8(m) ((m) & 0xF0 ? 4 + FFS4 ((m) >> 4) : FFS4 (m))
#define FFS16(m) ((m) & 0xFF00 ? 8 + FFS8 ((m) >> 8) : FFS8 (m))
#define FFS32(m) ((m) & 0xFFFF0000 ? 16 + FFS16 ((m) >> 16) : FFS16 (m))
#define FFS(m) (0 == (m) ? 0 : FFS32 (m))
/* Masks m bits out of v, then shifts msb to position n (0 ... 31).
E.g. v=0x1234, m=0x0FF0, n=7 -> 0x0023. */
#undef MASKED_SHIFT
#define MASKED_SHIFT(v, m, n) \
((FFS (m) > (int)(n) + 1) ? \
(((v) & (m)) >> (FFS (m) - (n) - 1)) : \
(((v) & (m)) << MIN ((int)(n) + 1 - FFS (m), 31)))
/* E.g. v=0x1234, m=0x0FF0, n=7 -> 0x0340. */
#undef SHIFT_MASKED
#define SHIFT_MASKED(v, m, n) \
((FFS (m) > (int)(n) + 1) ? \
(((v) << (FFS (m) - (n) - 1)) & (m)) : \
(((v) >> MIN ((int)(n) + 1 - FFS32 (m), 31) & (m)))
#undef CLAMP
#define CLAMP(n, min, max) SATURATE (n, min, max)
/* NB gcc inlines and optimizes when size is const. */
#define SET(var) memset (&(var), ~0, sizeof (var))
#define CLEAR(var) memset (&(var), 0, sizeof (var))
#define COPY(d, s) /* useful to copy arrays, otherwise use assignment */ \
(assert (sizeof (d) == sizeof (s)), memcpy (d, s, sizeof (d)))
/* legacy, to be removed/replaced */
#ifndef printv
#include <stdio.h>
extern int debug_msg;
#define printv(format, args...) \
do { \
if (debug_msg) { \
fprintf(stderr, format ,##args); \
fflush(stderr); } \
} while (0)
#endif
#ifdef HAVE_STRLCPY
# define _tv_strlcpy strlcpy
#else
extern size_t
_tv_strlcpy (char * dst,
const char * src,
size_t len);
#endif
#ifdef HAVE_STRNDUP
# define _tv_strndup strndup
#else
extern char *
_tv_strndup (const char * s,
size_t len);
#endif
#ifdef HAVE_ASPRINTF
# define _tv_asprintf asprintf
#else
int
_tv_asprintf (char ** dstp,
const char * templ,
...);
#endif
typedef void
clear_plane_fn (uint8_t * dst,
unsigned int value,
unsigned int width,
unsigned int height,
unsigned long padding);
typedef void
copy_plane_fn (uint8_t * dst,
const uint8_t * src,
unsigned int width,
unsigned int height,
unsigned long dst_padding,
unsigned long src_padding);
#endif /* MISC_H */
|