sys-defines.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. #ifndef _SYS_DEFINES_H_
  16. #define _SYS_DEFINES_H_ 1
  17. #include <config.h> /* built by autoconf */
  18. /**********************************************************************/
  19. /* SUPPORT C++. */
  20. /**********************************************************************/
  21. /* Support declarations of C linkage in C++, for functions not declared in
  22. C headers the way they should be. */
  23. #ifdef __cplusplus
  24. # define __C_LINKAGE "C"
  25. #else
  26. # define __C_LINKAGE /* empty */
  27. #endif
  28. /* config.h, built by `configure', includes a definition of RETSIGTYPE, the
  29. return value for signal(). But `configure' may get it wrong in C++. */
  30. #ifdef __cplusplus
  31. #ifdef RETSIGTYPE
  32. #undef RETSIGTYPE
  33. #endif
  34. #define RETSIGTYPE void
  35. #endif
  36. /**********************************************************************/
  37. /* Include all the C headers we'll need. Because many platforms lack one
  38. or more standard headers or function declarations, there are numerous
  39. tests and substitutions here. */
  40. /**********************************************************************/
  41. /**********************************************************************/
  42. /* If compiling libplot/libplotter, add support for multithreading,
  43. provided that libc includes pthread functions and pthread.h is present.
  44. (This comes first, because defining _REENTRANT may alter system header
  45. files.) */
  46. /**********************************************************************/
  47. #ifdef LIBPLOT
  48. #ifdef PTHREAD_SUPPORT
  49. #ifdef HAVE_PTHREAD_H
  50. #define _REENTRANT
  51. #include <pthread.h>
  52. #endif
  53. #endif
  54. #endif
  55. /**********************************************************************/
  56. /* INCLUDE stdio.h, ctype.h, errno.h. (SUBSTITUTE AS NECESSARY.) */
  57. /**********************************************************************/
  58. #ifdef __cplusplus
  59. #include <cstdio>
  60. #include <cctype> /* why is this needed? */
  61. #include <cerrno>
  62. #else /* not __cplusplus */
  63. #include <stdio.h>
  64. #include <ctype.h> /* why is this needed? */
  65. #include <errno.h>
  66. #endif /* not __cplusplus */
  67. /***************************************************************************/
  68. /* INCLUDE math.h, float.h, limits.h. (SUBSTITUTE AS NECESSARY.) */
  69. /***************************************************************************/
  70. #ifdef __DJGPP__
  71. /* for DJGPP math.h, must specify that -lm will be used;
  72. thanks mdruiter@cs.vu.nl */
  73. #define _USE_LIBM_MATH_H
  74. #endif
  75. /* Include math.h, and whichever other math-related header files we have */
  76. #ifdef __cplusplus
  77. #include <cmath>
  78. #include <cfloat>
  79. #include <climits>
  80. #else /* not __cplusplus */
  81. #include <math.h>
  82. #ifdef HAVE_FLOAT_H
  83. #include <float.h> /* for DBL_MAX, FLT_MAX */
  84. #endif
  85. #ifdef HAVE_LIMITS_H
  86. #include <limits.h> /* for INT_MAX */
  87. #endif
  88. #ifdef HAVE_VALUES_H
  89. #include <values.h> /* for MAXDOUBLE, MAXFLOAT, MAXINT (backups) */
  90. #endif
  91. #endif /* not __cplusplus */
  92. /* Bounds on integer datatypes (should be in limits.h, but may not be). */
  93. #ifndef UINT_MAX
  94. #ifdef __STDC__
  95. #define UINT_MAX ((unsigned int)(~(0U)))
  96. #else
  97. #define UINT_MAX ((unsigned int)(~((unsigned int)0)))
  98. #endif
  99. #endif /* not UINT_MAX */
  100. #ifndef INT_MAX
  101. #ifdef MAXINT
  102. #define INT_MAX MAXINT
  103. #else
  104. #define INT_MAX ((int)(~(1U << (8 * (int)sizeof(int) - 1))))
  105. #endif
  106. #endif /* not INT_MAX */
  107. /* IBM's definition of INT_MAX is bizarre, in AIX 4.1 at least, and using
  108. IROUND() below will yield a warning message unless we repair it */
  109. #ifdef _AIX
  110. #ifdef __GNUC__
  111. #undef INT_MAX
  112. #define INT_MAX ((int)(~(1U << (8 * (int)sizeof(int) - 1))))
  113. #endif
  114. #endif
  115. /* Bounds on floating point datatypes (should be in float.h, but may not be).*/
  116. #ifndef DBL_MAX
  117. #ifdef MAXDOUBLE
  118. #define DBL_MAX MAXDOUBLE
  119. #else
  120. /* make a very conservative (Vax-like) assumption */
  121. #define DBL_MAX (1.701411834604692293e+38)
  122. #endif
  123. #endif /* not DBL_MAX */
  124. #ifndef FLT_MAX
  125. #ifdef MAXFLOAT
  126. #define FLT_MAX MAXFLOAT
  127. #else
  128. /* make a very conservative (Vax-like) assumption */
  129. #define FLT_MAX (1.7014117331926443e+38)
  130. #endif
  131. #endif /* not FLT_MAX */
  132. /**********************************************************************/
  133. /* INCLUDE stdlib.h, string.h. (SUBSTITUTE AS NECESSARY; if STDC_HEADERS
  134. is defined then they're both present, and stdarg.h and float.h too.)
  135. Note: on some systems, e.g., Darwin, stdio.h must be included before
  136. stdlib.h; which is why we included stdio.h above. */
  137. /**********************************************************************/
  138. #ifdef __cplusplus
  139. #include <cstdlib>
  140. #include <cstring>
  141. #else /* not __cplusplus */
  142. #ifdef STDC_HEADERS
  143. #include <stdlib.h> /* for getenv, atoi, atof, etc. */
  144. #include <string.h> /* for memcpy, memmove, strchr, malloc, etc. */
  145. #else /* not STDC_HEADERS, must do a LOT of declarations by hand */
  146. #ifdef HAVE_SYS_STDTYPES_H
  147. #include <sys/stdtypes.h> /* SunOS, at least, needs this for size_t */
  148. #endif
  149. /* supply declarations for functions declared in stdlib.h */
  150. extern __C_LINKAGE char *getenv (const char *name);
  151. extern __C_LINKAGE int atoi (const char *nptr);
  152. extern __C_LINKAGE double atof (const char *nptr);
  153. /* supply definitions in stdlib.h */
  154. #define EXIT_FAILURE 1 /* Failing exit status. */
  155. #define EXIT_SUCCESS 0 /* Successful exit status. */
  156. /* determine how to declare (or define) functions declared in string.h */
  157. #ifdef HAVE_STRCHR
  158. #ifdef HAVE_STRING_H
  159. #include <string.h>
  160. #else
  161. #ifdef HAVE_STRINGS_H
  162. #include <strings.h>
  163. #endif
  164. #endif
  165. #else /* don't have strchr, prefer strings.h */
  166. #ifdef HAVE_STRINGS_H
  167. #include <strings.h>
  168. #else
  169. #ifdef HAVE_STRING_H
  170. #include <string.h>
  171. #endif
  172. #endif
  173. #define strchr index
  174. #define strrchr rindex
  175. #endif /* not HAVE_STRCHR */
  176. #ifndef HAVE_MEMCPY
  177. #define memcpy(d, s, n) bcopy ((s), (d), (n))
  178. #endif /* not HAVE_MEMCPY */
  179. #ifndef HAVE_MEMMOVE
  180. #define memmove(d, s, n) bcopy ((s), (d), (n))
  181. #endif /* not HAVE_MEMMOVE */
  182. #ifndef HAVE_STRCASECMP /* will use local version */
  183. extern __C_LINKAGE int strcasecmp (const char *s1, const char *s2);
  184. #endif /* not HAVE_STRCASECMP */
  185. /* supply declarations for more functions declared in stdlib.h */
  186. #ifdef HAVE_MALLOC_H
  187. #include <malloc.h>
  188. #else
  189. extern __C_LINKAGE void * malloc (size_t size);
  190. extern __C_LINKAGE void * realloc (void * ptr, size_t size);
  191. extern __C_LINKAGE void * calloc (size_t nmemb, size_t size);
  192. extern __C_LINKAGE void free (void * ptr);
  193. #endif /* not HAVE_MALLOC_H */
  194. #endif /* not STDC_HEADERS */
  195. #endif /* not __cplusplus */
  196. /**************************************************************************/
  197. /* Define NULL (necessary on some noncompliant or very old platforms?) */
  198. /**************************************************************************/
  199. #ifndef NULL
  200. #define NULL 0
  201. #endif
  202. /**************************************************************************/
  203. /* Support the `bool' datatype, which is used widely in this package. */
  204. /**************************************************************************/
  205. #ifndef __cplusplus
  206. #ifdef __STDC__
  207. typedef enum { false = 0, true = 1 } bool;
  208. #else /* not __STDC__, do things the old-fashioned way */
  209. typedef int bool;
  210. #define false 0
  211. #define true 1
  212. #endif /* not __STDC__ */
  213. #endif /* not __cplusplus */
  214. /**************************************************************************/
  215. /* Define numerical constants (unofficial, so may not be in math.h). */
  216. /**************************************************************************/
  217. #ifndef M_PI
  218. #define M_PI 3.14159265358979323846264
  219. #endif
  220. #ifndef M_PI_2
  221. #define M_PI_2 1.57079632679489661923
  222. #endif
  223. #ifndef M_SQRT2
  224. #define M_SQRT2 1.41421356237309504880
  225. #endif
  226. #ifndef M_SQRT3
  227. #define M_SQRT3 1.73205080756887719
  228. #endif
  229. /**************************************************************************/
  230. /* Work around a longstanding botch (the gamma function supplied in some */
  231. /* versions of the math library). */
  232. /**************************************************************************/
  233. /* gamma() and lgamma() both compute the log of the gamma function. There
  234. are some ancient systems out there which do not have lgamma (the name
  235. was introduced after BSD 4.2), but which do have gamma. Also, systems
  236. that are merely very old (e.g. Apollos), and some modern systems, have
  237. lgamma but not gamma. Some systems, old and new (e.g. cygwin32) have
  238. neither. Also at least one vendor's gamma()/lgamma() is buggy, so we
  239. allow the installer to do -DNO_SYSTEM_GAMMA to prevent the use of vendor
  240. code. What a mess! */
  241. #ifdef _AIX
  242. #define NO_SYSTEM_GAMMA /* AIX gamma support in libm.a is buggy */
  243. #endif
  244. #ifdef NO_SYSTEM_GAMMA
  245. #define F_LGAMMA f_lgamma /* our own version, see ode/specfun.c */
  246. #else /* not NO_SYSTEM_GAMMA */
  247. #ifdef HAVE_LGAMMA
  248. #define F_LGAMMA lgamma
  249. #else
  250. #ifdef HAVE_GAMMA
  251. #define F_LGAMMA gamma
  252. #else
  253. #define F_LGAMMA f_lgamma
  254. #define NO_SYSTEM_GAMMA
  255. #endif
  256. #endif
  257. #endif /* not NO_SYSTEM_GAMMA */
  258. /**************************************************************************/
  259. /* Define misc. math macros (in GCC, can be evaluated more rapidly). */
  260. /**************************************************************************/
  261. #ifdef __GNUC__
  262. #define DMAX(a,b) ({double _a = (a), _b = (b); _a > _b ? _a : _b; })
  263. #define DMIN(a,b) ({double _a = (a), _b = (b); _a < _b ? _a : _b; })
  264. #define IMAX(a,b) ({int _a = (a), _b = (b); _a > _b ? _a : _b; })
  265. #define IMIN(a,b) ({int _a = (a), _b = (b); _a < _b ? _a : _b; })
  266. #define UMAX(a,b) ({unsigned int _a = (a), _b = (b); _a > _b ? _a : _b; })
  267. #define UMIN(a,b) ({unsigned int _a = (a), _b = (b); _a < _b ? _a : _b; })
  268. #define IROUND(x) ({double _x = (x); int _i; \
  269. if (_x >= INT_MAX) _i = INT_MAX; \
  270. else if (_x <= -(INT_MAX)) _i = -(INT_MAX); \
  271. else _i = (_x > 0.0 ? (int)(_x + 0.5) : (int)(_x - 0.5)); \
  272. _i;})
  273. #define FROUND(x) ({double _x = (x); float _f; \
  274. if (_x >= FLT_MAX) _f = FLT_MAX; \
  275. else if (_x <= -(FLT_MAX)) _f = -(FLT_MAX); \
  276. else _f = _x; \
  277. _f;})
  278. #define FABS(x) ((x) >= 0.0 ? (x) : -(x))
  279. #define ICEIL(x) ({double _x = (x); int _i = (int)_x; \
  280. ((_x == _i) || (_x < 0.0)) ? _i : _i + 1;})
  281. #define IFLOOR(x) ({double _x = (x); int _i = (int)_x; \
  282. ((_x == _i) || (_x > 0.0)) ? _i : _i - 1;})
  283. #else
  284. #define DMAX(a,b) ((a) > (b) ? (a) : (b))
  285. #define DMIN(a,b) ((a) < (b) ? (a) : (b))
  286. #define IMAX(a,b) ((a) > (b) ? (a) : (b))
  287. #define IMIN(a,b) ((a) < (b) ? (a) : (b))
  288. #define UMAX(a,b) ((a) > (b) ? (a) : (b))
  289. #define UMIN(a,b) ((a) < (b) ? (a) : (b))
  290. #define IROUND(x) ((int) ((x) > 0 ? (x) + 0.5 : (x) - 0.5))
  291. #define FROUND(x) ((float)(x))
  292. #define FABS(x) ((x) >= 0.0 ? (x) : -(x))
  293. #define ICEIL(x) ((int)ceil(x))
  294. #define IFLOOR(x) ((int)floor(x))
  295. #endif
  296. #endif /* not _SYS_DEFINES_H_ */