compiler_opt.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C) 2007 iptelorg GmbH
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*!
  17. * \file
  18. * \brief Kamailio core :: Compiler specific optimizations
  19. * \see \ref CompilerOptions
  20. * \auth Andrei
  21. *
  22. * \ingroup core
  23. * Module: \ref core
  24. *
  25. * \page CompilerOptions compiler specific optimizations:
  26. *
  27. \verbatim
  28. * - likely(expr) - branch predicition optimization - is more likely
  29. * that expr value will be 1 so optimize for this
  30. * case.
  31. * Example: if (likely(p!=NULL)) {... }
  32. * - unlikely(expr) - branch prediction optimization - is unlikely that
  33. * expr will be true, so optimize for this case
  34. * - prefetch(addr) - will prefetch addr. for reading
  35. * - prefetch_w(addr) - will prefetch addr. for writing
  36. * - prefetch_loc_r(addr, loc) - prefetch for reading, data at addr has
  37. * no temporal locality (loc==0), a short
  38. * degree of temporal locality (loc==1),
  39. * moderate (loc==2) or high (loc==3).
  40. * prefetch(addr) is equiv. to
  41. * prefetch_loc_r(addr, 3).
  42. * prefetch_loc_w(addr, loc) - like above but for writing.
  43. \endverbatim
  44. */
  45. #ifndef __compiler_opt_h
  46. #define __compiler_opt_h
  47. /* likely/unlikely */
  48. #if __GNUC__ >= 3
  49. #define likely(expr) __builtin_expect(!!(expr), 1)
  50. #define unlikely(expr) __builtin_expect(!!(expr), 0)
  51. #else /* __GNUC__ */
  52. /* #warning "No compiler optimizations supported try gcc 4.x" */
  53. #define likely(expr) (expr)
  54. #define unlikely(expr) (expr)
  55. #endif /* __GNUC__ */
  56. /* prefetch* */
  57. #if ( __GNUC__ > 3 ) || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 )
  58. #define prefetch(addr) __builtin_prefetch((addr))
  59. #define prefetch_w(addr) __builtin_prefetch((addr), 1)
  60. #define prefetch_loc_r(addr, loc) __builtin_prefetch((addr), 0, (loc))
  61. #define prefetch_loc_w(addr, loc) __builtin_prefetch((addr), 1, (loc))
  62. #else
  63. #define prefetch(addr)
  64. #define prefetch_w(addr)
  65. #define prefetch_loc_r(addr, loc)
  66. #define prefetch_loc_w(addr, loc)
  67. #endif /* __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) */
  68. #endif