Robotics Library  0.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
endian.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the Technische Universitaet Muenchen nor the names of
14 // its contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef _RL_HAL_ENDIAN_H_
31 #define _RL_HAL_ENDIAN_H_
32 
33 #include "types.h"
34 
35 namespace rl
36 {
37  namespace hal
38  {
39  uint32_t bigEndianDoubleWord(const uint16_t& highWord, const uint16_t& lowWord);
40 
41  void bigEndianToHostEndian(uint16_t& word);
42 
43  void bigEndianToHostEndian(uint32_t& doubleWord);
44 
45  uint16_t bigEndianWord(const uint8_t& highByte, const uint8_t& lowByte);
46 
47  uint8_t highByteFromBigEndian(const uint16_t& word);
48 
49  inline uint8_t highByteFromHostEndian(const uint16_t& word)
50  {
51  return (word >> 8);
52  }
53 
54  uint8_t highByteFromLittleEndian(const uint16_t& word);
55 
56  uint16_t highWordFromBigEndian(const uint32_t& doubleWord);
57 
58  inline uint16_t highWordFromHostEndian(const uint32_t& doubleWord)
59  {
60  return (doubleWord >> 16);
61  }
62 
63  uint16_t highWordFromLittleEndian(const uint32_t& doubleWord);
64 
65  inline uint32_t hostEndianDoubleWord(const uint16_t& highWord, const uint16_t& lowWord)
66  {
67  return (highWord << 16) | lowWord;
68  }
69 
70  void hostEndianToBigEndian(uint16_t& word);
71 
72  void hostEndianToBigEndian(uint32_t& doubleWord);
73 
74  void hostEndianToLittleEndian(uint16_t& word);
75 
76  void hostEndianToLittleEndian(uint32_t& doubleWord);
77 
78  inline uint16_t hostEndianWord(const uint8_t& highByte, const uint8_t& lowByte)
79  {
80  return (highByte << 8) | lowByte;
81  }
82 
83  uint32_t littleEndianDoubleWord(const uint16_t& highWord, const uint16_t& lowWord);
84 
85  void littleEndianToHostEndian(uint16_t& word);
86 
87  void littleEndianToHostEndian(uint32_t& doubleWord);
88 
89  uint16_t littleEndianWord(const uint8_t& highByte, const uint8_t& lowByte);
90 
91  uint8_t lowByteFromBigEndian(const uint16_t& word);
92 
93  inline uint8_t lowByteFromHostEndian(const uint16_t& word)
94  {
95  return (word & 0xFF);
96  }
97 
98  uint8_t lowByteFromLittleEndian(const uint16_t& word);
99 
100  uint16_t lowWordFromBigEndian(const uint32_t& doubleWord);
101 
102  inline uint16_t lowWordFromHostEndian(const uint32_t& doubleWord)
103  {
104  return (doubleWord & 0xFFFF);
105  }
106 
107  uint16_t lowWordFromLittleEndian(const uint32_t& doubleWord);
108 
109  inline void swapByteOrder(uint16_t& word)
110  {
111  word = (word >> 8) | (word << 8); // TODO
112  }
113 
114  inline void swapByteOrder(uint32_t& doubleWord)
115  {
116  doubleWord = ((doubleWord & 0x000000FF) << 24) | ((doubleWord & 0x0000FF00) << 8) | ((doubleWord & 0x00FF0000) >> 8) | ((doubleWord & 0xFF000000) >> 24); // TODO
117  }
118  }
119 }
120 
121 #endif // _RL_HAL_ENDIAN_H_