initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / test / PackedList / PackedListTest.C
blob9bf29923fe029b2aafd948c9d57d3b31cf89cfaa
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Application
27 Description
29 \*---------------------------------------------------------------------------*/
31 #include "argList.H"
32 #include "uLabel.H"
33 #include "IOobject.H"
34 #include "IOstreams.H"
35 #include "IFstream.H"
36 #include "PackedBoolList.H"
37 #include <climits>
40 using namespace Foam;
42 template<unsigned nBits>
43 inline void reportInfo()
45     unsigned offset = PackedList<nBits>::packing();
47     unsigned useSHL = ((1u << (nBits * offset)) - 1);
48     unsigned useSHR = (~0u >> (sizeof(unsigned)*CHAR_BIT - nBits * offset));
50     Info<< nl
51         << "PackedList<" << nBits << ">" << nl
52         << " max_value: " << PackedList<nBits>::max_value() << nl
53         << " packing: " << PackedList<nBits>::packing() << nl
54         << " utilization: " << (nBits * offset) << nl;
56     Info<< " Masking:" << nl
57         << "  shift << " << unsigned(nBits * offset) << nl
58         << "  shift >> " << unsigned((sizeof(unsigned)*CHAR_BIT) - nBits * offset)
59         << nl;
61     hex(Info);
62     Info<< "   maskLower: " << PackedList<nBits>::maskLower(PackedList<nBits>::packing())
63         << nl
64         << "      useSHL: " << useSHL << nl
65         << "      useSHR: " << useSHR << nl;
67     if (useSHL != useSHR)
68     {
69         Info<< "WARNING:  different results for SHL and SHR" << nl;
70     }
72     Info<< nl;
73     dec(Info);
77 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
78 //  Main program:
80 int main(int argc, char *argv[])
82     argList::noParallel();
83     argList::validArgs.insert("file .. fileN");
85     argList::validOptions.insert("mask", "");
86     argList::validOptions.insert("count", "");
87     argList::validOptions.insert("info", "");
89     argList args(argc, argv, false, true);
92     if (args.optionFound("mask"))
93     {
94         Info<< "bit width: " << unsigned(sizeof(unsigned)*CHAR_BIT) << endl;
95         reportInfo<1>();
96         reportInfo<2>();
97         reportInfo<3>();
98         reportInfo<4>();
99         reportInfo<5>();
100         reportInfo<6>();
101         reportInfo<7>();
102         reportInfo<8>();
103         reportInfo<9>();
104         reportInfo<10>();
105         reportInfo<11>();
106         reportInfo<12>();
107         reportInfo<13>();
108         reportInfo<14>();
109         reportInfo<15>();
110         reportInfo<16>();
111         reportInfo<17>();
112         reportInfo<18>();
113         reportInfo<19>();
114         reportInfo<20>();
115         reportInfo<21>();
116         reportInfo<22>();
117         reportInfo<23>();
118         reportInfo<24>();
119         reportInfo<25>();
120         reportInfo<26>();
121         reportInfo<27>();
122         reportInfo<28>();
123         reportInfo<29>();
124         reportInfo<30>();
125         reportInfo<31>();
127         return 0;
128     }
129     else if (args.additionalArgs().empty())
130     {
131         args.printUsage();
132     }
135     forAll(args.additionalArgs(), argI)
136     {
137         const string& srcFile = args.additionalArgs()[argI];
138         Info<< nl << "reading " << srcFile << nl;
140         IFstream ifs(srcFile);
141         List<label> rawLst(ifs);
143         PackedBoolList packLst(rawLst);
145         Info<< "size: " << packLst.size() << nl;
147         if (args.optionFound("count"))
148         {
149             unsigned int rawCount = 0;
150             forAll(rawLst, elemI)
151             {
152                 if (rawLst[elemI])
153                 {
154                     rawCount++;
155                 }
156             }
157             Info<< "raw count: " << rawCount << nl
158                 << "packed count: " << packLst.count() << nl;
159         }
161         if (args.optionFound("info"))
162         {
163             packLst.print(Info);
164         }
166         Info<< nl;
167         IOobject::writeDivider(Info);
168     }
170     return 0;
173 // ************************************************************************* //