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
)
29 a_u32
a_hash_bkdr(void const *str_
, a_u32 val
)
31 a_byte
const *str
= (a_byte
const *)str_
;
36 val
= val
* 131 + *str
;
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
;
52 a_u32
a_hash_sdbm(void const *str_
, a_u32 val
)
54 a_byte
const *str
= (a_byte
const *)str_
;
59 val
= val
* 65599 + *str
;
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
;
75 void a_float_push_fore(a_float
*p
, a_size n
, a_float x
)
79 a_move(p
+ 1, p
, sizeof(a_float
) * n
);
84 void a_float_push_back(a_float
*p
, a_size n
, a_float x
)
88 a_move(p
, p
+ 1, sizeof(a_float
) * n
);
93 void a_float_push_fore_(a_float
*block_p
, a_size block_n
,
94 a_float
const *cache_p
, a_size cache_n
)
96 a_size
const n
= A_MIN(cache_n
, block_n
);
99 a_size
const m
= block_n
- n
;
100 cache_p
= cache_p
+ cache_n
- n
;
101 a_move(block_p
+ n
, block_p
, sizeof(a_float
) * m
);
102 a_copy(block_p
, cache_p
, sizeof(a_float
) * n
);
106 void a_float_push_back_(a_float
*block_p
, a_size block_n
,
107 a_float
const *cache_p
, a_size cache_n
)
109 a_size
const n
= A_MIN(cache_n
, block_n
);
112 a_size
const m
= block_n
- n
;
113 cache_p
= cache_p
+ cache_n
- n
;
114 a_move(block_p
, block_p
+ n
, sizeof(a_float
) * m
);
115 a_copy(block_p
+ m
, cache_p
, sizeof(a_float
) * n
);
119 void a_float_roll_fore(a_float
*p
, a_size n
)
124 a_move(p
, p
+ 1, sizeof(a_float
) * n
);
129 void a_float_roll_back(a_float
*p
, a_size n
)
134 a_move(p
+ 1, p
, sizeof(a_float
) * n
);
139 void a_float_roll_fore_(a_float
*block_p
, a_size block_n
,
140 a_float
*shift_p
, a_size shift_n
)
142 a_size
const shift
= shift_n
% block_n
;
143 a_size
const start
= block_n
- shift
;
144 a_copy(shift_p
, block_p
, sizeof(a_float
) * shift
);
145 a_move(block_p
, block_p
+ shift
, sizeof(a_float
) * start
);
146 a_copy(block_p
+ start
, shift_p
, sizeof(a_float
) * shift
);
149 void a_float_roll_back_(a_float
*block_p
, a_size block_n
,
150 a_float
*shift_p
, a_size shift_n
)
152 a_size
const shift
= shift_n
% block_n
;
153 a_size
const start
= block_n
- shift
;
154 a_copy(shift_p
, block_p
+ start
, sizeof(a_float
) * shift
);
155 a_move(block_p
+ shift
, block_p
, sizeof(a_float
) * start
);
156 a_copy(block_p
, shift_p
, sizeof(a_float
) * shift
);
159 a_float
a_float_mean(a_float
const *p
, a_size n
)
162 a_float
const inv
= 1 / (a_float
)n
;
163 for (; n
; --n
, ++p
) { res
+= *p
* inv
; }
167 a_float
a_float_sum(a_float
const *p
, a_size n
)
170 for (; n
; --n
, ++p
) { res
+= *p
; }
174 a_float
a_float_sum1(a_float
const *p
, a_size n
)
177 for (; n
; --n
, ++p
) { res
+= A_ABS(*p
); }
181 a_float
a_float_sum2(a_float
const *p
, a_size n
)
184 for (; n
; --n
, ++p
) { res
+= A_SQ(*p
); }
188 A_ALLOC((*a_alloc
), addr
, size
) = a_alloc_
;
189 A_ALLOC(a_alloc_
, addr
, size
)
195 return realloc(addr
, size
);