bump copyright year to 2025
[liba.git] / include / a / a.h
blob51c94b2ee311bb15c77f40e8fd3fcc427d6bd804
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(__MINGW32__)
41 #undef __USE_MINGW_ANSI_STDIO
42 #define __USE_MINGW_ANSI_STDIO 1
43 #endif /* __MINGW32__ */
45 /* https://en.wikipedia.org/wiki/Microsoft_Visual_C++ */
46 #if defined(_MSC_VER)
47 #define A_PREREQ_MSVC(maj, min) (_MSC_VER >= (maj * 100 + min))
48 #else /* !_MSC_VER */
49 #define A_PREREQ_MSVC(maj, min) 0
50 #endif /* _MSC_VER */
52 /* https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html */
53 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
54 #define A_PREREQ_GNUC(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
55 #else /* !__GNUC__ */
56 #define A_PREREQ_GNUC(maj, min) 0
57 #endif /* __GNUC__ */
59 /* https://clang.llvm.org/docs/LanguageExtensions.html */
60 #if defined(__clang_major__) && defined(__clang_minor__)
61 #define A_PREREQ_LLVM(maj, min) ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
62 #else /* !__clang__ */
63 #define A_PREREQ_LLVM(maj, min) 0
64 #endif /* __clang__ */
66 #if !defined A_FUNC
67 #if defined(_MSC_VER)
68 #define A_FUNC __FUNCTION__
69 #elif defined(__GNUC__)
70 #define A_FUNC (__extension__ __PRETTY_FUNCTION__)
71 #elif defined(__cplusplus) && (__cplusplus > 201100L) || \
72 defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L)
73 #define A_FUNC __func__
74 #else /* !__func__ */
75 #define A_FUNC __FUNCTION__
76 #endif /* __func__ */
77 #endif /* A_FUNC */
79 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L) || \
80 defined(__cplusplus) && (__cplusplus > 201100L) || A_PREREQ_MSVC(18, 0)
82 #if !defined A_HAVE_LONG_LONG_TYPE
83 #define A_HAVE_LONG_LONG_TYPE 1
84 #endif /* A_HAVE_LONG_LONG_TYPE */
86 #endif /* C > 199900 or C++ > 201100 */
87 #if defined(A_HAVE_LONG_LONG_TYPE) && (A_HAVE_LONG_LONG_TYPE + 0 < 1)
88 #undef A_HAVE_LONG_LONG_TYPE
89 #endif /* A_HAVE_LONG_LONG_TYPE */
91 #if defined(__cplusplus) && (__cplusplus > 201100L)
93 #if !defined A_HAVE_NULLPTR
94 #define A_HAVE_NULLPTR 1
95 #endif /* A_HAVE_NULLPTR */
97 #endif /* C++ > 201100 */
98 #if defined(A_HAVE_NULLPTR) && (A_HAVE_NULLPTR + 0 < 1)
99 #undef A_HAVE_NULLPTR
100 #endif /* A_HAVE_NULLPTR */
102 #if A_PREREQ_GNUC(2, 96) || __has_builtin(__builtin_expect)
103 #define A_UNLIKELY(x) __builtin_expect(!!(x), 0)
104 #define A_LIKELY(x) __builtin_expect(!!(x), 1)
105 #else /* !likely */
106 #define A_UNLIKELY(x) (!!(x))
107 #define A_LIKELY(x) (!!(x))
108 #endif /* likely */
110 #if defined(_WIN32) || defined(__CYGWIN__)
111 #define A_DECLSPEC(x) __declspec(x)
112 #else /* !__declspec */
113 #define A_DECLSPEC(x)
114 #endif /* __declspec */
116 #if defined(__GNUC__) || defined(__clang__)
117 #define A_ATTRIBUTE(x) __attribute__(x)
118 #else /* !__attribute__ */
119 #define A_ATTRIBUTE(x)
120 #endif /* __attribute__ */
122 #if __has_builtin(__builtin_assume)
123 #define A_ASSUME(x) __builtin_assume(x)
124 #elif A_PREREQ_GNUC(13, 0)
125 #define A_ASSUME(x) __attribute__((__assume__(x)))
126 #elif A_PREREQ_GNUC(4, 5)
127 #define A_ASSUME(x) \
128 do { \
129 if (!(x)) { __builtin_unreachable(); } \
130 } while (0)
131 #elif defined(_MSC_VER)
132 #define A_ASSUME(x) __assume(x)
133 #else /* !assume */
134 #define A_ASSUME(x) (void)0
135 #endif /* assume */
137 /* attribute nonnull */
138 #if A_PREREQ_GNUC(3, 3) || __has_attribute(__nonnull__)
139 #define A_NONULL(x) __attribute__((__nonnull__ x))
140 #else /* !nonnull */
141 #define A_NONULL(x)
142 #endif /* nonnull */
144 /* attribute format */
145 #if A_PREREQ_GNUC(2, 4) || __has_attribute(__format__)
146 #define A_FORMAT(_, a, b) __attribute__((__format__(_, a, b)))
147 #else /* !format */
148 #define A_FORMAT(_, a, b)
149 #endif /* format */
151 /* attribute fallthrough */
152 #if A_PREREQ_GNUC(7, 0) || __has_attribute(__fallthrough__)
153 #define A_FALLTHROUGH __attribute__((__fallthrough__))
154 #else /* !fallthrough */
155 #define A_FALLTHROUGH (void)0
156 #endif /* fallthrough */
158 /* attribute deprecated */
159 #if A_PREREQ_GNUC(3, 2) || __has_attribute(__deprecated__)
160 #define A_DEPRECATED __attribute__((__deprecated__))
161 #elif defined(_WIN32) || defined(__CYGWIN__)
162 #define A_DEPRECATED __declspec(deprecated)
163 #else /* !deprecated */
164 #define A_DEPRECATED
165 #endif /* deprecated */
167 /* attribute always inline */
168 #if A_PREREQ_GNUC(3, 2) || __has_attribute(__always_inline__)
169 #define A_INLINE __inline __attribute__((__always_inline__))
170 #elif defined(_MSC_VER)
171 #define A_INLINE __inline __forceinline
172 #else /* !_MSC_VER */
173 #define A_INLINE __inline
174 #endif /* _MSC_VER */
175 #if !defined A_INTERN
176 #define A_INTERN static A_INLINE
177 #endif /* A_INTERN */
179 /* attribute visibility */
180 #if defined(_WIN32) || defined(__CYGWIN__)
181 #define A_EXPORT __declspec(dllexport)
182 #define A_IMPORT __declspec(dllimport)
183 #define A_HIDDEN
184 #elif A_PREREQ_GNUC(4, 0) || __has_attribute(__visibility__)
185 #define A_EXPORT __attribute__((__visibility__("default")))
186 #define A_IMPORT __attribute__((__visibility__("default")))
187 #define A_HIDDEN __attribute__((__visibility__("hidden")))
188 #else /* !visibility */
189 #define A_EXPORT
190 #define A_IMPORT
191 #define A_HIDDEN
192 #endif /* visibility */
193 #if defined(A_EXPORTS)
194 #define A_PUBLIC A_EXPORT
195 #elif defined(A_IMPORTS)
196 #define A_PUBLIC A_IMPORT
197 #else /* !A_PUBLIC */
198 #define A_PUBLIC
199 #endif /* A_PUBLIC */
201 #if !defined A_EXTERN
202 #define A_EXTERN A_PUBLIC extern
203 #endif /* A_EXTERN */
204 #if !defined __cplusplus
205 #define A_EXTERN_C
206 #define A_EXTERN_C_ENTER
207 #define A_EXTERN_C_LEAVE
208 #else /* !__cplusplus */
209 #define A_EXTERN_C extern "C"
210 #define A_EXTERN_C_ENTER extern "C" {
211 #define A_EXTERN_C_LEAVE }
212 #endif /* __cplusplus */
214 /*! @endcond */
216 #include <stddef.h>
217 #include <limits.h>
218 #include <float.h>
221 @addtogroup liba algorithm library
226 @def A_ORDER_LITTLE
227 @brief byte order of little endian architecture
229 #if defined(__ORDER_LITTLE_ENDIAN__)
230 #define A_ORDER_LITTLE __ORDER_LITTLE_ENDIAN__
231 #else /* !__ORDER_LITTLE_ENDIAN__ */
232 #define A_ORDER_LITTLE 1234
233 #endif /* __ORDER_LITTLE_ENDIAN__ */
235 @def A_ORDER_BIG
236 @brief byte order of big endian architecture
238 #if defined(__ORDER_BIG_ENDIAN__)
239 #define A_ORDER_BIG __ORDER_BIG_ENDIAN__
240 #else /* !__ORDER_BIG_ENDIAN__ */
241 #define A_ORDER_BIG 4321
242 #endif /* __ORDER_BIG_ENDIAN__ */
243 #if !defined A_BYTE_ORDER
245 @def A_BYTE_ORDER
246 @brief byte order of architecture
248 #if defined(__BYTE_ORDER__)
249 #define A_BYTE_ORDER __BYTE_ORDER__
250 #else /* !__BYTE_ORDER__ */
251 #define A_BYTE_ORDER 1234
252 #endif /* __BYTE_ORDER__ */
253 #endif /* A_BYTE_ORDER */
255 #if !defined A_SIZE_POINTER
257 @def A_SIZE_POINTER
258 @brief size of void pointer
260 #if defined(__SIZEOF_POINTER__)
261 #define A_SIZE_POINTER __SIZEOF_POINTER__
262 #elif defined(_WIN64) || defined(__LP64__)
263 #define A_SIZE_POINTER 8
264 #else /* !__SIZEOF_POINTER__ */
265 #define A_SIZE_POINTER 4
266 #endif /* __SIZEOF_POINTER__ */
267 #endif /* A_SIZE_POINTER */
269 /*! @brief assert a build-time dependency, as an expression */
270 #define A_BUILD_ASSERT(x) (void)(sizeof(char[1 - 2 * !(x)]))
271 /*! @brief assert a build-time dependency, as an expression */
272 #define A_BUILD_BUG_ON(x) (void)(sizeof(char[1 - 2 * !!(x)]))
274 #if defined(__cplusplus)
275 #define a_cast_r(T, x) reinterpret_cast<T>(x)
276 #define a_cast_d(T, x) dynamic_cast<T>(x)
277 #define a_cast_s(T, x) static_cast<T>(x)
278 #define a_cast_c(T, x) const_cast<T>(x)
279 #else /* !__cplusplus */
280 #define a_cast_r(T, x) ((T)(x))
281 #define a_cast_d(T, x) ((T)(x))
282 #define a_cast_s(T, x) ((T)(x))
283 #define a_cast_c(T, x) ((T)(x))
284 #endif /* __cplusplus */
285 #define A_CAST_3(a, b, c) a##b##c
286 #define A_CAST_2(a, b) a##b
287 #define A_CAST_1(a) #a
289 #if defined(__cplusplus) && defined(A_HAVE_NULLPTR)
290 #define A_NULL nullptr
291 #else /* !__cplusplus */
292 #define A_NULL NULL
293 #endif /* __cplusplus */
294 /*! @brief static cast to \ref a_void */
295 #define a_void_c(x) a_cast_s(void, x)
296 #define a_void_(_, x) a_cast_s(void _, x)
297 /*! @brief incomplete type or no parameter or no return value */
298 #define a_void void
300 #if defined(__cplusplus)
301 #define A_TRUE true
302 #define A_FALSE false
303 #if !defined A_BOOL
304 #define A_BOOL bool
305 #endif /* A_BOOL */
306 #else /* !__cplusplus */
307 #define A_TRUE 1
308 #define A_FALSE 0
309 #if !defined A_BOOL
310 #if defined(__STDC_VERSION__) || A_PREREQ_MSVC(18, 0)
311 #define A_BOOL _Bool
312 #else /* !__STDC_VERSION__ */
313 #define A_BOOL unsigned char
314 #endif /* __STDC_VERSION__ */
315 #endif /* A_BOOL */
316 #endif /* __cplusplus */
317 /*! @brief static cast to \ref a_bool */
318 #define a_bool_c(x) (!!(x))
319 /*! @brief type, capable of holding one of the two values: 1 and 0 */
320 typedef A_BOOL a_bool;
322 #define A_INT int
323 #define A_INT_MAX INT_MAX
324 #define A_INT_MIN INT_MIN
325 /*! @brief 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 /*! @brief signed integer type is guaranteed to be at least 16 bits */
329 typedef A_INT a_int;
331 #define A_UINT unsigned int
332 #define A_UINT_MAX UINT_MAX
333 /*! @brief 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 /*! @brief unsigned integer type is guaranteed to be at least 16 bits */
337 typedef A_UINT a_uint;
339 #define A_SHRT short
340 #define A_SHRT_MAX SHRT_MAX
341 #define A_SHRT_MIN SHRT_MIN
342 /*! @brief 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 /*! @brief signed integer type is guaranteed to be at least 16 bits */
346 typedef A_SHRT a_shrt;
348 #define A_USHRT unsigned short
349 #define A_USHRT_MAX USHRT_MAX
350 /*! @brief 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 /*! @brief unsigned integer type is guaranteed to be at least 16 bits */
354 typedef A_USHRT a_ushrt;
356 #define A_LONG long
357 #define A_LONG_MIN LONG_MIN
358 #define A_LONG_MAX LONG_MAX
359 /*! @brief 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 /*! @brief signed integer type is guaranteed to be at least 32 bits */
363 typedef A_LONG a_long;
365 #define A_ULONG unsigned long
366 #define A_ULONG_MAX ULONG_MAX
367 /*! @brief 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 /*! @brief unsigned integer type is guaranteed to be at least 32 bits */
371 typedef A_ULONG a_ulong;
373 #if defined(A_HAVE_LONG_LONG_TYPE)
375 #define A_LLONG long long
376 #define A_LLONG_MAX LLONG_MAX
377 #define A_LLONG_MIN LLONG_MIN
378 /*! @brief 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 /*! @brief signed integer type is guaranteed to be at least 64 bits */
382 typedef A_LLONG a_llong;
384 #define A_ULLONG unsigned long long
385 #define A_ULLONG_MAX ULLONG_MAX
386 /*! @brief 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 /*! @brief unsigned integer type is guaranteed to be at least 64 bits */
390 typedef 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 /*! @brief 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 /*! @brief type for unsigned character representation */
400 typedef A_BYTE a_byte;
402 #define A_C8 char
403 #define A_C8_MAX CHAR_MAX
404 #define A_C8_MIN CHAR_MIN
405 /*! @brief 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 /*! @brief type for character representation */
409 typedef A_C8 a_c8;
411 #if !defined A_I8
412 #define A_I8 signed char
413 #endif /* A_I8 */
414 #if !defined A_I8_C
415 #define A_I8_C(X) X
416 #endif /* A_I8_C */
417 #if !defined A_I8_MAX
418 #define A_I8_MAX A_I8_C(0x7F)
419 #endif /* A_I8_MAX */
420 #if !defined A_I8_MIN
421 #define A_I8_MIN A_I8_C(~0x7F)
422 #endif /* A_I8_MIN */
423 /*! @brief 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 /*! @brief signed integer type with width of exactly 8 bits */
427 typedef A_I8 a_i8;
429 #if !defined A_U8
430 #define A_U8 unsigned char
431 #endif /* A_U8 */
432 #if !defined A_U8_C
433 #define A_U8_C(X) X
434 #endif /* A_U8_C */
435 #if !defined A_U8_MAX
436 #define A_U8_MAX A_U8_C(0xFF)
437 #endif /* A_U8_MAX */
438 /*! @brief 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 /*! @brief unsigned integer type with width of exactly 8 bits */
442 typedef A_U8 a_u8;
444 #if !defined A_PRI8
445 #define A_PRI8
446 #endif /* A_PRI8 */
447 #if !defined A_SCN8
448 #define A_SCN8 "hh"
449 #endif /* A_SCN8 */
451 #if !defined A_I16 && (INT_MAX == 0x7FFFL)
452 #define A_I16 int
453 #elif !defined A_I16
454 #define A_I16 short
455 #endif /* A_I16 */
456 #if !defined A_I16_C
457 #define A_I16_C(X) X
458 #endif /* A_I16_C */
459 #if !defined A_I16_MAX
460 #define A_I16_MAX A_I16_C(0x7FFF)
461 #endif /* A_I16_MAX */
462 #if !defined A_I16_MIN
463 #define A_I16_MIN A_I16_C(~0x7FFF)
464 #endif /* A_I16_MIN */
465 /*! @brief static cast to \ref a_i16 */
466 #define a_i16_c(x) a_cast_s(a_i16, x)
467 #define a_i16_(_, x) a_cast_s(a_i16 _, x)
468 /*! @brief signed integer type with width of exactly 16 bits */
469 typedef A_I16 a_i16;
471 #if !defined A_U16 && (UINT_MAX == 0xFFFFUL)
472 #define A_U16 unsigned int
473 #elif !defined A_U16
474 #define A_U16 unsigned short
475 #endif /* A_U16 */
476 #if !defined A_U16_C
477 #define A_U16_C(X) X
478 #endif /* A_U16_C */
479 #if !defined A_U16_MAX
480 #define A_U16_MAX A_U16_C(0xFFFF)
481 #endif /* A_U16_MAX */
482 /*! @brief static cast to \ref a_u16 */
483 #define a_u16_c(x) a_cast_s(a_u16, x)
484 #define a_u16_(_, x) a_cast_s(a_u16 _, x)
485 /*! @brief unsigned integer type with width of exactly 16 bits */
486 typedef A_U16 a_u16;
488 #if !defined A_PRI16 && (UINT_MAX == 0xFFFFUL)
489 #define A_PRI16
490 #elif !defined A_PRI16
491 #define A_PRI16
492 #endif /* A_PRI16 */
493 #if !defined A_SCN16 && (UINT_MAX == 0xFFFFUL)
494 #define A_SCN16
495 #elif !defined A_SCN16
496 #define A_SCN16 "h"
497 #endif /* A_SCN16 */
499 #if !defined A_I32 && (INT_MAX == 0x7FFFFFFFL)
500 #define A_I32 int
501 #elif !defined A_I32
502 #define A_I32 long
503 #endif /* A_I32 */
504 #if !defined A_I32_C && (INT_MAX == 0x7FFFFFFFL)
505 #define A_I32_C(X) X
506 #elif !defined A_I32_C
507 #define A_I32_C(X) A_CAST_2(X, L)
508 #endif /* A_I32_C */
509 #if !defined A_I32_MAX
510 #define A_I32_MAX A_I32_C(0x7FFFFFFF)
511 #endif /* A_I32_MAX */
512 #if !defined A_I32_MIN
513 #define A_I32_MIN A_I32_C(~0x7FFFFFFF)
514 #endif /* A_I32_MIN */
515 /*! @brief static cast to \ref a_i32 */
516 #define a_i32_c(x) a_cast_s(a_i32, x)
517 #define a_i32_(_, x) a_cast_s(a_i32 _, x)
518 /*! @brief signed integer type with width of exactly 32 bits */
519 typedef A_I32 a_i32;
521 #if !defined A_U32 && (UINT_MAX == 0xFFFFFFFFUL)
522 #define A_U32 unsigned int
523 #elif !defined A_U32
524 #define A_U32 unsigned long
525 #endif /* A_U32 */
526 #if !defined A_U32_C && (UINT_MAX == 0xFFFFFFFFUL)
527 #define A_U32_C(X) A_CAST_2(X, U)
528 #elif !defined A_U32_C
529 #define A_U32_C(X) A_CAST_2(X, UL)
530 #endif /* A_U32_C */
531 #if !defined A_U32_MAX
532 #define A_U32_MAX A_U32_C(0xFFFFFFFF)
533 #endif /* A_U32_MAX */
534 /*! @brief static cast to \ref a_u32 */
535 #define a_u32_c(x) a_cast_s(a_u32, x)
536 #define a_u32_(_, x) a_cast_s(a_u32 _, x)
537 /*! @brief unsigned integer type with width of exactly 32 bits */
538 typedef A_U32 a_u32;
540 #if !defined A_PRI32 && (UINT_MAX == 0xFFFFFFFFUL)
541 #define A_PRI32
542 #elif !defined A_PRI32
543 #define A_PRI32 "l"
544 #endif /* A_PRI32 */
545 #if !defined A_SCN32 && (UINT_MAX == 0xFFFFFFFFUL)
546 #define A_SCN32
547 #elif !defined A_SCN32
548 #define A_SCN32 "l"
549 #endif /* A_SCN32 */
551 #if !defined A_I64 && (LONG_MAX == 0x7FFFFFFFL)
552 #if defined(_MSC_VER) && (_MSC_VER < 1800) || \
553 defined(__WATCOMC__) && (__WATCOMC__ >= 1100) || \
554 defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
555 #define A_I64 __int64
556 #else /* !extension */
557 #define A_I64 long long
558 #endif /* extension */
559 #elif !defined A_I64
560 #define A_I64 long
561 #endif /* A_I64 */
562 #if !defined A_I64_C && (LONG_MAX == 0x7FFFFFFFL)
563 #if defined(_MSC_VER) && (_MSC_VER < 1800) || \
564 defined(__WATCOMC__) && (__WATCOMC__ >= 1100) || \
565 defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
566 #define A_I64_C(X) A_CAST_2(X, i64)
567 #elif defined(__GNUC__)
568 #define A_I64_C(X) (A_CAST_2(__extension__ X, LL))
569 #else /* !extension */
570 #define A_I64_C(X) A_CAST_2(X, LL)
571 #endif /* extension */
572 #elif !defined A_I64_C
573 #define A_I64_C(X) A_CAST_2(X, L)
574 #endif /* A_I64_C */
575 #if !defined A_I64_MAX
576 #define A_I64_MAX A_I64_C(0x7FFFFFFFFFFFFFFF)
577 #endif /* A_I64_MAX */
578 #if !defined A_I64_MIN
579 #define A_I64_MIN A_I64_C(~0x7FFFFFFFFFFFFFFF)
580 #endif /* A_I64_MIN */
581 /*! @brief static cast to \ref a_i64 */
582 #define a_i64_c(x) a_cast_s(a_i64, x)
583 #define a_i64_(_, x) a_cast_s(a_i64 _, x)
584 /* clang-format off */
585 #if defined(__GNUC__)
586 __extension__
587 #endif /* __GNUC__ */
588 /*! @brief signed integer type with width of exactly 64 bits */
589 typedef A_I64 a_i64;
590 /* clang-format on */
592 #if !defined A_U64 && (ULONG_MAX == 0xFFFFFFFFUL)
593 #if defined(_MSC_VER) && (_MSC_VER < 1800) || \
594 defined(__WATCOMC__) && (__WATCOMC__ >= 1100) || \
595 defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
596 #define A_U64 unsigned __int64
597 #else /* !extension */
598 #define A_U64 unsigned long long
599 #endif /* extension */
600 #elif !defined A_U64
601 #define A_U64 unsigned long
602 #endif /* A_U64 */
603 #if !defined A_U64_C && (ULONG_MAX == 0xFFFFFFFFUL)
604 #if defined(_MSC_VER) && (_MSC_VER < 1800) || \
605 defined(__WATCOMC__) && (__WATCOMC__ >= 1100) || \
606 defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
607 #define A_U64_C(X) A_CAST_2(X, ui64)
608 #elif defined(__GNUC__)
609 #define A_U64_C(X) (A_CAST_2(__extension__ X, ULL))
610 #else /* !extension */
611 #define A_U64_C(X) A_CAST_2(X, ULL)
612 #endif /* extension */
613 #elif !defined A_U64_C
614 #define A_U64_C(X) A_CAST_2(X, UL)
615 #endif /* A_U64_C */
616 #if !defined A_U64_MAX
617 #define A_U64_MAX A_U64_C(0xFFFFFFFFFFFFFFFF)
618 #endif /* A_U64_MAX */
619 /*! @brief static cast to \ref a_u64 */
620 #define a_u64_c(x) a_cast_s(a_u64, x)
621 #define a_u64_(_, x) a_cast_s(a_u64 _, x)
622 /* clang-format off */
623 #if defined(__GNUC__)
624 __extension__
625 #endif /* __GNUC__ */
626 /*! @brief unsigned integer type with width of exactly 64 bits */
627 typedef A_U64 a_u64;
628 /* clang-format on */
630 #if !defined A_PRI64 && (ULONG_MAX == 0xFFFFFFFFUL)
631 #if defined(_MSC_VER) && (_MSC_VER < 1800) || \
632 defined(__WATCOMC__) && (__WATCOMC__ >= 1100) || \
633 defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
634 #define A_PRI64 "I64"
635 #else /* !extension */
636 #define A_PRI64 "ll"
637 #endif /* extension */
638 #elif !defined A_PRI64
639 #define A_PRI64 "l"
640 #endif /* A_PRI64 */
641 #if !defined A_SCN64 && (ULONG_MAX == 0xFFFFFFFFUL)
642 #if defined(_MSC_VER) && (_MSC_VER < 1800) || \
643 defined(__WATCOMC__) && (__WATCOMC__ >= 1100) || \
644 defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
645 #define A_SCN64 "I64"
646 #else /* !extension */
647 #define A_SCN64 "ll"
648 #endif /* extension */
649 #elif !defined A_SCN64
650 #define A_SCN64 "l"
651 #endif /* A_SCN64 */
653 #if !defined A_IMAX
654 #define A_IMAX A_I64
655 #endif /* A_IMAX */
656 #if !defined A_IMAX_C
657 #define A_IMAX_C(X) A_I64_C(X)
658 #endif /* A_IMAX_C */
659 #if !defined A_IMAX_MAX
660 #define A_IMAX_MAX A_I64_MAX
661 #endif /* A_IMAX_MAX */
662 #if !defined A_IMAX_MIN
663 #define A_IMAX_MIN A_I64_MIN
664 #endif /* A_IMAX_MIN */
665 /*! @brief static cast to \ref a_imax */
666 #define a_imax_c(x) a_cast_s(a_imax, x)
667 #define a_imax_(_, x) a_cast_s(a_imax _, x)
668 /*! @brief maximum-width signed integer type */
669 typedef A_IMAX a_imax;
671 #if !defined A_UMAX
672 #define A_UMAX A_U64
673 #endif /* A_UMAX */
674 #if !defined A_UMAX_C
675 #define A_UMAX_C(X) A_U64_C(X)
676 #endif /* A_UMAX_C */
677 #if !defined A_UMAX_MAX
678 #define A_UMAX_MAX A_U64_MAX
679 #endif /* A_UMAX_MAX */
680 /*! @brief static cast to \ref a_umax */
681 #define a_umax_c(x) a_cast_s(a_umax, x)
682 #define a_umax_(_, x) a_cast_s(a_umax _, x)
683 /*! @brief maximum-width unsigned integer type */
684 typedef A_UMAX a_umax;
686 #if !defined A_PRIMAX
687 #define A_PRIMAX A_PRI64
688 #endif /* A_PRIMAX */
689 #if !defined A_SCNMAX
690 #define A_SCNMAX A_SCN64
691 #endif /* A_SCNMAX */
693 #if !defined A_IPTR && (A_SIZE_POINTER == 2)
694 #define A_IPTR A_I16
695 #elif !defined A_IPTR && (A_SIZE_POINTER == 4)
696 #define A_IPTR A_I32
697 #elif !defined A_IPTR
698 #define A_IPTR A_I64
699 #endif /* A_IPTR */
700 #if !defined A_IPTR_MAX && (A_SIZE_POINTER == 2)
701 #define A_IPTR_MAX A_I16_MAX
702 #elif !defined A_IPTR_MAX && (A_SIZE_POINTER == 4)
703 #define A_IPTR_MAX A_I32_MAX
704 #elif !defined A_IPTR_MAX
705 #define A_IPTR_MAX A_I64_MAX
706 #endif /* A_IPTR_MAX */
707 #if !defined A_IPTR_MIN && (A_SIZE_POINTER == 2)
708 #define A_IPTR_MIN A_I16_MIN
709 #elif !defined A_IPTR_MIN && (A_SIZE_POINTER == 4)
710 #define A_IPTR_MIN A_I32_MIN
711 #elif !defined A_IPTR_MIN
712 #define A_IPTR_MIN A_I64_MIN
713 #endif /* A_IPTR_MIN */
714 /*! @brief static cast to \ref a_iptr */
715 #define a_iptr_c(x) a_cast_s(a_iptr, x)
716 #define a_iptr_(_, x) a_cast_s(a_iptr _, x)
717 /*! @brief signed integer type capable of holding a pointer to void */
718 typedef A_IPTR a_iptr;
720 #if !defined A_UPTR && (A_SIZE_POINTER == 2)
721 #define A_UPTR A_U16
722 #elif !defined A_UPTR && (A_SIZE_POINTER == 4)
723 #define A_UPTR A_U32
724 #elif !defined A_UPTR
725 #define A_UPTR A_U64
726 #endif /* A_UPTR */
727 #if !defined A_UPTR_MAX && (A_SIZE_POINTER == 2)
728 #define A_UPTR_MAX A_U16_MAX
729 #elif !defined A_UPTR_MAX && (A_SIZE_POINTER == 4)
730 #define A_UPTR_MAX A_U32_MAX
731 #elif !defined A_UPTR_MAX
732 #define A_UPTR_MAX A_U64_MAX
733 #endif /* A_UPTR_MAX */
734 /*! @brief static cast to \ref a_uptr */
735 #define a_uptr_c(x) a_cast_s(a_uptr, x)
736 #define a_uptr_(_, x) a_cast_s(a_uptr _, x)
737 /*! @brief unsigned integer type capable of holding a pointer to void */
738 typedef A_UPTR a_uptr;
740 #if !defined A_PRIPTR && (A_SIZE_POINTER == 2)
741 #define A_PRIPTR A_PRI16
742 #elif !defined A_PRIPTR && (A_SIZE_POINTER == 4)
743 #define A_PRIPTR A_PRI32
744 #elif !defined A_PRIPTR
745 #define A_PRIPTR A_PRI64
746 #endif /* A_PRIPTR */
747 #if !defined A_SCNPTR && (A_SIZE_POINTER == 2)
748 #define A_SCNPTR A_SCN16
749 #elif !defined A_SCNPTR && (A_SIZE_POINTER == 4)
750 #define A_SCNPTR A_SCN32
751 #elif !defined A_SCNPTR
752 #define A_SCNPTR A_SCN64
753 #endif /* A_SCNPTR */
755 #if !defined A_DIFF
756 #define A_DIFF ptrdiff_t
757 #endif /* A_DIFF */
758 #if !defined A_DIFF_MAX && defined(__PTRDIFF_MAX__)
759 #define A_DIFF_MAX __PTRDIFF_MAX__
760 #elif !defined A_DIFF_MAX && (A_SIZE_POINTER == 2)
761 #define A_DIFF_MAX A_I16_MAX
762 #elif !defined A_DIFF_MAX && (A_SIZE_POINTER == 4)
763 #define A_DIFF_MAX A_I32_MAX
764 #elif !defined A_DIFF_MAX
765 #define A_DIFF_MAX A_I64_MAX
766 #endif /* A_DIFF_MAX */
767 #if !defined A_DIFF_MIN && defined(__PTRDIFF_MAX__)
768 #define A_DIFF_MIN (~__PTRDIFF_MAX__)
769 #elif !defined A_DIFF_MIN && (A_SIZE_POINTER == 2)
770 #define A_DIFF_MIN A_I16_MIN
771 #elif !defined A_DIFF_MIN && (A_SIZE_POINTER == 4)
772 #define A_DIFF_MIN A_I32_MIN
773 #elif !defined A_DIFF_MIN
774 #define A_DIFF_MIN A_I64_MIN
775 #endif /* A_DIFF_MIN */
776 /*! @brief static cast to \ref a_diff */
777 #define a_diff_c(x) a_cast_s(a_diff, x)
778 #define a_diff_(_, x) a_cast_s(a_diff _, x)
779 /*! @brief signed integer type returned when subtracting two pointers */
780 typedef A_DIFF a_diff;
782 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L) || \
783 defined(__cplusplus) && (__cplusplus > 201100L) || A_PREREQ_MSVC(19, 0)
784 #if !defined A_PRIt
785 #define A_PRIt "t"
786 #endif /* A_PRIt */
787 #if !defined A_SCNt
788 #define A_SCNt "t"
789 #endif /* A_SCNt */
790 #else /* C < 199900 and C++ < 201100 */
791 #if !defined A_PRIt && (A_SIZE_POINTER == 2)
792 #define A_PRIt A_PRI16
793 #elif !defined A_PRIt && (A_SIZE_POINTER == 4)
794 #define A_PRIt A_PRI32
795 #elif !defined A_PRIt
796 #define A_PRIt A_PRI64
797 #endif /* A_PRIt */
798 #if !defined A_SCNt && (A_SIZE_POINTER == 2)
799 #define A_SCNt A_SCN16
800 #elif !defined A_SCNt && (A_SIZE_POINTER == 4)
801 #define A_SCNt A_SCN32
802 #elif !defined A_SCNt
803 #define A_SCNt A_SCN64
804 #endif /* A_SCNt */
805 #endif /* C > 199900 or C++ > 201100 */
807 #if !defined A_SIZE
808 #define A_SIZE size_t
809 #endif /* A_SIZE */
810 #if !defined A_SIZE_MAX && defined(__SIZE_MAX__)
811 #define A_SIZE_MAX __SIZE_MAX__
812 #elif !defined A_SIZE_MAX && (A_SIZE_POINTER == 2)
813 #define A_SIZE_MAX A_U16_MAX
814 #elif !defined A_SIZE_MAX && (A_SIZE_POINTER == 4)
815 #define A_SIZE_MAX A_U32_MAX
816 #elif !defined A_SIZE_MAX
817 #define A_SIZE_MAX A_U64_MAX
818 #endif /* A_SIZE_MAX */
819 /*! @brief static cast to \ref a_size */
820 #define a_size_c(x) a_cast_s(a_size, x)
821 #define a_size_(_, x) a_cast_s(a_size _, x)
822 /*! @brief unsigned integer type returned by the sizeof operator */
823 typedef A_SIZE a_size;
825 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L) || \
826 defined(__cplusplus) && (__cplusplus > 201100L) || A_PREREQ_MSVC(19, 0)
827 #if !defined A_PRIz
828 #define A_PRIz "z"
829 #endif /* A_PRIz */
830 #if !defined A_SCNz
831 #define A_SCNz "z"
832 #endif /* A_SCNz */
833 #else /* C < 199900 and C++ < 201100 */
834 #if !defined A_PRIz && (A_SIZE_POINTER == 2)
835 #define A_PRIz A_PRI16
836 #elif !defined A_PRIz && (A_SIZE_POINTER == 4)
837 #define A_PRIz A_PRI32
838 #elif !defined A_PRIz
839 #define A_PRIz A_PRI64
840 #endif /* A_PRIz */
841 #if !defined A_SCNz && (A_SIZE_POINTER == 2)
842 #define A_SCNz A_SCN16
843 #elif !defined A_SCNz && (A_SIZE_POINTER == 4)
844 #define A_SCNz A_SCN32
845 #elif !defined A_SCNz
846 #define A_SCNz A_SCN64
847 #endif /* A_SCNz */
848 #endif /* C > 199900 or C++ > 201100 */
850 #define A_F16_NNAN A_U16_C(0xFE00)
851 #define A_F16_PNAN A_U16_C(0x7E00)
852 #define A_F16_NINF A_U16_C(0xFC00)
853 #define A_F16_PINF A_U16_C(0x7C00)
855 #define A_F32 float
856 #define A_F32_C(X) A_CAST_2(X, F)
857 #define A_F32_F(F) A_CAST_2(F, f)
858 #define A_F32_DIG FLT_DIG
859 #define A_F32_EPSILON FLT_EPSILON
860 #define A_F32_MANT_DIG FLT_MANT_DIG
861 #define A_F32_MAX FLT_MAX
862 #define A_F32_MAX_10_EXP FLT_MAX_10_EXP
863 #define A_F32_MAX_EXP FLT_MAX_EXP
864 #define A_F32_MIN FLT_MIN
865 #define A_F32_MIN_10_EXP FLT_MIN_10_EXP
866 #define A_F32_MIN_EXP FLT_MIN_EXP
867 #define A_F32_INF a_cast_s(a_f32, A_F64_INF)
868 #define A_F32_NAN (A_F32_C(0.0) * A_F32_INF)
869 #define A_F32_NNAN A_U32_C(0xFFC00000)
870 #define A_F32_PNAN A_U32_C(0x7FC00000)
871 #define A_F32_NINF A_U32_C(0xFF800000)
872 #define A_F32_PINF A_U32_C(0x7F800000)
873 /*! @brief static cast to \ref a_f32 */
874 #define a_f32_c(x) a_cast_s(a_f32, x)
875 #define a_f32_(_, x) a_cast_s(a_f32 _, x)
876 /*! @brief single precision floating point type. Matches IEEE-754 binary32 format if supported. */
877 typedef A_F32 a_f32;
879 #define A_F64 double
880 #define A_F64_C(X) X
881 #define A_F64_F(F) F
882 #define A_F64_DIG DBL_DIG
883 #define A_F64_EPSILON DBL_EPSILON
884 #define A_F64_MANT_DIG DBL_MANT_DIG
885 #define A_F64_MAX DBL_MAX
886 #define A_F64_MAX_10_EXP DBL_MAX_10_EXP
887 #define A_F64_MAX_EXP DBL_MAX_EXP
888 #define A_F64_MIN DBL_MIN
889 #define A_F64_MIN_10_EXP DBL_MIN_10_EXP
890 #define A_F64_MIN_EXP DBL_MIN_EXP
891 #define A_F64_INF (DBL_MAX * DBL_MAX)
892 #define A_F64_NAN (A_F64_C(0.0) * A_F64_INF)
893 #define A_F64_NNAN A_U64_C(0xFFF8000000000000)
894 #define A_F64_PNAN A_U64_C(0x7FF8000000000000)
895 #define A_F64_NINF A_U64_C(0xFFF0000000000000)
896 #define A_F64_PINF A_U64_C(0x7FF0000000000000)
897 /*! @brief static cast to \ref a_f64 */
898 #define a_f64_c(x) a_cast_s(a_f64, x)
899 #define a_f64_(_, x) a_cast_s(a_f64 _, x)
900 /*! @brief double precision floating point type. Matches IEEE-754 binary64 format if supported. */
901 typedef A_F64 a_f64;
904 @addtogroup a_float floating-point number
909 @def A_FLOAT_TYPE
910 @brief floating-point number bytes
912 #if !defined A_FLOAT_TYPE
913 #if !defined A_SIZE_FLOAT
914 #define A_FLOAT_TYPE A_FLOAT_DOUBLE
915 #else /* !A_SIZE_FLOAT */
916 #define A_FLOAT_TYPE A_SIZE_FLOAT
917 #endif /* A_SIZE_FLOAT */
918 #endif /* A_FLOAT_TYPE */
919 #define A_FLOAT_SINGLE 0x04
920 #define A_FLOAT_DOUBLE 0x08
921 #define A_FLOAT_EXTEND 0x10
922 #if defined(A_FLOAT)
923 #elif A_FLOAT_TYPE + 0 == A_FLOAT_SINGLE
925 /*! @brief floating-point number stored using `float` */
926 #define A_FLOAT float
927 #define A_FLOAT_DIG FLT_DIG
928 #define A_FLOAT_EPSILON FLT_EPSILON
929 #define A_FLOAT_MANT_DIG FLT_MANT_DIG
930 #define A_FLOAT_MAX FLT_MAX
931 #define A_FLOAT_MAX_10_EXP FLT_MAX_10_EXP
932 #define A_FLOAT_MAX_EXP FLT_MAX_EXP
933 #define A_FLOAT_MIN FLT_MIN
934 #define A_FLOAT_MIN_10_EXP FLT_MIN_10_EXP
935 #define A_FLOAT_MIN_EXP FLT_MIN_EXP
936 #define A_FLOAT_C(X) A_CAST_2(X, F)
937 #define A_FLOAT_F(F) A_CAST_2(F, f)
938 #define A_FLOAT_PRI
939 #define A_FLOAT_SCN
941 #elif A_FLOAT_TYPE + 0 == A_FLOAT_DOUBLE
943 /*! @brief floating-point number stored using `double` */
944 #define A_FLOAT double
945 #define A_FLOAT_DIG DBL_DIG
946 #define A_FLOAT_EPSILON DBL_EPSILON
947 #define A_FLOAT_MANT_DIG DBL_MANT_DIG
948 #define A_FLOAT_MAX DBL_MAX
949 #define A_FLOAT_MAX_10_EXP DBL_MAX_10_EXP
950 #define A_FLOAT_MAX_EXP DBL_MAX_EXP
951 #define A_FLOAT_MIN DBL_MIN
952 #define A_FLOAT_MIN_10_EXP DBL_MIN_10_EXP
953 #define A_FLOAT_MIN_EXP DBL_MIN_EXP
954 #define A_FLOAT_C(X) X
955 #define A_FLOAT_F(F) F
956 #define A_FLOAT_PRI
957 #define A_FLOAT_SCN "l"
959 #elif A_FLOAT_TYPE + 0 == A_FLOAT_EXTEND
961 /*! @brief floating-point number stored using `long double` */
962 #define A_FLOAT long double
963 #define A_FLOAT_DIG LDBL_DIG
964 #define A_FLOAT_EPSILON LDBL_EPSILON
965 #define A_FLOAT_MANT_DIG LDBL_MANT_DIG
966 #define A_FLOAT_MAX LDBL_MAX
967 #define A_FLOAT_MAX_10_EXP LDBL_MAX_10_EXP
968 #define A_FLOAT_MAX_EXP LDBL_MAX_EXP
969 #define A_FLOAT_MIN LDBL_MIN
970 #define A_FLOAT_MIN_10_EXP LDBL_MIN_10_EXP
971 #define A_FLOAT_MIN_EXP LDBL_MIN_EXP
972 #define A_FLOAT_C(X) A_CAST_2(X, L)
973 #define A_FLOAT_F(F) A_CAST_2(F, l)
974 #define A_FLOAT_PRI "L"
975 #define A_FLOAT_SCN "L"
977 #else /* !A_FLOAT_TYPE */
978 #error unsupported precision
979 #endif /* A_FLOAT_TYPE */
981 #define A_FLOAT_INF a_cast_s(A_FLOAT, A_F64_INF)
982 #define A_FLOAT_NAN (A_FLOAT_C(0.0) * A_FLOAT_INF)
984 @def A_FLOAT_C(X)
985 @brief expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
988 @def A_FLOAT_F(F)
989 @brief expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
992 @def A_FLOAT_SCN
993 @brief format constants for the fscanf family of functions
996 @def A_FLOAT_PRI
997 @brief format constants for the fprintf family of functions
999 /*! @brief static cast to \ref a_float */
1000 #define a_float_c(x) a_cast_s(a_float, x)
1001 #define a_float_(_, x) a_cast_s(a_float _, x)
1002 /*! @brief compiler built-in floating-point number type */
1003 typedef A_FLOAT a_float;
1005 /*! @} a_float */
1007 typedef union a_cast
1009 a_c8 c;
1010 a_int i;
1011 a_uint u;
1012 a_shrt ih;
1013 a_ushrt uh;
1014 a_long il;
1015 a_ulong ul;
1016 #if defined(A_HAVE_LONG_LONG_TYPE)
1017 a_llong ill;
1018 a_ullong ull;
1019 #endif /* A_HAVE_LONG_LONG_TYPE */
1020 a_i8 i8;
1021 a_u8 u8;
1022 a_i16 i16;
1023 a_u16 u16;
1024 a_i32 i32;
1025 a_u32 u32;
1026 a_i64 i64;
1027 a_u64 u64;
1028 a_f32 f32;
1029 a_f64 f64;
1030 a_imax imax;
1031 a_umax umax;
1032 a_iptr iptr;
1033 a_uptr uptr;
1034 a_diff diff;
1035 a_size size;
1036 void const *PTR;
1037 void *ptr;
1038 char const *STR;
1039 char *str;
1040 #if defined(A_FLOAT_TYPE) && (A_FLOAT_TYPE + 0 < A_FLOAT_EXTEND)
1041 a_float f;
1042 #endif /* A_FLOAT_TYPE */
1043 } a_cast;
1046 @brief square of x, \f$ x^2 \f$
1048 #define A_SQ(x) ((x) * (x))
1051 @brief absolute value of x, \f$ |x| \f$
1053 #define A_ABS(x) ((x) < 0 ? -(x) : (x))
1055 @brief absolute value of f-g, \f$ |f-g| \f$
1057 #define A_ABS_(f, g) ((f) < (g) ? (g) - (f) : (f) - (g))
1060 @brief minimum value between x and y
1062 #define A_MIN(x, y) (((x) < (y)) ? (x) : (y))
1065 @brief maximum value between x and y
1067 #define A_MAX(x, y) (((x) > (y)) ? (x) : (y))
1070 @brief signum function, \f$ \texttt{sgn}(x)=\begin{cases}+1&x>0\\0&0\\-1&x<0\end{cases} \f$
1072 #define A_SGN(x) ((0 < (x)) - ((x) < 0))
1074 @brief signum function, \f$ \texttt{sgn}(f,g)=\begin{cases}+1&f>g\\0&f=g\\-1&f<g\end{cases} \f$
1076 #define A_SGN_(f, g) (((f) > (g)) - ((f) < (g)))
1079 @brief saturation value of x, \f$ \texttt{sat}(x,min,max)=\begin{cases}min&min>x\\max&x>max\\x&else\end{cases} \f$
1081 #define A_SAT(x, min, max) ((min) < (x) ? (x) < (max) ? (x) : (max) : (min))
1084 @brief number of elements in a visible array
1085 @param a must be a visible array
1087 #define A_LEN(a) (sizeof(a) / sizeof(*(a)))
1090 @brief offset of a structure member
1091 @param type structure type
1092 @param member member variable
1094 #if defined(offsetof)
1095 #define a_offsetof(type, member) offsetof(type, member)
1096 #else /* !offsetof */
1097 #define a_offsetof(type, member) a_cast_r(a_size, &a_cast_r(type *, 0)->member)
1098 #endif /* offsetof */
1101 @brief container of a structure member
1102 @param ptr pointer to a member variable
1103 @param type structure type
1104 @param member member variable
1106 #define a_container_of(ptr, type, member) a_cast_r(type *, a_cast_r(a_uptr, ptr) - a_offsetof(type, member))
1109 @brief round down size `n` to be a multiple of `a`
1111 #define a_size_down(a, n) (a_cast_s(a_size, n) & ~a_cast_s(a_size, (a) - 1))
1114 @brief round up size `n` to be a multiple of `a`
1116 #define a_size_up(a, n) ((a_cast_s(a_size, n) + (a) - 1) & ~a_cast_s(a_size, (a) - 1))
1119 @brief round pointer `p` down to the closest `a`, aligned address <= `p`
1121 #define a_align_down(a, p) a_cast_r(void *, a_cast_r(a_uptr, p) & ~a_cast_s(a_uptr, (a) - 1))
1124 @brief round pointer `p` up to the closest `a`, aligned address >= `p`
1126 #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))
1129 @brief iterate from 0 to n and not include n
1130 @param I index type of the iteration
1131 @param i index variable of the iteration
1132 @param n final value of the iteration
1134 #define a_forenum(I, i, n) for (I i = 0; i < (n); ++i)
1135 #define A_FORENUM(I, i, n) for (i = 0; i < a_cast_s(I, n); ++i)
1138 @brief iterate from n to 0 and not include n
1139 @param I index type of the iteration
1140 @param i index variable of the iteration
1141 @param n final value of the iteration
1143 #define a_forenum_reverse(I, i, n) for (I i = (n); i-- > 0;)
1144 #define A_FORENUM_REVERSE(I, i, n) for (i = a_cast_s(I, n); i-- > 0;)
1147 @brief iterate over an array
1148 @param T the prefix of the element type
1149 @param S the suffix of the element type
1150 @param it pointer to the current element
1151 @param ptr starting address of this array
1152 @param num number of elements in this array
1154 #define a_foreach(T, S, it, ptr, num) \
1155 for (T S it = a_cast_s(T S, ptr), S it##_ = it + (num); it < it##_; ++it)
1156 #define A_FOREACH(T, it, at, ptr, num) \
1157 for (it = a_cast_s(T, ptr), at = it + (num); it < at; ++it)
1158 /*! @copydoc a_foreach */
1159 #define a_forsafe(T, S, it, ptr, num) \
1160 for (T S it = a_cast_s(T S, ptr), S it##_ = (num) ? it + (num) : it; it < it##_; ++it)
1161 #define A_FORSAFE(T, it, at, ptr, num) \
1162 for (it = a_cast_s(T, ptr), at = (num) ? it + (num) : it; it < at; ++it)
1165 @brief iterate over an array in reverse
1166 @param T the prefix of the element type
1167 @param S the suffix of the element type
1168 @param it pointer to the current element
1169 @param ptr starting address of this array
1170 @param num number of elements in this array
1172 #define a_foreach_reverse(T, S, it, ptr, num) \
1173 for (T S it##_ = a_cast_s(T S, ptr) - 1, S it = it##_ + (num); it > it##_; --it)
1174 #define A_FOREACH_REVERSE(T, it, at, ptr, num) \
1175 for (at = a_cast_s(T, ptr) - 1, it = at + (num); it > at; --it)
1176 /*! @copydoc a_foreach_reverse */
1177 #define a_forsafe_reverse(T, S, it, ptr, num) \
1178 for (T S it##_ = (num) ? a_cast_s(T S, ptr) - 1 : a_cast_s(T S, ptr), \
1179 S it = (num) ? it##_ + (num) : it##_; \
1180 it > it##_; --it)
1181 #define A_FORSAFE_REVERSE(T, it, at, ptr, num) \
1182 for (at = (num) ? a_cast_s(T, ptr) - 1 : a_cast_s(T, ptr), \
1183 it = (num) ? at + (num) : at; \
1184 it > at; --it)
1187 @brief iterate over an array
1188 @param T the prefix of the element type
1189 @param S the suffix of the element type
1190 @param it pointer to the current element
1191 @param ptr starting address of this array
1192 @param end the end address of this array
1194 #define a_iterate(T, S, it, ptr, end) \
1195 for (T S it = a_cast_s(T S, ptr), S it##_ = a_cast_s(T S, end); it < it##_; ++it)
1196 #define A_ITERATE(T, it, at, ptr, end) \
1197 for (it = a_cast_s(T, ptr), at = a_cast_s(T, end); it < at; ++it)
1200 @brief iterate over an array in reverse
1201 @param T the prefix of the element type
1202 @param S the suffix of the element type
1203 @param it pointer to the current element
1204 @param ptr starting address of this array
1205 @param end the end address of this array
1207 #define a_iterate_reverse(T, S, it, ptr, end) \
1208 for (T S it = a_cast_s(T S, end) - 1, S it##_ = a_cast_s(T S, ptr) - 1; it > it##_; --it)
1209 #define A_ITERATE_REVERSE(T, it, at, ptr, end) \
1210 for (it = a_cast_s(T, end) - 1, at = a_cast_s(T, ptr) - 1; it > at; --it)
1213 @brief enumeration for return values
1215 enum
1217 A_SUCCESS /*!< return success */,
1218 A_FAILURE /*!< return failure */,
1219 A_INVALID /*!< return invalid */
1222 #if defined(__cplusplus)
1223 extern "C" {
1224 #endif /* __cplusplus */
1225 #if defined(LIBA_A_C)
1226 #undef A_INTERN
1227 #define A_INTERN A_INLINE
1228 #endif /* LIBA_A_C */
1231 @brief reverse the bits in an 8-bit unsigned integer
1232 @param x an 8-bit unsigned integer to be reversed
1233 @return reversed 8-bit unsigned integer
1235 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1236 A_EXTERN a_u8 a_u8_rev(a_u8 x);
1237 #endif /* A_HAVE_INLINE */
1238 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1239 A_INTERN a_u8 a_u8_rev(a_u8 x)
1241 x = a_cast_s(a_u8, (x >> 4) | (x << 4));
1242 x = a_cast_s(a_u8, ((x & 0xCC) >> 2) | ((x & 0x33) << 2));
1243 x = a_cast_s(a_u8, ((x & 0xAA) >> 1) | ((x & 0x55) << 1));
1244 return x;
1246 #endif /* A_HAVE_INLINE */
1249 @brief reverse the bits in a 16-bit unsigned integer
1250 @param x a 16-bit unsigned integer to be reversed
1251 @return reversed 16-bit unsigned integer
1253 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1254 A_EXTERN a_u16 a_u16_rev(a_u16 x);
1255 #endif /* A_HAVE_INLINE */
1256 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1257 A_INTERN a_u16 a_u16_rev(a_u16 x)
1259 x = a_cast_s(a_u16, (x >> 8) | (x << 8));
1260 x = a_cast_s(a_u16, ((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4));
1261 x = a_cast_s(a_u16, ((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2));
1262 x = a_cast_s(a_u16, ((x & 0xAAAA) >> 1) | ((x & 0x5555) << 1));
1263 return x;
1265 #endif /* A_HAVE_INLINE */
1266 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1267 A_EXTERN A_NONULL((1)) a_u16 a_u16_getl(void const *b);
1268 #endif /* A_HAVE_INLINE */
1269 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1270 A_INTERN A_NONULL((1)) a_u16 a_u16_getl(void const *b)
1272 a_byte const *p = a_cast_s(a_byte const *, b);
1273 return a_cast_s(a_u16, (p[0] << 0) | (p[1] << 8));
1275 #endif /* A_HAVE_INLINE */
1276 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1277 A_EXTERN A_NONULL((1)) a_u16 a_u16_getb(void const *b);
1278 #endif /* A_HAVE_INLINE */
1279 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1280 A_INTERN A_NONULL((1)) a_u16 a_u16_getb(void const *b)
1282 a_byte const *p = a_cast_s(a_byte const *, b);
1283 return a_cast_s(a_u16, (p[1] << 0) | (p[0] << 8));
1285 #endif /* A_HAVE_INLINE */
1286 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1287 A_EXTERN A_NONULL((1)) void a_u16_setl(void *b, a_u16 x);
1288 #endif /* A_HAVE_INLINE */
1289 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1290 A_INTERN A_NONULL((1)) void a_u16_setl(void *b, a_u16 x)
1292 a_byte *p = a_cast_s(a_byte *, b);
1293 p[0] = a_cast_s(a_byte, x >> 0);
1294 p[1] = a_cast_s(a_byte, x >> 8);
1296 #endif /* A_HAVE_INLINE */
1297 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1298 A_EXTERN A_NONULL((1)) void a_u16_setb(void *b, a_u16 x);
1299 #endif /* A_HAVE_INLINE */
1300 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1301 A_INTERN A_NONULL((1)) void a_u16_setb(void *b, a_u16 x)
1303 a_byte *p = a_cast_s(a_byte *, b);
1304 p[0] = a_cast_s(a_byte, x >> 8);
1305 p[1] = a_cast_s(a_byte, x >> 0);
1307 #endif /* A_HAVE_INLINE */
1310 @brief reverse the bits in a 32-bit unsigned integer
1311 @param x a 32-bit unsigned integer to be reversed
1312 @return reversed 32-bit unsigned integer
1314 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1315 A_EXTERN a_u32 a_u32_rev(a_u32 x);
1316 #endif /* A_HAVE_INLINE */
1317 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1318 A_INTERN a_u32 a_u32_rev(a_u32 x)
1320 x = (x >> 16) | (x << 16);
1321 x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
1322 x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
1323 x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
1324 x = ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
1325 return x;
1327 #endif /* A_HAVE_INLINE */
1328 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1329 A_EXTERN A_NONULL((1)) a_u32 a_u32_getl(void const *b);
1330 #endif /* A_HAVE_INLINE */
1331 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1332 A_INTERN A_NONULL((1)) a_u32 a_u32_getl(void const *b)
1334 a_byte const *p = a_cast_s(a_byte const *, b);
1335 return (a_cast_s(a_u32, p[0]) << 0x00) |
1336 (a_cast_s(a_u32, p[1]) << 0x08) |
1337 (a_cast_s(a_u32, p[2]) << 0x10) |
1338 (a_cast_s(a_u32, p[3]) << 0x18);
1340 #endif /* A_HAVE_INLINE */
1341 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1342 A_EXTERN A_NONULL((1)) a_u32 a_u32_getb(void const *b);
1343 #endif /* A_HAVE_INLINE */
1344 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1345 A_INTERN A_NONULL((1)) a_u32 a_u32_getb(void const *b)
1347 a_byte const *p = a_cast_s(a_byte const *, b);
1348 return (a_cast_s(a_u32, p[0]) << 0x18) |
1349 (a_cast_s(a_u32, p[1]) << 0x10) |
1350 (a_cast_s(a_u32, p[2]) << 0x08) |
1351 (a_cast_s(a_u32, p[3]) << 0x00);
1353 #endif /* A_HAVE_INLINE */
1354 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1355 A_EXTERN A_NONULL((1)) void a_u32_setl(void *b, a_u32 x);
1356 #endif /* A_HAVE_INLINE */
1357 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1358 A_INTERN A_NONULL((1)) void a_u32_setl(void *b, a_u32 x)
1360 a_byte *p = a_cast_s(a_byte *, b);
1361 p[0] = a_cast_s(a_byte, x >> 0x00);
1362 p[1] = a_cast_s(a_byte, x >> 0x08);
1363 p[2] = a_cast_s(a_byte, x >> 0x10);
1364 p[3] = a_cast_s(a_byte, x >> 0x18);
1366 #endif /* A_HAVE_INLINE */
1367 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1368 A_EXTERN A_NONULL((1)) void a_u32_setb(void *b, a_u32 x);
1369 #endif /* A_HAVE_INLINE */
1370 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1371 A_INTERN A_NONULL((1)) void a_u32_setb(void *b, a_u32 x)
1373 a_byte *p = a_cast_s(a_byte *, b);
1374 p[0] = a_cast_s(a_byte, x >> 0x18);
1375 p[1] = a_cast_s(a_byte, x >> 0x10);
1376 p[2] = a_cast_s(a_byte, x >> 0x08);
1377 p[3] = a_cast_s(a_byte, x >> 0x00);
1379 #endif /* A_HAVE_INLINE */
1382 @brief reverse the bits in a 64-bit unsigned integer
1383 @param x a 64-bit unsigned integer to be reversed
1384 @return reversed 64-bit unsigned integer
1386 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1387 A_EXTERN a_u64 a_u64_rev(a_u64 x);
1388 #endif /* A_HAVE_INLINE */
1389 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1390 A_INTERN a_u64 a_u64_rev(a_u64 x)
1392 x = (x >> 32) | (x << 32);
1393 x = ((x & 0xFFFF0000FFFF0000) >> 0x10) | ((x & 0x0000FFFF0000FFFF) << 0x10);
1394 x = ((x & 0xFF00FF00FF00FF00) >> 0x08) | ((x & 0x00FF00FF00FF00FF) << 0x08);
1395 x = ((x & 0xF0F0F0F0F0F0F0F0) >> 0x04) | ((x & 0x0F0F0F0F0F0F0F0F) << 0x04);
1396 x = ((x & 0xCCCCCCCCCCCCCCCC) >> 0x02) | ((x & 0x3333333333333333) << 0x02);
1397 x = ((x & 0xAAAAAAAAAAAAAAAA) >> 0x01) | ((x & 0x5555555555555555) << 0x01);
1398 return x;
1400 #endif /* A_HAVE_INLINE */
1401 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1402 A_EXTERN A_NONULL((1)) a_u64 a_u64_getl(void const *b);
1403 #endif /* A_HAVE_INLINE */
1404 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1405 A_INTERN A_NONULL((1)) a_u64 a_u64_getl(void const *b)
1407 a_byte const *p = a_cast_s(a_byte const *, b);
1408 return (a_cast_s(a_u64, p[0]) << 0x00) |
1409 (a_cast_s(a_u64, p[1]) << 0x08) |
1410 (a_cast_s(a_u64, p[2]) << 0x10) |
1411 (a_cast_s(a_u64, p[3]) << 0x18) |
1412 (a_cast_s(a_u64, p[4]) << 0x20) |
1413 (a_cast_s(a_u64, p[5]) << 0x28) |
1414 (a_cast_s(a_u64, p[6]) << 0x30) |
1415 (a_cast_s(a_u64, p[7]) << 0x38);
1417 #endif /* A_HAVE_INLINE */
1418 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1419 A_EXTERN A_NONULL((1)) a_u64 a_u64_getb(void const *b);
1420 #endif /* A_HAVE_INLINE */
1421 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1422 A_INTERN A_NONULL((1)) a_u64 a_u64_getb(void const *b)
1424 a_byte const *p = a_cast_s(a_byte const *, b);
1425 return (a_cast_s(a_u64, p[0]) << 0x38) |
1426 (a_cast_s(a_u64, p[1]) << 0x30) |
1427 (a_cast_s(a_u64, p[2]) << 0x28) |
1428 (a_cast_s(a_u64, p[3]) << 0x20) |
1429 (a_cast_s(a_u64, p[4]) << 0x18) |
1430 (a_cast_s(a_u64, p[5]) << 0x10) |
1431 (a_cast_s(a_u64, p[6]) << 0x08) |
1432 (a_cast_s(a_u64, p[7]) << 0x00);
1434 #endif /* A_HAVE_INLINE */
1435 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1436 A_EXTERN A_NONULL((1)) void a_u64_setl(void *b, a_u64 x);
1437 #endif /* A_HAVE_INLINE */
1438 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1439 A_INTERN A_NONULL((1)) void a_u64_setl(void *b, a_u64 x)
1441 a_byte *p = a_cast_s(a_byte *, b);
1442 p[0] = a_cast_s(a_byte, x >> 0x00);
1443 p[1] = a_cast_s(a_byte, x >> 0x08);
1444 p[2] = a_cast_s(a_byte, x >> 0x10);
1445 p[3] = a_cast_s(a_byte, x >> 0x18);
1446 p[4] = a_cast_s(a_byte, x >> 0x20);
1447 p[5] = a_cast_s(a_byte, x >> 0x28);
1448 p[6] = a_cast_s(a_byte, x >> 0x30);
1449 p[7] = a_cast_s(a_byte, x >> 0x38);
1451 #endif /* A_HAVE_INLINE */
1452 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1453 A_EXTERN A_NONULL((1)) void a_u64_setb(void *b, a_u64 x);
1454 #endif /* A_HAVE_INLINE */
1455 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1456 A_INTERN A_NONULL((1)) void a_u64_setb(void *b, a_u64 x)
1458 a_byte *p = a_cast_s(a_byte *, b);
1459 p[0] = a_cast_s(a_byte, x >> 0x38);
1460 p[1] = a_cast_s(a_byte, x >> 0x30);
1461 p[2] = a_cast_s(a_byte, x >> 0x28);
1462 p[3] = a_cast_s(a_byte, x >> 0x20);
1463 p[4] = a_cast_s(a_byte, x >> 0x18);
1464 p[5] = a_cast_s(a_byte, x >> 0x10);
1465 p[6] = a_cast_s(a_byte, x >> 0x08);
1466 p[7] = a_cast_s(a_byte, x >> 0x00);
1468 #endif /* A_HAVE_INLINE */
1471 @brief copy one buffer to another
1472 @param[in] dst pointer to the object to copy to
1473 @param[in] src pointer to the object to copy from
1474 @param[in] siz number of bytes to copy
1475 @return a copy of dest
1477 A_EXTERN A_NONULL((1, 2)) void *a_copy(void *__restrict dst, void const *__restrict src, a_size siz);
1480 @brief move one buffer to another
1481 @param[in] dst pointer to the object to copy to
1482 @param[in] src pointer to the object to copy from
1483 @param[in] siz number of bytes to copy
1484 @return a copy of dest
1486 A_EXTERN A_NONULL((1, 2)) void *a_move(void *dst, void const *src, a_size siz);
1489 @brief fill a buffer with a character
1490 @param[in] ptr pointer to the object to fill
1491 @param[in] siz number of bytes to fill
1492 @param[in] val fill byte
1493 @return a copy of dest
1495 A_EXTERN A_NONULL((1)) void *a_fill(void *ptr, a_size siz, int val);
1498 @brief fill a buffer with zero
1499 @param[in] ptr pointer to the object to fill
1500 @param[in] siz number of bytes to fill
1501 @return a copy of dest
1503 A_EXTERN A_NONULL((1)) void *a_zero(void *ptr, a_size siz);
1506 @brief swap two different memory blocks of the same size
1507 @param[in,out] lhs points to memory block on the left
1508 @param[in,out] rhs points to memory block on the right
1509 @param[in] siz the size of memory block being swapped
1511 A_EXTERN A_NONULL((1, 2)) void a_swap(void *lhs, void *rhs, a_size siz);
1514 @brief allocation function pointer
1515 @param[in] addr address of memory block
1516 @param[in] size new size of memory block
1517 @return new address of memory block or null
1519 A_EXTERN void *(*a_alloc)(void *addr, a_size size);
1522 @brief default allocation function
1523 @param[in] addr address of memory block
1524 @param[in] size new size of memory block
1525 @return new address of memory block or null
1527 A_EXTERN void *a_alloc_(void *addr, a_size size);
1529 #if defined(LIBA_A_C)
1530 #undef A_INTERN
1531 #define A_INTERN static A_INLINE
1532 #endif /* LIBA_A_C */
1533 #if defined(__cplusplus)
1534 } /* extern "C" */
1535 #endif /* __cplusplus */
1537 /*! @brief declare allocation function */
1538 #define A_ALLOC(alloc, addr, size) void *alloc(void *addr, a_size size)
1539 #define a_new(T, ptr, num) a_cast_s(T *, a_alloc(ptr, sizeof(T) * (num)))
1540 #define a_die(ptr) a_alloc(ptr, 0)
1542 /*! @} liba */
1544 #endif /* a/a.h */