rename a_float_push,roll to a_float_push_,roll_ and create new a_float_push,roll
[liba.git] / include / a / a.h
blob9a4fbd5762b9045f3859c855905b1f57a5cee7a5
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 defined(__GNUC__)
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 /* byte order of little endian architecture */
217 #if defined(__ORDER_LITTLE_ENDIAN__)
218 #define A_ORDER_LITTLE __ORDER_LITTLE_ENDIAN__
219 #else /* !__ORDER_LITTLE_ENDIAN__ */
220 #define A_ORDER_LITTLE 1234
221 #endif /* __ORDER_LITTLE_ENDIAN__ */
222 /* byte order of big endian architecture */
223 #if defined(__ORDER_BIG_ENDIAN__)
224 #define A_ORDER_BIG __ORDER_BIG_ENDIAN__
225 #else /* !__ORDER_BIG_ENDIAN__ */
226 #define A_ORDER_BIG 4321
227 #endif /* __ORDER_BIG_ENDIAN__ */
228 /* byte order of architecture */
229 #if !defined A_BYTE_ORDER
230 #if defined(__BYTE_ORDER__)
231 #define A_BYTE_ORDER __BYTE_ORDER__
232 #endif /* __BYTE_ORDER__ */
233 #endif /* A_BYTE_ORDER */
235 /* size of void pointer */
236 #if !defined A_SIZE_POINTER
237 #if defined(__SIZEOF_POINTER__)
238 #define A_SIZE_POINTER __SIZEOF_POINTER__
239 #elif defined(_WIN64)
240 #define A_SIZE_POINTER 8
241 #elif defined(_WIN32)
242 #define A_SIZE_POINTER 4
243 #endif /* __SIZEOF_POINTER__ */
244 #endif /* A_SIZE_POINTER */
246 #include <stddef.h>
247 #include <limits.h>
248 #include <stdint.h>
249 #include <float.h>
252 @addtogroup liba algorithm library
256 /*! assert a build-time dependency, as an expression */
257 #define A_BUILD_ASSERT(x) (void)(sizeof(char[1 - 2 * !(x)]))
258 #define A_BUILD_BUG_ON(x) (void)(sizeof(char[1 - 2 * !!(x)]))
260 #if defined(__cplusplus)
261 #define a_cast_r(T, x) reinterpret_cast<T>(x)
262 #define a_cast_d(T, x) dynamic_cast<T>(x)
263 #define a_cast_s(T, x) static_cast<T>(x)
264 #define a_cast_c(T, x) const_cast<T>(x)
265 #else /* !__cplusplus */
266 #define a_cast_r(T, x) ((T)(x))
267 #define a_cast_d(T, x) ((T)(x))
268 #define a_cast_s(T, x) ((T)(x))
269 #define a_cast_c(T, x) ((T)(x))
270 #endif /* __cplusplus */
271 #define A_CAST_3(a, b, c) a##b##c
272 #define A_CAST_2(a, b) a##b
273 #define A_CAST_1(a) #a
275 #if defined(__cplusplus) && defined(A_HAVE_NULLPTR)
276 #define A_NULL nullptr
277 #else /* !__cplusplus */
278 #define A_NULL NULL
279 #endif /* __cplusplus */
280 /*! static cast to \ref a_void */
281 #define a_void_c(_, x) a_cast_s(void, x)
282 #define a_void_(_, x) a_cast_s(void _, x)
283 /*! incomplete type or no parameter or no return value */
284 #define a_void void
286 #if defined(__cplusplus)
287 #define A_TRUE true
288 #define A_FALSE false
289 #if !defined A_BOOL
290 #define A_BOOL bool
291 #endif /* A_BOOL */
292 #else /* !__cplusplus */
293 #define A_TRUE 1
294 #define A_FALSE 0
295 #if !defined A_BOOL
296 #define A_BOOL _Bool
297 #endif /* A_BOOL */
298 #endif /* __cplusplus */
299 /*! static cast to \ref a_bool */
300 #define a_bool_c(x) (!!(x))
301 /*! type, capable of holding one of the two values: 1 and 0 */
302 #define a_bool A_BOOL
304 #define A_INT int
305 #define A_INT_MIN INT_MIN
306 #define A_INT_MAX INT_MAX
307 /*! static cast to \ref a_int */
308 #define a_int_c(x) a_cast_s(A_INT, x)
309 #define a_int_(_, x) a_cast_s(A_INT _, x)
310 /*! signed integer type is guaranteed to be at least 16 bits */
311 #define a_int A_INT
313 #define A_UINT unsigned int
314 #define A_UINT_MAX UINT_MAX
315 /*! static cast to \ref a_uint */
316 #define a_uint_c(x) a_cast_s(A_UINT, x)
317 #define a_uint_(_, x) a_cast_s(A_UINT _, x)
318 /*! unsigned integer type is guaranteed to be at least 16 bits */
319 #define a_uint A_UINT
321 #define A_SHRT short
322 #define A_SHRT_MIN SHRT_MIN
323 #define A_SHRT_MAX SHRT_MAX
324 /*! static cast to \ref a_shrt */
325 #define a_shrt_c(x) a_cast_s(A_SHRT, x)
326 #define a_shrt_(_, x) a_cast_s(A_SHRT _, x)
327 /*! signed integer type is guaranteed to be at least 16 bits */
328 #define a_shrt A_SHRT
330 #define A_USHRT unsigned short
331 #define A_USHRT_MAX USHRT_MAX
332 /*! static cast to \ref a_ushrt */
333 #define a_ushrt_c(x) a_cast_s(A_USHRT, x)
334 #define a_ushrt_(_, x) a_cast_s(A_USHRT _, x)
335 /*! unsigned integer type is guaranteed to be at least 16 bits */
336 #define a_ushrt A_USHRT
338 #define A_LONG long
339 #define A_LONG_MIN LONG_MIN
340 #define A_LONG_MAX LONG_MAX
341 /*! static cast to \ref a_long */
342 #define a_long_c(x) a_cast_s(A_LONG, x)
343 #define a_long_(_, x) a_cast_s(A_LONG _, x)
344 /*! signed integer type is guaranteed to be at least 32 bits */
345 #define a_long A_LONG
347 #define A_ULONG unsigned long
348 #define A_ULONG_MAX ULONG_MAX
349 /*! static cast to \ref a_ulong */
350 #define a_ulong_c(x) a_cast_s(A_ULONG, x)
351 #define a_ulong_(_, x) a_cast_s(A_ULONG _, x)
352 /*! unsigned integer type is guaranteed to be at least 32 bits */
353 #define a_ulong A_ULONG
355 #if defined(A_HAVE_LONG_LONG_TYPE)
357 #define A_LLONG long long
358 #define A_LLONG_MIN LLONG_MIN
359 #define A_LLONG_MAX LLONG_MAX
360 /*! static cast to \ref a_llong */
361 #define a_llong_c(x) a_cast_s(A_LLONG, x)
362 #define a_llong_(_, x) a_cast_s(A_LLONG _, x)
363 /*! signed integer type is guaranteed to be at least 64 bits */
364 #define a_llong A_LLONG
366 #define A_ULLONG unsigned long long
367 #define A_ULLONG_MAX ULLONG_MAX
368 /*! static cast to \ref a_ullong */
369 #define a_ullong_c(x) a_cast_s(A_ULLONG, x)
370 #define a_ullong_(_, x) a_cast_s(A_ULLONG _, x)
371 /*! unsigned integer type is guaranteed to be at least 64 bits */
372 #define a_ullong A_ULLONG
374 #endif /* A_HAVE_LONG_LONG_TYPE */
376 #define A_BYTE unsigned char
377 #define A_BYTE_MAX UCHAR_MAX
378 /*! static cast to \ref a_byte */
379 #define a_byte_c(x) a_cast_s(A_BYTE, x)
380 #define a_byte_(_, x) a_cast_s(A_BYTE _, x)
381 /*! type for unsigned character representation */
382 #define a_byte A_BYTE
384 #define A_C8 char
385 #define A_C8_MIN CHAR_MIN
386 #define A_C8_MAX CHAR_MAX
387 /*! static cast to \ref a_c8 */
388 #define a_c8_c(x) a_cast_s(A_C8, x)
389 #define a_c8_(_, x) a_cast_s(A_C8 _, x)
390 /*! type for character representation */
391 #define a_c8 A_C8
393 #if !defined A_I8
394 #define A_I8 int8_t
395 #endif /* A_I8 */
396 #if !defined A_I8_C && defined(INT8_C)
397 #define A_I8_C(X) INT8_C(X)
398 #endif /* A_I8_C */
399 #if !defined A_I8_MIN && defined(INT8_MIN)
400 #define A_I8_MIN INT8_MIN
401 #endif /* A_I8_MIN */
402 #if !defined A_I8_MAX && defined(INT8_MAX)
403 #define A_I8_MAX INT8_MAX
404 #endif /* A_I8_MAX */
405 /*! static cast to \ref a_i8 */
406 #define a_i8_c(x) a_cast_s(A_I8, x)
407 #define a_i8_(_, x) a_cast_s(A_I8 _, x)
408 /*! signed integer type with width of exactly 8 bits */
409 #define a_i8 A_I8
411 #if !defined A_U8
412 #define A_U8 uint8_t
413 #endif /* A_U8 */
414 #if !defined A_U8_C && defined(UINT8_C)
415 #define A_U8_C(X) UINT8_C(X)
416 #endif /* A_U8_C */
417 #if !defined A_U8_MAX && defined(UINT8_MAX)
418 #define A_U8_MAX UINT8_MAX
419 #endif /* A_U8_MAX */
420 /*! static cast to \ref a_u8 */
421 #define a_u8_c(x) a_cast_s(A_U8, x)
422 #define a_u8_(_, x) a_cast_s(A_U8 _, x)
423 /*! unsigned integer type with width of exactly 8 bits */
424 #define a_u8 A_U8
426 #if !defined A_I16
427 #define A_I16 int16_t
428 #endif /* A_I16 */
429 #if !defined A_I16_C && defined(INT16_C)
430 #define A_I16_C(X) INT16_C(X)
431 #endif /* A_I16_C */
432 #if !defined A_I16_MIN && defined(INT16_MIN)
433 #define A_I16_MIN INT16_MIN
434 #endif /* A_I16_MIN */
435 #if !defined A_I16_MAX && defined(INT16_MAX)
436 #define A_I16_MAX INT16_MAX
437 #endif /* A_I16_MAX */
438 /*! static cast to \ref a_i16 */
439 #define a_i16_c(x) a_cast_s(A_I16, x)
440 #define a_i16_(_, x) a_cast_s(A_I16 _, x)
441 /*! signed integer type with width of exactly 16 bits */
442 #define a_i16 A_I16
444 #if !defined A_U16
445 #define A_U16 uint16_t
446 #endif /* A_U16 */
447 #if !defined A_U16_C && defined(UINT16_C)
448 #define A_U16_C(X) UINT16_C(X)
449 #endif /* A_U16_C */
450 #if !defined A_U16_MAX && defined(UINT16_MAX)
451 #define A_U16_MAX UINT16_MAX
452 #endif /* A_U16_MAX */
453 /*! static cast to \ref a_u16 */
454 #define a_u16_c(x) a_cast_s(A_U16, x)
455 #define a_u16_(_, x) a_cast_s(A_U16 _, x)
456 /*! unsigned integer type with width of exactly 16 bits */
457 #define a_u16 A_U16
459 #if !defined A_I32
460 #define A_I32 int32_t
461 #endif /* A_I32 */
462 #if !defined A_I32_C && defined(INT32_C)
463 #define A_I32_C(X) INT32_C(X)
464 #endif /* A_I32_C */
465 #if !defined A_I32_MIN && defined(INT32_MIN)
466 #define A_I32_MIN INT32_MIN
467 #endif /* A_I32_MIN */
468 #if !defined A_I32_MAX && defined(INT32_MAX)
469 #define A_I32_MAX INT32_MAX
470 #endif /* A_I32_MAX */
471 /*! static cast to \ref a_i32 */
472 #define a_i32_c(x) a_cast_s(A_I32, x)
473 #define a_i32_(_, x) a_cast_s(A_I32 _, x)
474 /*! signed integer type with width of exactly 32 bits */
475 #define a_i32 A_I32
477 #if !defined A_U32
478 #define A_U32 uint32_t
479 #endif /* A_U32 */
480 #if !defined A_U32_C && defined(UINT32_C)
481 #define A_U32_C(X) UINT32_C(X)
482 #endif /* A_U32_C */
483 #if !defined A_U32_MAX && defined(UINT32_MAX)
484 #define A_U32_MAX UINT32_MAX
485 #endif /* A_U32_MAX */
486 /*! static cast to \ref a_u32 */
487 #define a_u32_c(x) a_cast_s(A_U32, x)
488 #define a_u32_(_, x) a_cast_s(A_U32 _, x)
489 /*! unsigned integer type with width of exactly 32 bits */
490 #define a_u32 A_U32
492 #if !defined A_I64
493 #define A_I64 int64_t
494 #endif /* A_I64 */
495 #if !defined A_I64_C && defined(INT64_C)
496 #define A_I64_C(X) INT64_C(X)
497 #endif /* A_I64_C */
498 #if !defined A_I64_MIN && defined(INT64_MIN)
499 #define A_I64_MIN INT64_MIN
500 #endif /* A_I64_MIN */
501 #if !defined A_I64_MAX && defined(INT64_MAX)
502 #define A_I64_MAX INT64_MAX
503 #endif /* A_I64_MAX */
504 /*! static cast to \ref a_i64 */
505 #define a_i64_c(x) a_cast_s(A_I64, x)
506 #define a_i64_(_, x) a_cast_s(A_I64 _, x)
507 /*! signed integer type with width of exactly 64 bits */
508 #define a_i64 A_I64
510 #if !defined A_U64
511 #define A_U64 uint64_t
512 #endif /* A_U64 */
513 #if !defined A_U64_C && defined(UINT64_C)
514 #define A_U64_C(X) UINT64_C(X)
515 #endif /* A_U64_C */
516 #if !defined A_U64_MAX && defined(UINT64_MAX)
517 #define A_U64_MAX UINT64_MAX
518 #endif /* A_U64_MAX */
519 /*! static cast to \ref a_u64 */
520 #define a_u64_c(x) a_cast_s(A_U64, x)
521 #define a_u64_(_, x) a_cast_s(A_U64 _, x)
522 /*! unsigned integer type with width of exactly 64 bits */
523 #define a_u64 A_U64
525 #if !defined A_IMAX
526 #define A_IMAX intmax_t
527 #endif /* A_IMAX */
528 #if !defined A_IMAX_C && defined(INTMAX_C)
529 #define A_IMAX_C(X) INTMAX_C(X)
530 #endif /* A_IMAX_C */
531 #if !defined A_IMAX_MIN && defined(INTMAX_MIN)
532 #define A_IMAX_MIN INTMAX_MIN
533 #endif /* A_IMAX_MIN */
534 #if !defined A_IMAX_MAX && defined(INTMAX_MAX)
535 #define A_IMAX_MAX INTMAX_MAX
536 #endif /* A_IMAX_MAX */
537 /*! static cast to \ref a_imax */
538 #define a_imax_c(x) a_cast_s(A_IMAX, x)
539 #define a_imax_(_, x) a_cast_s(A_IMAX _, x)
540 /*! maximum-width signed integer type */
541 #define a_imax A_IMAX
543 #if !defined A_UMAX
544 #define A_UMAX uintmax_t
545 #endif /* A_UMAX */
546 #if !defined A_UMAX_C && defined(UINTMAX_C)
547 #define A_UMAX_C(X) UINTMAX_C(X)
548 #endif /* A_UMAX_C */
549 #if !defined A_UMAX_MAX && defined(UINTMAX_MAX)
550 #define A_UMAX_MAX UINTMAX_MAX
551 #endif /* A_UMAX_MAX */
552 /*! static cast to \ref a_umax */
553 #define a_umax_c(x) a_cast_s(A_UMAX, x)
554 #define a_umax_(_, x) a_cast_s(A_UMAX _, x)
555 /*! maximum-width unsigned integer type */
556 #define a_umax A_UMAX
558 #if !defined A_IPTR
559 #define A_IPTR intptr_t
560 #endif /* A_IPTR */
561 #if !defined A_IPTR_MIN && defined(INTPTR_MIN)
562 #define A_IPTR_MIN INTPTR_MIN
563 #endif /* A_IPTR_MIN */
564 #if !defined A_IPTR_MAX && defined(INTPTR_MAX)
565 #define A_IPTR_MAX INTPTR_MAX
566 #endif /* A_IPTR_MAX */
567 /*! static cast to \ref a_iptr */
568 #define a_iptr_c(x) a_cast_s(A_IPTR, x)
569 #define a_iptr_(_, x) a_cast_s(A_IPTR _, x)
570 /*! signed integer type capable of holding a pointer to void */
571 #define a_iptr A_IPTR
573 #if !defined A_UPTR
574 #define A_UPTR uintptr_t
575 #endif /* A_UPTR */
576 #if !defined A_UPTR_MAX && defined(UINTPTR_MAX)
577 #define A_UPTR_MAX UINTPTR_MAX
578 #endif /* A_UPTR_MAX */
579 /*! static cast to \ref a_uptr */
580 #define a_uptr_c(x) a_cast_s(A_UPTR, x)
581 #define a_uptr_(_, x) a_cast_s(A_UPTR _, x)
582 /*! unsigned integer type capable of holding a pointer to void */
583 #define a_uptr A_UPTR
585 #if !defined A_DIFF
586 #define A_DIFF ptrdiff_t
587 #endif /* A_DIFF */
588 #if !defined A_DIFF_MIN && defined(PTRDIFF_MIN)
589 #define A_DIFF_MIN PTRDIFF_MIN
590 #endif /* A_DIFF_MIN */
591 #if !defined A_DIFF_MAX && defined(PTRDIFF_MAX)
592 #define A_DIFF_MAX PTRDIFF_MAX
593 #endif /* A_DIFF_MAX */
594 /*! static cast to \ref a_diff */
595 #define a_diff_c(x) a_cast_s(A_DIFF, x)
596 #define a_diff_(_, x) a_cast_s(A_DIFF _, x)
597 /*! signed integer type returned when subtracting two pointers */
598 #define a_diff A_DIFF
600 #if !defined A_SIZE
601 #define A_SIZE size_t
602 #endif /* A_SIZE */
603 #if !defined A_SIZE_MAX && defined(SIZE_MAX)
604 #define A_SIZE_MAX SIZE_MAX
605 #endif /* A_SIZE_MAX */
606 /*! static cast to \ref a_size */
607 #define a_size_c(x) a_cast_s(A_SIZE, x)
608 #define a_size_(_, x) a_cast_s(A_SIZE _, x)
609 /*! unsigned integer type returned by the sizeof operator */
610 #define a_size A_SIZE
612 #define A_F16_NNAN A_U16_C(0xFE00)
613 #define A_F16_PNAN A_U16_C(0x7E00)
614 #define A_F16_NINF A_U16_C(0xFC00)
615 #define A_F16_PINF A_U16_C(0x7C00)
617 #define A_F32 float
618 #define A_F32_C(X) A_CAST_2(X, F)
619 #define A_F32_F(F) A_CAST_2(F, f)
620 #define A_F32_DIG FLT_DIG
621 #define A_F32_EPSILON FLT_EPSILON
622 #define A_F32_MANT_DIG FLT_MANT_DIG
623 #define A_F32_MAX FLT_MAX
624 #define A_F32_MAX_10_EXP FLT_MAX_10_EXP
625 #define A_F32_MAX_EXP FLT_MAX_EXP
626 #define A_F32_MIN FLT_MIN
627 #define A_F32_MIN_10_EXP FLT_MIN_10_EXP
628 #define A_F32_MIN_EXP FLT_MIN_EXP
629 #define A_F32_INF a_cast_s(A_F32, A_F64_INF)
630 #define A_F32_NAN (A_F32_C(0.0) * A_F32_INF)
631 #define A_F32_NNAN A_U32_C(0xFFC00000)
632 #define A_F32_PNAN A_U32_C(0x7FC00000)
633 #define A_F32_NINF A_U32_C(0xFF800000)
634 #define A_F32_PINF A_U32_C(0x7F800000)
635 /*! format constants for the fprintf family of functions */
636 #define A_F32_PRI(F, C) "%" F C
637 /*! format constants for the fscanf family of functions */
638 #define A_F32_SCN(F, C) "%" F C
639 /*! static cast to \ref a_f32 */
640 #define a_f32_c(x) a_cast_s(A_F32, x)
641 #define a_f32_(_, x) a_cast_s(A_F32 _, x)
642 /*! single precision floating point type. Matches IEEE-754 binary32 format if supported. */
643 #define a_f32 A_F32
645 #define A_F64 double
646 #define A_F64_C(X) X
647 #define A_F64_F(F) F
648 #define A_F64_DIG DBL_DIG
649 #define A_F64_EPSILON DBL_EPSILON
650 #define A_F64_MANT_DIG DBL_MANT_DIG
651 #define A_F64_MAX DBL_MAX
652 #define A_F64_MAX_10_EXP DBL_MAX_10_EXP
653 #define A_F64_MAX_EXP DBL_MAX_EXP
654 #define A_F64_MIN DBL_MIN
655 #define A_F64_MIN_10_EXP DBL_MIN_10_EXP
656 #define A_F64_MIN_EXP DBL_MIN_EXP
657 #define A_F64_INF (DBL_MAX * DBL_MAX)
658 #define A_F64_NAN (A_F64_C(0.0) * A_F64_INF)
659 #define A_F64_NNAN A_U64_C(0xFFF8000000000000)
660 #define A_F64_PNAN A_U64_C(0x7FF8000000000000)
661 #define A_F64_NINF A_U64_C(0xFFF0000000000000)
662 #define A_F64_PINF A_U64_C(0x7FF0000000000000)
663 /*! format constants for the fprintf family of functions */
664 #define A_F64_PRI(F, C) "%" F "l" C
665 /*! format constants for the fscanf family of functions */
666 #define A_F64_SCN(F, C) "%" F "l" C
667 /*! static cast to \ref a_f64 */
668 #define a_f64_c(x) a_cast_s(A_F64, x)
669 #define a_f64_(_, x) a_cast_s(A_F64 _, x)
670 /*! double precision floating point type. Matches IEEE-754 binary64 format if supported. */
671 #define a_f64 A_F64
674 @addtogroup a_float floating-point number
678 /*! floating-point number bytes */
679 #if !defined A_FLOAT_TYPE
680 #if !defined A_SIZE_FLOAT
681 #define A_FLOAT_TYPE A_FLOAT_DOUBLE
682 #else /* !A_SIZE_FLOAT */
683 #define A_FLOAT_TYPE A_SIZE_FLOAT
684 #endif /* A_SIZE_FLOAT */
685 #endif /* A_FLOAT_TYPE */
686 #define A_FLOAT_SINGLE 0x04
687 #define A_FLOAT_DOUBLE 0x08
688 #define A_FLOAT_EXTEND 0x10
689 #if defined(A_FLOAT)
690 #elif A_FLOAT_TYPE + 0 == A_FLOAT_SINGLE
692 /*! floating-point number stored using `float` */
693 #define A_FLOAT float
694 #define A_FLOAT_DIG FLT_DIG
695 #define A_FLOAT_EPSILON FLT_EPSILON
696 #define A_FLOAT_MANT_DIG FLT_MANT_DIG
697 #define A_FLOAT_MAX FLT_MAX
698 #define A_FLOAT_MAX_10_EXP FLT_MAX_10_EXP
699 #define A_FLOAT_MAX_EXP FLT_MAX_EXP
700 #define A_FLOAT_MIN FLT_MIN
701 #define A_FLOAT_MIN_10_EXP FLT_MIN_10_EXP
702 #define A_FLOAT_MIN_EXP FLT_MIN_EXP
705 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
707 #define A_FLOAT_C(X) A_CAST_2(X, F)
709 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
711 #define A_FLOAT_F(F) A_CAST_2(F, f)
713 /*! format constants for the fprintf family of functions */
714 #define A_FLOAT_PRI(F, C) "%" F C
715 /*! format constants for the fscanf family of functions */
716 #define A_FLOAT_SCN(F, C) "%" F C
718 #elif A_FLOAT_TYPE + 0 == A_FLOAT_DOUBLE
720 /*! floating-point number stored using `double` */
721 #define A_FLOAT double
722 #define A_FLOAT_DIG DBL_DIG
723 #define A_FLOAT_EPSILON DBL_EPSILON
724 #define A_FLOAT_MANT_DIG DBL_MANT_DIG
725 #define A_FLOAT_MAX DBL_MAX
726 #define A_FLOAT_MAX_10_EXP DBL_MAX_10_EXP
727 #define A_FLOAT_MAX_EXP DBL_MAX_EXP
728 #define A_FLOAT_MIN DBL_MIN
729 #define A_FLOAT_MIN_10_EXP DBL_MIN_10_EXP
730 #define A_FLOAT_MIN_EXP DBL_MIN_EXP
733 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
735 #define A_FLOAT_C(X) X
737 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
739 #define A_FLOAT_F(F) F
741 /*! format constants for the fprintf family of functions */
742 #define A_FLOAT_PRI(F, C) "%" F C
743 /*! format constants for the fscanf family of functions */
744 #define A_FLOAT_SCN(F, C) "%" F "l" C
746 #elif A_FLOAT_TYPE + 0 == A_FLOAT_EXTEND
748 /*! floating-point number stored using `long double` */
749 #define A_FLOAT long double
750 #define A_FLOAT_DIG LDBL_DIG
751 #define A_FLOAT_EPSILON LDBL_EPSILON
752 #define A_FLOAT_MANT_DIG LDBL_MANT_DIG
753 #define A_FLOAT_MAX LDBL_MAX
754 #define A_FLOAT_MAX_10_EXP LDBL_MAX_10_EXP
755 #define A_FLOAT_MAX_EXP LDBL_MAX_EXP
756 #define A_FLOAT_MIN LDBL_MIN
757 #define A_FLOAT_MIN_10_EXP LDBL_MIN_10_EXP
758 #define A_FLOAT_MIN_EXP LDBL_MIN_EXP
761 expands to a floating-point constant expression having the value specified by its argument and the type \ref a_float
763 #define A_FLOAT_C(X) A_CAST_2(X, L)
765 expands to a floating-point function expression having the value specified by its argument and the type \ref a_float
767 #define A_FLOAT_F(F) A_CAST_2(F, l)
769 /*! format constants for the fprintf family of functions */
770 #define A_FLOAT_PRI(F, C) "%" F "L" C
771 /*! format constants for the fscanf family of functions */
772 #define A_FLOAT_SCN(F, C) "%" F "L" C
774 #else /* !A_FLOAT_TYPE */
775 #error unsupported precision
776 #endif /* A_FLOAT_TYPE */
778 #define A_FLOAT_INF a_cast_s(A_FLOAT, A_F64_INF)
779 #define A_FLOAT_NAN (A_FLOAT_C(0.0) * A_FLOAT_INF)
781 /*! static cast to \ref a_float */
782 #define a_float_c(x) a_cast_s(A_FLOAT, x)
783 #define a_float_(_, x) a_cast_s(A_FLOAT _, x)
784 /*! compiler built-in floating-point number type */
785 #define a_float A_FLOAT
787 /*! @} a_float */
789 typedef union a_cast
791 a_c8 c;
792 a_int i;
793 a_uint u;
794 a_shrt ih;
795 a_ushrt uh;
796 a_long il;
797 a_ulong ul;
798 #if defined(A_HAVE_LONG_LONG_TYPE)
799 a_llong ill;
800 a_ullong ull;
801 #endif /* A_HAVE_LONG_LONG_TYPE */
802 a_i8 i8;
803 a_u8 u8;
804 a_i16 i16;
805 a_u16 u16;
806 a_i32 i32;
807 a_u32 u32;
808 a_i64 i64;
809 a_u64 u64;
810 a_f32 f32;
811 a_f64 f64;
812 a_imax imax;
813 a_umax umax;
814 a_iptr iptr;
815 a_uptr uptr;
816 a_diff diff;
817 a_size size;
818 void const *PTR;
819 void *ptr;
820 char const *STR;
821 char *str;
822 #if defined(A_FLOAT_TYPE) && (A_FLOAT_TYPE + 0 < A_FLOAT_EXTEND)
823 a_float f;
824 #endif /* A_FLOAT_TYPE */
825 } a_cast;
828 @brief square of x, \f$ x^2 \f$
830 #define A_SQ(x) ((x) * (x))
833 @brief absolute value of x, \f$ |x| \f$
835 #define A_ABS(x) ((x) < 0 ? -(x) : (x))
837 @brief absolute value of f-g, \f$ |f-g| \f$
839 #define A_ABS_(f, g) ((f) < (g) ? (g) - (f) : (f) - (g))
842 @brief minimum value between x and y
844 #define A_MIN(x, y) (((x) < (y)) ? (x) : (y))
847 @brief maximum value between x and y
849 #define A_MAX(x, y) (((x) > (y)) ? (x) : (y))
852 @brief signum function, \f$ \texttt{sgn}(x)=\begin{cases}+1&x>0\\0&0\\-1&x<0\end{cases} \f$
854 #define A_SGN(x) ((0 < (x)) - ((x) < 0))
856 @brief signum function, \f$ \texttt{sgn}(f,g)=\begin{cases}+1&f>g\\0&f=g\\-1&f<g\end{cases} \f$
858 #define A_SGN_(f, g) (((f) > (g)) - ((f) < (g)))
861 @brief saturation value of x, \f$ \texttt{sat}(x,min,max)=\begin{cases}min&min>x\\max&x>max\\x&else\end{cases} \f$
863 #define A_SAT(x, min, max) ((min) < (x) ? (x) < (max) ? (x) : (max) : (min))
866 @brief number of elements in a visible array
867 @param a must be a visible array
869 #define A_LEN(a) (sizeof(a) / sizeof(*(a)))
872 @brief offset of a structure member
873 @param type structure type
874 @param member member variable
876 #if defined(offsetof)
877 #define a_offsetof(type, member) offsetof(type, member)
878 #else /* !offsetof */
879 #define a_offsetof(type, member) a_cast_r(a_size, &a_cast_r(type *, 0)->member)
880 #endif /* offsetof */
883 @brief container of a structure member
884 @param ptr pointer to a member variable
885 @param type structure type
886 @param member member variable
888 #define a_container_of(ptr, type, member) a_cast_r(type *, a_cast_r(a_uptr, ptr) - a_offsetof(type, member))
891 @brief round down size `n` to be a multiple of `a`
893 #define a_size_down(a, n) (a_cast_s(a_size, n) & ~a_cast_s(a_size, (a) - 1))
896 @brief round up size `n` to be a multiple of `a`
898 #define a_size_up(a, n) ((a_cast_s(a_size, n) + (a) - 1) & ~a_cast_s(a_size, (a) - 1))
901 @brief round pointer `p` down to the closest `a`, aligned address <= `p`
903 #define a_align_down(a, p) a_cast_r(void *, a_cast_r(a_uptr, p) & ~a_cast_s(a_uptr, (a) - 1))
906 @brief round pointer `p` up to the closest `a`, aligned address >= `p`
908 #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))
911 @brief iterate from 0 to n and not include n
912 @param I index type of the iteration
913 @param i index variable of the iteration
914 @param n final value of the iteration
916 #define a_forenum(I, i, n) for (I i = 0; i < (n); ++i)
919 @brief iterate from n to 0 and not include n
920 @param I index type of the iteration
921 @param i index variable of the iteration
922 @param n final value of the iteration
924 #define a_forenum_reverse(I, i, n) for (I i = (n); i-- > 0;)
927 @brief iterate over an array
928 @param T the prefix of the element type
929 @param P the suffix of the element type
930 @param it pointer to the current element
931 @param ptr starting address of this array
932 @param num number of elements in this array
934 #define a_foreach(T, P, it, ptr, num) \
935 for (T P it = a_cast_s(T P, ptr), P it##_ = it + (num); it < it##_; ++it)
936 /*! @copydoc a_foreach */
937 #define a_forsafe(T, P, it, ptr, num) \
938 for (T P it = a_cast_s(T P, ptr), P it##_ = (num) ? it + (num) : it; it < it##_; ++it)
941 @brief iterate over an array in reverse
942 @param T the prefix of the element type
943 @param P the suffix of the element type
944 @param it pointer to the current element
945 @param ptr starting address of this array
946 @param num number of elements in this array
948 #define a_foreach_reverse(T, P, it, ptr, num) \
949 for (T P it##_ = a_cast_s(T P, ptr) - 1, P it = it##_ + (num); it > it##_; --it)
950 /*! @copydoc a_foreach_reverse */
951 #define a_forsafe_reverse(T, P, it, ptr, num) \
952 for (T P it##_ = (num) ? a_cast_s(T P, ptr) - 1 : a_cast_s(T P, ptr), \
953 P it = (num) ? it##_ + (num) : it##_; \
954 it > it##_; --it)
957 @brief iterate over an array
958 @param T the prefix of the element type
959 @param P the suffix of the element type
960 @param it pointer to the current element
961 @param ptr starting address of this array
962 @param end the end address of this array
964 #define a_iterate(T, P, it, ptr, end) \
965 for (T P it = a_cast_s(T P, ptr), P it##_ = a_cast_s(T P, end); it < it##_; ++it)
968 @brief iterate over an array in reverse
969 @param T the prefix of the element type
970 @param P the suffix of the element type
971 @param it pointer to the current element
972 @param ptr starting address of this array
973 @param end the end address of this array
975 #define a_iterate_reverse(T, P, it, ptr, end) \
976 for (T P it = a_cast_s(T P, end) - 1, P it##_ = a_cast_s(T P, ptr) - 1; it > it##_; --it)
979 @brief enumeration for return values
981 enum
983 A_SUCCESS /*!< return success */,
984 A_FAILURE /*!< return failure */,
985 A_INVALID /*!< return invalid */
988 #if defined(__cplusplus)
989 extern "C" {
990 #endif /* __cplusplus */
991 #if defined(LIBA_A_C)
992 #undef A_INTERN
993 #define A_INTERN A_INLINE
994 #endif /* LIBA_A_C */
997 @brief reverse the bits in an 8-bit unsigned integer
998 @param x an 8-bit unsigned integer to be reversed
999 @return reversed 8-bit unsigned integer
1001 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1002 A_EXTERN a_u8 a_u8_rev(a_u8 x);
1003 #endif /* A_HAVE_INLINE */
1004 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1005 A_INTERN a_u8 a_u8_rev(a_u8 x)
1007 x = a_cast_s(a_u8, (x >> 4) | (x << 4));
1008 x = a_cast_s(a_u8, ((x & 0xCC) >> 2) | ((x & 0x33) << 2));
1009 x = a_cast_s(a_u8, ((x & 0xAA) >> 1) | ((x & 0x55) << 1));
1010 return x;
1012 #endif /* A_HAVE_INLINE */
1015 @brief reverse the bits in a 16-bit unsigned integer
1016 @param x a 16-bit unsigned integer to be reversed
1017 @return reversed 16-bit unsigned integer
1019 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1020 A_EXTERN a_u16 a_u16_rev(a_u16 x);
1021 #endif /* A_HAVE_INLINE */
1022 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1023 A_INTERN a_u16 a_u16_rev(a_u16 x)
1025 x = a_cast_s(a_u16, (x >> 8) | (x << 8));
1026 x = a_cast_s(a_u16, ((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4));
1027 x = a_cast_s(a_u16, ((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2));
1028 x = a_cast_s(a_u16, ((x & 0xAAAA) >> 1) | ((x & 0x5555) << 1));
1029 return x;
1031 #endif /* A_HAVE_INLINE */
1032 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1033 A_EXTERN A_NONULL((1)) a_u16 a_u16_getl(void const *b);
1034 #endif /* A_HAVE_INLINE */
1035 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1036 A_INTERN A_NONULL((1)) a_u16 a_u16_getl(void const *b)
1038 a_byte const *p = a_cast_s(a_byte const *, b);
1039 return a_cast_s(a_u16, (p[0] << 0) | (p[1] << 8));
1041 #endif /* A_HAVE_INLINE */
1042 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1043 A_EXTERN A_NONULL((1)) a_u16 a_u16_getb(void const *b);
1044 #endif /* A_HAVE_INLINE */
1045 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1046 A_INTERN A_NONULL((1)) a_u16 a_u16_getb(void const *b)
1048 a_byte const *p = a_cast_s(a_byte const *, b);
1049 return a_cast_s(a_u16, (p[1] << 0) | (p[0] << 8));
1051 #endif /* A_HAVE_INLINE */
1052 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1053 A_EXTERN A_NONULL((1)) void a_u16_setl(void *b, a_u16 x);
1054 #endif /* A_HAVE_INLINE */
1055 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1056 A_INTERN A_NONULL((1)) void a_u16_setl(void *b, a_u16 x)
1058 a_byte *p = a_cast_s(a_byte *, b);
1059 p[0] = a_cast_s(a_byte, x >> 0);
1060 p[1] = a_cast_s(a_byte, x >> 8);
1062 #endif /* A_HAVE_INLINE */
1063 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1064 A_EXTERN A_NONULL((1)) void a_u16_setb(void *b, a_u16 x);
1065 #endif /* A_HAVE_INLINE */
1066 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1067 A_INTERN A_NONULL((1)) void a_u16_setb(void *b, a_u16 x)
1069 a_byte *p = a_cast_s(a_byte *, b);
1070 p[0] = a_cast_s(a_byte, x >> 8);
1071 p[1] = a_cast_s(a_byte, x >> 0);
1073 #endif /* A_HAVE_INLINE */
1076 @brief reverse the bits in a 32-bit unsigned integer
1077 @param x a 32-bit unsigned integer to be reversed
1078 @return reversed 32-bit unsigned integer
1080 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1081 A_EXTERN a_u32 a_u32_rev(a_u32 x);
1082 #endif /* A_HAVE_INLINE */
1083 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1084 A_INTERN a_u32 a_u32_rev(a_u32 x)
1086 x = (x >> 16) | (x << 16);
1087 x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
1088 x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
1089 x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
1090 x = ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
1091 return x;
1093 #endif /* A_HAVE_INLINE */
1094 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1095 A_EXTERN A_NONULL((1)) a_u32 a_u32_getl(void const *b);
1096 #endif /* A_HAVE_INLINE */
1097 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1098 A_INTERN A_NONULL((1)) a_u32 a_u32_getl(void const *b)
1100 a_byte const *p = a_cast_s(a_byte const *, b);
1101 return (a_cast_s(a_u32, p[0]) << 0x00) |
1102 (a_cast_s(a_u32, p[1]) << 0x08) |
1103 (a_cast_s(a_u32, p[2]) << 0x10) |
1104 (a_cast_s(a_u32, p[3]) << 0x18);
1106 #endif /* A_HAVE_INLINE */
1107 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1108 A_EXTERN A_NONULL((1)) a_u32 a_u32_getb(void const *b);
1109 #endif /* A_HAVE_INLINE */
1110 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1111 A_INTERN A_NONULL((1)) a_u32 a_u32_getb(void const *b)
1113 a_byte const *p = a_cast_s(a_byte const *, b);
1114 return (a_cast_s(a_u32, p[0]) << 0x18) |
1115 (a_cast_s(a_u32, p[1]) << 0x10) |
1116 (a_cast_s(a_u32, p[2]) << 0x08) |
1117 (a_cast_s(a_u32, p[3]) << 0x00);
1119 #endif /* A_HAVE_INLINE */
1120 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1121 A_EXTERN A_NONULL((1)) void a_u32_setl(void *b, a_u32 x);
1122 #endif /* A_HAVE_INLINE */
1123 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1124 A_INTERN A_NONULL((1)) void a_u32_setl(void *b, a_u32 x)
1126 a_byte *p = a_cast_s(a_byte *, b);
1127 p[0] = a_cast_s(a_byte, x >> 0x00);
1128 p[1] = a_cast_s(a_byte, x >> 0x08);
1129 p[2] = a_cast_s(a_byte, x >> 0x10);
1130 p[3] = a_cast_s(a_byte, x >> 0x18);
1132 #endif /* A_HAVE_INLINE */
1133 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1134 A_EXTERN A_NONULL((1)) void a_u32_setb(void *b, a_u32 x);
1135 #endif /* A_HAVE_INLINE */
1136 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1137 A_INTERN A_NONULL((1)) void a_u32_setb(void *b, a_u32 x)
1139 a_byte *p = a_cast_s(a_byte *, b);
1140 p[0] = a_cast_s(a_byte, x >> 0x18);
1141 p[1] = a_cast_s(a_byte, x >> 0x10);
1142 p[2] = a_cast_s(a_byte, x >> 0x08);
1143 p[3] = a_cast_s(a_byte, x >> 0x00);
1145 #endif /* A_HAVE_INLINE */
1148 @brief reverse the bits in a 64-bit unsigned integer
1149 @param x a 64-bit unsigned integer to be reversed
1150 @return reversed 64-bit unsigned integer
1152 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1153 A_EXTERN a_u64 a_u64_rev(a_u64 x);
1154 #endif /* A_HAVE_INLINE */
1155 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1156 A_INTERN a_u64 a_u64_rev(a_u64 x)
1158 x = (x >> 32) | (x << 32);
1159 x = ((x & 0xFFFF0000FFFF0000) >> 0x10) | ((x & 0x0000FFFF0000FFFF) << 0x10);
1160 x = ((x & 0xFF00FF00FF00FF00) >> 0x08) | ((x & 0x00FF00FF00FF00FF) << 0x08);
1161 x = ((x & 0xF0F0F0F0F0F0F0F0) >> 0x04) | ((x & 0x0F0F0F0F0F0F0F0F) << 0x04);
1162 x = ((x & 0xCCCCCCCCCCCCCCCC) >> 0x02) | ((x & 0x3333333333333333) << 0x02);
1163 x = ((x & 0xAAAAAAAAAAAAAAAA) >> 0x01) | ((x & 0x5555555555555555) << 0x01);
1164 return x;
1166 #endif /* A_HAVE_INLINE */
1167 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1168 A_EXTERN A_NONULL((1)) a_u64 a_u64_getl(void const *b);
1169 #endif /* A_HAVE_INLINE */
1170 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1171 A_INTERN A_NONULL((1)) a_u64 a_u64_getl(void const *b)
1173 a_byte const *p = a_cast_s(a_byte const *, b);
1174 return (a_cast_s(a_u64, p[0]) << 0x00) |
1175 (a_cast_s(a_u64, p[1]) << 0x08) |
1176 (a_cast_s(a_u64, p[2]) << 0x10) |
1177 (a_cast_s(a_u64, p[3]) << 0x18) |
1178 (a_cast_s(a_u64, p[4]) << 0x20) |
1179 (a_cast_s(a_u64, p[5]) << 0x28) |
1180 (a_cast_s(a_u64, p[6]) << 0x30) |
1181 (a_cast_s(a_u64, p[7]) << 0x38);
1183 #endif /* A_HAVE_INLINE */
1184 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1185 A_EXTERN A_NONULL((1)) a_u64 a_u64_getb(void const *b);
1186 #endif /* A_HAVE_INLINE */
1187 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1188 A_INTERN A_NONULL((1)) a_u64 a_u64_getb(void const *b)
1190 a_byte const *p = a_cast_s(a_byte const *, b);
1191 return (a_cast_s(a_u64, p[0]) << 0x38) |
1192 (a_cast_s(a_u64, p[1]) << 0x30) |
1193 (a_cast_s(a_u64, p[2]) << 0x28) |
1194 (a_cast_s(a_u64, p[3]) << 0x20) |
1195 (a_cast_s(a_u64, p[4]) << 0x18) |
1196 (a_cast_s(a_u64, p[5]) << 0x10) |
1197 (a_cast_s(a_u64, p[6]) << 0x08) |
1198 (a_cast_s(a_u64, p[7]) << 0x00);
1200 #endif /* A_HAVE_INLINE */
1201 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1202 A_EXTERN A_NONULL((1)) void a_u64_setl(void *b, a_u64 x);
1203 #endif /* A_HAVE_INLINE */
1204 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1205 A_INTERN A_NONULL((1)) void a_u64_setl(void *b, a_u64 x)
1207 a_byte *p = a_cast_s(a_byte *, b);
1208 p[0] = a_cast_s(a_byte, x >> 0x00);
1209 p[1] = a_cast_s(a_byte, x >> 0x08);
1210 p[2] = a_cast_s(a_byte, x >> 0x10);
1211 p[3] = a_cast_s(a_byte, x >> 0x18);
1212 p[4] = a_cast_s(a_byte, x >> 0x20);
1213 p[5] = a_cast_s(a_byte, x >> 0x28);
1214 p[6] = a_cast_s(a_byte, x >> 0x30);
1215 p[7] = a_cast_s(a_byte, x >> 0x38);
1217 #endif /* A_HAVE_INLINE */
1218 #if !defined A_HAVE_INLINE || defined(LIBA_A_C)
1219 A_EXTERN A_NONULL((1)) void a_u64_setb(void *b, a_u64 x);
1220 #endif /* A_HAVE_INLINE */
1221 #if defined(A_HAVE_INLINE) || defined(LIBA_A_C)
1222 A_INTERN A_NONULL((1)) void a_u64_setb(void *b, a_u64 x)
1224 a_byte *p = a_cast_s(a_byte *, b);
1225 p[0] = a_cast_s(a_byte, x >> 0x38);
1226 p[1] = a_cast_s(a_byte, x >> 0x30);
1227 p[2] = a_cast_s(a_byte, x >> 0x28);
1228 p[3] = a_cast_s(a_byte, x >> 0x20);
1229 p[4] = a_cast_s(a_byte, x >> 0x18);
1230 p[5] = a_cast_s(a_byte, x >> 0x10);
1231 p[6] = a_cast_s(a_byte, x >> 0x08);
1232 p[7] = a_cast_s(a_byte, x >> 0x00);
1234 #endif /* A_HAVE_INLINE */
1237 @brief copy one buffer to another
1238 @param[in] dst pointer to the object to copy to
1239 @param[in] src pointer to the object to copy from
1240 @param[in] siz number of bytes to copy
1241 @return a copy of dest
1243 A_EXTERN A_NONULL((1, 2)) void *a_copy(void *__restrict dst, void const *__restrict src, a_size siz);
1246 @brief move one buffer to another
1247 @param[in] dst pointer to the object to copy to
1248 @param[in] src pointer to the object to copy from
1249 @param[in] siz number of bytes to copy
1250 @return a copy of dest
1252 A_EXTERN A_NONULL((1, 2)) void *a_move(void *dst, void const *src, a_size siz);
1255 @brief fill a buffer with a character
1256 @param[in] ptr pointer to the object to fill
1257 @param[in] siz number of bytes to fill
1258 @param[in] val fill byte
1259 @return a copy of dest
1261 A_EXTERN A_NONULL((1)) void *a_fill(void *ptr, a_size siz, int val);
1264 @brief fill a buffer with zero
1265 @param[in] ptr pointer to the object to fill
1266 @param[in] siz number of bytes to fill
1267 @return a copy of dest
1269 A_EXTERN A_NONULL((1)) void *a_zero(void *ptr, a_size siz);
1272 @brief swap two different memory blocks of the same size
1273 @param[in] siz the size of memory block being swapped
1274 @param[in,out] lhs points to memory block on the left
1275 @param[in,out] rhs points to memory block on the right
1277 A_EXTERN A_NONULL((1, 2)) void a_swap(void *lhs, void *rhs, a_size siz);
1280 @brief a hash function whose prime number is 131
1281 @param[in] str string to be processed
1282 @param[in] val initial value
1283 @return hash value
1285 A_EXTERN a_u32 a_hash_bkdr(void const *str, a_u32 val);
1288 @brief a hash function whose prime number is 131
1289 @param[in] ptr points to string to be processed
1290 @param[in] siz length of string to be processed
1291 @param[in] val initial value
1292 @return hash value
1294 A_EXTERN a_u32 a_hash_bkdr_(void const *ptr, a_size siz, a_u32 val);
1297 @brief a hash function whose prime number is 65599
1298 @param[in] str string to be processed
1299 @param[in] val initial value
1300 @return hash value
1302 A_EXTERN a_u32 a_hash_sdbm(void const *str, a_u32 val);
1305 @brief a hash function whose prime number is 65599
1306 @param[in] ptr points to string to be processed
1307 @param[in] siz length of string to be processed
1308 @param[in] val initial value
1309 @return hash value
1311 A_EXTERN a_u32 a_hash_sdbm_(void const *ptr, a_size siz, a_u32 val);
1314 @brief roll back the elements of a float array and save the value
1315 @param[in] p points to a float array
1316 @param[in] n number of a float array
1317 @param[in] x value
1319 A_EXTERN void a_float_push(a_float *p, a_size n, a_float x);
1322 @brief roll back the elements of a float array and save the cache array
1323 @param[in] block_p points to a float array
1324 @param[in] block_n number of a float array
1325 @param[in] cache_p points to a cache array
1326 @param[in] cache_n number of a cache array
1328 A_EXTERN void a_float_push_(a_float *block_p, a_size block_n,
1329 a_float const *cache_p, a_size cache_n);
1332 @brief roll back the elements of a float array circularly
1333 @param[in] p points to a float array
1334 @param[in] n number of a float array
1336 A_EXTERN void a_float_roll(a_float *p, a_size n);
1339 @brief roll back the elements of a float array circularly, array>>shift
1340 @param[in] block_p points to a float array
1341 @param[in] block_n number of a float array
1342 @param[in] shift_p points to a shift array
1343 @param[in] shift_n number of a shift array
1345 A_EXTERN void a_float_roll_(a_float *block_p, a_size block_n,
1346 a_float *shift_p, a_size shift_n);
1349 @brief calculate the mean of a float array
1350 @param[in] p points to a float array
1351 @param[in] n number of a float array
1352 @return mean of a float array
1354 A_EXTERN a_float a_float_mean(a_float const *p, a_size n);
1357 @brief calculate the sum of a float array
1358 @param[in] p points to a float array
1359 @param[in] n number of a float array
1360 @return sum of a float array
1362 A_EXTERN a_float a_float_sum(a_float const *p, a_size n);
1365 @brief calculate the absolute sum of a float array
1366 @param[in] p points to a float array
1367 @param[in] n number of a float array
1368 @return absolute sum of a float array
1370 A_EXTERN a_float a_float_sum1(a_float const *p, a_size n);
1373 @brief calculate the sum of squares of a float array
1374 @param[in] p points to a float array
1375 @param[in] n number of a float array
1376 @return sum of squares of a float array
1378 A_EXTERN a_float a_float_sum2(a_float const *p, a_size n);
1381 @brief allocation function pointer
1382 @param[in] addr address of memory block
1383 @param[in] size new size of memory block
1384 @return new address of memory block or null
1386 A_EXTERN void *(*a_alloc)(void *addr, a_size size);
1389 @brief default allocation function
1390 @param[in] addr address of memory block
1391 @param[in] size new size of memory block
1392 @return new address of memory block or null
1394 A_EXTERN void *a_alloc_(void *addr, a_size size);
1396 #if defined(LIBA_A_C)
1397 #undef A_INTERN
1398 #define A_INTERN static A_INLINE
1399 #endif /* LIBA_A_C */
1400 #if defined(__cplusplus)
1401 } /* extern "C" */
1402 #endif /* __cplusplus */
1404 /*! @brief declare allocation function */
1405 #define A_ALLOC(alloc, addr, size) void *alloc(void *addr, a_size size)
1406 #define a_new(T, ptr, num) a_cast_s(T *, a_alloc(ptr, sizeof(T) * (num)))
1407 #define a_die(ptr) a_alloc(ptr, 0)
1409 /*! @} liba */
1411 #endif /* a/a.h */