blob: fe98e0aaa9a980b6294d5011342bcdfd1b6a9142 [file] [log] [blame]
Jim Flynnbbfe6032020-07-20 16:57:44 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <common/include/Assert.hpp>
7#include <common/include/CommonProfilingUtils.hpp>
8
9#include <sstream>
10
11namespace arm
12{
13
14namespace pipe
15{
16void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
17{
18 ARM_PIPE_ASSERT(buffer);
19 ARM_PIPE_ASSERT(outValue);
20
21 for (unsigned int i = 0; i < valueSize; i++, offset++)
22 {
23 outValue[i] = static_cast<uint8_t>(buffer[offset]);
24 }
25}
26
27uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
28{
29 ARM_PIPE_ASSERT(buffer);
30
31 uint64_t value = 0;
32 value = static_cast<uint64_t>(buffer[offset]);
33 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
34 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
35 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
36 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
37 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
38 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
39 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
40
41 return value;
42}
43
44uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
45{
46 ARM_PIPE_ASSERT(buffer);
47
48 uint32_t value = 0;
49 value = static_cast<uint32_t>(buffer[offset]);
50 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
51 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
52 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
53 return value;
54}
55
56uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
57{
58 ARM_PIPE_ASSERT(buffer);
59
60 uint32_t value = 0;
61 value = static_cast<uint32_t>(buffer[offset]);
62 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
63 return static_cast<uint16_t>(value);
64}
65
66uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
67{
68 ARM_PIPE_ASSERT(buffer);
69
70 return buffer[offset];
71}
72
73void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
74{
75 ARM_PIPE_ASSERT(buffer);
76 ARM_PIPE_ASSERT(value);
77
78 for (unsigned int i = 0; i < valueSize; i++, offset++)
79 {
80 buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
81 }
82}
83
84void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
85{
86 ARM_PIPE_ASSERT(buffer);
87
88 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
89 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
90 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
91 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
92 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
93 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
94 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
95 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
96}
97
98void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
99{
100 ARM_PIPE_ASSERT(buffer);
101
102 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
103 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
104 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
105 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
106}
107
108void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
109{
110 ARM_PIPE_ASSERT(buffer);
111
112 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
113 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
114}
115
116void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value)
117{
118 ARM_PIPE_ASSERT(buffer);
119
120 buffer[offset] = static_cast<unsigned char>(value);
121}
122
123std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
124{
125 std::stringstream outputStream, centrePadding;
126 int padding = spacingWidth - static_cast<int>(stringToPass.size());
127
128 for (int i = 0; i < padding / 2; ++i)
129 {
130 centrePadding << " ";
131 }
132
133 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
134
135 if (padding > 0 && padding %2 != 0)
136 {
137 outputStream << " ";
138 }
139
140 return outputStream.str();
141}
142
143
144} // namespace pipe
145} // namespace arm