Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
endian_ops.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/endian_ops.h
10 //! @brief Endian operations.
11 
12 #ifndef ROC_CORE_ENDIAN_OPS_H_
13 #define ROC_CORE_ENDIAN_OPS_H_
14 
15 #include "roc_core/cpu_traits.h"
16 #include "roc_core/stddefs.h"
17 
18 namespace roc {
19 namespace core {
20 
21 //! Endian operations.
22 class EndianOps {
23 public:
24  //! Swap between endians.
25  template <class T> static inline T swap_endian(T v) {
26  return reverse_octets_(v);
27  }
28 
29 #if ROC_CPU_ENDIAN == ROC_CPU_BE
30  //! Swap between native endian and big endian.
31  template <class T> static inline T swap_native_be(T v) {
32  return v;
33  }
34 #else
35  //! Swap between native endian and big endian.
36  template <class T> static inline T swap_native_be(T v) {
37  return reverse_octets_(v);
38  }
39 #endif
40 
41 #if ROC_CPU_ENDIAN == ROC_CPU_BE
42  //! Swap between native endian and little endian.
43  template <class T> static inline T swap_native_le(T v) {
44  return reverse_octets_(v);
45  }
46 #else
47  //! Swap between native endian and little endian.
48  template <class T> static inline T swap_native_le(T v) {
49  return v;
50  }
51 #endif
52 
53 private:
54  static uint8_t reverse_octets_(uint8_t v);
55  static int8_t reverse_octets_(int8_t v);
56  static uint16_t reverse_octets_(uint16_t v);
57  static int16_t reverse_octets_(int16_t v);
58  static uint32_t reverse_octets_(uint32_t v);
59  static int32_t reverse_octets_(int32_t v);
60  static uint64_t reverse_octets_(uint64_t v);
61  static int64_t reverse_octets_(int64_t v);
62  static float reverse_octets_(float v);
63  static double reverse_octets_(double v);
64 };
65 
66 } // namespace core
67 } // namespace roc
68 
69 #endif // ROC_CORE_ENDIAN_OPS_H_
Endian operations.
Definition: endian_ops.h:22
static T swap_native_be(T v)
Swap between native endian and big endian.
Definition: endian_ops.h:31
static T swap_native_le(T v)
Swap between native endian and little endian.
Definition: endian_ops.h:43
static T swap_endian(T v)
Swap between endians.
Definition: endian_ops.h:25
Root namespace.
Commonly used types and functions.