Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Support/SwapByteOrder.h
Line
Count
Source (jump to first uncovered line)
1
//===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file declares generic and optimized functions to swap the byte order of
10
// an integral type.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_SUPPORT_SWAPBYTEORDER_H
15
#define LLVM_SUPPORT_SWAPBYTEORDER_H
16
17
#include "llvm/Support/Compiler.h"
18
#include "llvm/Support/DataTypes.h"
19
#include <cstddef>
20
#include <type_traits>
21
#if defined(_MSC_VER) && !defined(_DEBUG)
22
#include <stdlib.h>
23
#endif
24
25
namespace llvm {
26
namespace sys {
27
28
/// SwapByteOrder_16 - This function returns a byte-swapped representation of
29
/// the 16-bit argument.
30
229k
inline uint16_t SwapByteOrder_16(uint16_t value) {
31
#if defined(_MSC_VER) && !defined(_DEBUG)
32
  // The DLL version of the runtime lacks these functions (bug!?), but in a
33
  // release build they're replaced with BSWAP instructions anyway.
34
  return _byteswap_ushort(value);
35
#else
36
  uint16_t Hi = value << 8;
37
229k
  uint16_t Lo = value >> 8;
38
229k
  return Hi | Lo;
39
229k
#endif
40
229k
}
41
42
/// SwapByteOrder_32 - This function returns a byte-swapped representation of
43
/// the 32-bit argument.
44
471k
inline uint32_t SwapByteOrder_32(uint32_t value) {
45
471k
#if defined(__llvm__) || (LLVM_GNUC_PREREQ(4, 3, 0) && !defined(__ICC))
46
471k
  return __builtin_bswap32(value);
47
#elif defined(_MSC_VER) && !defined(_DEBUG)
48
  return _byteswap_ulong(value);
49
#else
50
  uint32_t Byte0 = value & 0x000000FF;
51
  uint32_t Byte1 = value & 0x0000FF00;
52
  uint32_t Byte2 = value & 0x00FF0000;
53
  uint32_t Byte3 = value & 0xFF000000;
54
  return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
55
#endif
56
}
57
58
/// SwapByteOrder_64 - This function returns a byte-swapped representation of
59
/// the 64-bit argument.
60
286k
inline uint64_t SwapByteOrder_64(uint64_t value) {
61
286k
#if defined(__llvm__) || (LLVM_GNUC_PREREQ(4, 3, 0) && !defined(__ICC))
62
286k
  return __builtin_bswap64(value);
63
#elif defined(_MSC_VER) && !defined(_DEBUG)
64
  return _byteswap_uint64(value);
65
#else
66
  uint64_t Hi = SwapByteOrder_32(uint32_t(value));
67
  uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
68
  return (Hi << 32) | Lo;
69
#endif
70
}
71
72
101k
inline unsigned char  getSwappedBytes(unsigned char C) { return C; }
73
94
inline   signed char  getSwappedBytes(signed char C) { return C; }
74
0
inline          char  getSwappedBytes(char C) { return C; }
75
76
219k
inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); }
77
83
inline   signed short getSwappedBytes(  signed short C) { return SwapByteOrder_16(C); }
78
79
409k
inline unsigned int   getSwappedBytes(unsigned int   C) { return SwapByteOrder_32(C); }
80
6.23k
inline   signed int   getSwappedBytes(  signed int   C) { return SwapByteOrder_32(C); }
81
82
#if __LONG_MAX__ == __INT_MAX__
83
inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_32(C); }
84
inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_32(C); }
85
#elif __LONG_MAX__ == __LONG_LONG_MAX__
86
4
inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_64(C); }
87
0
inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_64(C); }
88
#else
89
#error "Unknown long size!"
90
#endif
91
92
264k
inline unsigned long long getSwappedBytes(unsigned long long C) {
93
264k
  return SwapByteOrder_64(C);
94
264k
}
95
13.8k
inline signed long long getSwappedBytes(signed long long C) {
96
13.8k
  return SwapByteOrder_64(C);
97
13.8k
}
98
99
inline float getSwappedBytes(float C) {
100
  union {
101
    uint32_t i;
102
    float f;
103
  } in, out;
104
  in.f = C;
105
  out.i = SwapByteOrder_32(in.i);
106
  return out.f;
107
}
108
109
inline double getSwappedBytes(double C) {
110
  union {
111
    uint64_t i;
112
    double d;
113
  } in, out;
114
  in.d = C;
115
  out.i = SwapByteOrder_64(in.i);
116
  return out.d;
117
}
118
119
template <typename T>
120
inline typename std::enable_if<std::is_enum<T>::value, T>::type
121
0
getSwappedBytes(T C) {
122
0
  return static_cast<T>(
123
0
      getSwappedBytes(static_cast<typename std::underlying_type<T>::type>(C)));
124
0
}
125
126
template<typename T>
127
1.01M
inline void swapByteOrder(T &Value) {
128
1.01M
  Value = getSwappedBytes(Value);
129
1.01M
}
void llvm::sys::swapByteOrder<unsigned long long>(unsigned long long&)
Line
Count
Source
127
263k
inline void swapByteOrder(T &Value) {
128
263k
  Value = getSwappedBytes(Value);
129
263k
}
void llvm::sys::swapByteOrder<unsigned int>(unsigned int&)
Line
Count
Source
127
409k
inline void swapByteOrder(T &Value) {
128
409k
  Value = getSwappedBytes(Value);
129
409k
}
void llvm::sys::swapByteOrder<unsigned short>(unsigned short&)
Line
Count
Source
127
219k
inline void swapByteOrder(T &Value) {
128
219k
  Value = getSwappedBytes(Value);
129
219k
}
void llvm::sys::swapByteOrder<short>(short&)
Line
Count
Source
127
46
inline void swapByteOrder(T &Value) {
128
46
  Value = getSwappedBytes(Value);
129
46
}
void llvm::sys::swapByteOrder<int>(int&)
Line
Count
Source
127
6.19k
inline void swapByteOrder(T &Value) {
128
6.19k
  Value = getSwappedBytes(Value);
129
6.19k
}
void llvm::sys::swapByteOrder<unsigned char>(unsigned char&)
Line
Count
Source
127
101k
inline void swapByteOrder(T &Value) {
128
101k
  Value = getSwappedBytes(Value);
129
101k
}
Unexecuted instantiation: void llvm::sys::swapByteOrder<char>(char&)
Unexecuted instantiation: void llvm::sys::swapByteOrder<llvm::Uint24>(llvm::Uint24&)
void llvm::sys::swapByteOrder<unsigned long>(unsigned long&)
Line
Count
Source
127
4
inline void swapByteOrder(T &Value) {
128
4
  Value = getSwappedBytes(Value);
129
4
}
void llvm::sys::swapByteOrder<long long>(long long&)
Line
Count
Source
127
13.7k
inline void swapByteOrder(T &Value) {
128
13.7k
  Value = getSwappedBytes(Value);
129
13.7k
}
Unexecuted instantiation: void llvm::sys::swapByteOrder<llvm::minidump::StreamType>(llvm::minidump::StreamType&)
void llvm::sys::swapByteOrder<signed char>(signed char&)
Line
Count
Source
127
57
inline void swapByteOrder(T &Value) {
128
57
  Value = getSwappedBytes(Value);
129
57
}
130
131
} // end namespace sys
132
} // end namespace llvm
133
134
#endif