compiler_opt.h 2.8 KB

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