sys-defines.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include <config.h> /* built by autoconf */
  2. #ifndef NULL
  3. #define NULL 0
  4. #endif
  5. /**********************************************************************/
  6. /* SUPPORT COMPILATION WITH A C++ COMPILER (IF DESIRED) */
  7. /**********************************************************************/
  8. /* Support declarations of C linkage in C++, for functions not declared in
  9. C headers the way they should be. */
  10. #ifdef __cplusplus
  11. # define __C_LINKAGE "C"
  12. #else
  13. # define __C_LINKAGE /* empty */
  14. #endif
  15. /**********************************************************************/
  16. /* Include all the C headers we'll need. Because many platforms lack one
  17. or more standard headers or function declarations, there are numerous
  18. tests and substitutions here. */
  19. /**********************************************************************/
  20. /**********************************************************************/
  21. /* If libxmi is being compiling as part of the libplot/libplotter package,
  22. add support for multithreading, provided that libc includes pthread
  23. functions and pthread.h is present. (This comes first, because defining
  24. _REENTRANT may alter system header files.) */
  25. /**********************************************************************/
  26. #ifdef LIBPLOT
  27. #ifdef PTHREAD_SUPPORT
  28. #ifdef HAVE_PTHREAD_H
  29. #define _REENTRANT
  30. #include <pthread.h>
  31. #endif
  32. #endif
  33. #endif
  34. /**********************************************************************/
  35. /* INCLUDE stdio.h, ctype.h, errno.h. (SUBSTITUTE AS NECESSARY.) */
  36. /**********************************************************************/
  37. #include <stdio.h>
  38. #include <ctype.h> /* why is this needed? */
  39. #include <errno.h>
  40. /***************************************************************************/
  41. /* INCLUDE math.h, limits.h. (SUBSTITUTE AS NECESSARY.) */
  42. /***************************************************************************/
  43. #ifdef __DJGPP__
  44. /* for DJGPP math.h, must specify that -lm will be used;
  45. thanks mdruiter@cs.vu.nl */
  46. #define _USE_LIBM_MATH_H
  47. #endif
  48. /* Include math.h, and whichever other math-related header files we have */
  49. #include <math.h>
  50. #ifdef HAVE_LIMITS_H
  51. #include <limits.h> /* for INT_MAX */
  52. #endif
  53. #ifdef HAVE_VALUES_H
  54. #include <values.h> /* for MAXINT (backup) */
  55. #endif
  56. /* Bounds on integer datatypes (should be in limits.h, but may not be). */
  57. #ifndef UINT_MAX
  58. #ifdef __STDC__
  59. #define UINT_MAX ((unsigned int)(~(0U)))
  60. #else
  61. #define UINT_MAX ((unsigned int)(~((unsigned int)0)))
  62. #endif
  63. #endif /* not UINT_MAX */
  64. #ifndef INT_MAX
  65. #ifdef MAXINT
  66. #define INT_MAX MAXINT
  67. #else
  68. #define INT_MAX ((int)(~(1U << (8 * (int)sizeof(int) - 1))))
  69. #endif
  70. #endif /* not INT_MAX */
  71. /* IBM's definition of INT_MAX is bizarre, in AIX 4.1 at least, and using
  72. IROUND() below will yield a warning message unless we repair it */
  73. #ifdef _AIX
  74. #ifdef __GNUC__
  75. #undef INT_MAX
  76. #define INT_MAX ((int)(~(1U << (8 * (int)sizeof(int) - 1))))
  77. #endif
  78. #endif
  79. /**********************************************************************/
  80. /* INCLUDE stdlib.h, string.h. (SUBSTITUTE AS NECESSARY; if STDC_HEADERS
  81. is defined then they're both present, and stdarg.h and float.h too.) */
  82. /**********************************************************************/
  83. #ifdef STDC_HEADERS
  84. #include <stdlib.h> /* for getenv, atoi, atof, etc. */
  85. #include <string.h> /* for memcpy, memmove, strchr, malloc, etc. */
  86. #else /* not STDC_HEADERS, must do a LOT of declarations by hand */
  87. #ifdef HAVE_SYS_STDTYPES_H
  88. #include <sys/stdtypes.h> /* SunOS needs this for size_t */
  89. #endif
  90. /* supply declarations for functions declared in stdlib.h */
  91. extern __C_LINKAGE char *getenv (const char *name);
  92. extern __C_LINKAGE int atoi (const char *nptr);
  93. extern __C_LINKAGE double atof (const char *nptr);
  94. /* supply definitions in stdlib.h */
  95. #define EXIT_FAILURE 1 /* Failing exit status. */
  96. #define EXIT_SUCCESS 0 /* Successful exit status. */
  97. /* determine how to declare (or define) functions declared in string.h */
  98. #ifdef HAVE_STRCHR
  99. #ifdef HAVE_STRING_H
  100. #include <string.h>
  101. #else
  102. #ifdef HAVE_STRINGS_H
  103. #include <strings.h>
  104. #endif
  105. #endif
  106. #else /* don't have strchr, prefer strings.h */
  107. #ifdef HAVE_STRINGS_H
  108. #include <strings.h>
  109. #else
  110. #ifdef HAVE_STRING_H
  111. #include <string.h>
  112. #endif
  113. #endif
  114. #define strchr index
  115. #define strrchr rindex
  116. #endif /* not HAVE_STRCHR */
  117. #ifndef HAVE_MEMCPY
  118. #define memcpy(d, s, n) bcopy ((s), (d), (n))
  119. #endif /* not HAVE_MEMCPY */
  120. #ifndef HAVE_MEMMOVE
  121. #define memmove(d, s, n) bcopy ((s), (d), (n))
  122. #endif /* not HAVE_MEMMOVE */
  123. #ifndef HAVE_STRCASECMP /* will use local version */
  124. extern __C_LINKAGE int strcasecmp (const char *s1, const char *s2);
  125. #endif /* not HAVE_STRCASECMP */
  126. /* supply declarations for more functions declared in stdlib.h */
  127. #ifdef HAVE_MALLOC_H
  128. #include <malloc.h>
  129. #else
  130. extern __C_LINKAGE void * malloc (size_t size);
  131. extern __C_LINKAGE void * realloc (void * ptr, size_t size);
  132. extern __C_LINKAGE void * calloc (size_t nmemb, size_t size);
  133. extern __C_LINKAGE void free (void * ptr);
  134. #endif /* not HAVE_MALLOC_H */
  135. #endif /* not STDC_HEADERS */
  136. /**************************************************************************/
  137. /* Support the `bool' datatype, which our code uses extensively. */
  138. /**************************************************************************/
  139. #ifndef __cplusplus
  140. #ifndef HAVE_BOOL_IN_CC
  141. #ifdef __STDC__
  142. typedef enum { false = 0, true = 1 } bool;
  143. #else /* not __STDC__, do things the old-fashioned way */
  144. typedef int bool;
  145. #define false 0
  146. #define true 1
  147. #endif
  148. #endif /* not HAVE_BOOL_IN_CC */
  149. #endif /* not __cplusplus */
  150. /**************************************************************************/
  151. /* Define numerical constants (unofficial, so may not be in math.h). */
  152. /**************************************************************************/
  153. #ifndef M_PI
  154. #define M_PI 3.14159265358979323846264
  155. #endif
  156. #ifndef M_PI_2
  157. #define M_PI_2 1.57079632679489661923
  158. #endif
  159. #ifndef M_SQRT2
  160. #define M_SQRT2 1.41421356237309504880
  161. #endif
  162. #ifndef M_SQRT3
  163. #define M_SQRT3 1.73205080756887719
  164. #endif
  165. /**************************************************************************/
  166. /* Define misc. math macros (in GCC, can be evaluated more rapidly). */
  167. /**************************************************************************/
  168. #ifdef __GNUC__
  169. #define DMAX(a,b) ({double _a = (a), _b = (b); _a > _b ? _a : _b; })
  170. #define DMIN(a,b) ({double _a = (a), _b = (b); _a < _b ? _a : _b; })
  171. #define IMAX(a,b) ({int _a = (a), _b = (b); _a > _b ? _a : _b; })
  172. #define IMIN(a,b) ({int _a = (a), _b = (b); _a < _b ? _a : _b; })
  173. #define UMAX(a,b) ({unsigned int _a = (a), _b = (b); _a > _b ? _a : _b; })
  174. #define UMIN(a,b) ({unsigned int _a = (a), _b = (b); _a < _b ? _a : _b; })
  175. #define IROUND(x) ({double _x = (x); int _i; \
  176. if (_x >= INT_MAX) _i = INT_MAX; \
  177. else if (_x <= -(INT_MAX)) _i = -(INT_MAX); \
  178. else _i = (_x > 0.0 ? (int)(_x + 0.5) : (int)(_x - 0.5)); \
  179. _i;})
  180. #define FROUND(x) ({double _x = (x); float _f; \
  181. if (_x >= FLT_MAX) _f = FLT_MAX; \
  182. else if (_x <= -(FLT_MAX)) _f = -(FLT_MAX); \
  183. else _f = _x; \
  184. _f;})
  185. #define FABS(x) ((x) >= 0.0 ? (x) : -(x))
  186. #define ICEIL(x) ({double _x = (x); int _i = (int)_x; \
  187. ((_x == _i) || (_x < 0.0)) ? _i : _i + 1;})
  188. #define IFLOOR(x) ({double _x = (x); int _i = (int)_x; \
  189. ((_x == _i) || (_x > 0.0)) ? _i : _i - 1;})
  190. #else
  191. #define DMAX(a,b) ((a) > (b) ? (a) : (b))
  192. #define DMIN(a,b) ((a) < (b) ? (a) : (b))
  193. #define IMAX(a,b) ((a) > (b) ? (a) : (b))
  194. #define IMIN(a,b) ((a) < (b) ? (a) : (b))
  195. #define UMAX(a,b) ((a) > (b) ? (a) : (b))
  196. #define UMIN(a,b) ((a) < (b) ? (a) : (b))
  197. #define IROUND(x) ((int) ((x) > 0 ? (x) + 0.5 : (x) - 0.5))
  198. #define FROUND(x) ((float)(x))
  199. #define FABS(x) ((x) >= 0.0 ? (x) : -(x))
  200. #define ICEIL(x) ((int)ceil(x))
  201. #define IFLOOR(x) ((int)floor(x))
  202. #endif