Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
int_tools.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 // $Id: int_tools.hpp,v 1.6 2011/01/09 17:25:58 edrusb Rel $
22 //
23 /*********************************************************************/
24 
28 
29 #ifndef INT_TOOLS_HPP
30 #define INT_TOOLS_HPP
31 
32 #include "../my_config.h"
33 
34 #include "integers.hpp"
35 #include "erreurs.hpp"
36 
37 namespace libdar
38 {
39 
42 
43  typedef unsigned char int_tools_bitfield[8];
44 
45  extern void int_tools_swap_bytes(unsigned char &a, unsigned char &b);
46  extern void int_tools_swap_bytes(unsigned char *a, U_I size);
47  extern void int_tools_expand_byte(unsigned char a, int_tools_bitfield &bit);
48  extern void int_tools_contract_byte(const int_tools_bitfield &b, unsigned char & a);
49 
50  // integer (agregates) manipulations
51  // argument must be a regular interger (a bit field).
52  template <class T> extern T int_tools_rotate_right_one_bit(T v)
53  {
54  bool retenue = (v & 1) != 0;
55 
56  v >>= 1;
57  if(retenue)
58  v |= T(1) << (sizeof(v)*8 - 1);
59 
60  return v;
61  }
62 
63  template <class T> extern T int_tools_maxof_agregate(T unused) { unused = 0; unused = ~unused; unused = unused > 0 ? unused : ~int_tools_rotate_right_one_bit(T(1)); return unused; }
64 
65  template <class B> static B int_tools_higher_power_of_2(B val)
66  {
67  B i = 0;
68 
69  while((val >> i) > 1)
70  i++;
71 
72  return i;
73  }
74 
76 
77 }
78 
79 #endif