gcc_ia32_common.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2005-2013 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_machine_gcc_ia32_common_H
  24. #define __TBB_machine_gcc_ia32_common_H
  25. //TODO: Add a higher-level function, e.g. tbb::interal::log2(), into tbb_stddef.h, which
  26. //uses __TBB_Log2 and contains the assert and remove the assert from here and all other
  27. //platform-specific headers.
  28. //TODO: Check if use of gcc intrinsic gives a better chance for cross call optimizations
  29. static inline intptr_t __TBB_machine_lg( uintptr_t x ) {
  30. __TBB_ASSERT(x, "__TBB_Log2(0) undefined");
  31. uintptr_t j;
  32. __asm__ ("bsr %1,%0" : "=r"(j) : "r"(x));
  33. return j;
  34. }
  35. #define __TBB_Log2(V) __TBB_machine_lg(V)
  36. #ifndef __TBB_Pause
  37. //TODO: check if raising a ratio of pause instructions to loop control instructions
  38. //(via e.g. loop unrolling) gives any benefit for HT. E.g, the current implementation
  39. //does about 2 CPU-consuming instructions for every pause instruction. Perhaps for
  40. //high pause counts it should use an unrolled loop to raise the ratio, and thus free
  41. //up more integer cycles for the other hyperthread. On the other hand, if the loop is
  42. //unrolled too far, it won't fit in the core's loop cache, and thus take away
  43. //instruction decode slots from the other hyperthread.
  44. //TODO: check if use of gcc __builtin_ia32_pause intrinsic gives a "some how" better performing code
  45. static inline void __TBB_machine_pause( int32_t delay ) {
  46. for (int32_t i = 0; i < delay; i++) {
  47. __asm__ __volatile__("pause;");
  48. }
  49. return;
  50. }
  51. #define __TBB_Pause(V) __TBB_machine_pause(V)
  52. #endif /* !__TBB_Pause */
  53. // API to retrieve/update FPU control setting
  54. #ifndef __TBB_CPU_CTL_ENV_PRESENT
  55. #define __TBB_CPU_CTL_ENV_PRESENT 1
  56. struct __TBB_cpu_ctl_env_t {
  57. int mxcsr;
  58. short x87cw;
  59. };
  60. inline void __TBB_get_cpu_ctl_env ( __TBB_cpu_ctl_env_t* ctl ) {
  61. #if __TBB_ICC_12_0_INL_ASM_FSTCW_BROKEN
  62. __TBB_cpu_ctl_env_t loc_ctl;
  63. __asm__ __volatile__ (
  64. "stmxcsr %0\n\t"
  65. "fstcw %1"
  66. : "=m"(loc_ctl.mxcsr), "=m"(loc_ctl.x87cw)
  67. );
  68. *ctl = loc_ctl;
  69. #else
  70. __asm__ __volatile__ (
  71. "stmxcsr %0\n\t"
  72. "fstcw %1"
  73. : "=m"(ctl->mxcsr), "=m"(ctl->x87cw)
  74. );
  75. #endif
  76. }
  77. inline void __TBB_set_cpu_ctl_env ( const __TBB_cpu_ctl_env_t* ctl ) {
  78. __asm__ __volatile__ (
  79. "ldmxcsr %0\n\t"
  80. "fldcw %1"
  81. : : "m"(ctl->mxcsr), "m"(ctl->x87cw)
  82. );
  83. }
  84. #endif /* !__TBB_CPU_CTL_ENV_PRESENT */
  85. #endif /* __TBB_machine_gcc_ia32_common_H */