source: trunk/lib/bitblock.hpp @ 1933

Last change on this file since 1933 was 1933, checked in by nmedfort, 7 years ago
File size: 3.2 KB
RevLine 
[1500]1/*
2    IDISA Library Import - Generic
[1617]3    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy
[1500]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*/
[1527]8#ifndef BITBLOCK_HPP
9#define BITBLOCK_HPP
[1500]10
[1903]11//#define NDEBUG //if NDEBUG then disable assertions
12
[1874]13#define __STDC_LIMIT_MACROS
[1542]14#include <stdint.h>
15#include <stdio.h>
[1898]16#include <cassert>
17
[1542]18#include "config.hpp"
[1617]19#include "builtins.hpp"
[1542]20#include "idisa.hpp"
21
[1808]22#define BytePack BitBlock
[1576]23#ifndef BLOCK_SIZE
24#define BLOCK_SIZE 128
[1500]25#endif
[1932]26#define ATTRIBUTE_SIMD_ALIGN __attribute__((aligned(sizeof(BitBlock))))
[1933]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
[1500]37
[1617]38template<class T> void print_register(const char * var_name, T v);
39
[1890]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);
[1898]43static IDISA_ALWAYS_INLINE BitBlock assert_align(BitBlock v);
[1890]44
[1815]45/*  BitBlock union type */
46union ubitblock;
47
[1576]48/*  Default BLOCK_SIZE is 128, compatible with SSE, Altivec, SPU */
49#if (BLOCK_SIZE == 128)
[1527]50#include "bitblock128.hpp"
[1500]51#endif
52
[1576]53/*  BLOCK_SIZE 256 for AVX */
54#if (BLOCK_SIZE == 256)
[1527]55#include "bitblock256.hpp"
[1500]56#endif
57
[1658]58template <class T> void print_register(const char * var_name, T v);
[1527]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
[1890]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
[1527]106#endif /* BITBLOCK_HPP */
107
108
Note: See TracBrowser for help on using the repository browser.