rename __call__ to mf in cython.mf
[liba.git] / include / a / a.h
blob8f3eb68a7466abb60b463a74e82636711eef2fc1
1 /*!
2 @file a.h
3 @brief algorithm library
4 */
6 #ifndef LIBA_A_H
7 #define LIBA_A_H
9 #if defined(A_HAVE_H)
10 #include A_HAVE_H
11 #endif /* A_HAVE_H */
13 /*! @cond */
15 #if !defined __has_attribute
16 #define __has_attribute(x) 0
17 #endif /* __has_attribute */
18 #if !defined __has_builtin
19 #define __has_builtin(x) 0
20 #endif /* __has_builtin */
21 #if !defined __has_feature
22 #define __has_feature(x) 0
23 #endif /* __has_feature */
24 #if !defined __has_include
25 #define __has_include(x) 0
26 #endif /* __has_include */
27 #if !defined __has_warning
28 #define __has_warning(x) 0
29 #endif /* __has_warning */
30 #if !defined A_HAVE_INLINE
31 #define A_HAVE_INLINE 1
32 #elif A_HAVE_INLINE + 0 < 1
33 #undef A_HAVE_INLINE
34 #endif /* A_HAVE_INLINE */
35 #if defined(__GNUC__) || \
36 defined(__clang__)
37 #pragma GCC system_header
38 #endif /* __GNUC__ */
40 #if defined(__STDC_LIB_EXT1__)
41 #if !defined __STDC_WANT_LIB_EXT1__
42 #define __STDC_WANT_LIB_EXT1__ 1
43 #endif /* __STDC_WANT_LIB_EXT1__ */
44 #endif /* __STDC_LIB_EXT1__ */
46 #if defined(__MINGW32__)
47 #undef __USE_MINGW_ANSI_STDIO
48 #define __USE_MINGW_ANSI_STDIO 1
49 #endif /* __MINGW32__ */
51 /* https://en.wikipedia.org/wiki/Microsoft_Visual_C++ */
52 #if defined(_MSC_VER)
53 #define A_PREREQ_MSVC(maj, min) (_MSC_VER >= (maj * 100 + min))
54 #else /* !_MSC_VER */
55 #define A_PREREQ_MSVC(maj, min) 0
56 #endif /* _MSC_VER */
58 /* https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html */
59 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
60 #define A_PREREQ_GNUC(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
61 #else /* !__GNUC__ */
62 #define A_PREREQ_GNUC(maj, min) 0
63 #endif /* __GNUC__ */
65 /* https://clang.llvm.org/docs/LanguageExtensions.html */
66 #if defined(__clang_major__) && defined(__clang_minor__)
67 #define A_PREREQ_LLVM(maj, min) ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
68 #else /* !__clang__ */
69 #define A_PREREQ_LLVM(maj, min) 0
70 #endif /* __clang__ */
72 #if __has_warning("-Wdisabled-macro-expansion")
73 #pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
74 #endif /* -Wdisabled-macro-expansion */
76 #if !defined A_FUNC
77 #if defined(_MSC_VER)
78 #define A_FUNC __FUNCTION__
79 #elif defined(__GNUC__)
80 #define A_FUNC (__extension__ __PRETTY_FUNCTION__)
81 #elif defined(__cplusplus) && (__cplusplus > 201100L) || \
82 defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L)
83 #define A_FUNC __func__
84 #else /* !__func__ */
85 #define A_FUNC __FUNCTION__
86 #endif /* __func__ */
87 #endif /* A_FUNC */
89 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L) || \
90 defined(__cplusplus) && (__cplusplus > 201100L) || A_PREREQ_MSVC(18, 0)
92 #if !defined A_HAVE_VARIADIC_MACROS
93 #define A_HAVE_VARIADIC_MACROS 1
94 #endif /* A_HAVE_VARIADIC_MACROS */
96 #if !defined A_HAVE_LONG_LONG_TYPE
97 #define A_HAVE_LONG_LONG_TYPE 1
98 #endif /* A_HAVE_LONG_LONG_TYPE */
100 #endif /* C > 199900 or C++ > 201100 */
101 #if defined(A_HAVE_VARIADIC_MACROS) && (A_HAVE_VARIADIC_MACROS + 0 < 1)
102 #undef A_HAVE_VARIADIC_MACROS
103 #endif /* A_HAVE_VARIADIC_MACROS */
104 #if defined(A_HAVE_LONG_LONG_TYPE) && (A_HAVE_LONG_LONG_TYPE + 0 < 1)
105 #undef A_HAVE_LONG_LONG_TYPE
106 #endif /* A_HAVE_LONG_LONG_TYPE */
108 #if defined(__cplusplus) && (__cplusplus > 201100L) || \
109 defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201100L)
111 #if !defined A_HAVE_STATIC_ASSERT
112 #define A_HAVE_STATIC_ASSERT 1
113 #endif /* A_HAVE_STATIC_ASSERT */
115 #endif /* C > 201100 or C++ > 201100 */
116 #if defined(A_HAVE_STATIC_ASSERT) && (A_HAVE_STATIC_ASSERT + 0 < 1)
117 #undef A_HAVE_STATIC_ASSERT
118 #endif /* A_HAVE_STATIC_ASSERT */
120 #if defined(__cplusplus) && (__cplusplus > 201100L)
122 #if !defined A_HAVE_NULLPTR
123 #define A_HAVE_NULLPTR 1
124 #endif /* A_HAVE_NULLPTR */
126 #endif /* C++ > 201100 */
127 #if defined(A_HAVE_NULLPTR) && (A_HAVE_NULLPTR + 0 < 1)
128 #undef A_HAVE_NULLPTR
129 #endif /* A_HAVE_NULLPTR */
131 #if A_PREREQ_GNUC(2, 96) || __has_builtin(__builtin_expect)
132 #define a_unlikely(x) __builtin_expect(!!(x), 0)
133 #define a_likely(x) __builtin_expect(!!(x), 1)
134 #else /* !__GNUC__ */
135 #define a_unlikely(x) (x)
136 #define a_likely(x) (x)
137 #endif /* __GNUC__ */
139 #if defined(_WIN32) || defined(__CYGWIN__)
140 #define A_DECLSPEC(x) __declspec(x)
141 #else /* !__declspec */
142 #define A_DECLSPEC(x)
143 #endif /* __declspec */
145 #if defined(__GNUC__) || defined(__clang__)
146 #define A_ATTRIBUTE(x) __attribute__(x)
147 #else /* !__attribute__ */
148 #define A_ATTRIBUTE(x)
149 #endif /* __attribute__ */
151 /* attribute format */
152 #if A_PREREQ_GNUC(2, 4) || __has_attribute(format)
153 #define A_FORMAT(_, a, b) __attribute__((format(_, a, b)))
154 #else /* !format */
155 #define A_FORMAT(_, a, b)
156 #endif /* format */
158 /* attribute fallthrough */
159 #if A_PREREQ_GNUC(7, 1) || __has_attribute(fallthrough)
160 #define A_FALLTHROUGH __attribute__((fallthrough))
161 #else /* !fallthrough */
162 #define A_FALLTHROUGH ((void)(0))
163 #endif /* fallthrough */
165 /* attribute deprecated */
166 #if A_PREREQ_GNUC(3, 2) || __has_attribute(deprecated)
167 #define A_DEPRECATED __attribute__((deprecated))
168 #elif defined(_WIN32) || defined(__CYGWIN__)
169 #define A_DEPRECATED __declspec(deprecated)
170 #else /* !deprecated */
171 #define A_DEPRECATED
172 #endif /* deprecated */
174 /* attribute always inline */
175 #if A_PREREQ_GNUC(3, 2) || __has_attribute(always_inline)
176 #define A_INLINE __inline __attribute__((always_inline))
177 #elif defined(_MSC_VER)
178 #define A_INLINE __inline __forceinline
179 #else /* !_MSC_VER */
180 #define A_INLINE __inline
181 #endif /* _MSC_VER */
182 #if !defined A_INTERN
183 #define A_INTERN static A_INLINE
184 #endif /* A_INTERN */
186 /* attribute visibility */
187 #if defined(_WIN32) || defined(__CYGWIN__)
188 #define A_EXPORT __declspec(dllexport)
189 #define A_IMPORT __declspec(dllimport)
190 #define A_HIDDEN
191 #elif A_PREREQ_GNUC(4, 0) || __has_attribute(visibility)
192 #define A_EXPORT __attribute__((visibility("default")))
193 #define A_IMPORT __attribute__((visibility("default")))
194 #define A_HIDDEN __attribute__((visibility("hidden")))
195 #else /* !visibility */
196 #define A_EXPORT
197 #define A_IMPORT
198 #define A_HIDDEN
199 #endif /* visibility */
200 #if defined(A_EXPORTS)
201 #define A_PUBLIC A_EXPORT
202 #elif defined(A_IMPORTS)
203 #define A_PUBLIC A_IMPORT
204 #else /* !A_PUBLIC */
205 #define A_PUBLIC
206 #endif /* A_PUBLIC */
208 #if !defined A_EXTERN
209 #define A_EXTERN A_PUBLIC extern
210 #endif /* A_EXTERN */
211 #if !defined __cplusplus
212 #define A_EXTERN_C
213 #define A_EXTERN_C_ENTER
214 #define A_EXTERN_C_LEAVE
215 #else /* !__cplusplus */
216 #define A_EXTERN_C extern "C"
217 #define A_EXTERN_C_ENTER extern "C" {
218 #define A_EXTERN_C_LEAVE }
219 #endif /* __cplusplus */
221 /*! @endcond */
223 /* byte order of little endian architecture */
224 #if defined(__ORDER_LITTLE_ENDIAN__)
225 #define A_ORDER_LITTLE __ORDER_LITTLE_ENDIAN__
226 #else /* !__ORDER_LITTLE_ENDIAN__ */
227 #define A_ORDER_LITTLE 1234
228 #endif /* __ORDER_LITTLE_ENDIAN__ */
229 /* byte order of big endian architecture */
230 #if defined(__ORDER_BIG_ENDIAN__)
231 #define A_ORDER_BIG __ORDER_BIG_ENDIAN__
232 #else /* !__ORDER_BIG_ENDIAN__ */
233 #define A_ORDER_BIG 4321
234 #endif /* __ORDER_BIG_ENDIAN__ */
235 /* byte order of architecture */
236 #if !defined A_BYTE_ORDER
237 #if defined(__BYTE_ORDER__)
238 #define A_BYTE_ORDER __BYTE_ORDER__
239 #endif /* __BYTE_ORDER__ */
240 #endif /* A_BYTE_ORDER */
242 /* size of void pointer */
243 #if !defined A_SIZE_POINTER
244 #if defined(__SIZEOF_POINTER__)
245 #define A_SIZE_POINTER __SIZEOF_POINTER__
246 #elif defined(_WIN64)
247 #define A_SIZE_POINTER 8
248 #elif defined(_WIN32)
249 #define A_SIZE_POINTER 4
250 #endif /* __SIZEOF_POINTER__ */
251 #endif /* A_SIZE_POINTER */
253 #include <stddef.h>
254 #include <limits.h>
255 #include <stdint.h>
256 #include <float.h>
259 @addtogroup A algorithm library
263 /*! assert a build-time dependency, as an expression */
264 #define A_ASSERT_BUILD(x) ((void)(sizeof(char[1 - 2 * !(x)]) - 1))
266 #if defined(__cplusplus)
267 #define a_cast_r(T, x) reinterpret_cast<T>(x)
268 #define a_cast_d(T, x) dynamic_cast<T>(x)
269 #define a_cast_s(T, x) static_cast<T>(x)
270 #define a_cast_c(T, x) const_cast<T>(x)
271 #else /* !__cplusplus */
272 #define a_cast_r(T, x) ((T)(x))
273 #define a_cast_d(T, x) ((T)(x))
274 #define a_cast_s(T, x) ((T)(x))
275 #define a_cast_c(T, x) ((T)(x))
276 #endif /* __cplusplus */
277 #define A_CAST_3(a, b, c) a##b##c
278 #define A_CAST_2(a, b) a##b
279 #define A_CAST_1(a) #a
281 #if defined(__cplusplus) && defined(A_HAVE_NULLPTR)
282 #define A_NULL nullptr
283 #else /* !__cplusplus */
284 #define A_NULL NULL
285 #endif /* __cplusplus */
286 #if !defined A_VOID
287 #define A_VOID void
288 #endif /* A_VOID */
289 /*! static cast to \ref a_void */
290 #define a_void_c(_, x) a_cast_s(A_VOID, x)
291 #define a_void_(_, x) a_cast_s(A_VOID _, x)
292 /*! incomplete type or no parameter or no return value */
293 #define a_void A_VOID
295 #if defined(__cplusplus)
296 #define A_TRUE true
297 #define A_FALSE false
298 #if !defined A_BOOL
299 #define A_BOOL bool
300 #endif /* A_BOOL */
301 #else /* !__cplusplus */
302 #define A_TRUE 1
303 #define A_FALSE 0
304 #if !defined A_BOOL
305 #define A_BOOL _Bool
306 #endif /* A_BOOL */
307 #endif /* __cplusplus */
308 /*! static cast to \ref a_bool */
309 #define a_bool_c(x) (!!(x))
310 /*! type, capable of holding one of the two values: 1 and 0 */
311 #define a_bool A_BOOL
313 #define A_INT int
314 #define A_INT_MIN INT_MIN
315 #define A_INT_MAX INT_MAX
316 /*! static cast to \ref a_int */
317 #define a_int_c(x) a_cast_s(A_INT, x)
318 #define a_int_(_, x) a_cast_s(A_INT _, x)
319 /*! signed integer type is guaranteed to be at least 16 bits */
320 #define a_int A_INT
322 #define A_UINT unsigned int
323 #define A_UINT_MAX UINT_MAX
324 /*! static cast to \ref a_uint */
325 #define a_uint_c(x) a_cast_s(A_UINT, x)
326 #define a_uint_(_, x) a_cast_s(A_UINT _, x)
327 /*! unsigned integer type is guaranteed to be at least 16 bits */
328 #define a_uint A_UINT
330 #define A_SHRT short
331 #define A_SHRT_MIN SHRT_MIN
332 #define A_SHRT_MAX SHRT_MAX
333 /*! static cast to \ref a_shrt */
334 #define a_shrt_c(x) a_cast_s(A_SHRT, x)
335 #define a_shrt_(_, x) a_cast_s(A_SHRT _, x)
336 /*! signed integer type is guaranteed to be at least 16 bits */
337 #define a_shrt A_SHRT
339 #define A_USHRT unsigned short
340 #define A_USHRT_MAX USHRT_MAX
341 /*! static cast to \ref a_ushrt */
342 #define a_ushrt_c(x) a_cast_s(A_USHRT, x)
343 #define a_ushrt_(_, x) a_cast_s(A_USHRT _, x)
344 /*! unsigned integer type is guaranteed to be at least 16 bits */
345 #define a_ushrt A_USHRT
347 #define A_LONG long
348 #define A_LONG_MIN LONG_MIN
349 #define A_LONG_MAX LONG_MAX
350 /*! static cast to \ref a_long */
351 #define a_long_c(x) a_cast_s(A_LONG, x)
352 #define a_long_(_, x) a_cast_s(A_LONG _, x)
353 /*! signed integer type is guaranteed to be at least 32 bits */
354 #define a_long A_LONG
356 #define A_ULONG unsigned long
357 #define A_ULONG_MAX ULONG_MAX
358 /*! static cast to \ref a_ulong */
359 #define a_ulong_c(x) a_cast_s(A_ULONG, x)
360 #define a_ulong_(_, x) a_cast_s(A_ULONG _, x)
361 /*! unsigned integer type is guaranteed to be at least 32 bits */
362 #define a_ulong A_ULONG
364 #if defined(A_HAVE_LONG_LONG_TYPE)
366 #define A_LLONG long long
367 #define A_LLONG_MIN LLONG_MIN
368 #define A_LLONG_MAX LLONG_MAX
369 /*! static cast to \ref a_llong */
370 #define a_llong_c(x) a_cast_s(A_LLONG, x)
371 #define a_llong_(_, x) a_cast_s(A_LLONG _, x)
372 /*! signed integer type is guaranteed to be at least 64 bits */
373 #define a_llong A_LLONG
375 #define A_ULLONG unsigned long long
376 #define A_ULLONG_MAX ULLONG_MAX
377 /*! static cast to \ref a_ullong */
378 #define a_ullong_c(x) a_cast_s(A_ULLONG, x)
379 #define a_ullong_(_, x) a_cast_s(A_ULLONG _, x)
380 /*! unsigned integer type is guaranteed to be at least 64 bits */
381 #define a_ullong A_ULLONG
383 #endif /* A_HAVE_LONG_LONG_TYPE */
385 #define A_BYTE unsigned char
386 #define A_BYTE_MAX UCHAR_MAX
387 /*! static cast to \ref a_byte */
388 #define a_byte_c(x) a_cast_s(A_BYTE, x)
389 #define a_byte_(_, x) a_cast_s(A_BYTE _, x)
390 /*! type for unsigned character representation */
391 #define a_byte A_BYTE
393 #define A_C8 char
394 #define A_C8_MIN CHAR_MIN
395 #define A_C8_MAX CHAR_MAX
396 /*! static cast to \ref a_c8 */
397 #define a_c8_c(x) a_cast_s(A_C8, x)
398 #define a_c8_(_, x) a_cast_s(A_C8 _, x)
399 /*! type for character representation */
400 #define a_c8 A_C8
402 #if !defined A_I8
403 #define A_I8 int8_t
404 #endif /* A_I8 */
405 #if !defined A_I8_C && defined(INT8_C)
406 #define A_I8_C(X) INT8_C(X)
407 #endif /* A_I8_C */
408 #if !defined A_I8_MIN && defined(INT8_MIN)
409 #define A_I8_MIN INT8_MIN
410 #endif /* A_I8_MIN */
411 #if !defined A_I8_MAX && defined(INT8_MAX)
412 #define A_I8_MAX INT8_MAX
413 #endif /* A_I8_MAX */
414 /*! static cast to \ref a_i8 */
415 #define a_i8_c(x) a_cast_s(A_I8, x)
416 #define a_i8_(_, x) a_cast_s(A_I8 _, x)
417 /*! signed integer type with width of exactly 8 bits */
418 #define a_i8 A_I8
420 #if !defined A_U8
421 #define A_U8 uint8_t
422 #endif /* A_U8 */
423 #if !defined A_U8_C && defined(UINT8_C)
424 #define A_U8_C(X) UINT8_C(X)
425 #endif /* A_U8_C */
426 #if !defined A_U8_MAX && defined(UINT8_MAX)
427 #define A_U8_MAX UINT8_MAX
428 #endif /* A_U8_MAX */
429 /*! static cast to \ref a_u8 */
430 #define a_u8_c(x) a_cast_s(A_U8, x)
431 #define a_u8_(_, x) a_cast_s(A_U8 _, x)
432 /*! unsigned integer type with width of exactly 8 bits */
433 #define a_u8 A_U8
435 #if !defined A_I16
436 #define A_I16 int16_t
437 #endif /* A_I16 */
438 #if !defined A_I16_C && defined(INT16_C)
439 #define A_I16_C(X) INT16_C(X)
440 #endif /* A_I16_C */
441 #if !defined A_I16_MIN && defined(INT16_MIN)
442 #define A_I16_MIN INT16_MIN
443 #endif /* A_I16_MIN */
444 #if !defined A_I16_MAX && defined(INT16_MAX)
445 #define A_I16_MAX INT16_MAX
446 #endif /* A_I16_MAX */
447 /*! static cast to \ref a_i16 */
448 #define a_i16_c(x) a_cast_s(A_I16, x)
449 #define a_i16_(_, x) a_cast_s(A_I16 _, x)
450 /*! signed integer type with width of exactly 16 bits */
451 #define a_i16 A_I16
453 #if !defined A_U16
454 #define A_U16 uint16_t
455 #endif /* A_U16 */
456 #if !defined A_U16_C && defined(UINT16_C)
457 #define A_U16_C(X) UINT16_C(X)
458 #endif /* A_U16_C */
459 #if !defined A_U16_MAX && defined(UINT16_MAX)
460 #define A_U16_MAX UINT16_MAX
461 #endif /* A_U16_MAX */
462 /*! static cast to \ref a_u16 */
463 #define a_u16_c(x) a_cast_s(A_U16, x)
464 #define a_u16_(_, x) a_cast_s(A_U16 _, x)
465 /*! unsigned integer type with width of exactly 16 bits */
466 #define a_u16 A_U16
468 #if !defined A_I32
469 #define A_I32 int32_t
470 #endif /* A_I32 */
471 #if !defined A_I32_C && defined(INT32_C)
472 #define A_I32_C(X) INT32_C(X)
473 #endif /* A_I32_C */
474 #if !defined A_I32_MIN && defined(INT32_MIN)
475 #define A_I32_MIN INT32_MIN
476 #endif /* A_I32_MIN */
477 #if !defined A_I32_MAX && defined(INT32_MAX)
478 #define A_I32_MAX INT32_MAX
479 #endif /* A_I32_MAX */
480 /*! static cast to \ref a_i32 */
481 #define a_i32_c(x) a_cast_s(A_I32, x)
482 #define a_i32_(_, x) a_cast_s(A_I32 _, x)
483 /*! signed integer type with width of exactly 32 bits */
484 #define a_i32 A_I32
486 #if !defined A_U32
487 #define A_U32 uint32_t
488 #endif /* A_U32 */
489 #if !defined A_U32_C && defined(UINT32_C)
490 #define A_U32_C(X) UINT32_C(X)
491 #endif /* A_U32_C */
492 #if !defined A_U32_MAX && defined(UINT32_MAX)
493 #define A_U32_MAX UINT32_MAX
494 #endif /* A_U32_MAX */
495 /*! static cast to \ref a_u32 */
496 #define a_u32_c(x) a_cast_s(A_U32, x)
497 #define a_u32_(_, x) a_cast_s(A_U32 _, x)
498 /*! unsigned integer type with width of exactly 32 bits */
499 #define a_u32 A_U32
501 #if !defined A_I64
502 #define A_I64 int64_t
503 #endif /* A_I64 */
504 #if !defined A_I64_C && defined(INT64_C)
505 #define A_I64_C(X) INT64_C(X)
506 #endif /* A_I64_C */
507 #if !defined A_I64_MIN && defined(INT64_MIN)
508 #define A_I64_MIN INT64_MIN
509 #endif /* A_I64_MIN */
510 #if !defined A_I64_MAX && defined(INT64_MAX)
511 #define A_I64_MAX INT64_MAX
512 #endif /* A_I64_MAX */
513 /*! static cast to \ref a_i64 */
514 #define a_i64_c(x) a_cast_s(A_I64, x)
515 #define a_i64_(_, x) a_cast_s(A_I64 _, x)
516 /*! signed integer type with width of exactly 64 bits */
517 #define a_i64 A_I64
519 #if !defined A_U64
520 #define A_U64 uint64_t
521 #endif /* A_U64 */
522 #if !defined A_U64_C && defined(UINT64_C)
523 #define A_U64_C(X) UINT64_C(X)
524 #endif /* A_U64_C */
525 #if !defined A_U64_MAX && defined(UINT64_MAX)
526 #define A_U64_MAX UINT64_MAX
527 #endif /* A_U64_MAX */
528 /*! static cast to \ref a_u64 */
529 #define a_u64_c(x) a_cast_s(A_U64, x)
530 #define a_u64_(_, x) a_cast_s(A_U64 _, x)
531 /*! unsigned integer type with width of exactly 64 bits */
532 #define a_u64 A_U64
534 #if !defined A_IMAX
535 #define A_IMAX intmax_t
536 #endif /* A_IMAX */
537 #if !defined A_IMAX_C && defined(INTMAX_C)
538 #define A_IMAX_C(X) INTMAX_C(X)
539 #endif /* A_IMAX_C */
540 #if !defined A_IMAX_MIN && defined(INTMAX_MIN)
541 #define A_IMAX_MIN INTMAX_MIN
542 #endif /* A_IMAX_MIN */
543 #if !defined A_IMAX_MAX && defined(INTMAX_MAX)
544 #define A_IMAX_MAX INTMAX_MAX
545 #endif /* A_IMAX_MAX */
546 /*! static cast to \ref a_imax */
547 #define a_imax_c(x) a_cast_s(A_IMAX, x)
548 #define a_imax_(_, x) a_cast_s(A_IMAX _, x)
549 /*! maximum-width signed integer type */
550 #define a_imax A_IMAX
552 #if !defined A_UMAX
553 #define A_UMAX uintmax_t
554 #endif /* A_UMAX */
555 #if !defined A_UMAX_C && defined(UINTMAX_C)
556 #define A_UMAX_C(X) UINTMAX_C(X)
557 #endif /* A_UMAX_C */
558 #if !defined A_UMAX_MAX && defined(UINTMAX_MAX)
559 #define A_UMAX_MAX UINTMAX_MAX
560 #endif /* A_UMAX_MAX */
561 /*! static cast to \ref a_umax */
562 #define a_umax_c(x) a_cast_s(A_UMAX, x)
563 #define a_umax_(_, x) a_cast_s(A_UMAX _, x)
564 /*! maximum-width unsigned integer type */
565 #define a_umax A_UMAX
567 #if !defined A_IPTR
568 #define A_IPTR intptr_t
569 #endif /* A_IPTR */
570 #if !defined A_IPTR_MIN && defined(INTPTR_MIN)
571 #define A_IPTR_MIN INTPTR_MIN
572 #endif /* A_IPTR_MIN */
573 #if !defined A_IPTR_MAX && defined(INTPTR_MAX)
574 #define A_IPTR_MAX INTPTR_MAX
575 #endif /* A_IPTR_MAX */
576 /*! static cast to \ref a_iptr */
577 #define a_iptr_c(x) a_cast_s(A_IPTR, x)
578 #define a_iptr_(_, x) a_cast_s(A_IPTR _, x)
579 /*! signed integer type capable of holding a pointer to void */
580 #define a_iptr A_IPTR
582 #if !defined A_UPTR
583 #define A_UPTR uintptr_t
584 #endif /* A_UPTR */
585 #if !defined A_UPTR_MAX && defined(UINTPTR_MAX)
586 #define A_UPTR_MAX UINTPTR_MAX
587 #endif /* A_UPTR_MAX */
588 /*! static cast to \ref a_uptr */
589 #define a_uptr_c(x) a_cast_s(A_UPTR, x)
590 #define a_uptr_(_, x) a_cast_s(A_UPTR _, x)
591 /*! unsigned integer type capable of holding a pointer to void */
592 #define a_uptr A_UPTR
594 #if !defined A_DIFF
595 #define A_DIFF ptrdiff_t
596 #endif /* A_DIFF */
597 #if !defined A_DIFF_MIN && defined(PTRDIFF_MIN)
598 #define A_DIFF_MIN PTRDIFF_MIN
599 #endif /* A_DIFF_MIN */
600 #if !defined A_DIFF_MAX && defined(PTRDIFF_MAX)
601 #define A_DIFF_MAX PTRDIFF_MAX
602 #endif /* A_DIFF_MAX */
603 /*! static cast to \ref a_diff */
604 #define a_diff_c(x) a_cast_s(A_DIFF, x)
605 #define a_diff_(_, x) a_cast_s(A_DIFF _, x)
606 /*! signed integer type returned when subtracting two pointers */
607 #define a_diff A_DIFF
609 #if !defined A_SIZE
610 #define A_SIZE size_t
611 #endif /* A_SIZE */
612 #if !defined A_SIZE_MAX && defined(SIZE_MAX)
613 #define A_SIZE_MAX SIZE_MAX
614 #endif /* A_SIZE_MAX */
615 /*! static cast to \ref a_size */
616 #define a_size_c(x) a_cast_s(A_SIZE, x)
617 #define a_size_(_, x) a_cast_s(A_SIZE _, x)
618 /*! unsigned integer type returned by the sizeof operator */
619 #define a_size A_SIZE
621 #define A_F16_NNAN A_U16_C(0xFE00)
622 #define A_F16_PNAN A_U16_C(0x7E00)
623 #define A_F16_NINF A_U16_C(0xFC00)
624 #define A_F16_PINF A_U16_C(0x7C00)
626 #define A_F32 float
627 #define A_F32_C(X) A_CAST_2(X, F)
628 #define A_F32_F(F) A_CAST_2(F, f)
629 #define A_F32_F1(F, a) A_CAST_2(F, f)(a)
630 #define A_F32_F2(F, a, b) A_CAST_2(F, f)(a, b)
631 #define A_F32_F3(F, a, b, c) A_CAST_2(F, f)(a, b, c)
632 #if defined(A_HAVE_VARIADIC_MACROS)
633 #define A_F32_FN(F, ...) A_CAST_2(F, f)(__VA_ARGS__)
634 #endif /* A_HAVE_VARIADIC_MACROS */
635 #define A_F32_DIG FLT_DIG
636 #define A_F32_EPSILON FLT_EPSILON
637 #define A_F32_MANT_DIG FLT_MANT_DIG
638 #define A_F32_MAX FLT_MAX
639 #define A_F32_MAX_10_EXP FLT_MAX_10_EXP
640 #define A_F32_MAX_EXP FLT_MAX_EXP
641 #define A_F32_MIN FLT_MIN
642 #define A_F32_MIN_10_EXP FLT_MIN_10_EXP
643 #define A_F32_MIN_EXP FLT_MIN_EXP
644 #define A_F32_INF a_cast_s(A_F32, A_F64_INF)
645 #define A_F32_NAN (A_F32_C(0.0) * A_F32_INF)
646 #define A_F32_NNAN A_U32_C(0xFFC00000)
647 #define A_F32_PNAN A_U32_C(0x7FC00000)
648 #define A_F32_NINF A_U32_C(0xFF800000)
649 #define A_F32_PINF A_U32_C(0x7F800000)
650 /*! format constants for the fprintf family of functions */
651 #define A_F32_PRI(F, C) "%" F C
652 /*! format constants for the fscanf family of functions */
653 #define A_F32_SCN(F, C) "%" F C
654 /*! static cast to \ref a_f32 */
655 #define a_f32_c(x) a_cast_s(A_F32, x)
656 #define a_f32_(_, x) a_cast_s(A_F32 _, x)
657 /*! single precision floating point type. Matches IEEE-754 binary32 format if supported. */
658 #define a_f32 A_F32
660 #define A_F64 double
661 #define A_F64_C(X) X
662 #define A_F64_F(F) F
663 #define A_F64_F1(F, a) F(a)
664 #define A_F64_F2(F, a, b) F(a, b)
665 #define A_F64_F3(F, a, b, c) F(a, b, c)
666 #if defined(A_HAVE_VARIADIC_MACROS)
667 #define A_F64_FN(F, ...) F(__VA_ARGS__)
668 #endif /* A_HAVE_VARIADIC_MACROS */
669 #define A_F64_DIG DBL_DIG
670 #define A_F64_EPSILON DBL_EPSILON
671 #define A_F64_MANT_DIG DBL_MANT_DIG
672 #define A_F64_MAX DBL_MAX
673 #define A_F64_MAX_10_EXP DBL_MAX_10_EXP
674 #define A_F64_MAX_EXP DBL_MAX_EXP
675 #define A_F64_MIN DBL_MIN
676 #define A_F64_MIN_10_EXP DBL_MIN_10_EXP
677 #define A_F64_MIN_EXP DBL_MIN_EXP
678 #define A_F64_INF (DBL_MAX * DBL_MAX)
679 #define A_F64_NAN (A_F64_C(0.0) * A_F64_INF)
680 #define A_F64_NNAN A_U64_C(0xFFF8000000000000)
681 #define A_F64_PNAN A_U64_C(0x7FF8000000000000)
682 #define A_F64_NINF A_U64_C(0xFFF0000000000000)
683 #define A_F64_PINF A_U64_C(0x7FF0000000000000)
684 /*! format constants for the fprintf family of functions */
685 #define A_F64_PRI(F, C) "%" F "l" C
686 /*! format constants for the fscanf family of functions */
687 #define A_F64_SCN(F, C) "%" F "l" C
688 /*! static cast to \ref a_f64 */
689 #define a_f64_c(x) a_cast_s(A_F64, x)
690 #define a_f64_(_, x) a_cast_s(A_F64 _, x)
691 /*! double precision floating point type. Matches IEEE-754 binary64 format if supported. */
692 #define a_f64 A_F64
695 @addtogroup A_FLOAT floating-point number
699 /*! floating-point number bytes */
700 #if !defined A_FLOAT_TYPE
701 #if !defined A_SIZE_FLOAT
702 #define A_FLOAT_TYPE A_FLOAT_DOUBLE
703 #else /* !A_SIZE_FLOAT */
704 #define A_FLOAT_TYPE A_SIZE_FLOAT
705 #endif /* A_SIZE_FLOAT */
706 #endif /* A_FLOAT_TYPE */
707 #define A_FLOAT_SINGLE 0x04
708 #define A_FLOAT_DOUBLE 0x08
709 #define A_FLOAT_EXTEND 0x10
710 #if defined(A_FLOAT_T)
711 #elif A_FLOAT_TYPE + 0 == A_FLOAT_SINGLE
713 /*! floating-point number stored using `float` */
714 #define A_FLOAT float
715 #define A_FLOAT_DIG FLT_DIG
716 #define A_FLOAT_EPSILON FLT_EPSILON
717 #define A_FLOAT_MANT_DIG FLT_MANT_DIG
718 #define A_FLOAT_MAX FLT_MAX
719 #define A_FLOAT_MAX_10_EXP FLT_MAX_10_EXP
720 #define A_FLOAT_MAX_EXP FLT_MAX_EXP
721 #define A_FLOAT_MIN FLT_MIN
722 #define A_FLOAT_MIN_10_EXP FLT_MIN_10_EXP
723 #define A_FLOAT_MIN_EXP FLT_MIN_EXP
726 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
728 #define A_FLOAT_C(X) A_CAST_2(X, F)
730 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
732 #define A_FLOAT_F(F) A_CAST_2(F, f)
733 #define A_FLOAT_F1(F, a) A_CAST_2(F, f)(a)
734 #define A_FLOAT_F2(F, a, b) A_CAST_2(F, f)(a, b)
735 #define A_FLOAT_F3(F, a, b, c) A_CAST_2(F, f)(a, b, c)
736 #if defined(A_HAVE_VARIADIC_MACROS)
737 #define A_FLOAT_FN(F, ...) A_CAST_2(F, f)(__VA_ARGS__)
738 #endif /* A_HAVE_VARIADIC_MACROS */
740 /*! format constants for the fprintf family of functions */
741 #define A_FLOAT_PRI(F, C) "%" F C
742 /*! format constants for the fscanf family of functions */
743 #define A_FLOAT_SCN(F, C) "%" F C
745 #elif A_FLOAT_TYPE + 0 == A_FLOAT_DOUBLE
747 /*! floating-point number stored using `double` */
748 #define A_FLOAT double
749 #define A_FLOAT_DIG DBL_DIG
750 #define A_FLOAT_EPSILON DBL_EPSILON
751 #define A_FLOAT_MANT_DIG DBL_MANT_DIG
752 #define A_FLOAT_MAX DBL_MAX
753 #define A_FLOAT_MAX_10_EXP DBL_MAX_10_EXP
754 #define A_FLOAT_MAX_EXP DBL_MAX_EXP
755 #define A_FLOAT_MIN DBL_MIN
756 #define A_FLOAT_MIN_10_EXP DBL_MIN_10_EXP
757 #define A_FLOAT_MIN_EXP DBL_MIN_EXP
760 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
762 #define A_FLOAT_C(X) X
764 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
766 #define A_FLOAT_F(F) F
767 #define A_FLOAT_F1(F, a) F(a)
768 #define A_FLOAT_F2(F, a, b) F(a, b)
769 #define A_FLOAT_F3(F, a, b, c) F(a, b, c)
770 #if defined(A_HAVE_VARIADIC_MACROS)
771 #define A_FLOAT_FN(F, ...) F(__VA_ARGS__)
772 #endif /* A_HAVE_VARIADIC_MACROS */
774 /*! format constants for the fprintf family of functions */
775 #define A_FLOAT_PRI(F, C) "%" F C
776 /*! format constants for the fscanf family of functions */
777 #define A_FLOAT_SCN(F, C) "%" F "l" C
779 #elif A_FLOAT_TYPE + 0 == A_FLOAT_EXTEND
781 /*! floating-point number stored using `long double` */
782 #define A_FLOAT long double
783 #define A_FLOAT_DIG LDBL_DIG
784 #define A_FLOAT_EPSILON LDBL_EPSILON
785 #define A_FLOAT_MANT_DIG LDBL_MANT_DIG
786 #define A_FLOAT_MAX LDBL_MAX
787 #define A_FLOAT_MAX_10_EXP LDBL_MAX_10_EXP
788 #define A_FLOAT_MAX_EXP LDBL_MAX_EXP
789 #define A_FLOAT_MIN LDBL_MIN
790 #define A_FLOAT_MIN_10_EXP LDBL_MIN_10_EXP
791 #define A_FLOAT_MIN_EXP LDBL_MIN_EXP
794 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
796 #define A_FLOAT_C(X) A_CAST_2(X, L)
798 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
800 #define A_FLOAT_F(F) A_CAST_2(F, l)
801 #define A_FLOAT_F1(F, a) A_CAST_2(F, l)(a)
802 #define A_FLOAT_F2(F, a, b) A_CAST_2(F, l)(a, b)
803 #define A_FLOAT_F3(F, a, b, c) A_CAST_2(F, l)(a, b, c)
804 #if defined(A_HAVE_VARIADIC_MACROS)
805 #define A_FLOAT_FN(F, ...) A_CAST_2(F, l)(__VA_ARGS__)
806 #endif /* A_HAVE_VARIADIC_MACROS */
808 /*! format constants for the fprintf family of functions */
809 #define A_FLOAT_PRI(F, C) "%" F "L" C
810 /*! format constants for the fscanf family of functions */
811 #define A_FLOAT_SCN(F, C) "%" F "L" C
813 #else /* !A_FLOAT_TYPE */
814 #error unknown precision
815 #endif /* A_FLOAT_TYPE */
817 #define A_FLOAT_INF a_cast_s(A_FLOAT, A_F64_INF)
818 #define A_FLOAT_NAN (A_FLOAT_C(0.0) * A_FLOAT_INF)
820 /*! static cast to \ref a_float */
821 #define a_float_c(x) a_cast_s(A_FLOAT, x)
822 #define a_float_(_, x) a_cast_s(A_FLOAT _, x)
823 /*! compiler built-in floating-point number type */
824 #define a_float A_FLOAT
826 /*! @} A_FLOAT */
828 typedef union a_cast
830 a_c8 c;
831 a_int i;
832 a_uint u;
833 a_shrt ih;
834 a_ushrt uh;
835 a_long il;
836 a_ulong ul;
837 #if defined(A_HAVE_LONG_LONG_TYPE)
838 a_llong ill;
839 a_ullong ull;
840 #endif /* A_HAVE_LONG_LONG_TYPE */
841 a_i8 i8;
842 a_u8 u8;
843 a_i16 i16;
844 a_u16 u16;
845 a_i32 i32;
846 a_u32 u32;
847 a_i64 i64;
848 a_u64 u64;
849 a_f32 f32;
850 a_f64 f64;
851 a_imax imax;
852 a_umax umax;
853 a_iptr iptr;
854 a_uptr uptr;
855 a_diff diff;
856 a_size size;
857 void const *PTR;
858 void *ptr;
859 char const *STR;
860 char *str;
861 #if defined(A_FLOAT_TYPE) && (A_FLOAT_TYPE + 0 < A_FLOAT_EXTEND)
862 a_float f;
863 #endif /* A_FLOAT_TYPE */
864 } a_cast;
867 @brief square of x, \f$ x^2 \f$
869 #define A_SQ(x) ((x) * (x))
872 @brief absolute value of x, \f$ |x| \f$
874 #define A_ABS(x) ((x) < 0 ? -(x) : (x))
877 @brief minimum value between x and y
879 #define A_MIN(x, y) (((x) < (y)) ? (x) : (y))
882 @brief maximum value between x and y
884 #define A_MAX(x, y) (((x) > (y)) ? (x) : (y))
887 @brief signum function, \f$ \texttt{sgn}{x}=\begin{cases}+1&x>0\\0&0\\-1&x<0\end{cases} \f$
889 #define A_SGN(x) ((0 < (x)) - ((x) < 0))
892 @brief saturation value of x, \f$ \texttt{sat}(x,min,max)=\begin{cases}min&min>x\\max&x>max\\x&else\end{cases} \f$
894 #define A_SAT(x, min, max) ((min) < (x) ? (x) < (max) ? (x) : (max) : (min))
897 @brief number of elements in a visible array
898 @param a must be a visible array
900 #define A_LEN(a) (sizeof(a) / sizeof(*(a)))
903 @brief offset of a structure member
904 @param type structure type
905 @param member member variable
907 #if defined(offsetof)
908 #define a_offsetof(type, member) offsetof(type, member)
909 #else /* !offsetof */
910 #define a_offsetof(type, member) a_cast_r(a_size, &a_cast_r(type *, 0)->member)
911 #endif /* offsetof */
914 @brief container of a structure member
915 @param ptr pointer to a member variable
916 @param type structure type
917 @param member member variable
919 #define a_container_of(ptr, type, member) a_cast_r(type *, a_cast_r(a_uptr, ptr) - a_offsetof(type, member))
922 @brief round down size "n" to be a multiple of "a"
924 #define a_size_down(a, n) (a_cast_s(a_size, n) & ~a_cast_s(a_size, (a) - 1))
927 @brief round up size "n" to be a multiple of "a"
929 #define a_size_up(a, n) ((a_cast_s(a_size, n) + (a) - 1) & ~a_cast_s(a_size, (a) - 1))
932 @brief round pointer "p" down to the closest "a"-aligned address <= "p"
934 #define a_align_down(a, p) a_cast_r(void *, a_cast_r(a_uptr, p) & ~a_cast_s(a_uptr, (a) - 1))
937 @brief round pointer "p" up to the closest "a"-aligned address >= "p"
939 #define a_align_up(a, p) a_cast_r(void *, (a_cast_r(a_uptr, p) + (a) - 1) & ~a_cast_s(a_uptr, (a) - 1))
942 @brief iterate from 0 to n and not include n
943 @param I index type of the iteration
944 @param i index variable of the iteration
945 @param n final value of the iteration
947 #define a_forenum(I, i, n) for (I i = 0; i < (n); ++i)
950 @brief iterate from n to 0 and not include n
951 @param I index type of the iteration
952 @param i index variable of the iteration
953 @param n final value of the iteration
955 #define a_forenum_reverse(I, i, n) for (I i = (n); i-- > 0;)
958 @brief iterate over an array
959 @param T the element type in this array
960 @param it pointer to the current element
961 @param ptr starting address of this array
962 @param num number of elements in this array
964 #define a_foreach(T, it, ptr, num) \
965 for (T *it = a_cast_s(T *, ptr), \
966 *it##_ = a_likely(it) ? it + (num) : A_NULL; \
967 it < it##_; ++it)
970 @brief iterate over an array in reverse
971 @param T the element type in this array
972 @param it pointer to the current element
973 @param ptr starting address of this array
974 @param num number of elements in this array
976 #define a_foreach_reverse(T, it, ptr, num) \
977 for (T *it##_ = a_likely(ptr) ? a_cast_s(T *, ptr) - 1 : A_NULL, \
978 *it = a_likely(it##_) ? it##_ + (num) : A_NULL; \
979 it > it##_; --it)
982 @brief iterate over an array
983 @param T the element type in this array
984 @param it pointer to the current element
985 @param ptr starting address of this array
986 @param end the end address of this array
988 #define a_iterate(T, it, ptr, end) \
989 for (T *it = a_cast_s(T *, ptr), *it##_ = a_cast_s(T *, end); it < it##_; ++it)
992 @brief iterate over an array in reverse
993 @param T the element type in this array
994 @param it pointer to the current element
995 @param ptr starting address of this array
996 @param end the end address of this array
998 #define a_iterate_reverse(T, it, ptr, end) \
999 for (T *it = a_likely(end) ? a_cast_s(T *, end) - 1 : A_NULL, \
1000 *it##_ = a_likely(it) ? a_cast_s(T *, ptr) - 1 : A_NULL; \
1001 it > it##_; --it)
1004 @brief enumeration for return values
1006 enum
1008 A_SUCCESS /*!< return success */,
1009 A_FAILURE /*!< return failure */,
1010 A_INVALID /*!< return invalid */
1013 #if defined(__cplusplus)
1014 extern "C" {
1015 #endif /* __cplusplus */
1016 #if defined(LIBA_A_C)
1017 #undef A_INTERN
1018 #define A_INTERN A_INLINE
1019 #endif /* LIBA_A_C */
1022 @brief reverse the bits in an 8-bit unsigned integer x
1023 @param x an 8-bit unsigned integer to be reversed
1024 @return reversed 8-bit unsigned integer
1026 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1027 A_EXTERN a_u8 a_u8_rev(a_u8 x);
1028 #endif /* A_HAVE_INLINE */
1029 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1030 A_INTERN a_u8 a_u8_rev(a_u8 x)
1032 x = a_cast_s(a_u8, (x >> 4) | (x << 4));
1033 x = a_cast_s(a_u8, ((x & 0xCC) >> 2) | ((x & 0x33) << 2));
1034 x = a_cast_s(a_u8, ((x & 0xAA) >> 1) | ((x & 0x55) << 1));
1035 return x;
1037 #endif /* A_HAVE_INLINE */
1040 @brief reverse the bits in a 16-bit unsigned integer x
1041 @param x a 16-bit unsigned integer to be reversed
1042 @return reversed 16-bit unsigned integer
1044 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1045 A_EXTERN a_u16 a_u16_rev(a_u16 x);
1046 #endif /* A_HAVE_INLINE */
1047 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1048 A_INTERN a_u16 a_u16_rev(a_u16 x)
1050 x = a_cast_s(a_u16, (x >> 8) | (x << 8));
1051 x = a_cast_s(a_u16, ((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4));
1052 x = a_cast_s(a_u16, ((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2));
1053 x = a_cast_s(a_u16, ((x & 0xAAAA) >> 1) | ((x & 0x5555) << 1));
1054 return x;
1056 #endif /* A_HAVE_INLINE */
1057 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1058 A_EXTERN a_u16 a_u16_getl(void const *b);
1059 #endif /* A_HAVE_INLINE */
1060 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1061 A_INTERN a_u16 a_u16_getl(void const *b)
1063 a_byte const *p = a_cast_s(a_byte const *, b);
1064 return a_cast_s(a_u16, (p[0] << 0) | (p[1] << 8));
1066 #endif /* A_HAVE_INLINE */
1067 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1068 A_EXTERN a_u16 a_u16_getb(void const *b);
1069 #endif /* A_HAVE_INLINE */
1070 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1071 A_INTERN a_u16 a_u16_getb(void const *b)
1073 a_byte const *p = a_cast_s(a_byte const *, b);
1074 return a_cast_s(a_u16, (p[1] << 0) | (p[0] << 8));
1076 #endif /* A_HAVE_INLINE */
1077 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1078 A_EXTERN void a_u16_setl(void *b, a_u16 x);
1079 #endif /* A_HAVE_INLINE */
1080 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1081 A_INTERN void a_u16_setl(void *b, a_u16 x)
1083 a_byte *p = a_cast_s(a_byte *, b);
1084 p[0] = a_cast_s(a_byte, x >> 0);
1085 p[1] = a_cast_s(a_byte, x >> 8);
1087 #endif /* A_HAVE_INLINE */
1088 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1089 A_EXTERN void a_u16_setb(void *b, a_u16 x);
1090 #endif /* A_HAVE_INLINE */
1091 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1092 A_INTERN void a_u16_setb(void *b, a_u16 x)
1094 a_byte *p = a_cast_s(a_byte *, b);
1095 p[0] = a_cast_s(a_byte, x >> 8);
1096 p[1] = a_cast_s(a_byte, x >> 0);
1098 #endif /* A_HAVE_INLINE */
1101 @brief reverse the bits in a 32-bit unsigned integer x
1102 @param x a 32-bit unsigned integer to be reversed
1103 @return reversed 32-bit unsigned integer
1105 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1106 A_EXTERN a_u32 a_u32_rev(a_u32 x);
1107 #endif /* A_HAVE_INLINE */
1108 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1109 A_INTERN a_u32 a_u32_rev(a_u32 x)
1111 x = (x >> 16) | (x << 16);
1112 x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
1113 x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
1114 x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
1115 x = ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
1116 return x;
1118 #endif /* A_HAVE_INLINE */
1119 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1120 A_EXTERN a_u32 a_u32_getl(void const *b);
1121 #endif /* A_HAVE_INLINE */
1122 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1123 A_INTERN a_u32 a_u32_getl(void const *b)
1125 a_byte const *p = a_cast_s(a_byte const *, b);
1126 return (a_cast_s(a_u32, p[0]) << 0x00) |
1127 (a_cast_s(a_u32, p[1]) << 0x08) |
1128 (a_cast_s(a_u32, p[2]) << 0x10) |
1129 (a_cast_s(a_u32, p[3]) << 0x18);
1131 #endif /* A_HAVE_INLINE */
1132 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1133 A_EXTERN a_u32 a_u32_getb(void const *b);
1134 #endif /* A_HAVE_INLINE */
1135 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1136 A_INTERN a_u32 a_u32_getb(void const *b)
1138 a_byte const *p = a_cast_s(a_byte const *, b);
1139 return (a_cast_s(a_u32, p[0]) << 0x18) |
1140 (a_cast_s(a_u32, p[1]) << 0x10) |
1141 (a_cast_s(a_u32, p[2]) << 0x08) |
1142 (a_cast_s(a_u32, p[3]) << 0x00);
1144 #endif /* A_HAVE_INLINE */
1145 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1146 A_EXTERN void a_u32_setl(void *b, a_u32 x);
1147 #endif /* A_HAVE_INLINE */
1148 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1149 A_INTERN void a_u32_setl(void *b, a_u32 x)
1151 a_byte *p = a_cast_s(a_byte *, b);
1152 p[0] = a_cast_s(a_byte, x >> 0x00);
1153 p[1] = a_cast_s(a_byte, x >> 0x08);
1154 p[2] = a_cast_s(a_byte, x >> 0x10);
1155 p[3] = a_cast_s(a_byte, x >> 0x18);
1157 #endif /* A_HAVE_INLINE */
1158 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1159 A_EXTERN void a_u32_setb(void *b, a_u32 x);
1160 #endif /* A_HAVE_INLINE */
1161 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1162 A_INTERN void a_u32_setb(void *b, a_u32 x)
1164 a_byte *p = a_cast_s(a_byte *, b);
1165 p[0] = a_cast_s(a_byte, x >> 0x18);
1166 p[1] = a_cast_s(a_byte, x >> 0x10);
1167 p[2] = a_cast_s(a_byte, x >> 0x08);
1168 p[3] = a_cast_s(a_byte, x >> 0x00);
1170 #endif /* A_HAVE_INLINE */
1173 @brief reverse the bits in a 64-bit unsigned integer x
1174 @param x a 64-bit unsigned integer to be reversed
1175 @return reversed 64-bit unsigned integer
1177 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1178 A_EXTERN a_u64 a_u64_rev(a_u64 x);
1179 #endif /* A_HAVE_INLINE */
1180 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1181 A_INTERN a_u64 a_u64_rev(a_u64 x)
1183 x = (x >> 32) | (x << 32);
1184 x = ((x & 0xFFFF0000FFFF0000) >> 0x10) | ((x & 0x0000FFFF0000FFFF) << 0x10);
1185 x = ((x & 0xFF00FF00FF00FF00) >> 0x08) | ((x & 0x00FF00FF00FF00FF) << 0x08);
1186 x = ((x & 0xF0F0F0F0F0F0F0F0) >> 0x04) | ((x & 0x0F0F0F0F0F0F0F0F) << 0x04);
1187 x = ((x & 0xCCCCCCCCCCCCCCCC) >> 0x02) | ((x & 0x3333333333333333) << 0x02);
1188 x = ((x & 0xAAAAAAAAAAAAAAAA) >> 0x01) | ((x & 0x5555555555555555) << 0x01);
1189 return x;
1191 #endif /* A_HAVE_INLINE */
1192 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1193 A_EXTERN a_u64 a_u64_getl(void const *b);
1194 #endif /* A_HAVE_INLINE */
1195 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1196 A_INTERN a_u64 a_u64_getl(void const *b)
1198 a_byte const *p = a_cast_s(a_byte const *, b);
1199 return (a_cast_s(a_u64, p[0]) << 0x00) |
1200 (a_cast_s(a_u64, p[1]) << 0x08) |
1201 (a_cast_s(a_u64, p[2]) << 0x10) |
1202 (a_cast_s(a_u64, p[3]) << 0x18) |
1203 (a_cast_s(a_u64, p[4]) << 0x20) |
1204 (a_cast_s(a_u64, p[5]) << 0x28) |
1205 (a_cast_s(a_u64, p[6]) << 0x30) |
1206 (a_cast_s(a_u64, p[7]) << 0x38);
1208 #endif /* A_HAVE_INLINE */
1209 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1210 A_EXTERN a_u64 a_u64_getb(void const *b);
1211 #endif /* A_HAVE_INLINE */
1212 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1213 A_INTERN a_u64 a_u64_getb(void const *b)
1215 a_byte const *p = a_cast_s(a_byte const *, b);
1216 return (a_cast_s(a_u64, p[0]) << 0x38) |
1217 (a_cast_s(a_u64, p[1]) << 0x30) |
1218 (a_cast_s(a_u64, p[2]) << 0x28) |
1219 (a_cast_s(a_u64, p[3]) << 0x20) |
1220 (a_cast_s(a_u64, p[4]) << 0x18) |
1221 (a_cast_s(a_u64, p[5]) << 0x10) |
1222 (a_cast_s(a_u64, p[6]) << 0x08) |
1223 (a_cast_s(a_u64, p[7]) << 0x00);
1225 #endif /* A_HAVE_INLINE */
1226 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1227 A_EXTERN void a_u64_setl(void *b, a_u64 x);
1228 #endif /* A_HAVE_INLINE */
1229 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1230 A_INTERN void a_u64_setl(void *b, a_u64 x)
1232 a_byte *p = a_cast_s(a_byte *, b);
1233 p[0] = a_cast_s(a_byte, x >> 0x00);
1234 p[1] = a_cast_s(a_byte, x >> 0x08);
1235 p[2] = a_cast_s(a_byte, x >> 0x10);
1236 p[3] = a_cast_s(a_byte, x >> 0x18);
1237 p[4] = a_cast_s(a_byte, x >> 0x20);
1238 p[5] = a_cast_s(a_byte, x >> 0x28);
1239 p[6] = a_cast_s(a_byte, x >> 0x30);
1240 p[7] = a_cast_s(a_byte, x >> 0x38);
1242 #endif /* A_HAVE_INLINE */
1243 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1244 A_EXTERN void a_u64_setb(void *b, a_u64 x);
1245 #endif /* A_HAVE_INLINE */
1246 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1247 A_INTERN void a_u64_setb(void *b, a_u64 x)
1249 a_byte *p = a_cast_s(a_byte *, b);
1250 p[0] = a_cast_s(a_byte, x >> 0x38);
1251 p[1] = a_cast_s(a_byte, x >> 0x30);
1252 p[2] = a_cast_s(a_byte, x >> 0x28);
1253 p[3] = a_cast_s(a_byte, x >> 0x20);
1254 p[4] = a_cast_s(a_byte, x >> 0x18);
1255 p[5] = a_cast_s(a_byte, x >> 0x10);
1256 p[6] = a_cast_s(a_byte, x >> 0x08);
1257 p[7] = a_cast_s(a_byte, x >> 0x00);
1259 #endif /* A_HAVE_INLINE */
1262 @brief copy one buffer to another
1263 @param[in] dst pointer to the object to copy to
1264 @param[in] src pointer to the object to copy from
1265 @param[in] siz number of bytes to copy
1266 @return a copy of dest
1268 A_EXTERN void *a_copy(void *__restrict dst, void const *__restrict src, a_size siz);
1271 @brief move one buffer to another
1272 @param[in] dst pointer to the object to copy to
1273 @param[in] src pointer to the object to copy from
1274 @param[in] siz number of bytes to copy
1275 @return a copy of dest
1277 A_EXTERN void *a_move(void *dst, void const *src, a_size siz);
1280 @brief fill a buffer with a character
1281 @param[in] ptr pointer to the object to fill
1282 @param[in] siz number of bytes to fill
1283 @param[in] val fill byte
1284 @return a copy of dest
1286 A_EXTERN void *a_fill(void *ptr, a_size siz, int val);
1289 @brief fill a buffer with zero
1290 @param[in] ptr pointer to the object to fill
1291 @param[in] siz number of bytes to fill
1292 @return a copy of dest
1294 A_EXTERN void *a_zero(void *ptr, a_size siz);
1297 @brief swap two different memory blocks of the same size
1298 @param[in] siz the size of memory block being swapped
1299 @param[in,out] lhs points to memory block on the left
1300 @param[in,out] rhs points to memory block on the right
1302 A_EXTERN void a_swap(void *lhs, void *rhs, a_size siz);
1305 @brief a hash function whose prime number is 131
1306 @param[in] str string to be processed
1307 @param[in] val initial value
1308 @return hash value
1310 A_EXTERN a_u32 a_hash_bkdr(void const *str, a_u32 val);
1313 @brief a hash function whose prime number is 131
1314 @param[in] ptr points to string to be processed
1315 @param[in] siz length of string to be processed
1316 @param[in] val initial value
1317 @return hash value
1319 A_EXTERN a_u32 a_hash_bkdr_(void const *ptr, a_size siz, a_u32 val);
1322 @brief a hash function whose prime number is 65599
1323 @param[in] str string to be processed
1324 @param[in] val initial value
1325 @return hash value
1327 A_EXTERN a_u32 a_hash_sdbm(void const *str, a_u32 val);
1330 @brief a hash function whose prime number is 65599
1331 @param[in] ptr points to string to be processed
1332 @param[in] siz length of string to be processed
1333 @param[in] val initial value
1334 @return hash value
1336 A_EXTERN a_u32 a_hash_sdbm_(void const *ptr, a_size siz, a_u32 val);
1339 @brief roll back the elements of a float array and save the cache array
1340 @param[in] block_p points to a float array
1341 @param[in] block_n number of a float array
1342 @param[in] cache_p points to a cache array
1343 @param[in] cache_n number of a cache array
1345 A_EXTERN void a_float_push(a_float *block_p, a_size block_n,
1346 a_float const *cache_p, a_size cache_n);
1349 @brief roll back the elements of a float array circularly, array>>shift
1350 @param[in] block_p points to a float array
1351 @param[in] block_n number of a float array
1352 @param[in] shift_p points to a shift array
1353 @param[in] shift_n number of a shift array
1355 A_EXTERN void a_float_roll(a_float *block_p, a_size block_n,
1356 a_float *shift_p, a_size shift_n);
1359 @brief allocation function pointer
1360 @param[in] addr address of memory block
1361 @param[in] size new size of memory block
1362 @return new address of memory block or null
1364 A_EXTERN void *(*a_alloc)(void *addr, a_size size);
1367 @brief default allocation function
1368 @param[in] addr address of memory block
1369 @param[in] size new size of memory block
1370 @return new address of memory block or null
1372 A_EXTERN void *a_alloc_(void *addr, a_size size);
1374 #if defined(LIBA_A_C)
1375 #undef A_INTERN
1376 #define A_INTERN static A_INLINE
1377 #endif /* LIBA_A_C */
1378 #if defined(__cplusplus)
1379 } /* extern "C" */
1380 #endif /* __cplusplus */
1383 @brief declare allocation function
1385 #define A_ALLOC(alloc, addr, size) void *alloc(void *addr, a_size size)
1386 #define a_new(T, ptr, num) a_cast_s(T *, a_alloc(ptr, sizeof(T) * (num)))
1387 #define a_die(ptr) a_alloc(ptr, 0)
1389 /*! @} A */
1391 #endif /* a/a.h */