source: trunk/lib/bitblock.hpp @ 4138

Last change on this file since 4138 was 4035, checked in by cameron, 5 years ago

Conditional define for STDC_LIMIT_MACROS

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