1 /* os-unix-sysdep.c -*-C-*-
3 *************************************************************************
6 * Copyright (C) 2009-2013, Intel Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
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
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.
50 # if defined(__i386__) || defined(__x86_64)
51 # define RESTORE_X86_FP_STATE
52 # endif // defined(__i386__) || defined(__x86_64)
56 COMMON_SYSDEP
unsigned long long __cilkrts_getticks(void)
58 #if defined __i386__ || defined __x86_64
60 __asm__
volatile("rdtsc" : "=a" (a
), "=d" (d
));
61 return ((unsigned long long)a
) | (((unsigned long long)d
) << 32);
63 # warning "unimplemented cycle counter"
68 COMMON_SYSDEP
void __cilkrts_short_pause(void)
71 # if __MIC__ || __MIC2__
72 _mm_delay_32(16); // stall for 16 cycles
76 #elif defined __i386__ || defined __x86_64
79 # warning __cilkrts_short_pause empty
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");
89 x
= __sync_lock_test_and_set(ptr
, 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"
107 : "m" (sf
->mxcsr
), "m" (sf
->fpcsr
));
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
));