poulpy/spqlios/lib/spqlios/commons_private.h
spqlios/lib/spqlios/commons_private.hRaw
1#ifndef SPQLIOS_COMMONS_PRIVATE_H
2#define SPQLIOS_COMMONS_PRIVATE_H
3
4#include "commons.h"
5
6#ifdef __cplusplus
7#include <cmath>
8#include <cstdio>
9#include <cstdlib>
10#else
11#include <math.h>
12#include <stdio.h>
13#include <stdlib.h>
14#define nullptr 0x0;
15#endif
16
17/** @brief log2 of a power of two (UB if m is not a power of two) */
18EXPORT uint32_t log2m(uint32_t m);
19
20/** @brief checks if the doublevalue is a power of two */
21EXPORT uint64_t is_not_pow2_double(void* doublevalue);
22
23#define UNDEFINED() \
24 { \
25 fprintf(stderr, "UNDEFINED!!!\n"); \
26 abort(); \
27 }
28#define NOT_IMPLEMENTED() \
29 { \
30 fprintf(stderr, "NOT IMPLEMENTED!!!\n"); \
31 abort(); \
32 }
33#define NOT_SUPPORTED() \
34 { \
35 fprintf(stderr, "NOT SUPPORTED!!!\n"); \
36 abort(); \
37 }
38#define FATAL_ERROR(MESSAGE) \
39 { \
40 fprintf(stderr, "ERROR: %s\n", (MESSAGE)); \
41 abort(); \
42 }
43
44#define STATIC_ASSERT(condition) (void)sizeof(char[-1 + 2 * !!(condition)])
45
46/** @brief reports the error and returns nullptr */
47EXPORT void* spqlios_error(const char* error);
48/** @brief if ptr2 is not null, returns ptr, otherwise free ptr and return null */
49EXPORT void* spqlios_keep_or_free(void* ptr, void* ptr2);
50
51#ifdef __x86_64__
52#define CPU_SUPPORTS __builtin_cpu_supports
53#else
54// TODO for now, we do not have any optimization for non x86 targets
55#define CPU_SUPPORTS(xxxx) 0
56#endif
57
58/** @brief returns the n bits of value in reversed order */
59EXPORT uint32_t revbits(uint32_t nbits, uint32_t value);
60
61/**
62 * @brief this computes the sequence: 0,1/2,1/4,3/4,1/8,5/8,3/8,7/8,...
63 * essentially: the bits of (i+1) in lsb order on the basis (1/2^k) mod 1*/
64EXPORT double fracrevbits(uint32_t i);
65
66/** @brief smallest multiple of 64 higher or equal to size */
67EXPORT uint64_t ceilto64b(uint64_t size);
68
69/** @brief smallest multiple of 32 higher or equal to size */
70EXPORT uint64_t ceilto32b(uint64_t size);
71
72#endif // SPQLIOS_COMMONS_PRIVATE_H