source: trunk/lib/bitblock.hpp @ 1899

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

Removed all_true declaration.

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