bitmagic-sys 0.2.4+bitmagic.7.7.7

Low-level bindings for the bitmagic.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

For more information please visit:  http://bitmagic.io
*/

/*! \file bmdef.h
    \brief Definitions(internal)
*/

#include <climits>
#include <stdint.h>

// Incorporate appropriate tuneups when the NCBI C++ Toolkit's core
// headers have been included.
//
#ifdef NCBI_ASSERT
#  define BM_ASSERT _ASSERT

#  ifdef HAVE_RESTRICT_CXX
#    define BM_HASRESTRICT
#    define BMRESTRICT NCBI_RESTRICT
#  endif

#  if defined(NCBI_FORCEINLINE)  &&  \
    ( !defined(NCBI_COMPILER_GCC)  ||  NCBI_COMPILER_VERSION >= 400  || \
      defined(__OPTIMIZE__))
#    define BM_HASFORCEINLINE
#    define BMFORCEINLINE NCBI_FORCEINLINE
#  endif

#  ifdef NCBI_SSE
#    if NCBI_SSE >= 20
#      define BMSSE2OPT 1
#    endif
#    if NCBI_SSE >= 40
#      define BMSSE2OPT 1
#    endif
#    if NCBI_SSE >= 42
#      define BMSSE42OPT 1
#    endif
#  endif
#endif


// macro to define/undefine unaligned memory access (x86, PowerPC)
//
#if defined(__i386) || defined(__x86_64) || defined(__ppc__) || \
    defined(__ppc64__) || defined(_M_IX86) || defined(_M_AMD64) || \
    defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
    defined(_M_ARM) || defined(_M_ARM64) || \
    defined(__arm__) || defined(__aarch64__) || \
    (defined(_M_MPPC) && !defined(BM_FORBID_UNALIGNED_ACCESS))
#define BM_UNALIGNED_ACCESS_OK 1
#endif

#if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
    defined(__i386) || defined(__x86_64) || defined(_M_AMD64) || \
    defined(BMSSE2OPT) || defined(BMSSE42OPT)
#define BM_x86
#endif

// cxx11 features
//
#if defined(BM_NO_CXX11) || (defined(_MSC_VER)  &&  _MSC_VER < 1900)
# define BMNOEXCEPT
# define BMNOEXCEPT2
#else
# ifndef BMNOEXCEPT
#  define BMNOEXCEPT noexcept
#if defined(__EMSCRIPTEN__)
#else
#  define BMNOEXCEPT2
#endif
# endif
#endif

// WebASM compilation settings
//
#if defined(__EMSCRIPTEN__)

#undef BM_x86

// EMSCRIPTEN specific tweaks
// WebAssemply compiles into 32-bit memory system but offers 64-bit wordsize
// WebASM also benefits from use GCC extensions (buildins like popcnt, lzcnt)
//
// BMNOEXCEPT2 is to declare "noexcept" for WebAsm only where needed
// and silence GCC warnings
//
# define BM64OPT
# define BM_USE_GCC_BUILD
# define BMNOEXCEPT2 noexcept

#else
#  define BMNOEXCEPT2
#endif


// Enable MSVC 8.0 (2005) specific optimization options
//
#if(_MSC_VER >= 1400)
#  define BM_HASFORCEINLINE
#  ifndef BMRESTRICT
#    define BMRESTRICT __restrict
#  endif
#endif 

#ifdef __GNUG__
#  ifndef BMRESTRICT
#    define BMRESTRICT __restrict__
#  endif

#  ifdef __OPTIMIZE__
#    define BM_NOASSERT
#  endif
#endif

# ifdef NDEBUG
#    define BM_NOASSERT
# endif


#ifndef BM_ASSERT
# ifndef BM_NOASSERT
#  include <cassert>
#  define BM_ASSERT assert
# else
#  ifndef BM_ASSERT
#    define BM_ASSERT(x)
#  endif
# endif
#endif


#if defined(__x86_64) || defined(_M_AMD64) || defined(_WIN64) || \
    defined(__LP64__) || defined(_LP64) || ( __WORDSIZE == 64 )
#ifndef BM64OPT
# define BM64OPT
#endif
#endif



#define FULL_BLOCK_REAL_ADDR bm::all_set<true>::_block._p
#define FULL_BLOCK_FAKE_ADDR bm::all_set<true>::_block._p_fullp
#define FULL_SUB_BLOCK_REAL_ADDR bm::all_set<true>::_block._s
#define BLOCK_ADDR_SAN(addr) (addr == FULL_BLOCK_FAKE_ADDR) ? FULL_BLOCK_REAL_ADDR : addr
#define IS_VALID_ADDR(addr) bm::all_set<true>::is_valid_block_addr(addr)
#define IS_FULL_BLOCK(addr) bm::all_set<true>::is_full_block(addr)
#define IS_EMPTY_BLOCK(addr) bool(addr == 0)

#define BM_BLOCK_TYPE(addr) bm::all_set<true>::block_type(addr)

// Macro definitions to manipulate bits in pointers
// This trick is based on the fact that pointers allocated by malloc are
// aligned and bit 0 is never set. It means we are safe to use it.
// BM library keeps GAP flag in pointer.


// TODO: consider UINTPTR_MAX == 0xFFFFFFFF
//
# if ULONG_MAX != 0xffffffff || defined(_WIN64)  // 64-bit

#  define BMPTR_SETBIT0(ptr)   ( ((bm::id64_t)ptr) | 1 )
#  define BMPTR_CLEARBIT0(ptr) ( ((bm::id64_t)ptr) & ~(bm::id64_t)1 )
#  define BMPTR_TESTBIT0(ptr)  ( ((bm::id64_t)ptr) & 1 )

# else // 32-bit

#  define BMPTR_SETBIT0(ptr)   ( ((bm::id_t)ptr) | 1 )
#  define BMPTR_CLEARBIT0(ptr) ( ((bm::id_t)ptr) & ~(bm::id_t)1 )
#  define BMPTR_TESTBIT0(ptr)  ( ((bm::id_t)ptr) & 1 )

# endif

# define BMGAP_PTR(ptr) ((bm::gap_word_t*)BMPTR_CLEARBIT0(ptr))
# define BMSET_PTRGAP(ptr) ptr = (bm::word_t*)BMPTR_SETBIT0(ptr)
# define BM_IS_GAP(ptr) bool(BMPTR_TESTBIT0(ptr)!=0)





#ifdef BM_HASRESTRICT
# ifndef BMRESTRICT
#  define BMRESTRICT restrict
# endif
#else
# ifndef BMRESTRICT
#   define BMRESTRICT
# endif
#endif

#ifndef BMFORCEINLINE
#ifdef BM_HASFORCEINLINE
# ifndef BMFORCEINLINE
#  define BMFORCEINLINE __forceinline
# endif
#else
# define BMFORCEINLINE inline
#endif
#endif


// --------------------------------
// SSE optmization macros
//

#ifdef BMSSE42OPT
# if defined(BM64OPT) || defined(__x86_64) || defined(_M_AMD64) || defined(_WIN64) || \
    defined(__LP64__) || defined(_LP64) || ( __WORDSIZE == 64 )
#   undef BM64OPT
#   define BM64_SSE4
# endif
# undef BMSSE2OPT
#endif

#ifdef BMAVX2OPT
# if defined(BM64OPT) || defined(__x86_64) || defined(_M_AMD64) || defined(_WIN64) || \
    defined(__LP64__) || defined(_LP64)
#   undef BM64OPT
#   undef BM64_SSE4
#   define BM64_AVX2
# endif
# undef BMSSE2OPT
# undef BMSSE42OPT
#endif

#ifdef BMAVX512OPT
# if defined(BM64OPT) || defined(__x86_64) || defined(_M_AMD64) || defined(_WIN64) || \
    defined(__LP64__) || defined(_LP64)
#   undef BM64OPT
#   undef BM64_SSE4
#   undef BM64_AVX2
#   define BM64_AVX512
# endif
# undef BMSSE2OPT
# undef BMSSE42OPT
#endif



# ifndef BM_SET_MMX_GUARD
#  define BM_SET_MMX_GUARD
# endif


#if (defined(BMSSE2OPT) || defined(BMSSE42OPT) || defined(BMAVX2OPT) || defined(BMAVX512OPT))

    # ifndef BM_SET_MMX_GUARD
    #  define BM_SET_MMX_GUARD  sse_empty_guard  bm_mmx_guard_;
    # endif

    #ifdef _MSC_VER

    #ifndef BM_ALIGN16
    #  define BM_ALIGN16 __declspec(align(16))
    #  define BM_ALIGN16ATTR
    #endif

    #ifndef BM_ALIGN32
    #  define BM_ALIGN32 __declspec(align(32))
    #  define BM_ALIGN32ATTR
    #endif

    #ifndef BM_ALIGN64
    #  define BM_ALIGN64 __declspec(align(64))
    #  define BM_ALIGN64ATTR
    #endif

    # else // GCC

    #ifndef BM_ALIGN16
    #  define BM_ALIGN16
    #  define BM_ALIGN16ATTR __attribute__((aligned(16)))
    #endif

    #ifndef BM_ALIGN32
    #  define BM_ALIGN32
    #  define BM_ALIGN32ATTR __attribute__((aligned(32)))
    #endif

    #ifndef BM_ALIGN64
    #  define BM_ALIGN64
    #  define BM_ALIGN64ATTR __attribute__((aligned(64)))
    #endif
    #endif

#else 

    #define BM_ALIGN16 
    #define BM_ALIGN16ATTR
    #define BM_ALIGN32
    #define BM_ALIGN32ATTR
    #define BM_ALIGN64
    #define BM_ALIGN64ATTR

#endif


/*
#if !(defined(BMSSE2OPT) || defined(BMSSE42OPT) || defined(BMAVX2OPT) || defined(BMAVX512OPT))

    #define BM_ALIGN16 
    #define BM_ALIGN16ATTR
    #define BM_ALIGN32
    #define BM_ALIGN32ATTR
    #define BM_ALIGN64
    #define BM_ALIGN64ATTR

#else  

    # ifndef BM_SET_MMX_GUARD
    #  define BM_SET_MMX_GUARD  sse_empty_guard  bm_mmx_guard_;
    # endif

    #ifdef _MSC_VER

        #ifndef BM_ALIGN16
        #  define BM_ALIGN16 __declspec(align(16))
        #  define BM_ALIGN16ATTR
        #endif

        #ifndef BM_ALIGN32
        #  define BM_ALIGN32 __declspec(align(32))
        #  define BM_ALIGN32ATTR
        #endif

        #ifndef BM_ALIGN64
        #  define BM_ALIGN64 __declspec(align(64))
        #  define BM_ALIGN64ATTR
        #endif

    # else // GCC

        #ifndef BM_ALIGN16
        #  define BM_ALIGN16
        #  define BM_ALIGN16ATTR __attribute__((aligned(16)))
        #endif

        #ifndef BM_ALIGN32
        #  define BM_ALIGN32
        #  define BM_ALIGN32ATTR __attribute__((aligned(32)))
        #endif

        #ifndef BM_ALIGN64
        #  define BM_ALIGN64
        #  define BM_ALIGN64ATTR __attribute__((aligned(64)))
        #endif
    #endif

#endif
*/


#if (defined(BMSSE2OPT) || defined(BMSSE42OPT) || defined(BMWASMSIMDOPT))
#   define BM_VECT_ALIGN BM_ALIGN16
#   define BM_VECT_ALIGN_ATTR BM_ALIGN16ATTR
#else
#   if defined(BMAVX2OPT)
#       define BM_VECT_ALIGN BM_ALIGN32
#       define BM_VECT_ALIGN_ATTR BM_ALIGN32ATTR
#   else
#       if defined(BMAVX512OPT)
#          define BM_VECT_ALIGN BM_ALIGN64
#          define BM_VECT_ALIGN_ATTR BM_ALIGN64ATTR
#       else
#          define BM_VECT_ALIGN
#          define BM_VECT_ALIGN_ATTR
#       endif
#   endif
#endif




/*! 
    Define calculates number of 1 bits in 32-bit word.
    @ingroup bitfunc 
*/
#ifndef BM_INCWORD_BITCOUNT

#if (defined(BMSSE42OPT) || defined(BMAVX2OPT) || defined(BMAVX512OPT))
# define BM_INCWORD_BITCOUNT(cnt, w) cnt += unsigned(_mm_popcnt_u32(w));
#else

# define BM_INCWORD_BITCOUNT(cnt, w) cnt += \
     bm::bit_count_table<true>::_count[(unsigned char)(w)] + \
     bm::bit_count_table<true>::_count[(unsigned char)((w) >> 8)] + \
     bm::bit_count_table<true>::_count[(unsigned char)((w) >> 16)] + \
     bm::bit_count_table<true>::_count[(unsigned char)((w) >> 24)];


#endif

// throw redefinintion for compatibility with language wrappers
//
#ifndef BM_ASSERT_THROW
#define BM_ASSERT_THROW(x, xerrcode)
#endif


#ifndef __has_cpp_attribute
#  define __has_cpp_attribute(x) 0
#endif
#ifndef __has_attribute
#  define __has_attribute(x) 0
#endif
#if __has_cpp_attribute(fallthrough)  &&  \
    (!defined(__clang__)  ||  (__clang_major__ > 7 && __cplusplus >= 201703L))
#  define BM_FALLTHROUGH [[fallthrough]]
#elif __has_cpp_attribute(gcc::fallthrough)
#  define BM_FALLTHROUGH [[gcc::fallthrough]]
#elif __has_cpp_attribute(clang::fallthrough)
#  define BM_FALLTHROUGH [[clang::fallthrough]]
#elif __has_attribute(fallthrough)
#  define BM_FALLTHROUGH __attribute__ ((fallthrough))
#else
#  define BM_FALLTHROUGH
#endif

#endif