liba 0.1.15
An algorithm library based on C/C++
 
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
Loading...
Searching...
No Matches
a.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_A_H
7#define LIBA_A_H
8
9#if defined(A_HAVE_H)
10#include A_HAVE_H
11#endif /* A_HAVE_H */
12
14
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__ */
39
40#if defined(__MINGW32__)
41#undef __USE_MINGW_ANSI_STDIO
42#define __USE_MINGW_ANSI_STDIO 1
43#endif /* __MINGW32__ */
44
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 */
51
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__ */
58
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__ */
65
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 */
78
79#if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199900L) || \
80 defined(__cplusplus) && (__cplusplus > 201100L) || A_PREREQ_MSVC(18, 0)
81
82#if !defined A_HAVE_LONG_LONG_TYPE
83#define A_HAVE_LONG_LONG_TYPE 1
84#endif /* A_HAVE_LONG_LONG_TYPE */
85
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 */
90
91#if defined(__cplusplus) && (__cplusplus > 201100L)
92
93#if !defined A_HAVE_NULLPTR
94#define A_HAVE_NULLPTR 1
95#endif /* A_HAVE_NULLPTR */
96
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 */
101
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 */
109
110#if defined(_WIN32) || defined(__CYGWIN__)
111#define A_DECLSPEC(x) __declspec(x)
112#else /* !__declspec */
113#define A_DECLSPEC(x)
114#endif /* __declspec */
115
116#if defined(__GNUC__) || defined(__clang__)
117#define A_ATTRIBUTE(x) __attribute__(x)
118#else /* !__attribute__ */
119#define A_ATTRIBUTE(x)
120#endif /* __attribute__ */
121
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 */
136
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 */
143
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 */
150
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 */
157
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 */
166
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 */
178
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 */
200
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 */
213
215
216#include <stddef.h>
217#include <limits.h>
218#include <float.h>
219
224
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__ */
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
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 */
254
255#if !defined A_SIZE_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 */
268
270#define A_BUILD_ASSERT(x) (void)(sizeof(char[1 - 2 * !(x)]))
272#define A_BUILD_BUG_ON(x) (void)(sizeof(char[1 - 2 * !!(x)]))
273
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
288
289#if defined(__cplusplus) && defined(A_HAVE_NULLPTR)
290#define A_NULL nullptr
291#else /* !__cplusplus */
292#define A_NULL NULL
293#endif /* __cplusplus */
295#define a_void_c(x) a_cast_s(void, x)
296#define a_void_(_, x) a_cast_s(void _, x)
298#define a_void void
299
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 */
318#define a_bool_c(x) (!!(x))
320typedef A_BOOL a_bool;
321
322#define A_INT int
323#define A_INT_MAX INT_MAX
324#define A_INT_MIN INT_MIN
326#define a_int_c(x) a_cast_s(a_int, x)
327#define a_int_(_, x) a_cast_s(a_int _, x)
329typedef A_INT a_int;
330
331#define A_UINT unsigned int
332#define A_UINT_MAX UINT_MAX
334#define a_uint_c(x) a_cast_s(a_uint, x)
335#define a_uint_(_, x) a_cast_s(a_uint _, x)
337typedef A_UINT a_uint;
338
339#define A_SHRT short
340#define A_SHRT_MAX SHRT_MAX
341#define A_SHRT_MIN SHRT_MIN
343#define a_shrt_c(x) a_cast_s(a_shrt, x)
344#define a_shrt_(_, x) a_cast_s(a_shrt _, x)
346typedef A_SHRT a_shrt;
347
348#define A_USHRT unsigned short
349#define A_USHRT_MAX USHRT_MAX
351#define a_ushrt_c(x) a_cast_s(a_ushrt, x)
352#define a_ushrt_(_, x) a_cast_s(a_ushrt _, x)
354typedef A_USHRT a_ushrt;
355
356#define A_LONG long
357#define A_LONG_MIN LONG_MIN
358#define A_LONG_MAX LONG_MAX
360#define a_long_c(x) a_cast_s(a_long, x)
361#define a_long_(_, x) a_cast_s(a_long _, x)
363typedef A_LONG a_long;
364
365#define A_ULONG unsigned long
366#define A_ULONG_MAX ULONG_MAX
368#define a_ulong_c(x) a_cast_s(a_ulong, x)
369#define a_ulong_(_, x) a_cast_s(a_ulong _, x)
371typedef A_ULONG a_ulong;
372
373#if defined(A_HAVE_LONG_LONG_TYPE)
374
375#define A_LLONG long long
376#define A_LLONG_MAX LLONG_MAX
377#define A_LLONG_MIN LLONG_MIN
379#define a_llong_c(x) a_cast_s(a_llong, x)
380#define a_llong_(_, x) a_cast_s(a_llong _, x)
382typedef A_LLONG a_llong;
383
384#define A_ULLONG unsigned long long
385#define A_ULLONG_MAX ULLONG_MAX
387#define a_ullong_c(x) a_cast_s(a_ullong, x)
388#define a_ullong_(_, x) a_cast_s(a_ullong _, x)
390typedef A_ULLONG a_ullong;
391
392#endif /* A_HAVE_LONG_LONG_TYPE */
393
394#define A_BYTE unsigned char
395#define A_BYTE_MAX UCHAR_MAX
397#define a_byte_c(x) a_cast_s(a_byte, x)
398#define a_byte_(_, x) a_cast_s(a_byte _, x)
400typedef A_BYTE a_byte;
401
402#define A_C8 char
403#define A_C8_MAX CHAR_MAX
404#define A_C8_MIN CHAR_MIN
406#define a_c8_c(x) a_cast_s(a_c8, x)
407#define a_c8_(_, x) a_cast_s(a_c8 _, x)
409typedef A_C8 a_c8;
410
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 */
424#define a_i8_c(x) a_cast_s(a_i8, x)
425#define a_i8_(_, x) a_cast_s(a_i8 _, x)
427typedef A_I8 a_i8;
428
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 */
439#define a_u8_c(x) a_cast_s(a_u8, x)
440#define a_u8_(_, x) a_cast_s(a_u8 _, x)
442typedef A_U8 a_u8;
443
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 */
450
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 */
466#define a_i16_c(x) a_cast_s(a_i16, x)
467#define a_i16_(_, x) a_cast_s(a_i16 _, x)
469typedef A_I16 a_i16;
470
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 */
483#define a_u16_c(x) a_cast_s(a_u16, x)
484#define a_u16_(_, x) a_cast_s(a_u16 _, x)
486typedef A_U16 a_u16;
487
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 */
498
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 */
516#define a_i32_c(x) a_cast_s(a_i32, x)
517#define a_i32_(_, x) a_cast_s(a_i32 _, x)
519typedef A_I32 a_i32;
520
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 */
535#define a_u32_c(x) a_cast_s(a_u32, x)
536#define a_u32_(_, x) a_cast_s(a_u32 _, x)
538typedef A_U32 a_u32;
539
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 */
550
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 */
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__ */
589typedef A_I64 a_i64;
590/* clang-format on */
591
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 */
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__ */
627typedef A_U64 a_u64;
628/* clang-format on */
629
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 */
652
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 */
666#define a_imax_c(x) a_cast_s(a_imax, x)
667#define a_imax_(_, x) a_cast_s(a_imax _, x)
669typedef A_IMAX a_imax;
670
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 */
681#define a_umax_c(x) a_cast_s(a_umax, x)
682#define a_umax_(_, x) a_cast_s(a_umax _, x)
684typedef A_UMAX a_umax;
685
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 */
692
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 */
715#define a_iptr_c(x) a_cast_s(a_iptr, x)
716#define a_iptr_(_, x) a_cast_s(a_iptr _, x)
718typedef A_IPTR a_iptr;
719
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 */
735#define a_uptr_c(x) a_cast_s(a_uptr, x)
736#define a_uptr_(_, x) a_cast_s(a_uptr _, x)
738typedef A_UPTR a_uptr;
739
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 */
754
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 */
777#define a_diff_c(x) a_cast_s(a_diff, x)
778#define a_diff_(_, x) a_cast_s(a_diff _, x)
780typedef A_DIFF a_diff;
781
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 */
806
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 */
820#define a_size_c(x) a_cast_s(a_size, x)
821#define a_size_(_, x) a_cast_s(a_size _, x)
823typedef A_SIZE a_size;
824
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 */
849
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)
854
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, DBL_MAX)
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)
874#define a_f32_c(x) a_cast_s(a_f32, x)
875#define a_f32_(_, x) a_cast_s(a_f32 _, x)
877typedef A_F32 a_f32;
878
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)
898#define a_f64_c(x) a_cast_s(a_f64, x)
899#define a_f64_(_, x) a_cast_s(a_f64 _, x)
901typedef A_F64 a_f64;
902
907
912#if !defined A_REAL_TYPE
913#if !defined A_SIZE_REAL
914#define A_REAL_TYPE A_REAL_DOUBLE
915#else /* !A_SIZE_REAL */
916#define A_REAL_TYPE A_SIZE_REAL
917#endif /* A_SIZE_REAL */
918#endif /* A_REAL_TYPE */
919#define A_REAL_SINGLE 0x04
920#define A_REAL_DOUBLE 0x08
921#define A_REAL_EXTEND 0x10
922#if defined(A_REAL)
923#elif A_REAL_TYPE + 0 == A_REAL_SINGLE
924
926#define A_REAL float
927#define A_REAL_DIG FLT_DIG
928#define A_REAL_EPSILON FLT_EPSILON
929#define A_REAL_MANT_DIG FLT_MANT_DIG
930#define A_REAL_MAX FLT_MAX
931#define A_REAL_MAX_10_EXP FLT_MAX_10_EXP
932#define A_REAL_MAX_EXP FLT_MAX_EXP
933#define A_REAL_MIN FLT_MIN
934#define A_REAL_MIN_10_EXP FLT_MIN_10_EXP
935#define A_REAL_MIN_EXP FLT_MIN_EXP
936#define A_REAL_C(X) A_CAST_2(X, F)
937#define A_REAL_F(F) A_CAST_2(F, f)
938#define A_REAL_PRI
939#define A_REAL_SCN
940#define a_str2num strtof
941
942#elif A_REAL_TYPE + 0 == A_REAL_DOUBLE
943
945#define A_REAL double
946#define A_REAL_DIG DBL_DIG
947#define A_REAL_EPSILON DBL_EPSILON
948#define A_REAL_MANT_DIG DBL_MANT_DIG
949#define A_REAL_MAX DBL_MAX
950#define A_REAL_MAX_10_EXP DBL_MAX_10_EXP
951#define A_REAL_MAX_EXP DBL_MAX_EXP
952#define A_REAL_MIN DBL_MIN
953#define A_REAL_MIN_10_EXP DBL_MIN_10_EXP
954#define A_REAL_MIN_EXP DBL_MIN_EXP
955#define A_REAL_C(X) X
956#define A_REAL_F(F) F
957#define A_REAL_PRI
958#define A_REAL_SCN "l"
959#define a_str2num strtod
960
961#elif A_REAL_TYPE + 0 == A_REAL_EXTEND
962
964#define A_REAL long double
965#define A_REAL_DIG LDBL_DIG
966#define A_REAL_EPSILON LDBL_EPSILON
967#define A_REAL_MANT_DIG LDBL_MANT_DIG
968#define A_REAL_MAX LDBL_MAX
969#define A_REAL_MAX_10_EXP LDBL_MAX_10_EXP
970#define A_REAL_MAX_EXP LDBL_MAX_EXP
971#define A_REAL_MIN LDBL_MIN
972#define A_REAL_MIN_10_EXP LDBL_MIN_10_EXP
973#define A_REAL_MIN_EXP LDBL_MIN_EXP
974#define A_REAL_C(X) A_CAST_2(X, L)
975#define A_REAL_F(F) A_CAST_2(F, l)
976#define A_REAL_PRI "L"
977#define A_REAL_SCN "L"
978#define a_str2num strtold
979
980#else /* !A_REAL_TYPE */
981#error unsupported precision
982#endif /* A_REAL_TYPE */
983
984#define A_REAL_INF a_cast_s(A_REAL, A_F64_INF)
985#define A_REAL_NAN (A_REAL_C(0.0) * A_REAL_INF)
1003#define a_real_c(x) a_cast_s(a_real, x)
1004#define a_real_(_, x) a_cast_s(a_real _, x)
1007
1009
1010typedef union a_cast
1011{
1012 a_c8 c;
1013 a_int i;
1014 a_uint u;
1015 a_shrt ih;
1016 a_ushrt uh;
1017 a_long il;
1018 a_ulong ul;
1019#if defined(A_HAVE_LONG_LONG_TYPE)
1020 a_llong ill;
1021 a_ullong ull;
1022#endif /* A_HAVE_LONG_LONG_TYPE */
1023 a_i8 i8;
1024 a_u8 u8;
1025 a_i16 i16;
1026 a_u16 u16;
1027 a_i32 i32;
1028 a_u32 u32;
1029 a_i64 i64;
1030 a_u64 u64;
1031 a_f32 f32;
1032 a_f64 f64;
1033 a_imax imax;
1034 a_umax umax;
1035 a_iptr iptr;
1036 a_uptr uptr;
1037 a_diff diff;
1038 a_size size;
1039 void const *PTR;
1040 void *ptr;
1041 char const *STR;
1042 char *str;
1043#if defined(A_REAL_TYPE) && (A_REAL_TYPE + 0 < A_REAL_EXTEND)
1044 a_real f;
1045#endif /* A_REAL_TYPE */
1046} a_cast;
1047
1051#define A_SQ(x) ((x) * (x))
1052
1056#define A_ABS(x) ((x) < 0 ? -(x) : (x))
1060#define A_ABS_(f, g) ((f) < (g) ? (g) - (f) : (f) - (g))
1061
1065#define A_MIN(x, y) (((x) < (y)) ? (x) : (y))
1066
1070#define A_MAX(x, y) (((x) > (y)) ? (x) : (y))
1071
1075#define A_SGN(x) ((0 < (x)) - ((x) < 0))
1079#define A_SGN_(f, g) (((f) > (g)) - ((f) < (g)))
1080
1084#define A_SAT(x, min, max) ((min) < (x) ? (x) < (max) ? (x) : (max) : (min))
1085
1090#define A_LEN(a) (sizeof(a) / sizeof(*(a)))
1091
1097#if defined(offsetof)
1098#define a_offsetof(type, member) offsetof(type, member)
1099#else /* !offsetof */
1100#define a_offsetof(type, member) a_cast_r(a_size, &a_cast_r(type *, 0)->member)
1101#endif /* offsetof */
1102
1109#define a_container_of(ptr, type, member) a_cast_r(type *, a_cast_r(a_uptr, ptr) - a_offsetof(type, member))
1110
1114#define a_size_down(a, n) (a_cast_s(a_size, n) & ~a_cast_s(a_size, (a) - 1))
1115
1119#define a_size_up(a, n) ((a_cast_s(a_size, n) + (a) - 1) & ~a_cast_s(a_size, (a) - 1))
1120
1124#define a_align_down(a, p) a_cast_r(void *, a_cast_r(a_uptr, p) & ~a_cast_s(a_uptr, (a) - 1))
1125
1129#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))
1130
1137#define a_forenum(I, i, n) for (I i = 0; i < (n); ++i)
1138#define A_FORENUM(I, i, n) for (i = 0; i < a_cast_s(I, n); ++i)
1139
1146#define a_forenum_reverse(I, i, n) for (I i = (n); i-- > 0;)
1147#define A_FORENUM_REVERSE(I, i, n) for (i = a_cast_s(I, n); i-- > 0;)
1148
1157#define a_foreach(T, S, it, ptr, num) \
1158 for (T S it = a_cast_s(T S, ptr), S it##_ = it + (num); it < it##_; ++it)
1159#define A_FOREACH(T, it, at, ptr, num) \
1160 for (it = a_cast_s(T, ptr), at = it + (num); it < at; ++it)
1162#define a_forsafe(T, S, it, ptr, num) \
1163 for (T S it = a_cast_s(T S, ptr), S it##_ = (num) ? it + (num) : it; it < it##_; ++it)
1164#define A_FORSAFE(T, it, at, ptr, num) \
1165 for (it = a_cast_s(T, ptr), at = (num) ? it + (num) : it; it < at; ++it)
1166
1175#define a_foreach_reverse(T, S, it, ptr, num) \
1176 for (T S it##_ = a_cast_s(T S, ptr) - 1, S it = it##_ + (num); it > it##_; --it)
1177#define A_FOREACH_REVERSE(T, it, at, ptr, num) \
1178 for (at = a_cast_s(T, ptr) - 1, it = at + (num); it > at; --it)
1180#define a_forsafe_reverse(T, S, it, ptr, num) \
1181 for (T S it##_ = (num) ? a_cast_s(T S, ptr) - 1 : a_cast_s(T S, ptr), \
1182 S it = (num) ? it##_ + (num) : it##_; \
1183 it > it##_; --it)
1184#define A_FORSAFE_REVERSE(T, it, at, ptr, num) \
1185 for (at = (num) ? a_cast_s(T, ptr) - 1 : a_cast_s(T, ptr), \
1186 it = (num) ? at + (num) : at; \
1187 it > at; --it)
1188
1197#define a_iterate(T, S, it, ptr, end) \
1198 for (T S it = a_cast_s(T S, ptr), S it##_ = a_cast_s(T S, end); it < it##_; ++it)
1199#define A_ITERATE(T, it, at, ptr, end) \
1200 for (it = a_cast_s(T, ptr), at = a_cast_s(T, end); it < at; ++it)
1201
1210#define a_iterate_reverse(T, S, it, ptr, end) \
1211 for (T S it = a_cast_s(T S, end) - 1, S it##_ = a_cast_s(T S, ptr) - 1; it > it##_; --it)
1212#define A_ITERATE_REVERSE(T, it, at, ptr, end) \
1213 for (it = a_cast_s(T, end) - 1, at = a_cast_s(T, ptr) - 1; it > at; --it)
1214
1219{
1220 A_SUCCESS,
1221 A_FAILURE,
1222 A_INVALID,
1223 A_OBOUNDS,
1224 A_OMEMORY
1225};
1226
1227#if defined(__cplusplus)
1228extern "C" {
1229#endif /* __cplusplus */
1230#if defined(LIBA_A_C)
1231#undef A_INTERN
1232#define A_INTERN A_INLINE
1233#endif /* LIBA_A_C */
1234
1240#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1241A_EXTERN a_u8 a_u8_rev(a_u8 x);
1242#endif /* A_HAVE_INLINE */
1243#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1244A_INTERN a_u8 a_u8_rev(a_u8 x)
1245{
1246 x = a_cast_s(a_u8, (x >> 4) | (x << 4));
1247 x = a_cast_s(a_u8, ((x & 0xCC) >> 2) | ((x & 0x33) << 2));
1248 x = a_cast_s(a_u8, ((x & 0xAA) >> 1) | ((x & 0x55) << 1));
1249 return x;
1250}
1251#endif /* A_HAVE_INLINE */
1252
1258#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1260#endif /* A_HAVE_INLINE */
1261#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1262A_INTERN a_u16 a_u16_rev(a_u16 x)
1263{
1264 x = a_cast_s(a_u16, (x >> 8) | (x << 8));
1265 x = a_cast_s(a_u16, ((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4));
1266 x = a_cast_s(a_u16, ((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2));
1267 x = a_cast_s(a_u16, ((x & 0xAAAA) >> 1) | ((x & 0x5555) << 1));
1268 return x;
1269}
1270#endif /* A_HAVE_INLINE */
1271#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1272A_EXTERN A_NONULL((1)) a_u16 a_u16_getl(void const *b);
1273#endif /* A_HAVE_INLINE */
1274#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1275A_INTERN A_NONULL((1)) a_u16 a_u16_getl(void const *b)
1276{
1277 a_byte const *p = a_cast_s(a_byte const *, b);
1278 return a_cast_s(a_u16, (p[0] << 0) | (p[1] << 8));
1279}
1280#endif /* A_HAVE_INLINE */
1281#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1282A_EXTERN A_NONULL((1)) a_u16 a_u16_getb(void const *b);
1283#endif /* A_HAVE_INLINE */
1284#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1285A_INTERN A_NONULL((1)) a_u16 a_u16_getb(void const *b)
1286{
1287 a_byte const *p = a_cast_s(a_byte const *, b);
1288 return a_cast_s(a_u16, (p[1] << 0) | (p[0] << 8));
1289}
1290#endif /* A_HAVE_INLINE */
1291#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1292A_EXTERN A_NONULL((1)) void a_u16_setl(void *b, a_u16 x);
1293#endif /* A_HAVE_INLINE */
1294#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1295A_INTERN A_NONULL((1)) void a_u16_setl(void *b, a_u16 x)
1296{
1297 a_byte *p = a_cast_s(a_byte *, b);
1298 p[0] = a_cast_s(a_byte, x >> 0);
1299 p[1] = a_cast_s(a_byte, x >> 8);
1300}
1301#endif /* A_HAVE_INLINE */
1302#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1303A_EXTERN A_NONULL((1)) void a_u16_setb(void *b, a_u16 x);
1304#endif /* A_HAVE_INLINE */
1305#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1306A_INTERN A_NONULL((1)) void a_u16_setb(void *b, a_u16 x)
1307{
1308 a_byte *p = a_cast_s(a_byte *, b);
1309 p[0] = a_cast_s(a_byte, x >> 8);
1310 p[1] = a_cast_s(a_byte, x >> 0);
1311}
1312#endif /* A_HAVE_INLINE */
1313
1319#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1321#endif /* A_HAVE_INLINE */
1322#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1323A_INTERN a_u32 a_u32_rev(a_u32 x)
1324{
1325 x = (x >> 16) | (x << 16);
1326 x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
1327 x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
1328 x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
1329 x = ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
1330 return x;
1331}
1332#endif /* A_HAVE_INLINE */
1333#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1334A_EXTERN A_NONULL((1)) a_u32 a_u32_getl(void const *b);
1335#endif /* A_HAVE_INLINE */
1336#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1337A_INTERN A_NONULL((1)) a_u32 a_u32_getl(void const *b)
1338{
1339 a_byte const *p = a_cast_s(a_byte const *, b);
1340 return (a_cast_s(a_u32, p[0]) << 0x00) |
1341 (a_cast_s(a_u32, p[1]) << 0x08) |
1342 (a_cast_s(a_u32, p[2]) << 0x10) |
1343 (a_cast_s(a_u32, p[3]) << 0x18);
1344}
1345#endif /* A_HAVE_INLINE */
1346#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1347A_EXTERN A_NONULL((1)) a_u32 a_u32_getb(void const *b);
1348#endif /* A_HAVE_INLINE */
1349#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1350A_INTERN A_NONULL((1)) a_u32 a_u32_getb(void const *b)
1351{
1352 a_byte const *p = a_cast_s(a_byte const *, b);
1353 return (a_cast_s(a_u32, p[0]) << 0x18) |
1354 (a_cast_s(a_u32, p[1]) << 0x10) |
1355 (a_cast_s(a_u32, p[2]) << 0x08) |
1356 (a_cast_s(a_u32, p[3]) << 0x00);
1357}
1358#endif /* A_HAVE_INLINE */
1359#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1360A_EXTERN A_NONULL((1)) void a_u32_setl(void *b, a_u32 x);
1361#endif /* A_HAVE_INLINE */
1362#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1363A_INTERN A_NONULL((1)) void a_u32_setl(void *b, a_u32 x)
1364{
1365 a_byte *p = a_cast_s(a_byte *, b);
1366 p[0] = a_cast_s(a_byte, x >> 0x00);
1367 p[1] = a_cast_s(a_byte, x >> 0x08);
1368 p[2] = a_cast_s(a_byte, x >> 0x10);
1369 p[3] = a_cast_s(a_byte, x >> 0x18);
1370}
1371#endif /* A_HAVE_INLINE */
1372#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1373A_EXTERN A_NONULL((1)) void a_u32_setb(void *b, a_u32 x);
1374#endif /* A_HAVE_INLINE */
1375#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1376A_INTERN A_NONULL((1)) void a_u32_setb(void *b, a_u32 x)
1377{
1378 a_byte *p = a_cast_s(a_byte *, b);
1379 p[0] = a_cast_s(a_byte, x >> 0x18);
1380 p[1] = a_cast_s(a_byte, x >> 0x10);
1381 p[2] = a_cast_s(a_byte, x >> 0x08);
1382 p[3] = a_cast_s(a_byte, x >> 0x00);
1383}
1384#endif /* A_HAVE_INLINE */
1385
1391#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1393#endif /* A_HAVE_INLINE */
1394#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1395A_INTERN a_u64 a_u64_rev(a_u64 x)
1396{
1397 x = (x >> 32) | (x << 32);
1398 x = ((x & 0xFFFF0000FFFF0000) >> 0x10) | ((x & 0x0000FFFF0000FFFF) << 0x10);
1399 x = ((x & 0xFF00FF00FF00FF00) >> 0x08) | ((x & 0x00FF00FF00FF00FF) << 0x08);
1400 x = ((x & 0xF0F0F0F0F0F0F0F0) >> 0x04) | ((x & 0x0F0F0F0F0F0F0F0F) << 0x04);
1401 x = ((x & 0xCCCCCCCCCCCCCCCC) >> 0x02) | ((x & 0x3333333333333333) << 0x02);
1402 x = ((x & 0xAAAAAAAAAAAAAAAA) >> 0x01) | ((x & 0x5555555555555555) << 0x01);
1403 return x;
1404}
1405#endif /* A_HAVE_INLINE */
1406#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1407A_EXTERN A_NONULL((1)) a_u64 a_u64_getl(void const *b);
1408#endif /* A_HAVE_INLINE */
1409#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1410A_INTERN A_NONULL((1)) a_u64 a_u64_getl(void const *b)
1411{
1412 a_byte const *p = a_cast_s(a_byte const *, b);
1413 return (a_cast_s(a_u64, p[0]) << 0x00) |
1414 (a_cast_s(a_u64, p[1]) << 0x08) |
1415 (a_cast_s(a_u64, p[2]) << 0x10) |
1416 (a_cast_s(a_u64, p[3]) << 0x18) |
1417 (a_cast_s(a_u64, p[4]) << 0x20) |
1418 (a_cast_s(a_u64, p[5]) << 0x28) |
1419 (a_cast_s(a_u64, p[6]) << 0x30) |
1420 (a_cast_s(a_u64, p[7]) << 0x38);
1421}
1422#endif /* A_HAVE_INLINE */
1423#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1424A_EXTERN A_NONULL((1)) a_u64 a_u64_getb(void const *b);
1425#endif /* A_HAVE_INLINE */
1426#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1427A_INTERN A_NONULL((1)) a_u64 a_u64_getb(void const *b)
1428{
1429 a_byte const *p = a_cast_s(a_byte const *, b);
1430 return (a_cast_s(a_u64, p[0]) << 0x38) |
1431 (a_cast_s(a_u64, p[1]) << 0x30) |
1432 (a_cast_s(a_u64, p[2]) << 0x28) |
1433 (a_cast_s(a_u64, p[3]) << 0x20) |
1434 (a_cast_s(a_u64, p[4]) << 0x18) |
1435 (a_cast_s(a_u64, p[5]) << 0x10) |
1436 (a_cast_s(a_u64, p[6]) << 0x08) |
1437 (a_cast_s(a_u64, p[7]) << 0x00);
1438}
1439#endif /* A_HAVE_INLINE */
1440#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1441A_EXTERN A_NONULL((1)) void a_u64_setl(void *b, a_u64 x);
1442#endif /* A_HAVE_INLINE */
1443#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1444A_INTERN A_NONULL((1)) void a_u64_setl(void *b, a_u64 x)
1445{
1446 a_byte *p = a_cast_s(a_byte *, b);
1447 p[0] = a_cast_s(a_byte, x >> 0x00);
1448 p[1] = a_cast_s(a_byte, x >> 0x08);
1449 p[2] = a_cast_s(a_byte, x >> 0x10);
1450 p[3] = a_cast_s(a_byte, x >> 0x18);
1451 p[4] = a_cast_s(a_byte, x >> 0x20);
1452 p[5] = a_cast_s(a_byte, x >> 0x28);
1453 p[6] = a_cast_s(a_byte, x >> 0x30);
1454 p[7] = a_cast_s(a_byte, x >> 0x38);
1455}
1456#endif /* A_HAVE_INLINE */
1457#if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1458A_EXTERN A_NONULL((1)) void a_u64_setb(void *b, a_u64 x);
1459#endif /* A_HAVE_INLINE */
1460#if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1461A_INTERN A_NONULL((1)) void a_u64_setb(void *b, a_u64 x)
1462{
1463 a_byte *p = a_cast_s(a_byte *, b);
1464 p[0] = a_cast_s(a_byte, x >> 0x38);
1465 p[1] = a_cast_s(a_byte, x >> 0x30);
1466 p[2] = a_cast_s(a_byte, x >> 0x28);
1467 p[3] = a_cast_s(a_byte, x >> 0x20);
1468 p[4] = a_cast_s(a_byte, x >> 0x18);
1469 p[5] = a_cast_s(a_byte, x >> 0x10);
1470 p[6] = a_cast_s(a_byte, x >> 0x08);
1471 p[7] = a_cast_s(a_byte, x >> 0x00);
1472}
1473#endif /* A_HAVE_INLINE */
1474
1482A_EXTERN A_NONULL((1, 2)) void *a_copy(void *__restrict dst, void const *__restrict src, a_size siz);
1483
1491A_EXTERN A_NONULL((1, 2)) void *a_move(void *dst, void const *src, a_size siz);
1492
1500A_EXTERN A_NONULL((1)) void *a_fill(void *ptr, a_size siz, int val);
1501
1508A_EXTERN A_NONULL((1)) void *a_zero(void *ptr, a_size siz);
1509
1516A_EXTERN A_NONULL((1, 2)) void a_swap(void *lhs, void *rhs, a_size siz);
1517
1524A_EXTERN void *(*a_alloc)(void *addr, a_size size);
1525
1532A_EXTERN void *a_alloc_(void *addr, a_size size);
1533
1534#if defined(LIBA_A_C)
1535#undef A_INTERN
1536#define A_INTERN static A_INLINE
1537#endif /* LIBA_A_C */
1538#if defined(__cplusplus)
1539} /* extern "C" */
1540#endif /* __cplusplus */
1541
1543#define A_ALLOC(alloc, addr, size) void *alloc(void *addr, a_size size)
1544#define a_new(T, ptr, num) a_cast_s(T *, a_alloc(ptr, sizeof(T) * (num)))
1545#define a_die(ptr) a_alloc(ptr, 0)
1546
1548
1549#endif /* a/a.h */
double a_real
compiler built-in floating-point number type
Definition a.h:1006
#define A_REAL
floating-point number stored using double
Definition a.h:945
float a_f32
single precision floating point type. Matches IEEE-754 binary32 format if supported.
Definition a.h:877
a_u64 a_u64_rev(a_u64 x)
reverse the bits in a 64-bit unsigned integer
signed char a_i8
signed integer type with width of exactly 8 bits
Definition a.h:427
unsigned long a_u64
unsigned integer type with width of exactly 64 bits
Definition a.h:627
long a_long
signed integer type is guaranteed to be at least 32 bits
Definition a.h:363
unsigned int a_uint
unsigned integer type is guaranteed to be at least 16 bits
Definition a.h:337
short a_i16
signed integer type with width of exactly 16 bits
Definition a.h:469
void * a_fill(void *ptr, a_size siz, int val)
fill a buffer with a character
void a_swap(void *lhs, void *rhs, a_size siz)
swap two different memory blocks of the same size
unsigned char a_u8
unsigned integer type with width of exactly 8 bits
Definition a.h:442
char a_c8
type for character representation
Definition a.h:409
long a_imax
maximum-width signed integer type
Definition a.h:669
a_u8 a_u8_rev(a_u8 x)
reverse the bits in an 8-bit unsigned integer
void * a_copy(void *__restrict dst, void const *__restrict src, a_size siz)
copy one buffer to another
void * a_zero(void *ptr, a_size siz)
fill a buffer with zero
void *(* a_alloc)(void *addr, a_size size)
allocation function pointer
Definition a.h:1524
long a_i64
signed integer type with width of exactly 64 bits
Definition a.h:589
ptrdiff_t a_diff
signed integer type returned when subtracting two pointers
Definition a.h:780
unsigned long a_umax
maximum-width unsigned integer type
Definition a.h:684
unsigned short a_ushrt
unsigned integer type is guaranteed to be at least 16 bits
Definition a.h:354
short a_shrt
signed integer type is guaranteed to be at least 16 bits
Definition a.h:346
unsigned long a_uptr
unsigned integer type capable of holding a pointer to void
Definition a.h:738
void * a_alloc_(void *addr, a_size size)
default allocation function
void * a_move(void *dst, void const *src, a_size siz)
move one buffer to another
long a_iptr
signed integer type capable of holding a pointer to void
Definition a.h:718
unsigned long a_ulong
unsigned integer type is guaranteed to be at least 32 bits
Definition a.h:371
a_u16 a_u16_rev(a_u16 x)
reverse the bits in a 16-bit unsigned integer
a_u32 a_u32_rev(a_u32 x)
reverse the bits in a 32-bit unsigned integer
a_return_code
enumeration for return values
Definition a.h:1219
unsigned short a_u16
unsigned integer type with width of exactly 16 bits
Definition a.h:486
double a_f64
double precision floating point type. Matches IEEE-754 binary64 format if supported.
Definition a.h:901
bool a_bool
type, capable of holding one of the two values: 1 and 0
Definition a.h:320
long a_i32
signed integer type with width of exactly 32 bits
Definition a.h:519
unsigned char a_byte
type for unsigned character representation
Definition a.h:400
unsigned long a_u32
unsigned integer type with width of exactly 32 bits
Definition a.h:538
size_t a_size
unsigned integer type returned by the sizeof operator
Definition a.h:823
int a_int
signed integer type is guaranteed to be at least 16 bits
Definition a.h:329
Definition a.h:1011