File: bits.cpp

package info (click to toggle)
graphite2 1.3.14-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,588 kB
  • sloc: cpp: 14,738; cs: 1,998; python: 1,737; ansic: 1,673; perl: 184; xml: 123; sh: 104; makefile: 62
file content (138 lines) | stat: -rw-r--r-- 4,177 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*-----------------------------------------------------------------------------
Copyright (C) 2011 SIL International
Responsibility: Tim Eves

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should also have received a copy of the GNU Lesser General Public
    License along with this library in the file named "LICENSE".
    If not, write to the Free Software Foundation, 51 Franklin Street,
    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    internet at http://www.fsf.org/licenses/lgpl.html.

Alternatively, the contents of this file may be used under the terms of the
Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
License, as published by the Free Software Foundation, either version 2
of the License or (at your option) any later version.

Description:
The test harness for the Sparse class. This validates the
sparse classe is working correctly.
-----------------------------------------------------------------------------*/

#include <cassert>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include "inc/Main.h"
#include "inc/bits.h"

using namespace graphite2;

#if (defined(__x86_64__) && !defined(__ILP32__)) || defined(_WIN64)
	#define HAS_64BIT
#endif

#define maskoff(n) (size_t(-1L) >> (8*sizeof(size_t) - n))

#define pat(b)   0x01##b, 0x03##b, 0x07##b, 0x0f##b
#define pat8(b)  pat(b), pat(f##b)
#define pat16(b) pat8(b), pat8(ff##b)
#define pat32(b) pat16(b), pat16(ffff##b)
#define pat64(b) pat32(b), pat32(ffffffff##b)

#define patterns(bw) \
    uint##bw const  u##bw##_pat[] = {0, pat##bw(UL) }; \
    int##bw const * s##bw##_pat   = reinterpret_cast<int##bw const *>(u##bw##_pat)

//#define BENCHMARK 40000000
#if defined BENCHMARK
#define benchmark() for (int n = BENCHMARK; n; --n)
#else
#define benchmark()
#endif


typedef     unsigned long long   uint64;
typedef     signed long long     int64;
namespace
{
    patterns(8);
    patterns(16);
    patterns(32);
#if defined(HAS_64BIT)
    patterns(64);
#endif

    int ret = 0;


    template<typename T>
    struct type_name {};

    // template<typename T>
    // inline
    // std::ostream & operator << (std::ostream & o, type_name<T>)
    // {
    //     if (!std::numeric_limits<T>::is_signed) o.put('u');
    //     o << "int" << std::dec << sizeof(T)*8;
    // }

    template<typename T>
    inline
    void test_bit_set_count(const T pat[])
    {
        for (unsigned int p = 0; p <= sizeof(T)*8; ++p)
        {
#if !defined BENCHMARK
            std::cout << "bit_set_count("
                        << (!std::numeric_limits<T>::is_signed ? "uint" : "int")
                        << std::dec << sizeof(T)*8 << "(0x"
                            << std::hex
                            << std::setw(sizeof(T)*2)
                            << std::setfill('0')
                            << (pat[p] & maskoff(8*sizeof(T)))
                      << ")) -> "
                        << std::dec
                        << bit_set_count(pat[p]) << std::endl;
#endif
            if (bit_set_count(pat[p]) != p)
            {
                std::cerr << " != " << std::dec << p << std::endl;
                ret = sizeof(T);
            }
        }
    }

}

int main(int argc , char *argv[])
{
    assert(sizeof(uint64) == 8);

    benchmark()
    {
        // Test bit_set_count
        test_bit_set_count(u8_pat);
        test_bit_set_count(s8_pat);
        test_bit_set_count(u16_pat);
        test_bit_set_count(s16_pat);
        test_bit_set_count(u32_pat);
        test_bit_set_count(s32_pat);
#if defined(HAS_64BIT)
        test_bit_set_count(u64_pat);
        test_bit_set_count(s64_pat);
#endif
    }

    return ret;
}