DGtal  1.5.beta
testBits.cpp File Reference
#include <cstdio>
#include <cmath>
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/base/Bits.h"
Include dependency graph for testBits.cpp:

Go to the source code of this file.

Functions

unsigned int index (DGtal::uint32_t n, unsigned int b)
 
int main ()
 

Detailed Description

This program 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 3 of the 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
Date
2012/07/02

This file is part of the DGtal library

Definition in file testBits.cpp.

Function Documentation

◆ index()

unsigned int index ( DGtal::uint32_t  n,
unsigned int  b 
)
Examples
examples/tutorial-examples/polyhedralizer.cpp, and tutorial-examples/polyhedralizer.cpp.

Definition at line 44 of file testBits.cpp.

45 {
46  int i = 0;
47  for ( ; b != 0; --b )
48  {
49  if ( n & 1 ) ++i;
50  n >>= 1;
51  }
52  return ( n & 1 ) ? i + 1 : 0;
53 }

Referenced by DGtal::ATSolver2D< TKSpace, TLinearAlgebra >::ATSolver2D(), main(), and testITKMethod().

◆ main()

int main ( void  )

Definition at line 56 of file testBits.cpp.

57 {
58  unsigned int nb = 0;
59  unsigned int nbok = 0;
60  ++nb; nbok += Bits::nbSetBits( (DGtal::uint8_t)-1) == 8 ? 1 : 0;
61  ++nb; nbok += Bits::nbSetBits( (DGtal::uint16_t)-1) == 16 ? 1 : 0;
62  ++nb; nbok += Bits::nbSetBits( (DGtal::uint32_t)-1) == 32 ? 1 : 0;
63  ++nb; nbok += Bits::nbSetBits( (DGtal::uint64_t)-1) == 64 ? 1 : 0;
64  ++nb; nbok += Bits::nbSetBits( (DGtal::uint8_t)1) == 1 ? 1 : 0;
65  ++nb; nbok += Bits::nbSetBits( (DGtal::uint16_t)1) == 1 ? 1 : 0;
66  ++nb; nbok += Bits::nbSetBits( (DGtal::uint32_t)1) == 1 ? 1 : 0;
67  ++nb; nbok += Bits::nbSetBits( (DGtal::uint64_t)1) == 1 ? 1 : 0;
68  ++nb; nbok += Bits::nbSetBits( (DGtal::uint8_t)-2) == 7 ? 1 : 0;
69  ++nb; nbok += Bits::nbSetBits( (DGtal::uint16_t)-2) == 15 ? 1 : 0;
70  ++nb; nbok += Bits::nbSetBits( (DGtal::uint32_t)-2) == 31 ? 1 : 0;
71  ++nb; nbok += Bits::nbSetBits( (DGtal::uint64_t)-2) == 63 ? 1 : 0;
72 
73  for ( unsigned int i = 0; i < 100; ++i )
74  {
75  DGtal::uint16_t n = (DGtal::uint16_t) ( rand() % 65536 );
76  for ( unsigned int b = 0; b < 16; ++b )
77  {
78  ++nb; nbok += Bits::indexInSetBits( n, b ) == index( n, b ) ? 1 : 0;
79  }
80  }
81 
82  std::cerr << "(" << nbok << "/" << nb << ")" << " tests." << std::endl;
83 
84  trace.beginBlock ( "Testing speed of loop version of indexInSetBits" );
85  srand( 0 );
86  unsigned int val = 0;
87  for ( unsigned int i = 0; i < 100000; ++i )
88  {
89  DGtal::uint32_t n = (DGtal::uint32_t) rand();
90  for ( unsigned int b = 0; b < 32; ++b )
91  val += index( n, b );
92  }
93  trace.info() << "- checksum = " << val << std::endl;
94  trace.endBlock();
95 
96  trace.beginBlock ( "Testing speed of look-up table version of indexInSetBits" );
97  srand( 0 );
98  unsigned int val2 = 0;
99  for ( unsigned int i = 0; i < 100000; ++i )
100  {
101  DGtal::uint32_t n = (DGtal::uint32_t) rand();
102  for ( unsigned int b = 0; b < 32; ++b )
103  val2 += Bits::indexInSetBits( n, b );
104  }
105  trace.info() << "- checksum = " << val2 << std::endl;
106  trace.endBlock();
107  ++nb; nbok += val == val2 ? 1 : 0;
108  return ( nb == nbok ) ? 0 : 1;
109 }
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
boost::uint16_t uint16_t
unsigned 16-bit integer.
Definition: BasicTypes.h:61
boost::uint8_t uint8_t
unsigned 8-bit integer.
Definition: BasicTypes.h:59
Trace trace
Definition: Common.h:153
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
unsigned int index(DGtal::uint32_t n, unsigned int b)
Definition: testBits.cpp:44
srand(0)

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), index(), DGtal::Bits::indexInSetBits(), DGtal::Trace::info(), DGtal::Bits::nbSetBits(), srand(), and DGtal::trace.