macros.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Macros Header - Various useful macro definitions
  5. This source is placed in the Public Domain, do with it what you will
  6. It was originally written by Brian C. White.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Private header
  10. #ifndef MACROS_H
  11. #define MACROS_H
  12. // MIN_VAL(SINT16) will return -0x8000 and MAX_VAL(SINT16) = 0x7FFF
  13. #define MIN_VAL(t) (((t)(-1) > 0) ? (t)( 0) : (t)(((1L<<(sizeof(t)*8-1)) )))
  14. #define MAX_VAL(t) (((t)(-1) > 0) ? (t)(-1) : (t)(((1L<<(sizeof(t)*8-1))-1)))
  15. // Min/Max functions
  16. #if !defined(MIN)
  17. #if defined(__HIGHC__)
  18. #define MIN(x,y) _min(x,y)
  19. #define MAX(x,y) _max(x,y)
  20. #endif
  21. // GNU C++ has a min/max operator <coolio>
  22. #if defined(__GNUG__)
  23. #define MIN(A,B) ((A) <? (B))
  24. #define MAX(A,B) ((A) >? (B))
  25. #endif
  26. /* Templates tend to mess up existing code that uses min/max because of the
  27. strict matching requirements */
  28. #if !defined(MIN)
  29. #define MIN(A,B) ((A) < (B)?(A):(B))
  30. #define MAX(A,B) ((A) > (B)?(A):(B))
  31. #endif
  32. #endif
  33. /* Bound functions, bound will return the value b within the limits a-c
  34. bounv will change b so that it is within the limits of a-c. */
  35. #define _bound(a,b,c) MIN(c,MAX(b,a))
  36. #define _boundv(a,b,c) b = _bound(a,b,c)
  37. #define ABS(a) (((a) < (0)) ?-(a) : (a))
  38. /* Useful count macro, use on an array of things and it will return the
  39. number of items in the array */
  40. #define _count(a) (sizeof(a)/sizeof(a[0]))
  41. // Flag Macros
  42. #define FLAG(f) (1L << (f))
  43. #define SETFLAG(v,f) ((v) |= FLAG(f))
  44. #define CLRFLAG(v,f) ((v) &=~FLAG(f))
  45. #define CHKFLAG(v,f) ((v) & FLAG(f) ? true : false)
  46. // some nice optional GNUC features
  47. #if __GNUC__ >= 3
  48. #define __must_check __attribute__ ((warn_unused_result))
  49. #define __deprecated __attribute__ ((deprecated))
  50. #define __attrib_const __attribute__ ((__const__))
  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 __attrib_const /* no const attribute */
  59. #define likely(x) (x)
  60. #define unlikely(x) (x)
  61. #endif
  62. // cold functions are unlikely() to be called
  63. #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4
  64. #define __cold __attribute__ ((__cold__))
  65. #define __hot __attribute__ ((__hot__))
  66. #else
  67. #define __cold /* no cold marker */
  68. #define __hot /* no hot marker */
  69. #endif
  70. #ifdef __GNUG__
  71. // Methods have a hidden this parameter that is visible to this attribute
  72. #define __like_printf(n) __attribute__((format(printf, n, n + 1)))
  73. #else
  74. #define __like_printf(n) /* no like-printf */
  75. #endif
  76. #endif