source: trunk/lib/bitblock.hpp @ 2015

Last change on this file since 2015 was 1970, checked in by ksherdy, 7 years ago

Minor update.

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