system.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: system.h,v 1.3 1999/12/10 23:40:29 jgg Exp $
  4. /* ######################################################################
  5. System Header - Usefull private definitions
  6. This source is placed in the Public Domain, do with it what you will
  7. It was originally written by Brian C. White.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Private header
  11. #ifndef SYSTEM_H
  12. #define SYSTEM_H
  13. // MIN_VAL(SINT16) will return -0x8000 and MAX_VAL(SINT16) = 0x7FFF
  14. #define MIN_VAL(t) (((t)(-1) > 0) ? (t)( 0) : (t)(((1L<<(sizeof(t)*8-1)) )))
  15. #define MAX_VAL(t) (((t)(-1) > 0) ? (t)(-1) : (t)(((1L<<(sizeof(t)*8-1))-1)))
  16. // Min/Max functions
  17. #if !defined(MIN)
  18. #if defined(__HIGHC__)
  19. #define MIN(x,y) _min(x,y)
  20. #define MAX(x,y) _max(x,y)
  21. #endif
  22. // GNU C++ has a min/max operator <coolio>
  23. #if defined(__GNUG__)
  24. #define MIN(A,B) ((A) <? (B))
  25. #define MAX(A,B) ((A) >? (B))
  26. #endif
  27. /* Templates tend to mess up existing code that uses min/max because of the
  28. strict matching requirements */
  29. #if !defined(MIN)
  30. #define MIN(A,B) ((A) < (B)?(A):(B))
  31. #define MAX(A,B) ((A) > (B)?(A):(B))
  32. #endif
  33. #endif
  34. /* Bound functions, bound will return the value b within the limits a-c
  35. bounv will change b so that it is within the limits of a-c. */
  36. #define _bound(a,b,c) MIN(c,MAX(b,a))
  37. #define _boundv(a,b,c) b = _bound(a,b,c)
  38. #define ABS(a) (((a) < (0)) ?-(a) : (a))
  39. /* Usefull count macro, use on an array of things and it will return the
  40. number of items in the array */
  41. #define _count(a) (sizeof(a)/sizeof(a[0]))
  42. // Flag Macros
  43. #define FLAG(f) (1L << (f))
  44. #define SETFLAG(v,f) ((v) |= FLAG(f))
  45. #define CLRFLAG(v,f) ((v) &=~FLAG(f))
  46. #define CHKFLAG(v,f) ((v) & FLAG(f) ? true : false)
  47. // some nice optional GNUC features
  48. #if __GNUC__ >= 3
  49. #define __must_check __attribute__ ((warn_unused_result))
  50. #define __deprecated __attribute__ ((deprecated))
  51. /* likely() and unlikely() can be used to mark boolean expressions
  52. as (not) likely true which will help the compiler to optimise */
  53. #define likely(x) __builtin_expect (!!(x), 1)
  54. #define unlikely(x) __builtin_expect (!!(x), 0)
  55. #else
  56. #define __must_check /* no warn_unused_result */
  57. #define __deprecated /* no deprecated */
  58. #define likely(x) (x)
  59. #define unlikely(x) (x)
  60. #endif
  61. // cold functions are unlikely() to be called
  62. #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4
  63. #define __cold __attribute__ ((__cold__))
  64. #else
  65. #define __cold /* no cold marker */
  66. #endif
  67. #endif