create functions a_float_sum{,sq,abs}
[liba.git] / src / a.c
blob54988ab3c3d787fd5f6f355045ef6dea47a625e7
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 byte; siz; --siz, ++lhs, ++rhs)
23 byte = *lhs;
24 *lhs = *rhs;
25 *rhs = byte;
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 for (; siz; --siz, ++ptr)
47 val = val * 131 + *ptr;
49 return val;
52 a_u32 a_hash_sdbm(void const *str_, a_u32 val)
54 a_byte const *str = (a_byte const *)str_;
55 if (str)
57 for (; *str; ++str)
59 val = val * 65599 + *str;
62 return val;
65 a_u32 a_hash_sdbm_(void const *ptr_, a_size siz, a_u32 val)
67 a_byte const *ptr = (a_byte const *)ptr_;
68 for (; siz; --siz, ++ptr)
70 val = val * 65599 + *ptr;
72 return val;
75 void a_float_push(a_float *block_p, a_size block_n,
76 a_float const *cache_p, a_size cache_n)
78 a_size const n = A_MIN(cache_n, block_n);
79 for (a_size t = block_n, s = block_n - n; s;)
81 block_p[--t] = block_p[--s];
83 for (a_size i = 0; i != n; ++i)
85 block_p[i] = cache_p[i];
89 void a_float_roll(a_float *block_p, a_size block_n,
90 a_float *shift_p, a_size shift_n)
92 a_size const shift = shift_n % block_n;
93 a_size const start = block_n - shift;
94 for (a_size t = 0, s = start; t != shift;)
96 shift_p[t++] = block_p[s++];
98 for (a_size t = block_n, s = start; s;)
100 block_p[--t] = block_p[--s];
102 for (a_size i = 0; i != shift; ++i)
104 block_p[i] = shift_p[i];
108 a_float a_float_mean(a_float const *p, a_size n)
110 a_float res = 0;
111 if (n)
113 a_float const inv = 1 / (a_float)n;
114 for (; n; --n, ++p) { res += *p * inv; }
116 return res;
119 a_float a_float_sum(a_float const *p, a_size n)
121 a_float res = 0;
122 for (; n; --n, ++p) { res += *p; }
123 return res;
126 a_float a_float_sumsq(a_float const *p, a_size n)
128 a_float res = 0;
129 for (; n; --n, ++p) { res += A_SQ(*p); }
130 return res;
133 a_float a_float_sumabs(a_float const *p, a_size n)
135 a_float res = 0;
136 for (; n; --n, ++p) { res += A_ABS(*p); }
137 return res;
140 A_ALLOC((*a_alloc), addr, size) = a_alloc_;
141 A_ALLOC(a_alloc_, addr, size)
143 if (size)
145 if (addr)
147 return realloc(addr, size);
149 return malloc(size);
151 free(addr);
152 return A_NULL;