rename variable to `variable`
[liba.git] / include / a / a.h
blob12b101643805bfa1325ddaa5dba788e687a58264
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 !defined A_FUNC
73 #if defined(_MSC_VER)
74 #define A_FUNC __FUNCTION__
75 #elif defined(__GNUC__)
76 #define A_FUNC (__extension__ __PRETTY_FUNCTION__)
77 #elif defined(__cplusplus) && (__cplusplus > 201100L) || \
78 defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L)
79 #define A_FUNC __func__
80 #else /* !__func__ */
81 #define A_FUNC __FUNCTION__
82 #endif /* __func__ */
83 #endif /* A_FUNC */
85 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L) || \
86 defined(__cplusplus) && (__cplusplus > 201100L) || A_PREREQ_MSVC(18, 0)
88 #if !defined A_HAVE_VARIADIC_MACROS
89 #define A_HAVE_VARIADIC_MACROS 1
90 #endif /* A_HAVE_VARIADIC_MACROS */
92 #if !defined A_HAVE_LONG_LONG_TYPE
93 #define A_HAVE_LONG_LONG_TYPE 1
94 #endif /* A_HAVE_LONG_LONG_TYPE */
96 #endif /* C > 199900 or C++ > 201100 */
97 #if defined(A_HAVE_VARIADIC_MACROS) && (A_HAVE_VARIADIC_MACROS + 0 < 1)
98 #undef A_HAVE_VARIADIC_MACROS
99 #endif /* A_HAVE_VARIADIC_MACROS */
100 #if defined(A_HAVE_LONG_LONG_TYPE) && (A_HAVE_LONG_LONG_TYPE + 0 < 1)
101 #undef A_HAVE_LONG_LONG_TYPE
102 #endif /* A_HAVE_LONG_LONG_TYPE */
104 #if defined(__cplusplus) && (__cplusplus > 201100L) || \
105 defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201100L)
107 #if !defined A_HAVE_STATIC_ASSERT
108 #define A_HAVE_STATIC_ASSERT 1
109 #endif /* A_HAVE_STATIC_ASSERT */
111 #endif /* C > 201100 or C++ > 201100 */
112 #if defined(A_HAVE_STATIC_ASSERT) && (A_HAVE_STATIC_ASSERT + 0 < 1)
113 #undef A_HAVE_STATIC_ASSERT
114 #endif /* A_HAVE_STATIC_ASSERT */
116 #if defined(__cplusplus) && (__cplusplus > 201100L)
118 #if !defined A_HAVE_NULLPTR
119 #define A_HAVE_NULLPTR 1
120 #endif /* A_HAVE_NULLPTR */
122 #endif /* C++ > 201100 */
123 #if defined(A_HAVE_NULLPTR) && (A_HAVE_NULLPTR + 0 < 1)
124 #undef A_HAVE_NULLPTR
125 #endif /* A_HAVE_NULLPTR */
127 #if A_PREREQ_GNUC(2, 96) || __has_builtin(__builtin_expect)
128 #define A_UNLIKELY(x) __builtin_expect(!!(x), 0)
129 #define A_LIKELY(x) __builtin_expect(!!(x), 1)
130 #else /* !likely */
131 #define A_UNLIKELY(x) (!!(x))
132 #define A_LIKELY(x) (!!(x))
133 #endif /* likely */
135 #if defined(_WIN32) || defined(__CYGWIN__)
136 #define A_DECLSPEC(x) __declspec(x)
137 #else /* !__declspec */
138 #define A_DECLSPEC(x)
139 #endif /* __declspec */
141 #if defined(__GNUC__) || defined(__clang__)
142 #define A_ATTRIBUTE(x) __attribute__(x)
143 #else /* !__attribute__ */
144 #define A_ATTRIBUTE(x)
145 #endif /* __attribute__ */
147 #if __has_builtin(__builtin_assume)
148 #define A_ASSUME(x) __builtin_assume(x)
149 #elif A_PREREQ_GNUC(13, 0)
150 #define A_ASSUME(x) __attribute__((__assume__(x)))
151 #elif defined(__GNUC__)
152 #define A_ASSUME(x) \
153 do { \
154 if (!(x)) { __builtin_unreachable(); } \
155 } while (0)
156 #elif defined(_MSC_VER)
157 #define A_ASSUME(x) __assume(x)
158 #else /* !assume */
159 #define A_ASSUME(x) (void)0
160 #endif /* assume */
162 /* attribute format */
163 #if A_PREREQ_GNUC(2, 4) || __has_attribute(format)
164 #define A_FORMAT(_, a, b) __attribute__((__format__(_, a, b)))
165 #else /* !format */
166 #define A_FORMAT(_, a, b)
167 #endif /* format */
169 /* attribute fallthrough */
170 #if A_PREREQ_GNUC(7, 0) || __has_attribute(fallthrough)
171 #define A_FALLTHROUGH __attribute__((__fallthrough__))
172 #else /* !fallthrough */
173 #define A_FALLTHROUGH (void)0
174 #endif /* fallthrough */
176 /* attribute deprecated */
177 #if A_PREREQ_GNUC(3, 2) || __has_attribute(deprecated)
178 #define A_DEPRECATED __attribute__((__deprecated__))
179 #elif defined(_WIN32) || defined(__CYGWIN__)
180 #define A_DEPRECATED __declspec(deprecated)
181 #else /* !deprecated */
182 #define A_DEPRECATED
183 #endif /* deprecated */
185 /* attribute always inline */
186 #if A_PREREQ_GNUC(3, 2) || __has_attribute(always_inline)
187 #define A_INLINE __inline __attribute__((__always_inline__))
188 #elif defined(_MSC_VER)
189 #define A_INLINE __inline __forceinline
190 #else /* !_MSC_VER */
191 #define A_INLINE __inline
192 #endif /* _MSC_VER */
193 #if !defined A_INTERN
194 #define A_INTERN static A_INLINE
195 #endif /* A_INTERN */
197 /* attribute visibility */
198 #if defined(_WIN32) || defined(__CYGWIN__)
199 #define A_EXPORT __declspec(dllexport)
200 #define A_IMPORT __declspec(dllimport)
201 #define A_HIDDEN
202 #elif A_PREREQ_GNUC(4, 0) || __has_attribute(visibility)
203 #define A_EXPORT __attribute__((__visibility__("default")))
204 #define A_IMPORT __attribute__((__visibility__("default")))
205 #define A_HIDDEN __attribute__((__visibility__("hidden")))
206 #else /* !visibility */
207 #define A_EXPORT
208 #define A_IMPORT
209 #define A_HIDDEN
210 #endif /* visibility */
211 #if defined(A_EXPORTS)
212 #define A_PUBLIC A_EXPORT
213 #elif defined(A_IMPORTS)
214 #define A_PUBLIC A_IMPORT
215 #else /* !A_PUBLIC */
216 #define A_PUBLIC
217 #endif /* A_PUBLIC */
219 #if !defined A_EXTERN
220 #define A_EXTERN A_PUBLIC extern
221 #endif /* A_EXTERN */
222 #if !defined __cplusplus
223 #define A_EXTERN_C
224 #define A_EXTERN_C_ENTER
225 #define A_EXTERN_C_LEAVE
226 #else /* !__cplusplus */
227 #define A_EXTERN_C extern "C"
228 #define A_EXTERN_C_ENTER extern "C" {
229 #define A_EXTERN_C_LEAVE }
230 #endif /* __cplusplus */
232 /*! @endcond */
234 /* byte order of little endian architecture */
235 #if defined(__ORDER_LITTLE_ENDIAN__)
236 #define A_ORDER_LITTLE __ORDER_LITTLE_ENDIAN__
237 #else /* !__ORDER_LITTLE_ENDIAN__ */
238 #define A_ORDER_LITTLE 1234
239 #endif /* __ORDER_LITTLE_ENDIAN__ */
240 /* byte order of big endian architecture */
241 #if defined(__ORDER_BIG_ENDIAN__)
242 #define A_ORDER_BIG __ORDER_BIG_ENDIAN__
243 #else /* !__ORDER_BIG_ENDIAN__ */
244 #define A_ORDER_BIG 4321
245 #endif /* __ORDER_BIG_ENDIAN__ */
246 /* byte order of architecture */
247 #if !defined A_BYTE_ORDER
248 #if defined(__BYTE_ORDER__)
249 #define A_BYTE_ORDER __BYTE_ORDER__
250 #endif /* __BYTE_ORDER__ */
251 #endif /* A_BYTE_ORDER */
253 /* size of void pointer */
254 #if !defined A_SIZE_POINTER
255 #if defined(__SIZEOF_POINTER__)
256 #define A_SIZE_POINTER __SIZEOF_POINTER__
257 #elif defined(_WIN64)
258 #define A_SIZE_POINTER 8
259 #elif defined(_WIN32)
260 #define A_SIZE_POINTER 4
261 #endif /* __SIZEOF_POINTER__ */
262 #endif /* A_SIZE_POINTER */
264 #include <stddef.h>
265 #include <limits.h>
266 #include <stdint.h>
267 #include <float.h>
270 @addtogroup liba algorithm library
274 /*! assert a build-time dependency, as an expression */
275 #define A_BUILD_ASSERT(x) (void)(sizeof(char[1 - 2 * !(x)]))
276 #define A_BUILD_BUG_ON(x) (void)(sizeof(char[1 - 2 * !!(x)]))
278 #if defined(__cplusplus)
279 #define a_cast_r(T, x) reinterpret_cast<T>(x)
280 #define a_cast_d(T, x) dynamic_cast<T>(x)
281 #define a_cast_s(T, x) static_cast<T>(x)
282 #define a_cast_c(T, x) const_cast<T>(x)
283 #else /* !__cplusplus */
284 #define a_cast_r(T, x) ((T)(x))
285 #define a_cast_d(T, x) ((T)(x))
286 #define a_cast_s(T, x) ((T)(x))
287 #define a_cast_c(T, x) ((T)(x))
288 #endif /* __cplusplus */
289 #define A_CAST_3(a, b, c) a##b##c
290 #define A_CAST_2(a, b) a##b
291 #define A_CAST_1(a) #a
293 #if defined(__cplusplus) && defined(A_HAVE_NULLPTR)
294 #define A_NULL nullptr
295 #else /* !__cplusplus */
296 #define A_NULL NULL
297 #endif /* __cplusplus */
298 /*! static cast to \ref a_void */
299 #define a_void_c(_, x) a_cast_s(void, x)
300 #define a_void_(_, x) a_cast_s(void _, x)
301 /*! incomplete type or no parameter or no return value */
302 #define a_void void
304 #if defined(__cplusplus)
305 #define A_TRUE true
306 #define A_FALSE false
307 #if !defined A_BOOL
308 #define A_BOOL bool
309 #endif /* A_BOOL */
310 #else /* !__cplusplus */
311 #define A_TRUE 1
312 #define A_FALSE 0
313 #if !defined A_BOOL
314 #define A_BOOL _Bool
315 #endif /* A_BOOL */
316 #endif /* __cplusplus */
317 /*! static cast to \ref a_bool */
318 #define a_bool_c(x) (!!(x))
319 /*! type, capable of holding one of the two values: 1 and 0 */
320 #define a_bool A_BOOL
322 #define A_INT int
323 #define A_INT_MIN INT_MIN
324 #define A_INT_MAX INT_MAX
325 /*! static cast to \ref a_int */
326 #define a_int_c(x) a_cast_s(A_INT, x)
327 #define a_int_(_, x) a_cast_s(A_INT _, x)
328 /*! signed integer type is guaranteed to be at least 16 bits */
329 #define a_int A_INT
331 #define A_UINT unsigned int
332 #define A_UINT_MAX UINT_MAX
333 /*! static cast to \ref a_uint */
334 #define a_uint_c(x) a_cast_s(A_UINT, x)
335 #define a_uint_(_, x) a_cast_s(A_UINT _, x)
336 /*! unsigned integer type is guaranteed to be at least 16 bits */
337 #define a_uint A_UINT
339 #define A_SHRT short
340 #define A_SHRT_MIN SHRT_MIN
341 #define A_SHRT_MAX SHRT_MAX
342 /*! static cast to \ref a_shrt */
343 #define a_shrt_c(x) a_cast_s(A_SHRT, x)
344 #define a_shrt_(_, x) a_cast_s(A_SHRT _, x)
345 /*! signed integer type is guaranteed to be at least 16 bits */
346 #define a_shrt A_SHRT
348 #define A_USHRT unsigned short
349 #define A_USHRT_MAX USHRT_MAX
350 /*! static cast to \ref a_ushrt */
351 #define a_ushrt_c(x) a_cast_s(A_USHRT, x)
352 #define a_ushrt_(_, x) a_cast_s(A_USHRT _, x)
353 /*! unsigned integer type is guaranteed to be at least 16 bits */
354 #define a_ushrt A_USHRT
356 #define A_LONG long
357 #define A_LONG_MIN LONG_MIN
358 #define A_LONG_MAX LONG_MAX
359 /*! static cast to \ref a_long */
360 #define a_long_c(x) a_cast_s(A_LONG, x)
361 #define a_long_(_, x) a_cast_s(A_LONG _, x)
362 /*! signed integer type is guaranteed to be at least 32 bits */
363 #define a_long A_LONG
365 #define A_ULONG unsigned long
366 #define A_ULONG_MAX ULONG_MAX
367 /*! static cast to \ref a_ulong */
368 #define a_ulong_c(x) a_cast_s(A_ULONG, x)
369 #define a_ulong_(_, x) a_cast_s(A_ULONG _, x)
370 /*! unsigned integer type is guaranteed to be at least 32 bits */
371 #define a_ulong A_ULONG
373 #if defined(A_HAVE_LONG_LONG_TYPE)
375 #define A_LLONG long long
376 #define A_LLONG_MIN LLONG_MIN
377 #define A_LLONG_MAX LLONG_MAX
378 /*! static cast to \ref a_llong */
379 #define a_llong_c(x) a_cast_s(A_LLONG, x)
380 #define a_llong_(_, x) a_cast_s(A_LLONG _, x)
381 /*! signed integer type is guaranteed to be at least 64 bits */
382 #define a_llong A_LLONG
384 #define A_ULLONG unsigned long long
385 #define A_ULLONG_MAX ULLONG_MAX
386 /*! static cast to \ref a_ullong */
387 #define a_ullong_c(x) a_cast_s(A_ULLONG, x)
388 #define a_ullong_(_, x) a_cast_s(A_ULLONG _, x)
389 /*! unsigned integer type is guaranteed to be at least 64 bits */
390 #define a_ullong A_ULLONG
392 #endif /* A_HAVE_LONG_LONG_TYPE */
394 #define A_BYTE unsigned char
395 #define A_BYTE_MAX UCHAR_MAX
396 /*! static cast to \ref a_byte */
397 #define a_byte_c(x) a_cast_s(A_BYTE, x)
398 #define a_byte_(_, x) a_cast_s(A_BYTE _, x)
399 /*! type for unsigned character representation */
400 #define a_byte A_BYTE
402 #define A_C8 char
403 #define A_C8_MIN CHAR_MIN
404 #define A_C8_MAX CHAR_MAX
405 /*! static cast to \ref a_c8 */
406 #define a_c8_c(x) a_cast_s(A_C8, x)
407 #define a_c8_(_, x) a_cast_s(A_C8 _, x)
408 /*! type for character representation */
409 #define a_c8 A_C8
411 #if !defined A_I8
412 #define A_I8 int8_t
413 #endif /* A_I8 */
414 #if !defined A_I8_C && defined(INT8_C)
415 #define A_I8_C(X) INT8_C(X)
416 #endif /* A_I8_C */
417 #if !defined A_I8_MIN && defined(INT8_MIN)
418 #define A_I8_MIN INT8_MIN
419 #endif /* A_I8_MIN */
420 #if !defined A_I8_MAX && defined(INT8_MAX)
421 #define A_I8_MAX INT8_MAX
422 #endif /* A_I8_MAX */
423 /*! static cast to \ref a_i8 */
424 #define a_i8_c(x) a_cast_s(A_I8, x)
425 #define a_i8_(_, x) a_cast_s(A_I8 _, x)
426 /*! signed integer type with width of exactly 8 bits */
427 #define a_i8 A_I8
429 #if !defined A_U8
430 #define A_U8 uint8_t
431 #endif /* A_U8 */
432 #if !defined A_U8_C && defined(UINT8_C)
433 #define A_U8_C(X) UINT8_C(X)
434 #endif /* A_U8_C */
435 #if !defined A_U8_MAX && defined(UINT8_MAX)
436 #define A_U8_MAX UINT8_MAX
437 #endif /* A_U8_MAX */
438 /*! static cast to \ref a_u8 */
439 #define a_u8_c(x) a_cast_s(A_U8, x)
440 #define a_u8_(_, x) a_cast_s(A_U8 _, x)
441 /*! unsigned integer type with width of exactly 8 bits */
442 #define a_u8 A_U8
444 #if !defined A_I16
445 #define A_I16 int16_t
446 #endif /* A_I16 */
447 #if !defined A_I16_C && defined(INT16_C)
448 #define A_I16_C(X) INT16_C(X)
449 #endif /* A_I16_C */
450 #if !defined A_I16_MIN && defined(INT16_MIN)
451 #define A_I16_MIN INT16_MIN
452 #endif /* A_I16_MIN */
453 #if !defined A_I16_MAX && defined(INT16_MAX)
454 #define A_I16_MAX INT16_MAX
455 #endif /* A_I16_MAX */
456 /*! static cast to \ref a_i16 */
457 #define a_i16_c(x) a_cast_s(A_I16, x)
458 #define a_i16_(_, x) a_cast_s(A_I16 _, x)
459 /*! signed integer type with width of exactly 16 bits */
460 #define a_i16 A_I16
462 #if !defined A_U16
463 #define A_U16 uint16_t
464 #endif /* A_U16 */
465 #if !defined A_U16_C && defined(UINT16_C)
466 #define A_U16_C(X) UINT16_C(X)
467 #endif /* A_U16_C */
468 #if !defined A_U16_MAX && defined(UINT16_MAX)
469 #define A_U16_MAX UINT16_MAX
470 #endif /* A_U16_MAX */
471 /*! static cast to \ref a_u16 */
472 #define a_u16_c(x) a_cast_s(A_U16, x)
473 #define a_u16_(_, x) a_cast_s(A_U16 _, x)
474 /*! unsigned integer type with width of exactly 16 bits */
475 #define a_u16 A_U16
477 #if !defined A_I32
478 #define A_I32 int32_t
479 #endif /* A_I32 */
480 #if !defined A_I32_C && defined(INT32_C)
481 #define A_I32_C(X) INT32_C(X)
482 #endif /* A_I32_C */
483 #if !defined A_I32_MIN && defined(INT32_MIN)
484 #define A_I32_MIN INT32_MIN
485 #endif /* A_I32_MIN */
486 #if !defined A_I32_MAX && defined(INT32_MAX)
487 #define A_I32_MAX INT32_MAX
488 #endif /* A_I32_MAX */
489 /*! static cast to \ref a_i32 */
490 #define a_i32_c(x) a_cast_s(A_I32, x)
491 #define a_i32_(_, x) a_cast_s(A_I32 _, x)
492 /*! signed integer type with width of exactly 32 bits */
493 #define a_i32 A_I32
495 #if !defined A_U32
496 #define A_U32 uint32_t
497 #endif /* A_U32 */
498 #if !defined A_U32_C && defined(UINT32_C)
499 #define A_U32_C(X) UINT32_C(X)
500 #endif /* A_U32_C */
501 #if !defined A_U32_MAX && defined(UINT32_MAX)
502 #define A_U32_MAX UINT32_MAX
503 #endif /* A_U32_MAX */
504 /*! static cast to \ref a_u32 */
505 #define a_u32_c(x) a_cast_s(A_U32, x)
506 #define a_u32_(_, x) a_cast_s(A_U32 _, x)
507 /*! unsigned integer type with width of exactly 32 bits */
508 #define a_u32 A_U32
510 #if !defined A_I64
511 #define A_I64 int64_t
512 #endif /* A_I64 */
513 #if !defined A_I64_C && defined(INT64_C)
514 #define A_I64_C(X) INT64_C(X)
515 #endif /* A_I64_C */
516 #if !defined A_I64_MIN && defined(INT64_MIN)
517 #define A_I64_MIN INT64_MIN
518 #endif /* A_I64_MIN */
519 #if !defined A_I64_MAX && defined(INT64_MAX)
520 #define A_I64_MAX INT64_MAX
521 #endif /* A_I64_MAX */
522 /*! static cast to \ref a_i64 */
523 #define a_i64_c(x) a_cast_s(A_I64, x)
524 #define a_i64_(_, x) a_cast_s(A_I64 _, x)
525 /*! signed integer type with width of exactly 64 bits */
526 #define a_i64 A_I64
528 #if !defined A_U64
529 #define A_U64 uint64_t
530 #endif /* A_U64 */
531 #if !defined A_U64_C && defined(UINT64_C)
532 #define A_U64_C(X) UINT64_C(X)
533 #endif /* A_U64_C */
534 #if !defined A_U64_MAX && defined(UINT64_MAX)
535 #define A_U64_MAX UINT64_MAX
536 #endif /* A_U64_MAX */
537 /*! static cast to \ref a_u64 */
538 #define a_u64_c(x) a_cast_s(A_U64, x)
539 #define a_u64_(_, x) a_cast_s(A_U64 _, x)
540 /*! unsigned integer type with width of exactly 64 bits */
541 #define a_u64 A_U64
543 #if !defined A_IMAX
544 #define A_IMAX intmax_t
545 #endif /* A_IMAX */
546 #if !defined A_IMAX_C && defined(INTMAX_C)
547 #define A_IMAX_C(X) INTMAX_C(X)
548 #endif /* A_IMAX_C */
549 #if !defined A_IMAX_MIN && defined(INTMAX_MIN)
550 #define A_IMAX_MIN INTMAX_MIN
551 #endif /* A_IMAX_MIN */
552 #if !defined A_IMAX_MAX && defined(INTMAX_MAX)
553 #define A_IMAX_MAX INTMAX_MAX
554 #endif /* A_IMAX_MAX */
555 /*! static cast to \ref a_imax */
556 #define a_imax_c(x) a_cast_s(A_IMAX, x)
557 #define a_imax_(_, x) a_cast_s(A_IMAX _, x)
558 /*! maximum-width signed integer type */
559 #define a_imax A_IMAX
561 #if !defined A_UMAX
562 #define A_UMAX uintmax_t
563 #endif /* A_UMAX */
564 #if !defined A_UMAX_C && defined(UINTMAX_C)
565 #define A_UMAX_C(X) UINTMAX_C(X)
566 #endif /* A_UMAX_C */
567 #if !defined A_UMAX_MAX && defined(UINTMAX_MAX)
568 #define A_UMAX_MAX UINTMAX_MAX
569 #endif /* A_UMAX_MAX */
570 /*! static cast to \ref a_umax */
571 #define a_umax_c(x) a_cast_s(A_UMAX, x)
572 #define a_umax_(_, x) a_cast_s(A_UMAX _, x)
573 /*! maximum-width unsigned integer type */
574 #define a_umax A_UMAX
576 #if !defined A_IPTR
577 #define A_IPTR intptr_t
578 #endif /* A_IPTR */
579 #if !defined A_IPTR_MIN && defined(INTPTR_MIN)
580 #define A_IPTR_MIN INTPTR_MIN
581 #endif /* A_IPTR_MIN */
582 #if !defined A_IPTR_MAX && defined(INTPTR_MAX)
583 #define A_IPTR_MAX INTPTR_MAX
584 #endif /* A_IPTR_MAX */
585 /*! static cast to \ref a_iptr */
586 #define a_iptr_c(x) a_cast_s(A_IPTR, x)
587 #define a_iptr_(_, x) a_cast_s(A_IPTR _, x)
588 /*! signed integer type capable of holding a pointer to void */
589 #define a_iptr A_IPTR
591 #if !defined A_UPTR
592 #define A_UPTR uintptr_t
593 #endif /* A_UPTR */
594 #if !defined A_UPTR_MAX && defined(UINTPTR_MAX)
595 #define A_UPTR_MAX UINTPTR_MAX
596 #endif /* A_UPTR_MAX */
597 /*! static cast to \ref a_uptr */
598 #define a_uptr_c(x) a_cast_s(A_UPTR, x)
599 #define a_uptr_(_, x) a_cast_s(A_UPTR _, x)
600 /*! unsigned integer type capable of holding a pointer to void */
601 #define a_uptr A_UPTR
603 #if !defined A_DIFF
604 #define A_DIFF ptrdiff_t
605 #endif /* A_DIFF */
606 #if !defined A_DIFF_MIN && defined(PTRDIFF_MIN)
607 #define A_DIFF_MIN PTRDIFF_MIN
608 #endif /* A_DIFF_MIN */
609 #if !defined A_DIFF_MAX && defined(PTRDIFF_MAX)
610 #define A_DIFF_MAX PTRDIFF_MAX
611 #endif /* A_DIFF_MAX */
612 /*! static cast to \ref a_diff */
613 #define a_diff_c(x) a_cast_s(A_DIFF, x)
614 #define a_diff_(_, x) a_cast_s(A_DIFF _, x)
615 /*! signed integer type returned when subtracting two pointers */
616 #define a_diff A_DIFF
618 #if !defined A_SIZE
619 #define A_SIZE size_t
620 #endif /* A_SIZE */
621 #if !defined A_SIZE_MAX && defined(SIZE_MAX)
622 #define A_SIZE_MAX SIZE_MAX
623 #endif /* A_SIZE_MAX */
624 /*! static cast to \ref a_size */
625 #define a_size_c(x) a_cast_s(A_SIZE, x)
626 #define a_size_(_, x) a_cast_s(A_SIZE _, x)
627 /*! unsigned integer type returned by the sizeof operator */
628 #define a_size A_SIZE
630 #define A_F16_NNAN A_U16_C(0xFE00)
631 #define A_F16_PNAN A_U16_C(0x7E00)
632 #define A_F16_NINF A_U16_C(0xFC00)
633 #define A_F16_PINF A_U16_C(0x7C00)
635 #define A_F32 float
636 #define A_F32_C(X) A_CAST_2(X, F)
637 #define A_F32_F(F) A_CAST_2(F, f)
638 #define A_F32_F1(F, a) A_CAST_2(F, f)(a)
639 #define A_F32_F2(F, a, b) A_CAST_2(F, f)(a, b)
640 #define A_F32_F3(F, a, b, c) A_CAST_2(F, f)(a, b, c)
641 #if defined(A_HAVE_VARIADIC_MACROS)
642 #define A_F32_FN(F, ...) A_CAST_2(F, f)(__VA_ARGS__)
643 #endif /* A_HAVE_VARIADIC_MACROS */
644 #define A_F32_DIG FLT_DIG
645 #define A_F32_EPSILON FLT_EPSILON
646 #define A_F32_MANT_DIG FLT_MANT_DIG
647 #define A_F32_MAX FLT_MAX
648 #define A_F32_MAX_10_EXP FLT_MAX_10_EXP
649 #define A_F32_MAX_EXP FLT_MAX_EXP
650 #define A_F32_MIN FLT_MIN
651 #define A_F32_MIN_10_EXP FLT_MIN_10_EXP
652 #define A_F32_MIN_EXP FLT_MIN_EXP
653 #define A_F32_INF a_cast_s(A_F32, A_F64_INF)
654 #define A_F32_NAN (A_F32_C(0.0) * A_F32_INF)
655 #define A_F32_NNAN A_U32_C(0xFFC00000)
656 #define A_F32_PNAN A_U32_C(0x7FC00000)
657 #define A_F32_NINF A_U32_C(0xFF800000)
658 #define A_F32_PINF A_U32_C(0x7F800000)
659 /*! format constants for the fprintf family of functions */
660 #define A_F32_PRI(F, C) "%" F C
661 /*! format constants for the fscanf family of functions */
662 #define A_F32_SCN(F, C) "%" F C
663 /*! static cast to \ref a_f32 */
664 #define a_f32_c(x) a_cast_s(A_F32, x)
665 #define a_f32_(_, x) a_cast_s(A_F32 _, x)
666 /*! single precision floating point type. Matches IEEE-754 binary32 format if supported. */
667 #define a_f32 A_F32
669 #define A_F64 double
670 #define A_F64_C(X) X
671 #define A_F64_F(F) F
672 #define A_F64_F1(F, a) F(a)
673 #define A_F64_F2(F, a, b) F(a, b)
674 #define A_F64_F3(F, a, b, c) F(a, b, c)
675 #if defined(A_HAVE_VARIADIC_MACROS)
676 #define A_F64_FN(F, ...) F(__VA_ARGS__)
677 #endif /* A_HAVE_VARIADIC_MACROS */
678 #define A_F64_DIG DBL_DIG
679 #define A_F64_EPSILON DBL_EPSILON
680 #define A_F64_MANT_DIG DBL_MANT_DIG
681 #define A_F64_MAX DBL_MAX
682 #define A_F64_MAX_10_EXP DBL_MAX_10_EXP
683 #define A_F64_MAX_EXP DBL_MAX_EXP
684 #define A_F64_MIN DBL_MIN
685 #define A_F64_MIN_10_EXP DBL_MIN_10_EXP
686 #define A_F64_MIN_EXP DBL_MIN_EXP
687 #define A_F64_INF (DBL_MAX * DBL_MAX)
688 #define A_F64_NAN (A_F64_C(0.0) * A_F64_INF)
689 #define A_F64_NNAN A_U64_C(0xFFF8000000000000)
690 #define A_F64_PNAN A_U64_C(0x7FF8000000000000)
691 #define A_F64_NINF A_U64_C(0xFFF0000000000000)
692 #define A_F64_PINF A_U64_C(0x7FF0000000000000)
693 /*! format constants for the fprintf family of functions */
694 #define A_F64_PRI(F, C) "%" F "l" C
695 /*! format constants for the fscanf family of functions */
696 #define A_F64_SCN(F, C) "%" F "l" C
697 /*! static cast to \ref a_f64 */
698 #define a_f64_c(x) a_cast_s(A_F64, x)
699 #define a_f64_(_, x) a_cast_s(A_F64 _, x)
700 /*! double precision floating point type. Matches IEEE-754 binary64 format if supported. */
701 #define a_f64 A_F64
704 @addtogroup a_float floating-point number
708 /*! floating-point number bytes */
709 #if !defined A_FLOAT_TYPE
710 #if !defined A_SIZE_FLOAT
711 #define A_FLOAT_TYPE A_FLOAT_DOUBLE
712 #else /* !A_SIZE_FLOAT */
713 #define A_FLOAT_TYPE A_SIZE_FLOAT
714 #endif /* A_SIZE_FLOAT */
715 #endif /* A_FLOAT_TYPE */
716 #define A_FLOAT_SINGLE 0x04
717 #define A_FLOAT_DOUBLE 0x08
718 #define A_FLOAT_EXTEND 0x10
719 #if defined(A_FLOAT)
720 #elif A_FLOAT_TYPE + 0 == A_FLOAT_SINGLE
722 /*! floating-point number stored using `float` */
723 #define A_FLOAT float
724 #define A_FLOAT_DIG FLT_DIG
725 #define A_FLOAT_EPSILON FLT_EPSILON
726 #define A_FLOAT_MANT_DIG FLT_MANT_DIG
727 #define A_FLOAT_MAX FLT_MAX
728 #define A_FLOAT_MAX_10_EXP FLT_MAX_10_EXP
729 #define A_FLOAT_MAX_EXP FLT_MAX_EXP
730 #define A_FLOAT_MIN FLT_MIN
731 #define A_FLOAT_MIN_10_EXP FLT_MIN_10_EXP
732 #define A_FLOAT_MIN_EXP FLT_MIN_EXP
735 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
737 #define A_FLOAT_C(X) A_CAST_2(X, F)
739 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
741 #define A_FLOAT_F(F) A_CAST_2(F, f)
742 #define A_FLOAT_F1(F, a) A_CAST_2(F, f)(a)
743 #define A_FLOAT_F2(F, a, b) A_CAST_2(F, f)(a, b)
744 #define A_FLOAT_F3(F, a, b, c) A_CAST_2(F, f)(a, b, c)
745 #if defined(A_HAVE_VARIADIC_MACROS)
746 #define A_FLOAT_FN(F, ...) A_CAST_2(F, f)(__VA_ARGS__)
747 #endif /* A_HAVE_VARIADIC_MACROS */
749 /*! format constants for the fprintf family of functions */
750 #define A_FLOAT_PRI(F, C) "%" F C
751 /*! format constants for the fscanf family of functions */
752 #define A_FLOAT_SCN(F, C) "%" F C
754 #elif A_FLOAT_TYPE + 0 == A_FLOAT_DOUBLE
756 /*! floating-point number stored using `double` */
757 #define A_FLOAT double
758 #define A_FLOAT_DIG DBL_DIG
759 #define A_FLOAT_EPSILON DBL_EPSILON
760 #define A_FLOAT_MANT_DIG DBL_MANT_DIG
761 #define A_FLOAT_MAX DBL_MAX
762 #define A_FLOAT_MAX_10_EXP DBL_MAX_10_EXP
763 #define A_FLOAT_MAX_EXP DBL_MAX_EXP
764 #define A_FLOAT_MIN DBL_MIN
765 #define A_FLOAT_MIN_10_EXP DBL_MIN_10_EXP
766 #define A_FLOAT_MIN_EXP DBL_MIN_EXP
769 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
771 #define A_FLOAT_C(X) X
773 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
775 #define A_FLOAT_F(F) F
776 #define A_FLOAT_F1(F, a) F(a)
777 #define A_FLOAT_F2(F, a, b) F(a, b)
778 #define A_FLOAT_F3(F, a, b, c) F(a, b, c)
779 #if defined(A_HAVE_VARIADIC_MACROS)
780 #define A_FLOAT_FN(F, ...) F(__VA_ARGS__)
781 #endif /* A_HAVE_VARIADIC_MACROS */
783 /*! format constants for the fprintf family of functions */
784 #define A_FLOAT_PRI(F, C) "%" F C
785 /*! format constants for the fscanf family of functions */
786 #define A_FLOAT_SCN(F, C) "%" F "l" C
788 #elif A_FLOAT_TYPE + 0 == A_FLOAT_EXTEND
790 /*! floating-point number stored using `long double` */
791 #define A_FLOAT long double
792 #define A_FLOAT_DIG LDBL_DIG
793 #define A_FLOAT_EPSILON LDBL_EPSILON
794 #define A_FLOAT_MANT_DIG LDBL_MANT_DIG
795 #define A_FLOAT_MAX LDBL_MAX
796 #define A_FLOAT_MAX_10_EXP LDBL_MAX_10_EXP
797 #define A_FLOAT_MAX_EXP LDBL_MAX_EXP
798 #define A_FLOAT_MIN LDBL_MIN
799 #define A_FLOAT_MIN_10_EXP LDBL_MIN_10_EXP
800 #define A_FLOAT_MIN_EXP LDBL_MIN_EXP
803 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
805 #define A_FLOAT_C(X) A_CAST_2(X, L)
807 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
809 #define A_FLOAT_F(F) A_CAST_2(F, l)
810 #define A_FLOAT_F1(F, a) A_CAST_2(F, l)(a)
811 #define A_FLOAT_F2(F, a, b) A_CAST_2(F, l)(a, b)
812 #define A_FLOAT_F3(F, a, b, c) A_CAST_2(F, l)(a, b, c)
813 #if defined(A_HAVE_VARIADIC_MACROS)
814 #define A_FLOAT_FN(F, ...) A_CAST_2(F, l)(__VA_ARGS__)
815 #endif /* A_HAVE_VARIADIC_MACROS */
817 /*! format constants for the fprintf family of functions */
818 #define A_FLOAT_PRI(F, C) "%" F "L" C
819 /*! format constants for the fscanf family of functions */
820 #define A_FLOAT_SCN(F, C) "%" F "L" C
822 #else /* !A_FLOAT_TYPE */
823 #error unsupported precision
824 #endif /* A_FLOAT_TYPE */
826 #define A_FLOAT_INF a_cast_s(A_FLOAT, A_F64_INF)
827 #define A_FLOAT_NAN (A_FLOAT_C(0.0) * A_FLOAT_INF)
829 /*! static cast to \ref a_float */
830 #define a_float_c(x) a_cast_s(A_FLOAT, x)
831 #define a_float_(_, x) a_cast_s(A_FLOAT _, x)
832 /*! compiler built-in floating-point number type */
833 #define a_float A_FLOAT
835 /*! @} a_float */
837 typedef union a_cast
839 a_c8 c;
840 a_int i;
841 a_uint u;
842 a_shrt ih;
843 a_ushrt uh;
844 a_long il;
845 a_ulong ul;
846 #if defined(A_HAVE_LONG_LONG_TYPE)
847 a_llong ill;
848 a_ullong ull;
849 #endif /* A_HAVE_LONG_LONG_TYPE */
850 a_i8 i8;
851 a_u8 u8;
852 a_i16 i16;
853 a_u16 u16;
854 a_i32 i32;
855 a_u32 u32;
856 a_i64 i64;
857 a_u64 u64;
858 a_f32 f32;
859 a_f64 f64;
860 a_imax imax;
861 a_umax umax;
862 a_iptr iptr;
863 a_uptr uptr;
864 a_diff diff;
865 a_size size;
866 void const *PTR;
867 void *ptr;
868 char const *STR;
869 char *str;
870 #if defined(A_FLOAT_TYPE) && (A_FLOAT_TYPE + 0 < A_FLOAT_EXTEND)
871 a_float f;
872 #endif /* A_FLOAT_TYPE */
873 } a_cast;
876 @brief square of x, \f$ x^2 \f$
878 #define A_SQ(x) ((x) * (x))
881 @brief absolute value of x, \f$ |x| \f$
883 #define A_ABS(x) ((x) < 0 ? -(x) : (x))
886 @brief minimum value between x and y
888 #define A_MIN(x, y) (((x) < (y)) ? (x) : (y))
891 @brief maximum value between x and y
893 #define A_MAX(x, y) (((x) > (y)) ? (x) : (y))
896 @brief signum function, \f$ \texttt{sgn}{x}=\begin{cases}+1&x>0\\0&0\\-1&x<0\end{cases} \f$
898 #define A_SGN(x) ((0 < (x)) - ((x) < 0))
901 @brief saturation value of x, \f$ \texttt{sat}(x,min,max)=\begin{cases}min&min>x\\max&x>max\\x&else\end{cases} \f$
903 #define A_SAT(x, min, max) ((min) < (x) ? (x) < (max) ? (x) : (max) : (min))
906 @brief number of elements in a visible array
907 @param a must be a visible array
909 #define A_LEN(a) (sizeof(a) / sizeof(*(a)))
912 @brief offset of a structure member
913 @param type structure type
914 @param member member variable
916 #if defined(offsetof)
917 #define a_offsetof(type, member) offsetof(type, member)
918 #else /* !offsetof */
919 #define a_offsetof(type, member) a_cast_r(a_size, &a_cast_r(type *, 0)->member)
920 #endif /* offsetof */
923 @brief container of a structure member
924 @param ptr pointer to a member variable
925 @param type structure type
926 @param member member variable
928 #define a_container_of(ptr, type, member) a_cast_r(type *, a_cast_r(a_uptr, ptr) - a_offsetof(type, member))
931 @brief round down size `n` to be a multiple of `a`
933 #define a_size_down(a, n) (a_cast_s(a_size, n) & ~a_cast_s(a_size, (a) - 1))
936 @brief round up size `n` to be a multiple of `a`
938 #define a_size_up(a, n) ((a_cast_s(a_size, n) + (a) - 1) & ~a_cast_s(a_size, (a) - 1))
941 @brief round pointer `p` down to the closest `a`, aligned address <= `p`
943 #define a_align_down(a, p) a_cast_r(void *, a_cast_r(a_uptr, p) & ~a_cast_s(a_uptr, (a) - 1))
946 @brief round pointer `p` up to the closest `a`, aligned address >= `p`
948 #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))
951 @brief iterate from 0 to n and not include n
952 @param I index type of the iteration
953 @param i index variable of the iteration
954 @param n final value of the iteration
956 #define a_forenum(I, i, n) for (I i = 0; i < (n); ++i)
959 @brief iterate from n to 0 and not include n
960 @param I index type of the iteration
961 @param i index variable of the iteration
962 @param n final value of the iteration
964 #define a_forenum_reverse(I, i, n) for (I i = (n); i-- > 0;)
967 @brief iterate over an array
968 @param T the prefix of the element type
969 @param P the suffix of the element type
970 @param it pointer to the current element
971 @param ptr starting address of this array
972 @param num number of elements in this array
974 #define a_foreach(T, P, it, ptr, num) \
975 for (T P it = a_cast_s(T P, ptr), P it##_ = it + (num); it < it##_; ++it)
976 /*! @copydoc a_foreach */
977 #define a_forsafe(T, P, it, ptr, num) \
978 for (T P it = a_cast_s(T P, ptr), P it##_ = (num) ? it + (num) : it; it < it##_; ++it)
981 @brief iterate over an array in reverse
982 @param T the prefix of the element type
983 @param P the suffix of the element type
984 @param it pointer to the current element
985 @param ptr starting address of this array
986 @param num number of elements in this array
988 #define a_foreach_reverse(T, P, it, ptr, num) \
989 for (T P it##_ = a_cast_s(T P, ptr) - 1, P it = it##_ + (num); it > it##_; --it)
990 /*! @copydoc a_foreach_reverse */
991 #define a_forsafe_reverse(T, P, it, ptr, num) \
992 for (T P it##_ = (num) ? a_cast_s(T P, ptr) - 1 : a_cast_s(T P, ptr), \
993 P it = (num) ? it##_ + (num) : it##_; \
994 it > it##_; --it)
997 @brief iterate over an array
998 @param T the prefix of the element type
999 @param P the suffix of the element type
1000 @param it pointer to the current element
1001 @param ptr starting address of this array
1002 @param end the end address of this array
1004 #define a_iterate(T, P, it, ptr, end) \
1005 for (T P it = a_cast_s(T P, ptr), P it##_ = a_cast_s(T P, end); it < it##_; ++it)
1008 @brief iterate over an array in reverse
1009 @param T the prefix of the element type
1010 @param P the suffix of the element type
1011 @param it pointer to the current element
1012 @param ptr starting address of this array
1013 @param end the end address of this array
1015 #define a_iterate_reverse(T, P, it, ptr, end) \
1016 for (T P it = a_cast_s(T P, end) - 1, P it##_ = a_cast_s(T P, ptr) - 1; it > it##_; --it)
1019 @brief enumeration for return values
1021 enum
1023 A_SUCCESS /*!< return success */,
1024 A_FAILURE /*!< return failure */,
1025 A_INVALID /*!< return invalid */
1028 #if defined(__cplusplus)
1029 extern "C" {
1030 #endif /* __cplusplus */
1031 #if defined(LIBA_A_C)
1032 #undef A_INTERN
1033 #define A_INTERN A_INLINE
1034 #endif /* LIBA_A_C */
1037 @brief reverse the bits in an 8-bit unsigned integer
1038 @param x an 8-bit unsigned integer to be reversed
1039 @return reversed 8-bit unsigned integer
1041 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1042 A_EXTERN a_u8 a_u8_rev(a_u8 x);
1043 #endif /* A_HAVE_INLINE */
1044 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1045 A_INTERN a_u8 a_u8_rev(a_u8 x)
1047 x = a_cast_s(a_u8, (x >> 4) | (x << 4));
1048 x = a_cast_s(a_u8, ((x & 0xCC) >> 2) | ((x & 0x33) << 2));
1049 x = a_cast_s(a_u8, ((x & 0xAA) >> 1) | ((x & 0x55) << 1));
1050 return x;
1052 #endif /* A_HAVE_INLINE */
1055 @brief reverse the bits in a 16-bit unsigned integer
1056 @param x a 16-bit unsigned integer to be reversed
1057 @return reversed 16-bit unsigned integer
1059 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1060 A_EXTERN a_u16 a_u16_rev(a_u16 x);
1061 #endif /* A_HAVE_INLINE */
1062 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1063 A_INTERN a_u16 a_u16_rev(a_u16 x)
1065 x = a_cast_s(a_u16, (x >> 8) | (x << 8));
1066 x = a_cast_s(a_u16, ((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4));
1067 x = a_cast_s(a_u16, ((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2));
1068 x = a_cast_s(a_u16, ((x & 0xAAAA) >> 1) | ((x & 0x5555) << 1));
1069 return x;
1071 #endif /* A_HAVE_INLINE */
1072 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1073 A_EXTERN a_u16 a_u16_getl(void const *b);
1074 #endif /* A_HAVE_INLINE */
1075 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1076 A_INTERN a_u16 a_u16_getl(void const *b)
1078 a_byte const *p = a_cast_s(a_byte const *, b);
1079 return a_cast_s(a_u16, (p[0] << 0) | (p[1] << 8));
1081 #endif /* A_HAVE_INLINE */
1082 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1083 A_EXTERN a_u16 a_u16_getb(void const *b);
1084 #endif /* A_HAVE_INLINE */
1085 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1086 A_INTERN a_u16 a_u16_getb(void const *b)
1088 a_byte const *p = a_cast_s(a_byte const *, b);
1089 return a_cast_s(a_u16, (p[1] << 0) | (p[0] << 8));
1091 #endif /* A_HAVE_INLINE */
1092 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1093 A_EXTERN void a_u16_setl(void *b, a_u16 x);
1094 #endif /* A_HAVE_INLINE */
1095 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1096 A_INTERN void a_u16_setl(void *b, a_u16 x)
1098 a_byte *p = a_cast_s(a_byte *, b);
1099 p[0] = a_cast_s(a_byte, x >> 0);
1100 p[1] = a_cast_s(a_byte, x >> 8);
1102 #endif /* A_HAVE_INLINE */
1103 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1104 A_EXTERN void a_u16_setb(void *b, a_u16 x);
1105 #endif /* A_HAVE_INLINE */
1106 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1107 A_INTERN void a_u16_setb(void *b, a_u16 x)
1109 a_byte *p = a_cast_s(a_byte *, b);
1110 p[0] = a_cast_s(a_byte, x >> 8);
1111 p[1] = a_cast_s(a_byte, x >> 0);
1113 #endif /* A_HAVE_INLINE */
1116 @brief reverse the bits in a 32-bit unsigned integer
1117 @param x a 32-bit unsigned integer to be reversed
1118 @return reversed 32-bit unsigned integer
1120 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1121 A_EXTERN a_u32 a_u32_rev(a_u32 x);
1122 #endif /* A_HAVE_INLINE */
1123 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1124 A_INTERN a_u32 a_u32_rev(a_u32 x)
1126 x = (x >> 16) | (x << 16);
1127 x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
1128 x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
1129 x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
1130 x = ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
1131 return x;
1133 #endif /* A_HAVE_INLINE */
1134 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1135 A_EXTERN a_u32 a_u32_getl(void const *b);
1136 #endif /* A_HAVE_INLINE */
1137 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1138 A_INTERN a_u32 a_u32_getl(void const *b)
1140 a_byte const *p = a_cast_s(a_byte const *, b);
1141 return (a_cast_s(a_u32, p[0]) << 0x00) |
1142 (a_cast_s(a_u32, p[1]) << 0x08) |
1143 (a_cast_s(a_u32, p[2]) << 0x10) |
1144 (a_cast_s(a_u32, p[3]) << 0x18);
1146 #endif /* A_HAVE_INLINE */
1147 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1148 A_EXTERN a_u32 a_u32_getb(void const *b);
1149 #endif /* A_HAVE_INLINE */
1150 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1151 A_INTERN a_u32 a_u32_getb(void const *b)
1153 a_byte const *p = a_cast_s(a_byte const *, b);
1154 return (a_cast_s(a_u32, p[0]) << 0x18) |
1155 (a_cast_s(a_u32, p[1]) << 0x10) |
1156 (a_cast_s(a_u32, p[2]) << 0x08) |
1157 (a_cast_s(a_u32, p[3]) << 0x00);
1159 #endif /* A_HAVE_INLINE */
1160 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1161 A_EXTERN void a_u32_setl(void *b, a_u32 x);
1162 #endif /* A_HAVE_INLINE */
1163 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1164 A_INTERN void a_u32_setl(void *b, a_u32 x)
1166 a_byte *p = a_cast_s(a_byte *, b);
1167 p[0] = a_cast_s(a_byte, x >> 0x00);
1168 p[1] = a_cast_s(a_byte, x >> 0x08);
1169 p[2] = a_cast_s(a_byte, x >> 0x10);
1170 p[3] = a_cast_s(a_byte, x >> 0x18);
1172 #endif /* A_HAVE_INLINE */
1173 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1174 A_EXTERN void a_u32_setb(void *b, a_u32 x);
1175 #endif /* A_HAVE_INLINE */
1176 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1177 A_INTERN void a_u32_setb(void *b, a_u32 x)
1179 a_byte *p = a_cast_s(a_byte *, b);
1180 p[0] = a_cast_s(a_byte, x >> 0x18);
1181 p[1] = a_cast_s(a_byte, x >> 0x10);
1182 p[2] = a_cast_s(a_byte, x >> 0x08);
1183 p[3] = a_cast_s(a_byte, x >> 0x00);
1185 #endif /* A_HAVE_INLINE */
1188 @brief reverse the bits in a 64-bit unsigned integer
1189 @param x a 64-bit unsigned integer to be reversed
1190 @return reversed 64-bit unsigned integer
1192 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1193 A_EXTERN a_u64 a_u64_rev(a_u64 x);
1194 #endif /* A_HAVE_INLINE */
1195 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1196 A_INTERN a_u64 a_u64_rev(a_u64 x)
1198 x = (x >> 32) | (x << 32);
1199 x = ((x & 0xFFFF0000FFFF0000) >> 0x10) | ((x & 0x0000FFFF0000FFFF) << 0x10);
1200 x = ((x & 0xFF00FF00FF00FF00) >> 0x08) | ((x & 0x00FF00FF00FF00FF) << 0x08);
1201 x = ((x & 0xF0F0F0F0F0F0F0F0) >> 0x04) | ((x & 0x0F0F0F0F0F0F0F0F) << 0x04);
1202 x = ((x & 0xCCCCCCCCCCCCCCCC) >> 0x02) | ((x & 0x3333333333333333) << 0x02);
1203 x = ((x & 0xAAAAAAAAAAAAAAAA) >> 0x01) | ((x & 0x5555555555555555) << 0x01);
1204 return x;
1206 #endif /* A_HAVE_INLINE */
1207 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1208 A_EXTERN a_u64 a_u64_getl(void const *b);
1209 #endif /* A_HAVE_INLINE */
1210 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1211 A_INTERN a_u64 a_u64_getl(void const *b)
1213 a_byte const *p = a_cast_s(a_byte const *, b);
1214 return (a_cast_s(a_u64, p[0]) << 0x00) |
1215 (a_cast_s(a_u64, p[1]) << 0x08) |
1216 (a_cast_s(a_u64, p[2]) << 0x10) |
1217 (a_cast_s(a_u64, p[3]) << 0x18) |
1218 (a_cast_s(a_u64, p[4]) << 0x20) |
1219 (a_cast_s(a_u64, p[5]) << 0x28) |
1220 (a_cast_s(a_u64, p[6]) << 0x30) |
1221 (a_cast_s(a_u64, p[7]) << 0x38);
1223 #endif /* A_HAVE_INLINE */
1224 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1225 A_EXTERN a_u64 a_u64_getb(void const *b);
1226 #endif /* A_HAVE_INLINE */
1227 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1228 A_INTERN a_u64 a_u64_getb(void const *b)
1230 a_byte const *p = a_cast_s(a_byte const *, b);
1231 return (a_cast_s(a_u64, p[0]) << 0x38) |
1232 (a_cast_s(a_u64, p[1]) << 0x30) |
1233 (a_cast_s(a_u64, p[2]) << 0x28) |
1234 (a_cast_s(a_u64, p[3]) << 0x20) |
1235 (a_cast_s(a_u64, p[4]) << 0x18) |
1236 (a_cast_s(a_u64, p[5]) << 0x10) |
1237 (a_cast_s(a_u64, p[6]) << 0x08) |
1238 (a_cast_s(a_u64, p[7]) << 0x00);
1240 #endif /* A_HAVE_INLINE */
1241 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1242 A_EXTERN void a_u64_setl(void *b, a_u64 x);
1243 #endif /* A_HAVE_INLINE */
1244 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1245 A_INTERN void a_u64_setl(void *b, a_u64 x)
1247 a_byte *p = a_cast_s(a_byte *, b);
1248 p[0] = a_cast_s(a_byte, x >> 0x00);
1249 p[1] = a_cast_s(a_byte, x >> 0x08);
1250 p[2] = a_cast_s(a_byte, x >> 0x10);
1251 p[3] = a_cast_s(a_byte, x >> 0x18);
1252 p[4] = a_cast_s(a_byte, x >> 0x20);
1253 p[5] = a_cast_s(a_byte, x >> 0x28);
1254 p[6] = a_cast_s(a_byte, x >> 0x30);
1255 p[7] = a_cast_s(a_byte, x >> 0x38);
1257 #endif /* A_HAVE_INLINE */
1258 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1259 A_EXTERN void a_u64_setb(void *b, a_u64 x);
1260 #endif /* A_HAVE_INLINE */
1261 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1262 A_INTERN void a_u64_setb(void *b, a_u64 x)
1264 a_byte *p = a_cast_s(a_byte *, b);
1265 p[0] = a_cast_s(a_byte, x >> 0x38);
1266 p[1] = a_cast_s(a_byte, x >> 0x30);
1267 p[2] = a_cast_s(a_byte, x >> 0x28);
1268 p[3] = a_cast_s(a_byte, x >> 0x20);
1269 p[4] = a_cast_s(a_byte, x >> 0x18);
1270 p[5] = a_cast_s(a_byte, x >> 0x10);
1271 p[6] = a_cast_s(a_byte, x >> 0x08);
1272 p[7] = a_cast_s(a_byte, x >> 0x00);
1274 #endif /* A_HAVE_INLINE */
1277 @brief copy one buffer to another
1278 @param[in] dst pointer to the object to copy to
1279 @param[in] src pointer to the object to copy from
1280 @param[in] siz number of bytes to copy
1281 @return a copy of dest
1283 A_EXTERN void *a_copy(void *__restrict dst, void const *__restrict src, a_size siz);
1286 @brief move one buffer to another
1287 @param[in] dst pointer to the object to copy to
1288 @param[in] src pointer to the object to copy from
1289 @param[in] siz number of bytes to copy
1290 @return a copy of dest
1292 A_EXTERN void *a_move(void *dst, void const *src, a_size siz);
1295 @brief fill a buffer with a character
1296 @param[in] ptr pointer to the object to fill
1297 @param[in] siz number of bytes to fill
1298 @param[in] val fill byte
1299 @return a copy of dest
1301 A_EXTERN void *a_fill(void *ptr, a_size siz, int val);
1304 @brief fill a buffer with zero
1305 @param[in] ptr pointer to the object to fill
1306 @param[in] siz number of bytes to fill
1307 @return a copy of dest
1309 A_EXTERN void *a_zero(void *ptr, a_size siz);
1312 @brief swap two different memory blocks of the same size
1313 @param[in] siz the size of memory block being swapped
1314 @param[in,out] lhs points to memory block on the left
1315 @param[in,out] rhs points to memory block on the right
1317 A_EXTERN void a_swap(void *lhs, void *rhs, a_size siz);
1320 @brief a hash function whose prime number is 131
1321 @param[in] str string to be processed
1322 @param[in] val initial value
1323 @return hash value
1325 A_EXTERN a_u32 a_hash_bkdr(void const *str, a_u32 val);
1328 @brief a hash function whose prime number is 131
1329 @param[in] ptr points to string to be processed
1330 @param[in] siz length of string to be processed
1331 @param[in] val initial value
1332 @return hash value
1334 A_EXTERN a_u32 a_hash_bkdr_(void const *ptr, a_size siz, a_u32 val);
1337 @brief a hash function whose prime number is 65599
1338 @param[in] str string to be processed
1339 @param[in] val initial value
1340 @return hash value
1342 A_EXTERN a_u32 a_hash_sdbm(void const *str, a_u32 val);
1345 @brief a hash function whose prime number is 65599
1346 @param[in] ptr points to string to be processed
1347 @param[in] siz length of string to be processed
1348 @param[in] val initial value
1349 @return hash value
1351 A_EXTERN a_u32 a_hash_sdbm_(void const *ptr, a_size siz, a_u32 val);
1354 @brief roll back the elements of a float array and save the cache array
1355 @param[in] block_p points to a float array
1356 @param[in] block_n number of a float array
1357 @param[in] cache_p points to a cache array
1358 @param[in] cache_n number of a cache array
1360 A_EXTERN void a_float_push(a_float *block_p, a_size block_n,
1361 a_float const *cache_p, a_size cache_n);
1364 @brief roll back the elements of a float array circularly, array>>shift
1365 @param[in] block_p points to a float array
1366 @param[in] block_n number of a float array
1367 @param[in] shift_p points to a shift array
1368 @param[in] shift_n number of a shift array
1370 A_EXTERN void a_float_roll(a_float *block_p, a_size block_n,
1371 a_float *shift_p, a_size shift_n);
1374 @brief allocation function pointer
1375 @param[in] addr address of memory block
1376 @param[in] size new size of memory block
1377 @return new address of memory block or null
1379 A_EXTERN void *(*a_alloc)(void *addr, a_size size);
1382 @brief default allocation function
1383 @param[in] addr address of memory block
1384 @param[in] size new size of memory block
1385 @return new address of memory block or null
1387 A_EXTERN void *a_alloc_(void *addr, a_size size);
1389 #if defined(LIBA_A_C)
1390 #undef A_INTERN
1391 #define A_INTERN static A_INLINE
1392 #endif /* LIBA_A_C */
1393 #if defined(__cplusplus)
1394 } /* extern "C" */
1395 #endif /* __cplusplus */
1397 /*! @brief declare allocation function */
1398 #define A_ALLOC(alloc, addr, size) void *alloc(void *addr, a_size size)
1399 #define a_new(T, ptr, num) a_cast_s(T *, a_alloc(ptr, sizeof(T) * (num)))
1400 #define a_die(ptr) a_alloc(ptr, 0)
1402 /*! @} liba */
1404 #endif /* a/a.h */