Reverting merge from trunk
[official-gcc.git] / libcilkrts / runtime / config / x86 / os-unix-sysdep.c
blob881bc3f4283e7eaee58dc055ce8ca61b7e1ca77f
1 /* os-unix-sysdep.c -*-C-*-
3 *************************************************************************
5 * @copyright
6 * Copyright (C) 2009-2013, Intel Corporation
7 * All rights reserved.
8 *
9 * @copyright
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 * * Neither the name of Intel Corporation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * @copyright
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
32 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 *************************************************************************
39 * This file contains system-specific code for Unix systems
42 #include "os.h"
43 #include "sysdep.h"
44 #include <internal/abi.h>
46 // On x86 processors (but not MIC processors), the compiler generated code to
47 // save the FP state (rounding mode and the like) before calling setjmp. We
48 // will need to restore that state when we resume.
49 #ifndef __MIC__
50 # if defined(__i386__) || defined(__x86_64)
51 # define RESTORE_X86_FP_STATE
52 # endif // defined(__i386__) || defined(__x86_64)
53 #endif // __MIC__
55 /* timer support */
56 COMMON_SYSDEP unsigned long long __cilkrts_getticks(void)
58 #if defined __i386__ || defined __x86_64
59 unsigned a, d;
60 __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
61 return ((unsigned long long)a) | (((unsigned long long)d) << 32);
62 #else
63 # warning "unimplemented cycle counter"
64 return 0;
65 #endif
68 COMMON_SYSDEP void __cilkrts_short_pause(void)
70 #if __ICC >= 1110
71 # if __MIC__ || __MIC2__
72 _mm_delay_32(16); // stall for 16 cycles
73 # else
74 _mm_pause();
75 # endif
76 #elif defined __i386__ || defined __x86_64
77 __asm__("pause");
78 #else
79 # warning __cilkrts_short_pause empty
80 #endif
83 COMMON_SYSDEP int __cilkrts_xchg(volatile int *ptr, int x)
85 #if defined __i386__ || defined __x86_64
86 /* asm statement here works around icc bugs */
87 __asm__("xchgl %0,%a1" :"=r" (x) : "r" (ptr), "0" (x) :"memory");
88 #else
89 x = __sync_lock_test_and_set(ptr, x);
90 #endif
91 return x;
96 * Restore the floating point state that is stored in a stack frame at each
97 * spawn. This should be called each time a frame is resumed.
99 * Only valid for IA32 and Intel64 processors.
101 void restore_x86_fp_state (__cilkrts_stack_frame *sf) {
102 #ifdef RESTORE_X86_FP_STATE
103 __asm__ ( "ldmxcsr %0\n\t"
104 "fnclex\n\t"
105 "fldcw %1"
107 : "m" (sf->mxcsr), "m" (sf->fpcsr));
108 #endif
112 void sysdep_save_fp_ctrl_state(__cilkrts_stack_frame *sf)
114 // If we're not going to restore, don't bother saving it
115 #ifdef RESTORE_X86_FP_STATE
116 if (CILK_FRAME_VERSION_VALUE(sf->flags) >= 1)
118 __asm__ ("stmxcsr %0" : "=m" (sf->mxcsr));
119 __asm__ ("fnstsw %0" : "=m" (sf->fpcsr));
121 #endif