source: trunk/lib/bitblock.hpp @ 1908

Last change on this file since 1908 was 1908, checked in by lindanl, 8 years ago

Define ATTRIBUTE_SIMD_ALIGN for MSVC.

File size: 3.2 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#ifndef ATTRIBUTE_SIMD_ALIGN
27    #if defined _MSC_VER
28        #define ATTRIBUTE_SIMD_ALIGN __declspec(align(sizeof(BitBlock)))
29    #elif defined __GNUC__
30        #define ATTRIBUTE_SIMD_ALIGN __attribute__((aligned(sizeof(BitBlock))))
31    #else
32        #define ATTRIBUTE_SIMD_ALIGN
33    #endif
34#endif
35#define ASSERT_BITBLOCK_ALIGN(v) assert(0 == (((uint64_t)(&v)) & (sizeof(BitBlock)-1)) )
36
37template<class T> void print_register(const char * var_name, T v);
38
39static IDISA_ALWAYS_INLINE uint32_t count_forward_zeroes(BitBlock v);
40static IDISA_ALWAYS_INLINE uint32_t count_reverse_zeroes(BitBlock v);
41static IDISA_ALWAYS_INLINE uint32_t bitstream_scan(BitBlock * v, uint32_t pos);
42static IDISA_ALWAYS_INLINE BitBlock assert_align(BitBlock v);
43
44/*  BitBlock union type */
45union ubitblock;
46
47/*  Default BLOCK_SIZE is 128, compatible with SSE, Altivec, SPU */
48#if (BLOCK_SIZE == 128)
49#include "bitblock128.hpp"
50#endif
51
52/*  BLOCK_SIZE 256 for AVX */
53#if (BLOCK_SIZE == 256)
54#include "bitblock256.hpp"
55#endif
56
57template <class T> void print_register(const char * var_name, T v);
58template <class T>
59void print_register(const char * var_name, T v) {
60        unsigned char c;
61        printf("%20s = ", var_name);
62        for(int i=sizeof(T)-1; i>=0; i--) {
63                c = *(((unsigned char *)&v)+i);
64                printf("%02X ", c);
65        }
66        printf("\n");
67}
68
69IDISA_ALWAYS_INLINE uint32_t count_forward_zeroes(BitBlock v) {
70        union {BitBlock bitblock; scanword_t elems[sizeof(BitBlock)/sizeof(scanword_t)];} u;
71        u.bitblock = v;
72        uint32_t so_far = 0;
73        for (int i = 0; i < sizeof(BitBlock)/sizeof(scanword_t); i++) {
74                if (u.elems[i] != 0) return so_far + scan_forward_zeroes(u.elems[i]);
75                so_far += 8 * sizeof(scanword_t);
76        }
77        return so_far;
78}
79
80IDISA_ALWAYS_INLINE uint32_t count_reverse_zeroes(BitBlock v) {
81        union {BitBlock bitblock; scanword_t elems[sizeof(BitBlock)/sizeof(scanword_t)];} u;
82        u.bitblock = v;
83        uint32_t so_far = 0;
84        for (int i = sizeof(BitBlock)/sizeof(scanword_t) - 1; i >= 0; i--) {
85                if (u.elems[i] != 0) return so_far + scan_backward_zeroes(u.elems[i]);
86                so_far += 8 * sizeof(scanword_t);
87        }
88        return so_far;
89}
90
91IDISA_ALWAYS_INLINE uint32_t bitstream_scan(BitBlock * v, uint32_t pos) {
92        scanword_t * bitstream_ptr = (scanword_t *) (((intptr_t) v) + pos/8);
93        scanword_t bitstream_slice = ((*bitstream_ptr) >> (pos % 8));
94        if (bitstream_slice != 0) return pos + scan_forward_zeroes(bitstream_slice);
95        else {
96                do {
97                        bitstream_ptr++;
98                        bitstream_slice = *bitstream_ptr;
99                } while (bitstream_slice == 0);
100                uint32_t base_posn = 8*((intptr_t) bitstream_ptr - (intptr_t) v);
101                return base_posn + scan_forward_zeroes(bitstream_slice);
102        }
103}
104
105#endif /* BITBLOCK_HPP */
106
107
Note: See TracBrowser for help on using the repository browser.