| 1 |
#ifndef PM_C_UTIL_INCLUDED |
| 2 |
#define PM_C_UTIL_INCLUDED |
| 3 |
|
| 4 |
/* Note that for MAX and MIN, if either of the operands is a floating point |
| 5 |
Not-A-Number, the result is the second operand. So if you're computing a |
| 6 |
running maximum and want to ignore the NaNs in the computation, put the |
| 7 |
running maximum variable second. |
| 8 |
*/ |
| 9 |
#undef MAX |
| 10 |
#define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 11 |
#undef MIN |
| 12 |
#define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 13 |
#undef ABS |
| 14 |
#define ABS(a) ((a) >= 0 ? (a) : -(a)) |
| 15 |
#undef SGN |
| 16 |
#define SGN(a) (((a)<0) ? -1 : 1) |
| 17 |
#undef ODD |
| 18 |
#define ODD(n) ((n) & 1) |
| 19 |
#undef ROUND |
| 20 |
#define ROUND(X) (((X) >= 0) ? (int)((X)+0.5) : (int)((X)-0.5)) |
| 21 |
#undef ROUNDU |
| 22 |
#define ROUNDU(X) ((unsigned int)((X)+0.5)) |
| 23 |
/* Note that imprecision in floating point arithmetic can make an exact |
| 24 |
half fractional part round down instead of up. What should be |
| 25 |
1000.5 might actually be 1000.49999999999. |
| 26 |
|
| 27 |
Use 'pnm_unnormalized_sample' instead of ROUNDU(samplen*maxval) to get |
| 28 |
consistent rounding up when you are unnormalizing sample values. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* ROUNDUP rounds up to a specified multiple. E.g. ROUNDUP(22, 8) == 24 */ |
| 32 |
|
| 33 |
#undef ROUNDUP |
| 34 |
#define ROUNDUP(X,M) (((X)+(M)-1)/(M)*(M)) |
| 35 |
#undef ROUNDDN |
| 36 |
#define ROUNDDN(X,M) ((X)/(M)*(M)) |
| 37 |
|
| 38 |
#define ROUNDDIV(DIVIDEND,DIVISOR) (((DIVIDEND) + (DIVISOR)/2)/(DIVISOR)) |
| 39 |
|
| 40 |
#undef SQR |
| 41 |
#define SQR(a) ((a)*(a)) |
| 42 |
|
| 43 |
/* NOTE: do not use "bool" as a type in an external interface. It could |
| 44 |
have different definitions on either side of the interface. Even if both |
| 45 |
sides include this interface header file, the conditional compilation |
| 46 |
here means one side may use the typedef below and the other side may |
| 47 |
use some other definition. For an external interface, be safe and just |
| 48 |
use "int". |
| 49 |
*/ |
| 50 |
|
| 51 |
/* We will probably never again see a system that does not have |
| 52 |
<stdbool.h>, but just in case, we have our own alternative here. |
| 53 |
|
| 54 |
Evidence shows that the compiler actually produces better code with |
| 55 |
<stdbool.h> than with bool simply typedefed to int. |
| 56 |
*/ |
| 57 |
|
| 58 |
#ifdef __cplusplus |
| 59 |
/* C++ has a bool type and false and true constants built in. */ |
| 60 |
#else |
| 61 |
/* The test for __STDC__ is paranoid. It is there just in case some |
| 62 |
nonstandard compiler defines __STDC_VERSION__ in an arbitrary manner. |
| 63 |
|
| 64 |
We know GCC 2.95.3 has stdbool; not sure about earlier GCC 2. |
| 65 |
*/ |
| 66 |
#if ( defined(__GNUC__) && (__GNUC__ >= 2) ) || \ |
| 67 |
( defined(__STDC__) && (__STDC__ == 1) && \ |
| 68 |
defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) ) |
| 69 |
#include <stdbool.h> |
| 70 |
#else |
| 71 |
/* We used to assume that if TRUE was defined, then bool was too. |
| 72 |
However, we had a report on 2001.09.21 of a Tru64 system that had |
| 73 |
TRUE but not bool and on 2002.03.21 of an AIX 4.3 system that was |
| 74 |
likewise. So now we define bool all the time, unless the macro |
| 75 |
HAVE_BOOL is defined. If someone is using the Netpbm libraries and |
| 76 |
also another library that defines bool, he can either make the |
| 77 |
other library define/respect HAVE_BOOL or just define HAVE_BOOL in |
| 78 |
the file that includes pm_config.h or with a compiler option. Note |
| 79 |
that C++ always has bool. |
| 80 |
*/ |
| 81 |
#ifndef HAVE_BOOL |
| 82 |
#define HAVE_BOOL 1 |
| 83 |
typedef int bool; |
| 84 |
#endif |
| 85 |
#ifndef true |
| 86 |
enum boolvalue {false=0, true=1}; |
| 87 |
#endif |
| 88 |
#endif |
| 89 |
#endif |
| 90 |
|
| 91 |
#ifndef TRUE |
| 92 |
#define TRUE true |
| 93 |
#endif |
| 94 |
#ifndef FALSE |
| 95 |
#define FALSE false |
| 96 |
#endif |
| 97 |
|
| 98 |
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
| 99 |
|
| 100 |
#endif |