nlib
Platform_ctr.h
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_PLATFORM_CTR_H_
4 #define INCLUDE_NN_NLIB_PLATFORM_CTR_H_
5 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_
6 # error do not include directly
7 #endif
8 
9 #ifdef NN_PLATFORM_CTR
10 
11 #define NLIB_HAS_STDHEADER_STDINT
12 #define NLIB_HAS_STDHEADER_INTTYPES
13 
14 #ifndef __USE_C99_MATH
15 # define __USE_C99_MATH
16 #endif
17 
18 #include <stdint.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define NLIB_ALWAYS_INLINE __forceinline
25 #define NLIB_NEVER_INLINE __declspec(noinline)
26 // Do not write like NLIB_(UN)LIKELY(a || b) or NLIB_(UN)LIKELY(a && b).
27 // Write like (NLIB_(UN)LIKELY(a) || NLIB_(UN)LIKELY(b)) instead.
28 // armcc optimizer may generate wrong binary.
29 #define NLIB_LIKELY(x) __builtin_expect(!!(x), 1)
30 #define NLIB_UNLIKELY(x) __builtin_expect(!!(x), 0)
31 #define NLIB_EXPECT(var, exp_value) __builtin_expect((var), (exp_value))
32 #define NLIB_CHECK_RESULT
33 #define NLIB_NORETURN __attribute__((noreturn))
34 #define NLIB_NONNULL __attribute__((nonnull))
35 #define NLIB_NONNULL_1 __attribute__((nonnull (1)))
36 #define NLIB_NONNULL_2 __attribute__((nonnull (2)))
37 #define NLIB_NONNULL_3 __attribute__((nonnull (3)))
38 #define NLIB_NONNULL_4 __attribute__((nonnull (4)))
39 #define NLIB_NONNULL_5 __attribute__((nonnull (5)))
40 #define NLIB_NONNULL_ENABLED
41 #define NLIB_ATTRIBUTE_MALLOC __attribute__((malloc))
42 #define NLIB_ATTRIBUTE_ALLOC_SIZE1(n)
43 #define NLIB_ATTRIBUTE_ALLOC_SIZE2(n0, n1)
44 #define NLIB_ATTRIBUTE_ALLOC_ALIGN(algn)
45 #define NLIB_ATTRIBUTE_ASSUME_ALIGNED(n)
46 #define NLIB_DEPRECATED
47 #define NLIB_DEPRECATED_MSG(msg)
48 #define NLIB_VIS_HIDDEN
49 #define NLIB_VIS_PUBLIC
50 #define NLIB_WEAKSYMBOL __attribute__((weak))
51 #define _Printf_format_string_
52 #define NLIB_LITTLE_ENDIAN
53 
54 void nlib_ctr_barrier(void);
55 #define NLIB_MEMORY_ORDER_RELEASE nlib_ctr_barrier()
56 #define NLIB_MEMORY_ORDER_ACQUIRE nlib_ctr_barrier()
57 #define NLIB_MEMORY_ORDER_ACQ_REL nlib_ctr_barrier()
58 
59 struct nlib_mutex_ {
60  unsigned int _[3]; // ::nn::os::CriticalSection
61 };
62 typedef struct nlib_mutex_ nlib_mutex;
63 #define NLIB_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
64 #define NLIB_RECURSIVE_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
65 #define NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
66 
67 typedef void* nlib_thread;
68 
69 struct nlib_semaphore_ {
70  unsigned int _[1]; // nn::os::Semaphore
71 };
72 typedef struct nlib_semaphore_ nlib_semaphore;
73 
74 struct nlib_cond_ {
75  unsigned int _; // nn::os::Event
76 };
77 typedef struct nlib_cond_ nlib_cond;
78 #define NLIB_COND_INITIALIZER {0}
79 
80 __declspec(noreturn) int MyNoreturn_(void);
81 #define NLIB_ASSUME(cond) (void)((cond) || MyNoreturn_())
82 
83 // nlib_clz64(INT64_MIN) -> 0, nlib_clz64(1) -> 63
84 static NLIB_ALWAYS_INLINE int nlib_clz64(uint64_t x) {
85  return x != 0 ? __builtin_clzll(x) : 64;
86 }
87 
88 // nlib_ctz64(INT64_MIN) -> 63, nlib_ctz64(1) -> 0
89 static NLIB_ALWAYS_INLINE int nlib_ctz64(uint64_t x) {
90  return 64 - nlib_clz64(~x & (x - 1));
91 }
92 
93 // nlib_clz(0x80000000) -> 0, nlib_clz(1) -> 31
94 static NLIB_ALWAYS_INLINE int nlib_clz(uint32_t x) {
95  return x != 0 ? __builtin_clz(x) : 32;
96 }
97 
98 // nlib_clz(0x80000000) -> 31, nlib_clz(1) -> 0
99 static NLIB_ALWAYS_INLINE int nlib_ctz(uint32_t x) {
100  return 32 - nlib_clz(~x & (x - 1));
101 }
102 
103 #define NLIB_ATOMIC_RELAXED (0)
104 #define NLIB_ATOMIC_ACQUIRE (1)
105 #define NLIB_ATOMIC_RELEASE (2)
106 #define NLIB_ATOMIC_ACQ_REL (3)
107 #define NLIB_ATOMIC_SEQ_CST (7)
108 
109 int32_t nlib_atomic_load32(const int32_t* ptr, int memorder);
110 void nlib_atomic_store32(int32_t* ptr, int32_t val, int memorder);
111 int32_t nlib_atomic_exchange32(int32_t* ptr, int32_t val, int memorder);
112 int nlib_atomic_compare_exchange32(int32_t* ptr, int32_t* expected,
113  int32_t desired, int weak,
114  int success_memorder, int failure_memorder);
115 int32_t nlib_atomic_add_fetch32(int32_t* ptr, int32_t val, int memorder);
116 int32_t nlib_atomic_sub_fetch32(int32_t* ptr, int32_t val, int memorder);
117 int32_t nlib_atomic_and_fetch32(int32_t* ptr, int32_t val, int memorder);
118 int32_t nlib_atomic_xor_fetch32(int32_t* ptr, int32_t val, int memorder);
119 int32_t nlib_atomic_or_fetch32(int32_t* ptr, int32_t val, int memorder);
120 int32_t nlib_atomic_fetch_add32(int32_t* ptr, int32_t val, int memorder);
121 int32_t nlib_atomic_fetch_sub32(int32_t* ptr, int32_t val, int memorder);
122 int32_t nlib_atomic_fetch_and32(int32_t* ptr, int32_t val, int memorder);
123 int32_t nlib_atomic_fetch_xor32(int32_t* ptr, int32_t val, int memorder);
124 int32_t nlib_atomic_fetch_or32(int32_t* ptr, int32_t val, int memorder);
125 
126 int64_t nlib_atomic_load64(const int64_t* ptr, int memorder);
127 void nlib_atomic_store64(int64_t* ptr, int64_t val, int memorder);
128 int64_t nlib_atomic_exchange64(int64_t* ptr, int64_t val, int memorder);
129 int nlib_atomic_compare_exchange64(int64_t* ptr, int64_t* expected,
130  int64_t desired, int weak,
131  int success_memorder, int failure_memorder);
132 int64_t nlib_atomic_add_fetch64(int64_t* ptr, int64_t val, int memorder);
133 int64_t nlib_atomic_sub_fetch64(int64_t* ptr, int64_t val, int memorder);
134 int64_t nlib_atomic_and_fetch64(int64_t* ptr, int64_t val, int memorder);
135 int64_t nlib_atomic_xor_fetch64(int64_t* ptr, int64_t val, int memorder);
136 int64_t nlib_atomic_or_fetch64(int64_t* ptr, int64_t val, int memorder);
137 int64_t nlib_atomic_fetch_add64(int64_t* ptr, int64_t val, int memorder);
138 int64_t nlib_atomic_fetch_sub64(int64_t* ptr, int64_t val, int memorder);
139 int64_t nlib_atomic_fetch_and64(int64_t* ptr, int64_t val, int memorder);
140 int64_t nlib_atomic_fetch_xor64(int64_t* ptr, int64_t val, int memorder);
141 int64_t nlib_atomic_fetch_or64(int64_t* ptr, int64_t val, int memorder);
142 
143 void* nlib_atomic_loadptr(void* const* ptr, int memorder);
144 void nlib_atomic_storeptr(void** ptr, void* val, int memorder);
145 int nlib_atomic_compare_exchangeptr(void** ptr, void** expected,
146  void* desired, int weak,
147  int success_memorder, int failure_memorder);
148 
149 static NLIB_ALWAYS_INLINE void nlib_atomic_thread_fence(int memorder) {
150  if (memorder != NLIB_ATOMIC_RELAXED)
151  nlib_ctr_barrier();
152 }
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #define NLIB_NOEXCEPT
159 
160 #endif
161 #endif // INCLUDE_NN_NLIB_PLATFORM_CTR_H_
int32_t nlib_atomic_xor_fetch32(int32_t *ptr, int32_t val, int memorder)
Calculates XOR of atomic values. Its behavior is similar to the one for __atomic_xor_fetch() of gcc...
int64_t nlib_atomic_fetch_and64(int64_t *ptr, int64_t val, int memorder)
Calculates AND of atomic values. Its behavior is similar to the one for __atomic_fetch_and() of gcc...
int nlib_atomic_compare_exchangeptr(void **ptr, void **expected, void *desired, int weak, int success_memorder, int failure_memorder)
Compares and swaps atomic values. Its behavior is similar to the one for __atomic_compare_exchange_n(...
int32_t nlib_atomic_load32(const int32_t *ptr, int memorder)
Loads a value in an atomic operation. Its behavior is similar to the one for __atomic_load_n() of gcc...
int64_t nlib_atomic_fetch_add64(int64_t *ptr, int64_t val, int memorder)
Adds atomic values. Its behavior is similar to the one for __atomic_fetch_add() of gcc...
sem_t nlib_semaphore
The type for a semaphore object.
int32_t nlib_atomic_or_fetch32(int32_t *ptr, int32_t val, int memorder)
Calculates OR of atomic values. Its behavior is similar to the one for __atomic_or_fetch() of gcc...
int64_t nlib_atomic_fetch_sub64(int64_t *ptr, int64_t val, int memorder)
Subtracts atomic values. Its behavior is similar to the one for __atomic_fetch_sub() of gcc...
int64_t nlib_atomic_and_fetch64(int64_t *ptr, int64_t val, int memorder)
Calculates AND of atomic values. Its behavior is similar to the one for __atomic_and_fetch() of gcc...
int nlib_atomic_compare_exchange64(int64_t *ptr, int64_t *expected, int64_t desired, int weak, int success_memorder, int failure_memorder)
Compares and swaps atomic values. Its behavior is similar to the one for __atomic_compare_exchange_n(...
int nlib_ctz(uint32_t x)
Returns the number of consecutive zero bits, with respect to the least significant bit (LSB)...
int64_t nlib_atomic_fetch_or64(int64_t *ptr, int64_t val, int memorder)
Calculates OR of atomic values. Its behavior is similar to the one for __atomic_fetch_or() of gcc...
int nlib_clz(uint32_t x)
Returns the number of consecutive zero bits, with respect to the most significant bit (MSB)...
void * nlib_atomic_loadptr(void *const *ptr, int memorder)
Loads a value in an atomic operation. Its behavior is similar to the one for __atomic_load_n() of gcc...
int64_t nlib_atomic_exchange64(int64_t *ptr, int64_t val, int memorder)
Swaps values in an atomic operation. Its behavior is similar to the one for __atomic_exchange_n() of ...
int64_t nlib_atomic_sub_fetch64(int64_t *ptr, int64_t val, int memorder)
Subtracts atomic values. Its behavior is similar to the one for __atomic_sub_fetch() of gcc...
int32_t nlib_atomic_fetch_xor32(int32_t *ptr, int32_t val, int memorder)
Calculates XOR of atomic values. Its behavior is similar to the one for __atomic_fetch_xor() of gcc...
int32_t nlib_atomic_sub_fetch32(int32_t *ptr, int32_t val, int memorder)
Subtracts atomic values. Its behavior is similar to the one for __atomic_sub_fetch() of gcc...
int32_t nlib_atomic_fetch_sub32(int32_t *ptr, int32_t val, int memorder)
Subtracts atomic values. Its behavior is similar to the one for __atomic_fetch_sub() of gcc...
int32_t nlib_atomic_add_fetch32(int32_t *ptr, int32_t val, int memorder)
Adds atomic values. Its behavior is similar to the one for __atomic_add_fetch() of gcc...
void nlib_atomic_storeptr(void **ptr, void *val, int memorder)
Stores a value in an atomic operation. Its behavior is similar to the one for __atomic_store_n() of g...
void nlib_atomic_thread_fence(int memorder)
Places the specified memory barrier.
int32_t nlib_atomic_fetch_and32(int32_t *ptr, int32_t val, int memorder)
Calculates AND of atomic values. Its behavior is similar to the one for __atomic_fetch_and() of gcc...
pthread_cond_t nlib_cond
The type for a condition variable object.
#define NLIB_ATOMIC_RELAXED
Similar to __ATOMIC_RELAXED of gcc or std::memory_order_relaxed of C++11.
pthread_mutex_t nlib_mutex
The type for mutex variables.
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:59
int64_t nlib_atomic_xor_fetch64(int64_t *ptr, int64_t val, int memorder)
Calculates XOR of atomic values. Its behavior is similar to the one for __atomic_xor_fetch() of gcc...
int64_t nlib_atomic_add_fetch64(int64_t *ptr, int64_t val, int memorder)
Adds atomic values. Its behavior is similar to the one for __atomic_add_fetch() of gcc...
int32_t nlib_atomic_and_fetch32(int32_t *ptr, int32_t val, int memorder)
Calculates AND of atomic values. Its behavior is similar to the one for __atomic_and_fetch() of gcc...
int nlib_atomic_compare_exchange32(int32_t *ptr, int32_t *expected, int32_t desired, int weak, int success_memorder, int failure_memorder)
Compares and swaps atomic values. Its behavior is similar to the one for __atomic_compare_exchange_n(...
void nlib_atomic_store64(int64_t *ptr, int64_t val, int memorder)
Stores a value in an atomic operation. Its behavior is similar to the one for __atomic_store_n() of g...
int nlib_ctz64(uint64_t x)
Returns the number of consecutive zero bits, with respect to the least significant bit (LSB)...
int64_t nlib_atomic_or_fetch64(int64_t *ptr, int64_t val, int memorder)
Calculates OR of atomic values. Its behavior is similar to the one for __atomic_or_fetch() of gcc...
int32_t nlib_atomic_fetch_or32(int32_t *ptr, int32_t val, int memorder)
Calculates OR of atomic values. Its behavior is similar to the one for __atomic_fetch_or() of gcc...
int64_t nlib_atomic_fetch_xor64(int64_t *ptr, int64_t val, int memorder)
Calculates XOR of atomic values. Its behavior is similar to the one for __atomic_fetch_xor() of gcc...
int32_t nlib_atomic_fetch_add32(int32_t *ptr, int32_t val, int memorder)
Adds atomic values. Its behavior is similar to the one for __atomic_fetch_add() of gcc...
void nlib_atomic_store32(int32_t *ptr, int32_t val, int memorder)
Stores a value in an atomic operation. Its behavior is similar to the one for __atomic_store_n() of g...
int nlib_clz64(uint64_t x)
Returns the number of consecutive zero bits, with respect to the most significant bit (MSB)...
int32_t nlib_atomic_exchange32(int32_t *ptr, int32_t val, int memorder)
Swaps values in an atomic operation. Its behavior is similar to the one for __atomic_exchange_n() of ...
pthread_t nlib_thread
The identifier for threads.
int64_t nlib_atomic_load64(const int64_t *ptr, int memorder)
Loads a value in an atomic operation. Its behavior is similar to the one for __atomic_load_n() of gcc...