remove the commit message of peaceiris/actions-gh-pages
[liba.git] / src / a.c
blob30e1de607a80d0209df593fb04ae9d9187c75005
1 #define LIBA_A_C
2 #include "a/a.h"
3 #include <string.h>
4 #include <stdlib.h>
5 #if defined(A_HAVE_MIMALLOC_H)
6 #include <mimalloc-override.h>
7 #endif /* A_HAVE_MIMALLOC_H */
9 void *a_copy(void *__restrict dst, void const *__restrict src, a_size siz) { return memcpy(dst, src, siz); }
11 void *a_move(void *dst, void const *src, a_size siz) { return memmove(dst, src, siz); }
13 void *a_fill(void *ptr, a_size siz, int val) { return memset(ptr, val, siz); }
15 void *a_zero(void *ptr, a_size siz) { return memset(ptr, 0, siz); }
17 void a_swap(void *_lhs, void *_rhs, a_size siz)
19 a_byte *lhs = (a_byte *)_lhs;
20 a_byte *rhs = (a_byte *)_rhs;
21 for (a_byte buf; siz; --siz, ++lhs, ++rhs)
23 buf = *lhs;
24 *lhs = *rhs;
25 *rhs = buf;
29 a_u32 a_hash_bkdr(void const *_str, a_u32 val)
31 a_byte const *str = (a_byte const *)_str;
32 if (str)
34 for (; *str; ++str)
36 val = val * 131 + *str;
39 return val;
42 a_u32 a_hash_bkdr_(void const *_ptr, a_size siz, a_u32 val)
44 a_byte const *ptr = (a_byte const *)_ptr;
45 if (ptr && siz)
47 for (; siz; --siz, ++ptr)
49 val = val * 131 + *ptr;
52 return val;
55 a_u32 a_hash_sdbm(void const *_str, a_u32 val)
57 a_byte const *str = (a_byte const *)_str;
58 if (str)
60 for (; *str; ++str)
62 val = val * 65599 + *str;
65 return val;
68 a_u32 a_hash_sdbm_(void const *_ptr, a_size siz, a_u32 val)
70 a_byte const *ptr = (a_byte const *)_ptr;
71 if (ptr && siz)
73 for (; siz; --siz, ++ptr)
75 val = val * 65599 + *ptr;
78 return val;
81 void a_float_push(a_float *block_p, a_size block_n,
82 a_float const *cache_p, a_size cache_n)
84 a_size const n = A_MIN(cache_n, block_n);
85 for (a_size t = block_n, s = block_n - n; s;)
87 block_p[--t] = block_p[--s];
89 for (a_size i = 0; i != n; ++i)
91 block_p[i] = cache_p[i];
95 void a_float_roll(a_float *block_p, a_size block_n,
96 a_float *shift_p, a_size shift_n)
98 a_size const shift = shift_n % block_n;
99 a_size const start = block_n - shift;
100 for (a_size t = 0, s = start; t != shift;)
102 shift_p[t++] = block_p[s++];
104 for (a_size t = block_n, s = start; s;)
106 block_p[--t] = block_p[--s];
108 for (a_size i = 0; i != shift; ++i)
110 block_p[i] = shift_p[i];
114 A_ALLOC((*a_alloc), addr, size) = a_alloc_;
115 A_ALLOC(a_alloc_, addr, size)
117 if (size)
119 if (addr)
121 return realloc(addr, size);
123 return malloc(size);
125 free(addr);
126 return A_NULL;