source: trunk/lib/bitblock.hpp @ 1932

Last change on this file since 1932 was 1932, checked in by ksherdy, 8 years ago

Update uint32_t to int32_t since negative positions indicate 'not
found'.

File size: 2.9 KB
Line 
1/*
2    IDISA Library Import - Generic
3    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy
4    Licensed to the public under the Open Software License 3.0.
5    Licensed to International Characters Inc.
6       under the Academic Free License version 3.0.
7*/
8#ifndef BITBLOCK_HPP
9#define BITBLOCK_HPP
10
11//#define NDEBUG //if NDEBUG then disable assertions
12
13#define __STDC_LIMIT_MACROS
14#include <stdint.h>
15#include <stdio.h>
16#include <cassert>
17
18#include "config.hpp"
19#include "builtins.hpp"
20#include "idisa.hpp"
21
22#define BytePack BitBlock
23#ifndef BLOCK_SIZE
24#define BLOCK_SIZE 128
25#endif
26#define ATTRIBUTE_SIMD_ALIGN __attribute__((aligned(sizeof(BitBlock))))
27#define ASSERT_BITBLOCK_ALIGN(v) assert(0 == (((uint64_t)(&v)) & (sizeof(BitBlock)-1)) )
28
29template<class T> void print_register(const char * var_name, T v);
30
31static IDISA_ALWAYS_INLINE uint32_t count_forward_zeroes(BitBlock v);
32static IDISA_ALWAYS_INLINE uint32_t count_reverse_zeroes(BitBlock v);
33static IDISA_ALWAYS_INLINE uint32_t bitstream_scan(BitBlock * v, uint32_t pos);
34static IDISA_ALWAYS_INLINE BitBlock assert_align(BitBlock v);
35
36/*  BitBlock union type */
37union ubitblock;
38
39/*  Default BLOCK_SIZE is 128, compatible with SSE, Altivec, SPU */
40#if (BLOCK_SIZE == 128)
41#include "bitblock128.hpp"
42#endif
43
44/*  BLOCK_SIZE 256 for AVX */
45#if (BLOCK_SIZE == 256)
46#include "bitblock256.hpp"
47#endif
48
49template <class T> void print_register(const char * var_name, T v);
50template <class T>
51void print_register(const char * var_name, T v) {
52        unsigned char c;
53        printf("%20s = ", var_name);
54        for(int i=sizeof(T)-1; i>=0; i--) {
55                c = *(((unsigned char *)&v)+i);
56                printf("%02X ", c);
57        }
58        printf("\n");
59}
60
61IDISA_ALWAYS_INLINE uint32_t count_forward_zeroes(BitBlock v) {
62        union {BitBlock bitblock; scanword_t elems[sizeof(BitBlock)/sizeof(scanword_t)];} u;
63        u.bitblock = v;
64        uint32_t so_far = 0;
65        for (int i = 0; i < sizeof(BitBlock)/sizeof(scanword_t); i++) {
66                if (u.elems[i] != 0) return so_far + scan_forward_zeroes(u.elems[i]);
67                so_far += 8 * sizeof(scanword_t);
68        }
69        return so_far;
70}
71
72IDISA_ALWAYS_INLINE uint32_t count_reverse_zeroes(BitBlock v) {
73        union {BitBlock bitblock; scanword_t elems[sizeof(BitBlock)/sizeof(scanword_t)];} u;
74        u.bitblock = v;
75        uint32_t so_far = 0;
76        for (int i = sizeof(BitBlock)/sizeof(scanword_t) - 1; i >= 0; i--) {
77                if (u.elems[i] != 0) return so_far + scan_backward_zeroes(u.elems[i]);
78                so_far += 8 * sizeof(scanword_t);
79        }
80        return so_far;
81}
82
83IDISA_ALWAYS_INLINE uint32_t bitstream_scan(BitBlock * v, uint32_t pos) {
84        scanword_t * bitstream_ptr = (scanword_t *) (((intptr_t) v) + pos/8);
85        scanword_t bitstream_slice = ((*bitstream_ptr) >> (pos % 8));
86        if (bitstream_slice != 0) return pos + scan_forward_zeroes(bitstream_slice);
87        else {
88                do {
89                        bitstream_ptr++;
90                        bitstream_slice = *bitstream_ptr;
91                } while (bitstream_slice == 0);
92                uint32_t base_posn = 8*((intptr_t) bitstream_ptr - (intptr_t) v);
93                return base_posn + scan_forward_zeroes(bitstream_slice);
94        }
95}
96
97#endif /* BITBLOCK_HPP */
98
99
Note: See TracBrowser for help on using the repository browser.