1 | /* |
1 | /* |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
3 | * |
3 | * |
4 | * Copyright (©) 2009-2014 Marc Alexander Lehmann <libecb@schmorp.de> |
4 | * Copyright (©) 2009-2015,2018-2021 Marc Alexander Lehmann <libecb@schmorp.de> |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
6 | * All rights reserved. |
6 | * All rights reserved. |
7 | * |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * Redistribution and use in source and binary forms, with or without modifica- |
9 | * tion, are permitted provided that the following conditions are met: |
9 | * tion, are permitted provided that the following conditions are met: |
… | |
… | |
40 | |
40 | |
41 | #ifndef ECB_H |
41 | #ifndef ECB_H |
42 | #define ECB_H |
42 | #define ECB_H |
43 | |
43 | |
44 | /* 16 bits major, 16 bits minor */ |
44 | /* 16 bits major, 16 bits minor */ |
45 | #define ECB_VERSION 0x00010004 |
45 | #define ECB_VERSION 0x00010009 |
46 | |
46 | |
47 | #ifdef _WIN32 |
47 | #include <string.h> /* for memcpy */ |
|
|
48 | |
|
|
49 | #if defined (_WIN32) && !defined (__MINGW32__) |
48 | typedef signed char int8_t; |
50 | typedef signed char int8_t; |
49 | typedef unsigned char uint8_t; |
51 | typedef unsigned char uint8_t; |
|
|
52 | typedef signed char int_fast8_t; |
|
|
53 | typedef unsigned char uint_fast8_t; |
50 | typedef signed short int16_t; |
54 | typedef signed short int16_t; |
51 | typedef unsigned short uint16_t; |
55 | typedef unsigned short uint16_t; |
|
|
56 | typedef signed int int_fast16_t; |
|
|
57 | typedef unsigned int uint_fast16_t; |
52 | typedef signed int int32_t; |
58 | typedef signed int int32_t; |
53 | typedef unsigned int uint32_t; |
59 | typedef unsigned int uint32_t; |
|
|
60 | typedef signed int int_fast32_t; |
|
|
61 | typedef unsigned int uint_fast32_t; |
54 | #if __GNUC__ |
62 | #if __GNUC__ |
55 | typedef signed long long int64_t; |
63 | typedef signed long long int64_t; |
56 | typedef unsigned long long uint64_t; |
64 | typedef unsigned long long uint64_t; |
57 | #else /* _MSC_VER || __BORLANDC__ */ |
65 | #else /* _MSC_VER || __BORLANDC__ */ |
58 | typedef signed __int64 int64_t; |
66 | typedef signed __int64 int64_t; |
59 | typedef unsigned __int64 uint64_t; |
67 | typedef unsigned __int64 uint64_t; |
60 | #endif |
68 | #endif |
|
|
69 | typedef int64_t int_fast64_t; |
|
|
70 | typedef uint64_t uint_fast64_t; |
61 | #ifdef _WIN64 |
71 | #ifdef _WIN64 |
62 | #define ECB_PTRSIZE 8 |
72 | #define ECB_PTRSIZE 8 |
63 | typedef uint64_t uintptr_t; |
73 | typedef uint64_t uintptr_t; |
64 | typedef int64_t intptr_t; |
74 | typedef int64_t intptr_t; |
65 | #else |
75 | #else |
… | |
… | |
67 | typedef uint32_t uintptr_t; |
77 | typedef uint32_t uintptr_t; |
68 | typedef int32_t intptr_t; |
78 | typedef int32_t intptr_t; |
69 | #endif |
79 | #endif |
70 | #else |
80 | #else |
71 | #include <inttypes.h> |
81 | #include <inttypes.h> |
72 | #if UINTMAX_MAX > 0xffffffffU |
82 | #if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU |
73 | #define ECB_PTRSIZE 8 |
83 | #define ECB_PTRSIZE 8 |
74 | #else |
84 | #else |
75 | #define ECB_PTRSIZE 4 |
85 | #define ECB_PTRSIZE 4 |
76 | #endif |
86 | #endif |
77 | #endif |
87 | #endif |
78 | |
88 | |
|
|
89 | #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) |
|
|
90 | #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) |
|
|
91 | |
|
|
92 | #ifndef ECB_OPTIMIZE_SIZE |
|
|
93 | #if __OPTIMIZE_SIZE__ |
|
|
94 | #define ECB_OPTIMIZE_SIZE 1 |
|
|
95 | #else |
|
|
96 | #define ECB_OPTIMIZE_SIZE 0 |
|
|
97 | #endif |
|
|
98 | #endif |
|
|
99 | |
79 | /* work around x32 idiocy by defining proper macros */ |
100 | /* work around x32 idiocy by defining proper macros */ |
80 | #if __amd64 || __x86_64 || _M_AMD64 || _M_X64 |
101 | #if ECB_GCC_AMD64 || ECB_MSVC_AMD64 |
81 | #if _ILP32 |
102 | #if _ILP32 |
82 | #define ECB_AMD64_X32 1 |
103 | #define ECB_AMD64_X32 1 |
83 | #else |
104 | #else |
84 | #define ECB_AMD64 1 |
105 | #define ECB_AMD64 1 |
85 | #endif |
106 | #endif |
|
|
107 | #endif |
|
|
108 | |
|
|
109 | #if ECB_PTRSIZE >= 8 || ECB_AMD64_X32 |
|
|
110 | #define ECB_64BIT_NATIVE 1 |
|
|
111 | #else |
|
|
112 | #define ECB_64BIT_NATIVE 0 |
86 | #endif |
113 | #endif |
87 | |
114 | |
88 | /* many compilers define _GNUC_ to some versions but then only implement |
115 | /* many compilers define _GNUC_ to some versions but then only implement |
89 | * what their idiot authors think are the "more important" extensions, |
116 | * what their idiot authors think are the "more important" extensions, |
90 | * causing enormous grief in return for some better fake benchmark numbers. |
117 | * causing enormous grief in return for some better fake benchmark numbers. |
… | |
… | |
112 | #define ECB_CLANG_EXTENSION(x) 0 |
139 | #define ECB_CLANG_EXTENSION(x) 0 |
113 | #endif |
140 | #endif |
114 | |
141 | |
115 | #define ECB_CPP (__cplusplus+0) |
142 | #define ECB_CPP (__cplusplus+0) |
116 | #define ECB_CPP11 (__cplusplus >= 201103L) |
143 | #define ECB_CPP11 (__cplusplus >= 201103L) |
|
|
144 | #define ECB_CPP14 (__cplusplus >= 201402L) |
|
|
145 | #define ECB_CPP17 (__cplusplus >= 201703L) |
117 | |
146 | |
118 | #if ECB_CPP |
147 | #if ECB_CPP |
119 | #define ECB_C 0 |
148 | #define ECB_C 0 |
120 | #define ECB_STDC_VERSION 0 |
149 | #define ECB_STDC_VERSION 0 |
121 | #else |
150 | #else |
… | |
… | |
123 | #define ECB_STDC_VERSION __STDC_VERSION__ |
152 | #define ECB_STDC_VERSION __STDC_VERSION__ |
124 | #endif |
153 | #endif |
125 | |
154 | |
126 | #define ECB_C99 (ECB_STDC_VERSION >= 199901L) |
155 | #define ECB_C99 (ECB_STDC_VERSION >= 199901L) |
127 | #define ECB_C11 (ECB_STDC_VERSION >= 201112L) |
156 | #define ECB_C11 (ECB_STDC_VERSION >= 201112L) |
|
|
157 | #define ECB_C17 (ECB_STDC_VERSION >= 201710L) |
128 | |
158 | |
129 | #if ECB_CPP |
159 | #if ECB_CPP |
130 | #define ECB_EXTERN_C extern "C" |
160 | #define ECB_EXTERN_C extern "C" |
131 | #define ECB_EXTERN_C_BEG ECB_EXTERN_C { |
161 | #define ECB_EXTERN_C_BEG ECB_EXTERN_C { |
132 | #define ECB_EXTERN_C_END } |
162 | #define ECB_EXTERN_C_END } |
… | |
… | |
147 | |
177 | |
148 | #if ECB_NO_SMP |
178 | #if ECB_NO_SMP |
149 | #define ECB_MEMORY_FENCE do { } while (0) |
179 | #define ECB_MEMORY_FENCE do { } while (0) |
150 | #endif |
180 | #endif |
151 | |
181 | |
|
|
182 | /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */ |
|
|
183 | #if __xlC__ && ECB_CPP |
|
|
184 | #include <builtins.h> |
|
|
185 | #endif |
|
|
186 | |
|
|
187 | #if 1400 <= _MSC_VER |
|
|
188 | #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ |
|
|
189 | #endif |
|
|
190 | |
152 | #ifndef ECB_MEMORY_FENCE |
191 | #ifndef ECB_MEMORY_FENCE |
153 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
192 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
193 | #define ECB_MEMORY_FENCE_RELAXED __asm__ __volatile__ ("" : : : "memory") |
154 | #if __i386 || __i386__ |
194 | #if __i386 || __i386__ |
155 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
195 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
156 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
196 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
157 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") |
197 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") |
158 | #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ |
198 | #elif ECB_GCC_AMD64 |
159 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
199 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
160 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
200 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
161 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") |
201 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") |
162 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
202 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
163 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
203 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
|
|
204 | #elif defined __ARM_ARCH_2__ \ |
|
|
205 | || defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \ |
|
|
206 | || defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \ |
|
|
207 | || defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \ |
|
|
208 | || defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \ |
|
|
209 | || defined __ARM_ARCH_5TEJ__ |
|
|
210 | /* should not need any, unless running old code on newer cpu - arm doesn't support that */ |
164 | #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ |
211 | #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ |
165 | || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ |
212 | || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \ |
|
|
213 | || defined __ARM_ARCH_6T2__ |
166 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
214 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
167 | #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ |
215 | #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ |
168 | || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ |
216 | || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__ |
169 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
217 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
170 | #elif __aarch64__ |
218 | #elif __aarch64__ |
171 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory") |
219 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory") |
172 | #elif (__sparc || __sparc__) && !__sparcv8 |
220 | #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8) |
173 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory") |
221 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory") |
174 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") |
222 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") |
175 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") |
223 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") |
176 | #elif defined __s390__ || defined __s390x__ |
224 | #elif defined __s390__ || defined __s390x__ |
177 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") |
225 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") |
… | |
… | |
200 | #if ECB_GCC_VERSION(4,7) |
248 | #if ECB_GCC_VERSION(4,7) |
201 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
249 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
202 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
250 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
203 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
251 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
204 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
252 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
253 | #define ECB_MEMORY_FENCE_RELAXED __atomic_thread_fence (__ATOMIC_RELAXED) |
205 | |
254 | |
206 | #elif ECB_CLANG_EXTENSION(c_atomic) |
255 | #elif ECB_CLANG_EXTENSION(c_atomic) |
207 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
256 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
208 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
257 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
209 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
258 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
210 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
259 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
260 | #define ECB_MEMORY_FENCE_RELAXED __c11_atomic_thread_fence (__ATOMIC_RELAXED) |
211 | |
261 | |
212 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
262 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
213 | #define ECB_MEMORY_FENCE __sync_synchronize () |
263 | #define ECB_MEMORY_FENCE __sync_synchronize () |
214 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
264 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
215 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
265 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
… | |
… | |
225 | #elif defined _WIN32 |
275 | #elif defined _WIN32 |
226 | #include <WinNT.h> |
276 | #include <WinNT.h> |
227 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
277 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
228 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
278 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
229 | #include <mbarrier.h> |
279 | #include <mbarrier.h> |
230 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
280 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
231 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
281 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_acq_barrier () |
232 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
282 | #define ECB_MEMORY_FENCE_RELEASE __machine_rel_barrier () |
|
|
283 | #define ECB_MEMORY_FENCE_RELAXED __compiler_barrier () |
233 | #elif __xlC__ |
284 | #elif __xlC__ |
234 | #define ECB_MEMORY_FENCE __sync () |
285 | #define ECB_MEMORY_FENCE __sync () |
235 | #endif |
286 | #endif |
236 | #endif |
287 | #endif |
237 | |
288 | |
238 | #ifndef ECB_MEMORY_FENCE |
289 | #ifndef ECB_MEMORY_FENCE |
239 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
290 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
240 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
291 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
241 | /* not just C11 atomics and atomic accesses */ |
292 | /* not just C11 atomics and atomic accesses */ |
242 | #include <stdatomic.h> |
293 | #include <stdatomic.h> |
243 | /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ |
|
|
244 | /* any fence other than seq_cst, which isn't very efficient for us. */ |
|
|
245 | /* Why that is, we don't know - either the C11 memory model is quite useless */ |
|
|
246 | /* for most usages, or gcc and clang have a bug */ |
|
|
247 | /* I *currently* lean towards the latter, and inefficiently implement */ |
|
|
248 | /* all three of ecb's fences as a seq_cst fence */ |
|
|
249 | /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */ |
|
|
250 | /* for all __atomic_thread_fence's except seq_cst */ |
|
|
251 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
294 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
|
|
295 | #define ECB_MEMORY_FENCE_ACQUIRE atomic_thread_fence (memory_order_acquire) |
|
|
296 | #define ECB_MEMORY_FENCE_RELEASE atomic_thread_fence (memory_order_release) |
252 | #endif |
297 | #endif |
253 | #endif |
298 | #endif |
254 | |
299 | |
255 | #ifndef ECB_MEMORY_FENCE |
300 | #ifndef ECB_MEMORY_FENCE |
256 | #if !ECB_AVOID_PTHREADS |
301 | #if !ECB_AVOID_PTHREADS |
… | |
… | |
276 | |
321 | |
277 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
322 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
278 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
323 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
279 | #endif |
324 | #endif |
280 | |
325 | |
|
|
326 | #if !defined ECB_MEMORY_FENCE_RELAXED && defined ECB_MEMORY_FENCE |
|
|
327 | #define ECB_MEMORY_FENCE_RELAXED ECB_MEMORY_FENCE /* very heavy-handed */ |
|
|
328 | #endif |
|
|
329 | |
281 | /*****************************************************************************/ |
330 | /*****************************************************************************/ |
282 | |
331 | |
283 | #if ECB_CPP |
332 | #if ECB_CPP |
284 | #define ecb_inline static inline |
333 | #define ecb_inline static inline |
285 | #elif ECB_GCC_VERSION(2,5) |
334 | #elif ECB_GCC_VERSION(2,5) |
… | |
… | |
302 | |
351 | |
303 | #define ECB_CONCAT_(a, b) a ## b |
352 | #define ECB_CONCAT_(a, b) a ## b |
304 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
353 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
305 | #define ECB_STRINGIFY_(a) # a |
354 | #define ECB_STRINGIFY_(a) # a |
306 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
355 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
|
|
356 | #define ECB_STRINGIFY_EXPR(expr) ((expr), ECB_STRINGIFY_ (expr)) |
307 | |
357 | |
308 | #define ecb_function_ ecb_inline |
358 | #define ecb_function_ ecb_inline |
309 | |
359 | |
310 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8) |
360 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8) |
311 | #define ecb_attribute(attrlist) __attribute__ (attrlist) |
361 | #define ecb_attribute(attrlist) __attribute__ (attrlist) |
… | |
… | |
348 | #define ecb_deprecated __declspec (deprecated) |
398 | #define ecb_deprecated __declspec (deprecated) |
349 | #else |
399 | #else |
350 | #define ecb_deprecated ecb_attribute ((__deprecated__)) |
400 | #define ecb_deprecated ecb_attribute ((__deprecated__)) |
351 | #endif |
401 | #endif |
352 | |
402 | |
|
|
403 | #if _MSC_VER >= 1500 |
|
|
404 | #define ecb_deprecated_message(msg) __declspec (deprecated (msg)) |
|
|
405 | #elif ECB_GCC_VERSION(4,5) |
|
|
406 | #define ecb_deprecated_message(msg) ecb_attribute ((__deprecated__ (msg)) |
|
|
407 | #else |
|
|
408 | #define ecb_deprecated_message(msg) ecb_deprecated |
|
|
409 | #endif |
|
|
410 | |
|
|
411 | #if _MSC_VER >= 1400 |
|
|
412 | #define ecb_noinline __declspec (noinline) |
|
|
413 | #else |
353 | #define ecb_noinline ecb_attribute ((__noinline__)) |
414 | #define ecb_noinline ecb_attribute ((__noinline__)) |
|
|
415 | #endif |
|
|
416 | |
354 | #define ecb_unused ecb_attribute ((__unused__)) |
417 | #define ecb_unused ecb_attribute ((__unused__)) |
355 | #define ecb_const ecb_attribute ((__const__)) |
418 | #define ecb_const ecb_attribute ((__const__)) |
356 | #define ecb_pure ecb_attribute ((__pure__)) |
419 | #define ecb_pure ecb_attribute ((__pure__)) |
357 | |
420 | |
358 | /* TODO http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx __declspec(noreturn) */ |
|
|
359 | #if ECB_C11 || __IBMC_NORETURN |
421 | #if ECB_C11 || __IBMC_NORETURN |
360 | /* http://pic.dhe.ibm.com/infocenter/compbg/v121v141/topic/com.ibm.xlcpp121.bg.doc/language_ref/noreturn.html */ |
422 | /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/language_ref/noreturn.html */ |
361 | #define ecb_noreturn _Noreturn |
423 | #define ecb_noreturn _Noreturn |
|
|
424 | #elif ECB_CPP11 |
|
|
425 | #define ecb_noreturn [[noreturn]] |
|
|
426 | #elif _MSC_VER >= 1200 |
|
|
427 | /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx */ |
|
|
428 | #define ecb_noreturn __declspec (noreturn) |
362 | #else |
429 | #else |
363 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
430 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
364 | #endif |
431 | #endif |
365 | |
432 | |
366 | #if ECB_GCC_VERSION(4,3) |
433 | #if ECB_GCC_VERSION(4,3) |
… | |
… | |
393 | #define ecb_ctz32(x) __builtin_ctz (x) |
460 | #define ecb_ctz32(x) __builtin_ctz (x) |
394 | #define ecb_ctz64(x) __builtin_ctzll (x) |
461 | #define ecb_ctz64(x) __builtin_ctzll (x) |
395 | #define ecb_popcount32(x) __builtin_popcount (x) |
462 | #define ecb_popcount32(x) __builtin_popcount (x) |
396 | /* no popcountll */ |
463 | /* no popcountll */ |
397 | #else |
464 | #else |
398 | ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const; |
465 | ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); |
399 | ecb_function_ int |
466 | ecb_function_ ecb_const int |
400 | ecb_ctz32 (uint32_t x) |
467 | ecb_ctz32 (uint32_t x) |
401 | { |
468 | { |
|
|
469 | #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) |
|
|
470 | unsigned long r; |
|
|
471 | _BitScanForward (&r, x); |
|
|
472 | return (int)r; |
|
|
473 | #else |
402 | int r = 0; |
474 | int r = 0; |
403 | |
475 | |
404 | x &= ~x + 1; /* this isolates the lowest bit */ |
476 | x &= ~x + 1; /* this isolates the lowest bit */ |
405 | |
477 | |
406 | #if ECB_branchless_on_i386 |
478 | #if ECB_branchless_on_i386 |
… | |
… | |
416 | if (x & 0xff00ff00) r += 8; |
488 | if (x & 0xff00ff00) r += 8; |
417 | if (x & 0xffff0000) r += 16; |
489 | if (x & 0xffff0000) r += 16; |
418 | #endif |
490 | #endif |
419 | |
491 | |
420 | return r; |
492 | return r; |
|
|
493 | #endif |
421 | } |
494 | } |
422 | |
495 | |
423 | ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; |
496 | ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); |
424 | ecb_function_ int |
497 | ecb_function_ ecb_const int |
425 | ecb_ctz64 (uint64_t x) |
498 | ecb_ctz64 (uint64_t x) |
426 | { |
499 | { |
|
|
500 | #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) |
|
|
501 | unsigned long r; |
|
|
502 | _BitScanForward64 (&r, x); |
|
|
503 | return (int)r; |
|
|
504 | #else |
427 | int shift = x & 0xffffffffU ? 0 : 32; |
505 | int shift = x & 0xffffffff ? 0 : 32; |
428 | return ecb_ctz32 (x >> shift) + shift; |
506 | return ecb_ctz32 (x >> shift) + shift; |
|
|
507 | #endif |
429 | } |
508 | } |
430 | |
509 | |
431 | ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; |
510 | ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); |
432 | ecb_function_ int |
511 | ecb_function_ ecb_const int |
433 | ecb_popcount32 (uint32_t x) |
512 | ecb_popcount32 (uint32_t x) |
434 | { |
513 | { |
435 | x -= (x >> 1) & 0x55555555; |
514 | x -= (x >> 1) & 0x55555555; |
436 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
515 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
437 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
516 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
438 | x *= 0x01010101; |
517 | x *= 0x01010101; |
439 | |
518 | |
440 | return x >> 24; |
519 | return x >> 24; |
441 | } |
520 | } |
442 | |
521 | |
443 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
522 | ecb_function_ ecb_const int ecb_ld32 (uint32_t x); |
444 | ecb_function_ int ecb_ld32 (uint32_t x) |
523 | ecb_function_ ecb_const int ecb_ld32 (uint32_t x) |
445 | { |
524 | { |
|
|
525 | #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) |
|
|
526 | unsigned long r; |
|
|
527 | _BitScanReverse (&r, x); |
|
|
528 | return (int)r; |
|
|
529 | #else |
446 | int r = 0; |
530 | int r = 0; |
447 | |
531 | |
448 | if (x >> 16) { x >>= 16; r += 16; } |
532 | if (x >> 16) { x >>= 16; r += 16; } |
449 | if (x >> 8) { x >>= 8; r += 8; } |
533 | if (x >> 8) { x >>= 8; r += 8; } |
450 | if (x >> 4) { x >>= 4; r += 4; } |
534 | if (x >> 4) { x >>= 4; r += 4; } |
451 | if (x >> 2) { x >>= 2; r += 2; } |
535 | if (x >> 2) { x >>= 2; r += 2; } |
452 | if (x >> 1) { r += 1; } |
536 | if (x >> 1) { r += 1; } |
453 | |
537 | |
454 | return r; |
538 | return r; |
|
|
539 | #endif |
455 | } |
540 | } |
456 | |
541 | |
457 | ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; |
542 | ecb_function_ ecb_const int ecb_ld64 (uint64_t x); |
458 | ecb_function_ int ecb_ld64 (uint64_t x) |
543 | ecb_function_ ecb_const int ecb_ld64 (uint64_t x) |
459 | { |
544 | { |
|
|
545 | #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) |
|
|
546 | unsigned long r; |
|
|
547 | _BitScanReverse64 (&r, x); |
|
|
548 | return (int)r; |
|
|
549 | #else |
460 | int r = 0; |
550 | int r = 0; |
461 | |
551 | |
462 | if (x >> 32) { x >>= 32; r += 32; } |
552 | if (x >> 32) { x >>= 32; r += 32; } |
463 | |
553 | |
464 | return r + ecb_ld32 (x); |
554 | return r + ecb_ld32 (x); |
465 | } |
|
|
466 | #endif |
555 | #endif |
|
|
556 | } |
|
|
557 | #endif |
467 | |
558 | |
468 | ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const; |
559 | ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x); |
469 | ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } |
560 | ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } |
470 | ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const; |
561 | ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x); |
471 | ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); } |
562 | ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); } |
472 | |
563 | |
473 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; |
564 | ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x); |
474 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) |
565 | ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x) |
475 | { |
566 | { |
476 | return ( (x * 0x0802U & 0x22110U) |
567 | return ( (x * 0x0802U & 0x22110U) |
477 | | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; |
568 | | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; |
478 | } |
569 | } |
479 | |
570 | |
480 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const; |
571 | ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x); |
481 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) |
572 | ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x) |
482 | { |
573 | { |
483 | x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); |
574 | x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); |
484 | x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); |
575 | x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); |
485 | x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); |
576 | x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); |
486 | x = ( x >> 8 ) | ( x << 8); |
577 | x = ( x >> 8 ) | ( x << 8); |
487 | |
578 | |
488 | return x; |
579 | return x; |
489 | } |
580 | } |
490 | |
581 | |
491 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const; |
582 | ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x); |
492 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) |
583 | ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x) |
493 | { |
584 | { |
494 | x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); |
585 | x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); |
495 | x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); |
586 | x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); |
496 | x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); |
587 | x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); |
497 | x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); |
588 | x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); |
… | |
… | |
500 | return x; |
591 | return x; |
501 | } |
592 | } |
502 | |
593 | |
503 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
594 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
504 | /* so for this version we are lazy */ |
595 | /* so for this version we are lazy */ |
505 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
596 | ecb_function_ ecb_const int ecb_popcount64 (uint64_t x); |
506 | ecb_function_ int |
597 | ecb_function_ ecb_const int |
507 | ecb_popcount64 (uint64_t x) |
598 | ecb_popcount64 (uint64_t x) |
508 | { |
599 | { |
509 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
600 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
510 | } |
601 | } |
511 | |
602 | |
512 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const; |
603 | ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count); |
513 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const; |
604 | ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count); |
514 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const; |
605 | ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count); |
515 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const; |
606 | ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count); |
516 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
607 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); |
517 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
608 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count); |
518 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const; |
609 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count); |
519 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const; |
610 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count); |
520 | |
611 | |
521 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
612 | ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
522 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
613 | ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
523 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
614 | ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
524 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
615 | ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
525 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
616 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
526 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
617 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
527 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
618 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
528 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
619 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
|
|
620 | |
|
|
621 | #if ECB_CPP |
|
|
622 | |
|
|
623 | inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); } |
|
|
624 | inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); } |
|
|
625 | inline uint32_t ecb_ctz (uint32_t v) { return ecb_ctz32 (v); } |
|
|
626 | inline uint64_t ecb_ctz (uint64_t v) { return ecb_ctz64 (v); } |
|
|
627 | |
|
|
628 | inline bool ecb_is_pot (uint8_t v) { return ecb_is_pot32 (v); } |
|
|
629 | inline bool ecb_is_pot (uint16_t v) { return ecb_is_pot32 (v); } |
|
|
630 | inline bool ecb_is_pot (uint32_t v) { return ecb_is_pot32 (v); } |
|
|
631 | inline bool ecb_is_pot (uint64_t v) { return ecb_is_pot64 (v); } |
|
|
632 | |
|
|
633 | inline int ecb_ld (uint8_t v) { return ecb_ld32 (v); } |
|
|
634 | inline int ecb_ld (uint16_t v) { return ecb_ld32 (v); } |
|
|
635 | inline int ecb_ld (uint32_t v) { return ecb_ld32 (v); } |
|
|
636 | inline int ecb_ld (uint64_t v) { return ecb_ld64 (v); } |
|
|
637 | |
|
|
638 | inline int ecb_popcount (uint8_t v) { return ecb_popcount32 (v); } |
|
|
639 | inline int ecb_popcount (uint16_t v) { return ecb_popcount32 (v); } |
|
|
640 | inline int ecb_popcount (uint32_t v) { return ecb_popcount32 (v); } |
|
|
641 | inline int ecb_popcount (uint64_t v) { return ecb_popcount64 (v); } |
|
|
642 | |
|
|
643 | inline uint8_t ecb_bitrev (uint8_t v) { return ecb_bitrev8 (v); } |
|
|
644 | inline uint16_t ecb_bitrev (uint16_t v) { return ecb_bitrev16 (v); } |
|
|
645 | inline uint32_t ecb_bitrev (uint32_t v) { return ecb_bitrev32 (v); } |
|
|
646 | |
|
|
647 | inline uint8_t ecb_rotl (uint8_t v, unsigned int count) { return ecb_rotl8 (v, count); } |
|
|
648 | inline uint16_t ecb_rotl (uint16_t v, unsigned int count) { return ecb_rotl16 (v, count); } |
|
|
649 | inline uint32_t ecb_rotl (uint32_t v, unsigned int count) { return ecb_rotl32 (v, count); } |
|
|
650 | inline uint64_t ecb_rotl (uint64_t v, unsigned int count) { return ecb_rotl64 (v, count); } |
|
|
651 | |
|
|
652 | inline uint8_t ecb_rotr (uint8_t v, unsigned int count) { return ecb_rotr8 (v, count); } |
|
|
653 | inline uint16_t ecb_rotr (uint16_t v, unsigned int count) { return ecb_rotr16 (v, count); } |
|
|
654 | inline uint32_t ecb_rotr (uint32_t v, unsigned int count) { return ecb_rotr32 (v, count); } |
|
|
655 | inline uint64_t ecb_rotr (uint64_t v, unsigned int count) { return ecb_rotr64 (v, count); } |
|
|
656 | |
|
|
657 | #endif |
529 | |
658 | |
530 | #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) |
659 | #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) |
|
|
660 | #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16) |
|
|
661 | #define ecb_bswap16(x) __builtin_bswap16 (x) |
|
|
662 | #else |
531 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
663 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
|
|
664 | #endif |
532 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
665 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
533 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
666 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
|
|
667 | #elif _MSC_VER |
|
|
668 | #include <stdlib.h> |
|
|
669 | #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x))) |
|
|
670 | #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x))) |
|
|
671 | #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x))) |
534 | #else |
672 | #else |
535 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
673 | ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x); |
536 | ecb_function_ uint16_t |
674 | ecb_function_ ecb_const uint16_t |
537 | ecb_bswap16 (uint16_t x) |
675 | ecb_bswap16 (uint16_t x) |
538 | { |
676 | { |
539 | return ecb_rotl16 (x, 8); |
677 | return ecb_rotl16 (x, 8); |
540 | } |
678 | } |
541 | |
679 | |
542 | ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
680 | ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x); |
543 | ecb_function_ uint32_t |
681 | ecb_function_ ecb_const uint32_t |
544 | ecb_bswap32 (uint32_t x) |
682 | ecb_bswap32 (uint32_t x) |
545 | { |
683 | { |
546 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
684 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
547 | } |
685 | } |
548 | |
686 | |
549 | ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; |
687 | ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x); |
550 | ecb_function_ uint64_t |
688 | ecb_function_ ecb_const uint64_t |
551 | ecb_bswap64 (uint64_t x) |
689 | ecb_bswap64 (uint64_t x) |
552 | { |
690 | { |
553 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
691 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
554 | } |
692 | } |
555 | #endif |
693 | #endif |
556 | |
694 | |
557 | #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable) |
695 | #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable) |
558 | #define ecb_unreachable() __builtin_unreachable () |
696 | #define ecb_unreachable() __builtin_unreachable () |
559 | #else |
697 | #else |
560 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
698 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
561 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
699 | ecb_inline ecb_noreturn void ecb_unreachable (void); |
562 | ecb_inline void ecb_unreachable (void) { } |
700 | ecb_inline ecb_noreturn void ecb_unreachable (void) { } |
563 | #endif |
701 | #endif |
564 | |
702 | |
565 | /* try to tell the compiler that some condition is definitely true */ |
703 | /* try to tell the compiler that some condition is definitely true */ |
566 | #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 |
704 | #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 |
567 | |
705 | |
568 | ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; |
706 | ecb_inline ecb_const uint32_t ecb_byteorder_helper (void); |
569 | ecb_inline unsigned char |
707 | ecb_inline ecb_const uint32_t |
570 | ecb_byteorder_helper (void) |
708 | ecb_byteorder_helper (void) |
571 | { |
709 | { |
572 | /* the union code still generates code under pressure in gcc, */ |
710 | /* the union code still generates code under pressure in gcc, */ |
573 | /* but less than using pointers, and always seems to */ |
711 | /* but less than using pointers, and always seems to */ |
574 | /* successfully return a constant. */ |
712 | /* successfully return a constant. */ |
575 | /* the reason why we have this horrible preprocessor mess */ |
713 | /* the reason why we have this horrible preprocessor mess */ |
576 | /* is to avoid it in all cases, at least on common architectures */ |
714 | /* is to avoid it in all cases, at least on common architectures */ |
577 | /* or when using a recent enough gcc version (>= 4.6) */ |
715 | /* or when using a recent enough gcc version (>= 4.6) */ |
578 | #if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64 |
|
|
579 | return 0x44; |
|
|
580 | #elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
716 | #if (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \ |
|
|
717 | || ((__i386 || __i386__ || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64) && !__VOS__) |
|
|
718 | #define ECB_LITTLE_ENDIAN 1 |
581 | return 0x44; |
719 | return 0x44332211; |
582 | #elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
720 | #elif (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \ |
|
|
721 | || ((__AARCH64EB__ || __MIPSEB__ || __ARMEB__) && !__VOS__) |
|
|
722 | #define ECB_BIG_ENDIAN 1 |
583 | return 0x11; |
723 | return 0x11223344; |
584 | #else |
724 | #else |
585 | union |
725 | union |
586 | { |
726 | { |
|
|
727 | uint8_t c[4]; |
587 | uint32_t i; |
728 | uint32_t u; |
588 | uint8_t c; |
|
|
589 | } u = { 0x11223344 }; |
729 | } u = { 0x11, 0x22, 0x33, 0x44 }; |
590 | return u.c; |
730 | return u.u; |
591 | #endif |
731 | #endif |
592 | } |
732 | } |
593 | |
733 | |
594 | ecb_inline ecb_bool ecb_big_endian (void) ecb_const; |
734 | ecb_inline ecb_const ecb_bool ecb_big_endian (void); |
595 | ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
735 | ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } |
596 | ecb_inline ecb_bool ecb_little_endian (void) ecb_const; |
736 | ecb_inline ecb_const ecb_bool ecb_little_endian (void); |
597 | ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
737 | ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } |
|
|
738 | |
|
|
739 | /*****************************************************************************/ |
|
|
740 | /* unaligned load/store */ |
|
|
741 | |
|
|
742 | ecb_inline uint_fast16_t ecb_be_u16_to_host (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; } |
|
|
743 | ecb_inline uint_fast32_t ecb_be_u32_to_host (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; } |
|
|
744 | ecb_inline uint_fast64_t ecb_be_u64_to_host (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; } |
|
|
745 | |
|
|
746 | ecb_inline uint_fast16_t ecb_le_u16_to_host (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; } |
|
|
747 | ecb_inline uint_fast32_t ecb_le_u32_to_host (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; } |
|
|
748 | ecb_inline uint_fast64_t ecb_le_u64_to_host (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; } |
|
|
749 | |
|
|
750 | ecb_inline uint_fast16_t ecb_peek_u16_u (const void *ptr) { uint16_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
751 | ecb_inline uint_fast32_t ecb_peek_u32_u (const void *ptr) { uint32_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
752 | ecb_inline uint_fast64_t ecb_peek_u64_u (const void *ptr) { uint64_t v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
753 | |
|
|
754 | ecb_inline uint_fast16_t ecb_peek_be_u16_u (const void *ptr) { return ecb_be_u16_to_host (ecb_peek_u16_u (ptr)); } |
|
|
755 | ecb_inline uint_fast32_t ecb_peek_be_u32_u (const void *ptr) { return ecb_be_u32_to_host (ecb_peek_u32_u (ptr)); } |
|
|
756 | ecb_inline uint_fast64_t ecb_peek_be_u64_u (const void *ptr) { return ecb_be_u64_to_host (ecb_peek_u64_u (ptr)); } |
|
|
757 | |
|
|
758 | ecb_inline uint_fast16_t ecb_peek_le_u16_u (const void *ptr) { return ecb_le_u16_to_host (ecb_peek_u16_u (ptr)); } |
|
|
759 | ecb_inline uint_fast32_t ecb_peek_le_u32_u (const void *ptr) { return ecb_le_u32_to_host (ecb_peek_u32_u (ptr)); } |
|
|
760 | ecb_inline uint_fast64_t ecb_peek_le_u64_u (const void *ptr) { return ecb_le_u64_to_host (ecb_peek_u64_u (ptr)); } |
|
|
761 | |
|
|
762 | ecb_inline uint_fast16_t ecb_host_to_be_u16 (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; } |
|
|
763 | ecb_inline uint_fast32_t ecb_host_to_be_u32 (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; } |
|
|
764 | ecb_inline uint_fast64_t ecb_host_to_be_u64 (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; } |
|
|
765 | |
|
|
766 | ecb_inline uint_fast16_t ecb_host_to_le_u16 (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; } |
|
|
767 | ecb_inline uint_fast32_t ecb_host_to_le_u32 (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; } |
|
|
768 | ecb_inline uint_fast64_t ecb_host_to_le_u64 (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; } |
|
|
769 | |
|
|
770 | ecb_inline void ecb_poke_u16_u (void *ptr, uint16_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
771 | ecb_inline void ecb_poke_u32_u (void *ptr, uint32_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
772 | ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
773 | |
|
|
774 | ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); } |
|
|
775 | ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); } |
|
|
776 | ecb_inline void ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_be_u64 (v)); } |
|
|
777 | |
|
|
778 | ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); } |
|
|
779 | ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); } |
|
|
780 | ecb_inline void ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_le_u64 (v)); } |
|
|
781 | |
|
|
782 | #if ECB_CPP |
|
|
783 | |
|
|
784 | inline uint8_t ecb_bswap (uint8_t v) { return v; } |
|
|
785 | inline uint16_t ecb_bswap (uint16_t v) { return ecb_bswap16 (v); } |
|
|
786 | inline uint32_t ecb_bswap (uint32_t v) { return ecb_bswap32 (v); } |
|
|
787 | inline uint64_t ecb_bswap (uint64_t v) { return ecb_bswap64 (v); } |
|
|
788 | |
|
|
789 | template<typename T> inline T ecb_be_to_host (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; } |
|
|
790 | template<typename T> inline T ecb_le_to_host (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; } |
|
|
791 | template<typename T> inline T ecb_peek (const void *ptr) { return *(const T *)ptr; } |
|
|
792 | template<typename T> inline T ecb_peek_be (const void *ptr) { return ecb_be_to_host (ecb_peek <T> (ptr)); } |
|
|
793 | template<typename T> inline T ecb_peek_le (const void *ptr) { return ecb_le_to_host (ecb_peek <T> (ptr)); } |
|
|
794 | template<typename T> inline T ecb_peek_u (const void *ptr) { T v; memcpy (&v, ptr, sizeof (v)); return v; } |
|
|
795 | template<typename T> inline T ecb_peek_be_u (const void *ptr) { return ecb_be_to_host (ecb_peek_u<T> (ptr)); } |
|
|
796 | template<typename T> inline T ecb_peek_le_u (const void *ptr) { return ecb_le_to_host (ecb_peek_u<T> (ptr)); } |
|
|
797 | |
|
|
798 | template<typename T> inline T ecb_host_to_be (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; } |
|
|
799 | template<typename T> inline T ecb_host_to_le (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; } |
|
|
800 | template<typename T> inline void ecb_poke (void *ptr, T v) { *(T *)ptr = v; } |
|
|
801 | template<typename T> inline void ecb_poke_be (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_be (v)); } |
|
|
802 | template<typename T> inline void ecb_poke_le (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_le (v)); } |
|
|
803 | template<typename T> inline void ecb_poke_u (void *ptr, T v) { memcpy (ptr, &v, sizeof (v)); } |
|
|
804 | template<typename T> inline void ecb_poke_be_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_be (v)); } |
|
|
805 | template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); } |
|
|
806 | |
|
|
807 | #endif |
|
|
808 | |
|
|
809 | /*****************************************************************************/ |
|
|
810 | /* division */ |
598 | |
811 | |
599 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
812 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
|
|
813 | /* C99 tightened the definition of %, so we can use a more efficient version */ |
600 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
814 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
601 | #else |
815 | #else |
602 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
816 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
603 | #endif |
817 | #endif |
604 | |
818 | |
… | |
… | |
615 | } |
829 | } |
616 | #else |
830 | #else |
617 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
831 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
618 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
832 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
619 | #endif |
833 | #endif |
|
|
834 | |
|
|
835 | /*****************************************************************************/ |
|
|
836 | /* array length */ |
620 | |
837 | |
621 | #if ecb_cplusplus_does_not_suck |
838 | #if ecb_cplusplus_does_not_suck |
622 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
839 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
623 | template<typename T, int N> |
840 | template<typename T, int N> |
624 | static inline int ecb_array_length (const T (&arr)[N]) |
841 | static inline int ecb_array_length (const T (&arr)[N]) |
… | |
… | |
627 | } |
844 | } |
628 | #else |
845 | #else |
629 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
846 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
630 | #endif |
847 | #endif |
631 | |
848 | |
|
|
849 | /*****************************************************************************/ |
|
|
850 | /* IEEE 754-2008 half float conversions */ |
|
|
851 | |
|
|
852 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
|
|
853 | ecb_function_ ecb_const uint32_t |
|
|
854 | ecb_binary16_to_binary32 (uint32_t x) |
|
|
855 | { |
|
|
856 | unsigned int s = (x & 0x8000) << (31 - 15); |
|
|
857 | int e = (x >> 10) & 0x001f; |
|
|
858 | unsigned int m = x & 0x03ff; |
|
|
859 | |
|
|
860 | if (ecb_expect_false (e == 31)) |
|
|
861 | /* infinity or NaN */ |
|
|
862 | e = 255 - (127 - 15); |
|
|
863 | else if (ecb_expect_false (!e)) |
|
|
864 | { |
|
|
865 | if (ecb_expect_true (!m)) |
|
|
866 | /* zero, handled by code below by forcing e to 0 */ |
|
|
867 | e = 0 - (127 - 15); |
|
|
868 | else |
|
|
869 | { |
|
|
870 | /* subnormal, renormalise */ |
|
|
871 | unsigned int s = 10 - ecb_ld32 (m); |
|
|
872 | |
|
|
873 | m = (m << s) & 0x3ff; /* mask implicit bit */ |
|
|
874 | e -= s - 1; |
|
|
875 | } |
|
|
876 | } |
|
|
877 | |
|
|
878 | /* e and m now are normalised, or zero, (or inf or nan) */ |
|
|
879 | e += 127 - 15; |
|
|
880 | |
|
|
881 | return s | (e << 23) | (m << (23 - 10)); |
|
|
882 | } |
|
|
883 | |
|
|
884 | ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x); |
|
|
885 | ecb_function_ ecb_const uint16_t |
|
|
886 | ecb_binary32_to_binary16 (uint32_t x) |
|
|
887 | { |
|
|
888 | unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */ |
|
|
889 | int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */ |
|
|
890 | unsigned int m = x & 0x007fffff; |
|
|
891 | |
|
|
892 | x &= 0x7fffffff; |
|
|
893 | |
|
|
894 | /* if it's within range of binary16 normals, use fast path */ |
|
|
895 | if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff)) |
|
|
896 | { |
|
|
897 | /* mantissa round-to-even */ |
|
|
898 | m += 0x00000fff + ((m >> (23 - 10)) & 1); |
|
|
899 | |
|
|
900 | /* handle overflow */ |
|
|
901 | if (ecb_expect_false (m >= 0x00800000)) |
|
|
902 | { |
|
|
903 | m >>= 1; |
|
|
904 | e += 1; |
|
|
905 | } |
|
|
906 | |
|
|
907 | return s | (e << 10) | (m >> (23 - 10)); |
|
|
908 | } |
|
|
909 | |
|
|
910 | /* handle large numbers and infinity */ |
|
|
911 | if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000)) |
|
|
912 | return s | 0x7c00; |
|
|
913 | |
|
|
914 | /* handle zero, subnormals and small numbers */ |
|
|
915 | if (ecb_expect_true (x < 0x38800000)) |
|
|
916 | { |
|
|
917 | /* zero */ |
|
|
918 | if (ecb_expect_true (!x)) |
|
|
919 | return s; |
|
|
920 | |
|
|
921 | /* handle subnormals */ |
|
|
922 | |
|
|
923 | /* too small, will be zero */ |
|
|
924 | if (e < (14 - 24)) /* might not be sharp, but is good enough */ |
|
|
925 | return s; |
|
|
926 | |
|
|
927 | m |= 0x00800000; /* make implicit bit explicit */ |
|
|
928 | |
|
|
929 | /* very tricky - we need to round to the nearest e (+10) bit value */ |
|
|
930 | { |
|
|
931 | unsigned int bits = 14 - e; |
|
|
932 | unsigned int half = (1 << (bits - 1)) - 1; |
|
|
933 | unsigned int even = (m >> bits) & 1; |
|
|
934 | |
|
|
935 | /* if this overflows, we will end up with a normalised number */ |
|
|
936 | m = (m + half + even) >> bits; |
|
|
937 | } |
|
|
938 | |
|
|
939 | return s | m; |
|
|
940 | } |
|
|
941 | |
|
|
942 | /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */ |
|
|
943 | m >>= 13; |
|
|
944 | |
|
|
945 | return s | 0x7c00 | m | !m; |
|
|
946 | } |
|
|
947 | |
632 | /*******************************************************************************/ |
948 | /*******************************************************************************/ |
633 | /* floating point stuff, can be disabled by defining ECB_NO_LIBM */ |
949 | /* floating point stuff, can be disabled by defining ECB_NO_LIBM */ |
634 | |
950 | |
635 | /* basically, everything uses "ieee pure-endian" floating point numbers */ |
951 | /* basically, everything uses "ieee pure-endian" floating point numbers */ |
636 | /* the only noteworthy exception is ancient armle, which uses order 43218765 */ |
952 | /* the only noteworthy exception is ancient armle, which uses order 43218765 */ |
637 | #if 0 \ |
953 | #if 0 \ |
638 | || __i386 || __i386__ \ |
954 | || __i386 || __i386__ \ |
639 | || __amd64 || __amd64__ || __x86_64 || __x86_64__ \ |
955 | || ECB_GCC_AMD64 \ |
640 | || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \ |
956 | || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \ |
641 | || defined __s390__ || defined __s390x__ \ |
957 | || defined __s390__ || defined __s390x__ \ |
642 | || defined __mips__ \ |
958 | || defined __mips__ \ |
643 | || defined __alpha__ \ |
959 | || defined __alpha__ \ |
644 | || defined __hppa__ \ |
960 | || defined __hppa__ \ |
645 | || defined __ia64__ \ |
961 | || defined __ia64__ \ |
646 | || defined __m68k__ \ |
962 | || defined __m68k__ \ |
647 | || defined __m88k__ \ |
963 | || defined __m88k__ \ |
648 | || defined __sh__ \ |
964 | || defined __sh__ \ |
649 | || defined _M_IX86 || defined _M_AMD64 || defined _M_IA64 \ |
965 | || defined _M_IX86 || defined ECB_MSVC_AMD64 || defined _M_IA64 \ |
650 | || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \ |
966 | || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \ |
651 | || defined __aarch64__ |
967 | || defined __aarch64__ |
652 | #define ECB_STDFP 1 |
968 | #define ECB_STDFP 1 |
653 | #include <string.h> /* for memcpy */ |
|
|
654 | #else |
969 | #else |
655 | #define ECB_STDFP 0 |
970 | #define ECB_STDFP 0 |
656 | #endif |
971 | #endif |
657 | |
972 | |
658 | #ifndef ECB_NO_LIBM |
973 | #ifndef ECB_NO_LIBM |
… | |
… | |
671 | #else |
986 | #else |
672 | #define ECB_NAN ECB_INFINITY |
987 | #define ECB_NAN ECB_INFINITY |
673 | #endif |
988 | #endif |
674 | |
989 | |
675 | #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L |
990 | #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L |
676 | #define ecb_ldexpf(x,e) ldexpf (x, e) |
991 | #define ecb_ldexpf(x,e) ldexpf ((x), (e)) |
|
|
992 | #define ecb_frexpf(x,e) frexpf ((x), (e)) |
677 | #else |
993 | #else |
678 | #define ecb_ldexpf(x,e) (float) ldexp ((double)(x), e) |
994 | #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e)) |
|
|
995 | #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e)) |
679 | #endif |
996 | #endif |
680 | |
|
|
681 | /* converts an ieee half/binary16 to a float */ |
|
|
682 | ecb_function_ float ecb_binary16_to_float (uint16_t x) ecb_const; |
|
|
683 | ecb_function_ float |
|
|
684 | ecb_binary16_to_float (uint16_t x) |
|
|
685 | { |
|
|
686 | int e = (x >> 10) & 0x1f; |
|
|
687 | int m = x & 0x3ff; |
|
|
688 | float r; |
|
|
689 | |
|
|
690 | if (!e ) r = ecb_ldexpf (m , -24); |
|
|
691 | else if (e != 31) r = ecb_ldexpf (m + 0x400, e - 25); |
|
|
692 | else if (m ) r = ECB_NAN; |
|
|
693 | else r = ECB_INFINITY; |
|
|
694 | |
|
|
695 | return x & 0x8000 ? -r : r; |
|
|
696 | } |
|
|
697 | |
997 | |
698 | /* convert a float to ieee single/binary32 */ |
998 | /* convert a float to ieee single/binary32 */ |
699 | ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const; |
999 | ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); |
700 | ecb_function_ uint32_t |
1000 | ecb_function_ ecb_const uint32_t |
701 | ecb_float_to_binary32 (float x) |
1001 | ecb_float_to_binary32 (float x) |
702 | { |
1002 | { |
703 | uint32_t r; |
1003 | uint32_t r; |
704 | |
1004 | |
705 | #if ECB_STDFP |
1005 | #if ECB_STDFP |
… | |
… | |
712 | if (x == 0e0f ) return 0x00000000U; |
1012 | if (x == 0e0f ) return 0x00000000U; |
713 | if (x > +3.40282346638528860e+38f) return 0x7f800000U; |
1013 | if (x > +3.40282346638528860e+38f) return 0x7f800000U; |
714 | if (x < -3.40282346638528860e+38f) return 0xff800000U; |
1014 | if (x < -3.40282346638528860e+38f) return 0xff800000U; |
715 | if (x != x ) return 0x7fbfffffU; |
1015 | if (x != x ) return 0x7fbfffffU; |
716 | |
1016 | |
717 | m = frexpf (x, &e) * 0x1000000U; |
1017 | m = ecb_frexpf (x, &e) * 0x1000000U; |
718 | |
1018 | |
719 | r = m & 0x80000000U; |
1019 | r = m & 0x80000000U; |
720 | |
1020 | |
721 | if (r) |
1021 | if (r) |
722 | m = -m; |
1022 | m = -m; |
… | |
… | |
734 | |
1034 | |
735 | return r; |
1035 | return r; |
736 | } |
1036 | } |
737 | |
1037 | |
738 | /* converts an ieee single/binary32 to a float */ |
1038 | /* converts an ieee single/binary32 to a float */ |
739 | ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const; |
1039 | ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x); |
740 | ecb_function_ float |
1040 | ecb_function_ ecb_const float |
741 | ecb_binary32_to_float (uint32_t x) |
1041 | ecb_binary32_to_float (uint32_t x) |
742 | { |
1042 | { |
743 | float r; |
1043 | float r; |
744 | |
1044 | |
745 | #if ECB_STDFP |
1045 | #if ECB_STDFP |
… | |
… | |
764 | |
1064 | |
765 | return r; |
1065 | return r; |
766 | } |
1066 | } |
767 | |
1067 | |
768 | /* convert a double to ieee double/binary64 */ |
1068 | /* convert a double to ieee double/binary64 */ |
769 | ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const; |
1069 | ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x); |
770 | ecb_function_ uint64_t |
1070 | ecb_function_ ecb_const uint64_t |
771 | ecb_double_to_binary64 (double x) |
1071 | ecb_double_to_binary64 (double x) |
772 | { |
1072 | { |
773 | uint64_t r; |
1073 | uint64_t r; |
774 | |
1074 | |
775 | #if ECB_STDFP |
1075 | #if ECB_STDFP |
… | |
… | |
804 | |
1104 | |
805 | return r; |
1105 | return r; |
806 | } |
1106 | } |
807 | |
1107 | |
808 | /* converts an ieee double/binary64 to a double */ |
1108 | /* converts an ieee double/binary64 to a double */ |
809 | ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const; |
1109 | ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x); |
810 | ecb_function_ double |
1110 | ecb_function_ ecb_const double |
811 | ecb_binary64_to_double (uint64_t x) |
1111 | ecb_binary64_to_double (uint64_t x) |
812 | { |
1112 | { |
813 | double r; |
1113 | double r; |
814 | |
1114 | |
815 | #if ECB_STDFP |
1115 | #if ECB_STDFP |
… | |
… | |
833 | #endif |
1133 | #endif |
834 | |
1134 | |
835 | return r; |
1135 | return r; |
836 | } |
1136 | } |
837 | |
1137 | |
838 | #endif |
1138 | /* convert a float to ieee half/binary16 */ |
|
|
1139 | ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x); |
|
|
1140 | ecb_function_ ecb_const uint16_t |
|
|
1141 | ecb_float_to_binary16 (float x) |
|
|
1142 | { |
|
|
1143 | return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x)); |
|
|
1144 | } |
839 | |
1145 | |
840 | #endif |
1146 | /* convert an ieee half/binary16 to float */ |
|
|
1147 | ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x); |
|
|
1148 | ecb_function_ ecb_const float |
|
|
1149 | ecb_binary16_to_float (uint16_t x) |
|
|
1150 | { |
|
|
1151 | return ecb_binary32_to_float (ecb_binary16_to_binary32 (x)); |
|
|
1152 | } |
841 | |
1153 | |
|
|
1154 | #endif |
|
|
1155 | |
|
|
1156 | #endif |
|
|
1157 | |