5 #ifndef SPVEC_ARRAYTOOLS_H 6 #define SPVEC_ARRAYTOOLS_H 31 static inline void alloc(
const size_t len, T **x)
33 *x = (T*) std::malloc(len*
sizeof(T));
45 static inline void zero_alloc(
const size_t len, T **x)
47 *x = (T*) std::calloc(len,
sizeof(T));
60 static inline void realloc(
const size_t len, T **x)
62 void *realloc_ptr = std::realloc(*x, len*
sizeof(T));
63 if (realloc_ptr == NULL)
66 *x = (T*) realloc_ptr;
77 static inline void free(T *x)
94 static inline void copy(
const size_t len,
const T *src, T *dst)
96 std::memcpy(dst, src, len*
sizeof(*src));
99 template <
typename SRC,
typename DST>
100 static inline void copy(
const size_t len,
const SRC *src, DST *dst)
103 for (
size_t i=0; i<len; i++)
104 dst[i] = (DST) src[i];
115 template <
typename T>
116 static inline void zero(
const size_t len, T *x)
118 std::memset(x, 0, len*
sizeof(*x));
125 static const int ALLOC_FAILED = -1;
126 static const int ALLOC_OK = 0;
130 static inline int check_alloc()
135 template <
typename T>
136 static inline int check_alloc(T *x)
144 template <
typename T,
typename... VAT>
145 static inline int check_alloc(T *x, VAT... vax)
147 return check_alloc(x) + check_alloc(vax ...);
152 static inline void vafree(){}
154 template <
typename T,
typename... VAT>
155 static inline void vafree(T *x, VAT... vax)
171 template <
typename T,
typename... VAT>
172 static inline void check_allocs(T *x, VAT... vax)
174 int check = check_alloc(x) + check_alloc(vax ...);
176 if (check != ALLOC_OK)
181 throw std::bad_alloc();
193 const __half eps_hlf = 1e-2;
195 const float eps_flt = 1e-4;
196 const double eps_dbl = 1e-8;
200 const __half min_normal_hlf = 0x1p-14;
202 const float min_normal_flt = 0x1p-126;
203 const double min_normal_dbl = 0x1p-1022;
206 const __half max_hlf = 1>>15;
208 const float max_flt = FLT_MAX;
209 const double max_dbl = DBL_MAX;
214 static inline __half fabsh(
const __half x)
216 return __float2half(fabsf(__half2float(x)));
222 static inline bool subnormal(
const float x)
224 return (x < min_normal_flt);
227 static inline bool subnormal(
const double x)
229 return (x < min_normal_dbl);
234 template <
typename T>
235 static inline bool abseq(
const T x,
const T y)
237 float fx = (float) x;
238 float fy = (float) y;
239 return fabsf(fx - fy) < (eps_flt * min_normal_flt);
242 static inline bool abseq(
const float x,
const float y)
244 return fabsf(x - y) < (eps_flt * min_normal_flt);
247 static inline bool abseq(
const double x,
const double y)
249 return fabs(x - y) < (eps_dbl * min_normal_dbl);
254 template <
typename T>
255 static inline bool releq(
const T x,
const T y)
257 float fx = (float) x;
258 float fy = (float) y;
259 return fabsf(fx - fy) / std::min(fabsf(fx)+fabsf(fy), max_flt) < eps_flt;
262 static inline bool releq(
const float x,
const float y)
264 return fabsf(x - y) / std::min(fabsf(x)+fabsf(y), max_flt) < eps_flt;
267 static inline bool releq(
const double x,
const double y)
269 return fabs(x - y) / std::min(fabs(x)+fabs(y), max_dbl) < eps_dbl;
276 template <
typename REAL>
277 static inline bool eq(
const REAL x,
const REAL y)
281 else if (x == 0.f || y == 0.f || subnormal(fabs(x)+fabs(y)))
289 static inline bool eq(
const int x,
const int y)
296 template <
typename REAL>
297 static inline bool eq(
const REAL x,
const int y)
299 return eq(x, (REAL) y);
302 template <
typename REAL>
303 static inline bool eq(
const int x,
const REAL y)
305 return eq((REAL) x, y);
310 static inline bool eq(
const float x,
double y)
312 return eq(x, (
float) y);
315 static inline bool eq(
const double x,
const float y)
317 return eq((
float) x, y);
331 template <
typename TA,
typename TB>
332 static inline size_t cmp_firstmiss(
const size_t len,
const TA *a,
const TB *b)
336 #pragma omp simd reduction(min:ret) 337 for (
size_t i=0; i<len; i++)
339 if (!fltcmp::eq(a[i], b[i]))
356 template <
typename TA,
typename TB>
357 static inline bool cmp(
const size_t len,
const TA *a,
const TB *b)
359 size_t fm = cmp_firstmiss(len, a, b);
360 return (fm == len ?
true :
false);