| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // defs.hfa -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Thierry Delisle | 
|---|
| 10 | // Created On       : Thu Nov  9 13:24:10 2017 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue Jan 28 22:38:27 2020 | 
|---|
| 13 | // Update Count     : 9 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <stdbool.h> | 
|---|
| 19 | #include <stddef.h> | 
|---|
| 20 | #include <stdint.h> | 
|---|
| 21 |  | 
|---|
| 22 | #define likely(x)   __builtin_expect(!!(x), 1) | 
|---|
| 23 | #define unlikely(x) __builtin_expect(!!(x), 0) | 
|---|
| 24 | #define thread_local _Thread_local | 
|---|
| 25 |  | 
|---|
| 26 | typedef void (*fptr_t)(); | 
|---|
| 27 | typedef int_fast16_t __lock_size_t; | 
|---|
| 28 |  | 
|---|
| 29 | #ifdef __cforall | 
|---|
| 30 | #define __cfa_anonymous_object(x) inline struct x | 
|---|
| 31 | #else | 
|---|
| 32 | #define __cfa_anonymous_object(x) x __cfa_anonymous_object | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #ifdef __cforall | 
|---|
| 36 | void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )); | 
|---|
| 37 | void abort( bool signalAbort, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )); | 
|---|
| 38 | extern "C" { | 
|---|
| 39 | #endif | 
|---|
| 40 | void __cabi_abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )); | 
|---|
| 41 | #ifdef __cforall | 
|---|
| 42 | } | 
|---|
| 43 | #endif | 
|---|
| 44 |  | 
|---|
| 45 | #if defined(__cforall_thread__) | 
|---|
| 46 | #define OPTIONAL_THREAD | 
|---|
| 47 | #else | 
|---|
| 48 | #define OPTIONAL_THREAD __attribute__((weak)) | 
|---|
| 49 | #endif | 
|---|
| 50 |  | 
|---|
| 51 | static inline long long rdtscl(void) { | 
|---|
| 52 | unsigned int lo, hi; | 
|---|
| 53 | __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); | 
|---|
| 54 | return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | // #define __CFA_NO_BIT_TEST_AND_SET__ | 
|---|
| 58 |  | 
|---|
| 59 | #if defined( __i386 ) | 
|---|
| 60 | static inline bool __atomic_bts(volatile unsigned long int * target, unsigned long int bit ) { | 
|---|
| 61 | #if defined(__CFA_NO_BIT_TEST_AND_SET__) | 
|---|
| 62 | unsigned long int mask = 1ul << bit; | 
|---|
| 63 | unsigned long int ret = __atomic_fetch_or(target, mask, (int)__ATOMIC_RELAXED); | 
|---|
| 64 | return (ret & mask) != 0; | 
|---|
| 65 | #else | 
|---|
| 66 | int result = 0; | 
|---|
| 67 | asm volatile( | 
|---|
| 68 | "LOCK btsl %[bit], %[target]\n\t" | 
|---|
| 69 | : "=@ccc" (result) | 
|---|
| 70 | : [target] "m" (*target), [bit] "r" (bit) | 
|---|
| 71 | ); | 
|---|
| 72 | return result != 0; | 
|---|
| 73 | #endif | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | static inline bool __atomic_btr(volatile unsigned long int * target, unsigned long int bit ) { | 
|---|
| 77 | #if defined(__CFA_NO_BIT_TEST_AND_SET__) | 
|---|
| 78 | unsigned long int mask = 1ul << bit; | 
|---|
| 79 | unsigned long int ret = __atomic_fetch_and(target, ~mask, (int)__ATOMIC_RELAXED); | 
|---|
| 80 | return (ret & mask) != 0; | 
|---|
| 81 | #else | 
|---|
| 82 | int result = 0; | 
|---|
| 83 | asm volatile( | 
|---|
| 84 | "LOCK btrl %[bit], %[target]\n\t" | 
|---|
| 85 | :"=@ccc" (result) | 
|---|
| 86 | : [target] "m" (*target), [bit] "r" (bit) | 
|---|
| 87 | ); | 
|---|
| 88 | return result != 0; | 
|---|
| 89 | #endif | 
|---|
| 90 | } | 
|---|
| 91 | #elif defined( __x86_64 ) | 
|---|
| 92 | static inline bool __atomic_bts(volatile unsigned long long int * target, unsigned long long int bit ) { | 
|---|
| 93 | #if defined(__CFA_NO_BIT_TEST_AND_SET__) | 
|---|
| 94 | unsigned long long int mask = 1ul << bit; | 
|---|
| 95 | unsigned long long int ret = __atomic_fetch_or(target, mask, (int)__ATOMIC_RELAXED); | 
|---|
| 96 | return (ret & mask) != 0; | 
|---|
| 97 | #else | 
|---|
| 98 | int result = 0; | 
|---|
| 99 | asm volatile( | 
|---|
| 100 | "LOCK btsq %[bit], %[target]\n\t" | 
|---|
| 101 | : "=@ccc" (result) | 
|---|
| 102 | : [target] "m" (*target), [bit] "r" (bit) | 
|---|
| 103 | ); | 
|---|
| 104 | return result != 0; | 
|---|
| 105 | #endif | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | static inline bool __atomic_btr(volatile unsigned long long int * target, unsigned long long int bit ) { | 
|---|
| 109 | #if defined(__CFA_NO_BIT_TEST_AND_SET__) | 
|---|
| 110 | unsigned long long int mask = 1ul << bit; | 
|---|
| 111 | unsigned long long int ret = __atomic_fetch_and(target, ~mask, (int)__ATOMIC_RELAXED); | 
|---|
| 112 | return (ret & mask) != 0; | 
|---|
| 113 | #else | 
|---|
| 114 | int result = 0; | 
|---|
| 115 | asm volatile( | 
|---|
| 116 | "LOCK btrq %[bit], %[target]\n\t" | 
|---|
| 117 | :"=@ccc" (result) | 
|---|
| 118 | : [target] "m" (*target), [bit] "r" (bit) | 
|---|
| 119 | ); | 
|---|
| 120 | return result != 0; | 
|---|
| 121 | #endif | 
|---|
| 122 | } | 
|---|
| 123 | #elif defined( __ARM_ARCH ) | 
|---|
| 124 | #error __atomic_bts and __atomic_btr not implemented for arm | 
|---|
| 125 | #else | 
|---|
| 126 | #error uknown hardware architecture | 
|---|
| 127 | #endif | 
|---|