PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/cilkrtssuspend/runtime/config/x86/os-unix-sysdep.c

https://gitlab.com/wustl-pctg-pub/porridge
C | 155 lines | 75 code | 9 blank | 71 comment | 11 complexity | 112609276568d0609b5a74a2e845ef56 MD5 | raw file
  1. /* os-unix-sysdep.c -*-C-*-
  2. *
  3. *************************************************************************
  4. *
  5. * Copyright (C) 2009-2015, Intel Corporation
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name of Intel Corporation nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  32. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * *********************************************************************
  36. *
  37. * PLEASE NOTE: This file is a downstream copy of a file mainitained in
  38. * a repository at cilkplus.org. Changes made to this file that are not
  39. * submitted through the contribution process detailed at
  40. * http://www.cilkplus.org/submit-cilk-contribution will be lost the next
  41. * time that a new version is released. Changes only submitted to the
  42. * GNU compiler collection or posted to the git repository at
  43. * https://bitbucket.org/intelcilkplusruntime/itnel-cilk-runtime.git are
  44. * not tracked.
  45. *
  46. * We welcome your contributions to this open source project. Thank you
  47. * for your assistance in helping us improve Cilk Plus.
  48. *************************************************************************
  49. *
  50. * This file contains system-specific code for Unix systems
  51. */
  52. #include "os.h"
  53. #include "sysdep.h"
  54. #include <internal/abi.h>
  55. // On x86 processors (but not MIC processors), the compiler generated code to
  56. // save the FP state (rounding mode and the like) before calling setjmp. We
  57. // will need to restore that state when we resume.
  58. #ifndef __MIC__
  59. # if defined(__i386__) || defined(__x86_64)
  60. # define RESTORE_X86_FP_STATE
  61. # endif // defined(__i386__) || defined(__x86_64)
  62. #endif // __MIC__
  63. /* timer support */
  64. COMMON_SYSDEP unsigned long long __cilkrts_getticks(void)
  65. {
  66. #if defined __i386__ || defined __x86_64
  67. unsigned a, d;
  68. __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
  69. return ((unsigned long long)a) | (((unsigned long long)d) << 32);
  70. #else
  71. # warning "unimplemented cycle counter"
  72. return 0;
  73. #endif
  74. }
  75. COMMON_SYSDEP void __cilkrts_short_pause(void)
  76. {
  77. #if __ICC >= 1110
  78. # if __MIC__ || __MIC2__
  79. _mm_delay_32(16); // stall for 16 cycles
  80. # else
  81. _mm_pause();
  82. # endif
  83. #elif defined __i386__ || defined __x86_64
  84. __asm__("pause");
  85. #else
  86. # warning __cilkrts_short_pause empty
  87. #endif
  88. }
  89. COMMON_SYSDEP int __cilkrts_xchg(volatile int *ptr, int x)
  90. {
  91. #if defined __i386__ || defined __x86_64
  92. /* asm statement here works around icc bugs */
  93. __asm__("xchgl %0,%a1" :"=r" (x) : "r" (ptr), "0" (x) :"memory");
  94. #else
  95. x = __sync_lock_test_and_set(ptr, x);
  96. #endif
  97. return x;
  98. }
  99. /*
  100. * The Intel compiler distribution assumes newer CPUs and doesn't yet support
  101. * the __builtin_cpu_supports intrinsic added by GCC 4.8, so just return 1 in
  102. * that environment.
  103. *
  104. * This declaration should generate an error when the Intel compiler adds
  105. * supprt for the intrinsic.
  106. */
  107. #if defined(__INTEL_COMPILER) || defined(__clang__)
  108. static inline int __builtin_cpu_supports(const char *feature)
  109. {
  110. return 1;
  111. }
  112. #endif
  113. /*
  114. * Restore the floating point state that is stored in a stack frame at each
  115. * spawn. This should be called each time a frame is resumed.
  116. *
  117. * Only valid for IA32 and Intel64 processors.
  118. */
  119. void restore_x86_fp_state (__cilkrts_stack_frame *sf) {
  120. #ifdef RESTORE_X86_FP_STATE
  121. if (__builtin_cpu_supports("sse"))
  122. {
  123. __asm__ ("ldmxcsr %0"
  124. :
  125. : "m" (sf->mxcsr));
  126. }
  127. __asm__ ("fnclex\n\t"
  128. "fldcw %0"
  129. :
  130. : "m" (sf->fpcsr));
  131. #endif
  132. }
  133. void sysdep_save_fp_ctrl_state(__cilkrts_stack_frame *sf)
  134. {
  135. // If we're not going to restore, don't bother saving it
  136. #ifdef RESTORE_X86_FP_STATE
  137. if (CILK_FRAME_VERSION_VALUE(sf->flags) >= 1)
  138. {
  139. if (__builtin_cpu_supports("sse"))
  140. {
  141. __asm__ ("stmxcsr %0" : "=m" (sf->mxcsr));
  142. }
  143. __asm__ ("fnstcw %0" : "=m" (sf->fpcsr));
  144. }
  145. #endif
  146. }